bender 0.31.0

A dependency management tool for hardware projects.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
// Copyright (c) 2022 ETH Zurich
// Michael Rogenmoser <michaero@iis.ee.ethz.ch>
// Nils Wistoff <nwistoff@iis.ee.ethz.ch>

//! The `vendor` subcommand.

use std::collections::HashSet;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;

use clap::{Args, Subcommand};
use futures::TryFutureExt;
use glob::Pattern;
use tempfile::TempDir;
use tokio::runtime::Runtime;

use crate::config;
use crate::config::PrefixPaths;
use crate::diagnostic::Warnings;
use crate::error::*;
use crate::git::Git;
use crate::progress::{GitProgressOps, ProgressHandler};
use crate::sess::{DependencySource, Session};
use crate::{fmt_path, fmt_pkg, stageln};

/// A patch linkage
#[derive(Clone)]
pub struct PatchLink {
    /// directory
    pub patch_dir: Option<PathBuf>,
    /// prefix for upstream
    pub from_prefix: PathBuf,
    /// prefix for local
    pub to_prefix: PathBuf,
    /// subdirs and files to exclude
    pub exclude: Vec<PathBuf>,
}

/// Copy source code from upstream external repositories into this repository
#[derive(Args, Debug)]
#[command(
    about = "Copy source code from upstream external repositories into this repository",
    long_about = "Copy source code from upstream external repositories into this repository. Functions similar to the lowrisc vendor.py script.",
    after_help = "Type 'bender vendor <SUBCOMMAND> --help' for more information about a vendor subcommand.",
    subcommand_required = true,
    arg_required_else_help = true
)]
pub struct VendorArgs {
    /// Subcommand for vendor
    #[command(subcommand)]
    pub vendor_subcommand: VendorSubcommand,
}

/// Copy source code from upstream external repositories into this repository
#[derive(Subcommand, Debug)]
pub enum VendorSubcommand {
    /// Display a diff of the local tree and the upstream tree with patches applied.
    Diff {
        /// Return error code 1 when a diff is encountered. (Optional) override the error message by providing a value.
        // alias is for backward compatibility
        #[arg(long, num_args(0..=1), alias="err_on_diff")]
        err_on_diff: Option<String>,
    },
    /// (Re-)initialize the external dependencies.
    #[command(
        long_about = "(Re-)initialize the external dependencies. Copies the upstream files into the target directories and applies existing patches."
    )]
    Init {
        /// Do not apply patches when initializing dependencies
        // alias is for backward compatibility
        #[arg(short, long, alias = "no_patch")]
        no_patch: bool,
    },
    /// Generate a patch file from staged local changes
    Patch {
        /// Generate a plain diff instead of a format-patch.
        #[arg(
            long,
            help = "Generate a plain diff instead of a format-patch.",
            long_help = "Generate a plain diff instead of a format-patch. Includes all local changes (not only the staged ones)."
        )]
        plain: bool,

        /// The message to be associated with the format-patch.
        #[arg(short, long)]
        message: Option<String>,
    },
}

/// Execute the `vendor` subcommand.
pub fn run(sess: &Session, args: &VendorArgs) -> Result<()> {
    let rt = Runtime::new()?;

    for vendor_package in &sess.manifest.vendor_package {
        // Clone upstream into a temporary directory (or make use of .bender/db?)
        let dep_src = DependencySource::from(&vendor_package.upstream);
        let tmp_dir = TempDir::new()?;
        let tmp_path = tmp_dir.path();
        let dep_path = match dep_src {
            DependencySource::Path(path) => path,
            DependencySource::Git(ref url) => {
                let git = Git::new(tmp_path, &sess.config.git, sess.git_throttle.clone());
                rt.block_on(async {
                    let pb = ProgressHandler::new(
                        sess.multiprogress.clone(),
                        GitProgressOps::Clone,
                        vendor_package.name.as_str(),
                    );
                    git.clone().spawn_with(|c| c.arg("clone").arg(url).arg("."), Some(pb))
                    .map_err(move |cause| {
                        Warnings::GitInitFailed {
                            is_ssh: url.contains("git@"),
                        }.emit();
                        Error::chain(
                            format!("Failed to initialize git database in {:?}.", tmp_path),
                            cause,
                        )
                    }).await?;
                    let rev_hash = match vendor_package.upstream {
                        config::Dependency::GitRevision { ref rev, .. } => Ok(rev),
                        _ => Err(Error::new("Please ensure your vendor reference is a commit hash to avoid upstream changes impacting your checkout")),
                    }?;
                    let pb = ProgressHandler::new(
                        sess.multiprogress.clone(),
                        GitProgressOps::Checkout,
                        vendor_package.name.as_str(),
                    );
                    git.clone().spawn_with(|c| c.arg("checkout").arg(rev_hash), Some(pb)).await?;
                    if *rev_hash != git.spawn_with(|c| c.arg("rev-parse").arg("--verify").arg(format!("{}^{{commit}}", rev_hash)), None).await?.trim_end_matches('\n') {
                        Err(Error::new("Please ensure your vendor reference is a commit hash to avoid upstream changes impacting your checkout"))
                    } else {
                        Ok(())
                    }
                })?;

                tmp_path.to_path_buf()
            }
            DependencySource::Registry => unimplemented!(),
        };

        // Extract patch dirs of links
        let mut patch_links: Vec<PatchLink> = Vec::new();
        for link in vendor_package.mapping.clone() {
            patch_links.push(PatchLink {
                patch_dir: link.patch_dir,
                from_prefix: link.from,
                to_prefix: link.to,
                exclude: vec![],
            })
        }

        // If links do not specify patch dirs, use package-wide patch dir
        let patch_links = {
            match patch_links[..] {
                [] => vec![PatchLink {
                    patch_dir: vendor_package.patch_dir.clone(),
                    from_prefix: PathBuf::from(""),
                    to_prefix: PathBuf::from(""),
                    exclude: vec![],
                }],
                _ => patch_links,
            }
        };

        // sort patch_links so more specific links have priority
        // 1. file links over directory links eg 'a/file -> c/file' before 'b/ -> c/'
        // 2. subdirs (deeper paths) first eg 'a/aa/ -> c/aa' before 'a/ab -> c/'
        let mut sorted_links: Vec<_> = patch_links.clone();
        sorted_links.sort_by(|a, b| {
            let a_is_file = a.to_prefix.is_file();
            let b_is_file = b.to_prefix.is_file();

            if a_is_file != b_is_file {
                return b_is_file.cmp(&a_is_file);
            }

            let a_depth = a.to_prefix.iter().count();
            let b_depth = b.to_prefix.iter().count();

            b_depth.cmp(&a_depth)
        });

        // Add all subdirs and files to the exclude list of above dirs
        // avoids duplicate handling of the same changes
        let mut seen_paths: HashSet<PathBuf> = HashSet::new();
        for patch_link in sorted_links.iter_mut() {
            patch_link.exclude = seen_paths
                .iter()
                .filter(|path| path.starts_with(&patch_link.to_prefix)) // subdir?
                .cloned()
                .collect();

            seen_paths.insert(patch_link.to_prefix.clone());
        }
        let git = Git::new(tmp_path, &sess.config.git, sess.git_throttle.clone());

        match &args.vendor_subcommand {
            VendorSubcommand::Diff { err_on_diff } => {
                // Apply patches
                sorted_links
                    .clone()
                    .into_iter()
                    .try_for_each(|patch_link| {
                        apply_patches(&rt, git.clone(), vendor_package.name.clone(), patch_link)
                            .map(|_| ())
                    })?;

                // Stage applied patches to clean working tree
                rt.block_on(git.clone().add_all())?;

                // Print diff for each link
                sorted_links.into_iter().try_for_each(|patch_link| {
                    let get_diff = diff(&rt, git.clone(), vendor_package, patch_link, dep_path.clone())
                        .map_err(|cause| Error::chain("Failed to get diff.", cause))?;
                    if !get_diff.is_empty() {
                        let _ = write!(std::io::stdout(), "{}", get_diff);
                        // If desired, return an error (e.g. for CI)
                        if err_on_diff.is_some() {
                            let err_msg : Option<&String> = err_on_diff.as_ref();
                            let err_msg = match err_msg {
                                Some(err_msg) => err_msg.to_string(),
                                _ => "Found differences, please patch (e.g. using bender vendor patch).".to_string()
                            };
                            return Err(Error::new(err_msg))
                        }
                    }
                    Ok(())
                })
            }

            VendorSubcommand::Init { no_patch } => {
                sorted_links.into_iter().rev().try_for_each(|patch_link| {
                    // Remove existing directories before importing them again
                    let target_path = patch_link
                        .clone()
                        .to_prefix
                        .prefix_paths(&vendor_package.target_dir)?;
                    if target_path.exists() {
                        if target_path.is_dir() {
                            std::fs::remove_dir_all(target_path.clone())
                        } else {
                            std::fs::remove_file(target_path.clone())
                        }
                        .map_err(|cause| {
                            Error::chain(format!("Failed to remove {:?}.", target_path), cause)
                        })?;
                    }

                    // init
                    let result = init(
                        &rt,
                        git.clone(),
                        vendor_package,
                        patch_link,
                        dep_path.clone(),
                        *no_patch,
                    );

                    stageln!(
                        "Copied",
                        "{} files from upstream",
                        fmt_pkg!(vendor_package.name)
                    );

                    result
                })
            }

            VendorSubcommand::Patch { plain, message } => {
                // Apply patches
                let mut num_patches = 0;
                sorted_links
                    .clone()
                    .into_iter()
                    .try_for_each(|patch_link| {
                        apply_patches(&rt, git.clone(), vendor_package.name.clone(), patch_link)
                            .map(|num| num_patches += num)
                    })
                    .map_err(|cause| Error::chain("Failed to apply patch.", cause))?;

                // Commit applied patches to clean working tree
                if num_patches > 0 {
                    rt.block_on(git.clone().add_all())?;
                    rt.block_on(git.clone().commit(Some(&"pre-patch".to_string())))?;
                }

                // Generate patch
                sorted_links.into_iter().try_for_each(|patch_link| {
                    match patch_link.patch_dir.clone() {
                        Some(patch_dir) => {
                            if *plain {
                                let get_diff = diff(
                                    &rt,
                                    git.clone(),
                                    vendor_package,
                                    patch_link,
                                    dep_path.clone(),
                                )
                                .map_err(|cause| Error::chain("Failed to get diff.", cause))?;
                                gen_plain_patch(get_diff, patch_dir, false)
                            } else {
                                gen_format_patch(
                                    &rt,
                                    sess,
                                    git.clone(),
                                    patch_link,
                                    vendor_package.target_dir.clone(),
                                    message.as_ref(),
                                )
                            }
                        }
                        None => {
                            Warnings::NoPatchDir {
                                vendor_pkg: vendor_package.name.clone(),
                                from_prefix: patch_link.from_prefix.clone(),
                                to_prefix: patch_link.to_prefix.clone(),
                            }
                            .emit();
                            Ok(())
                        }
                    }
                })
            }
        }?;
    }

    Ok(())
}

/// initialize the external dependency
pub fn init(
    rt: &Runtime,
    git: Git,
    vendor_package: &config::VendorPackage,
    patch_link: PatchLink,
    dep_path: impl AsRef<Path>,
    no_patch: bool,
) -> Result<()> {
    // import necessary files from upstream, apply patches
    let dep_path = dep_path.as_ref();

    // Make sure the target directory actually exists
    let link_to = patch_link
        .to_prefix
        .clone()
        .prefix_paths(&vendor_package.target_dir)?;
    let link_from = patch_link.from_prefix.clone().prefix_paths(dep_path)?;
    std::fs::create_dir_all(link_to.parent().unwrap()).map_err(|cause| {
        Error::chain(
            format!("Failed to create directory {:?}", link_to.parent()),
            cause,
        )
    })?;

    if !no_patch {
        apply_patches(
            rt,
            git.clone(),
            vendor_package.name.clone(),
            patch_link.clone(),
        )?;
    }

    // Check if includes exist
    for path in vendor_package.include_from_upstream.clone() {
        if !PathBuf::from(extend_paths(std::slice::from_ref(&path), dep_path, true)?[0].clone())
            .exists()
        {
            Warnings::NotInUpstream { path }.emit();
        }
    }

    // Copy src to dst recursively.
    match link_from.is_dir() {
        true => copy_recursively(
            &link_from,
            &link_to,
            &extend_paths(&vendor_package.include_from_upstream, dep_path, false)?,
            &vendor_package
                .exclude_from_upstream
                .clone()
                .into_iter()
                .map(|excl| format!("{}/{}", &dep_path.to_str().unwrap(), &excl))
                .collect(),
        )?,
        false => {
            if link_from.exists() {
                std::fs::copy(&link_from, &link_to).map_err(|cause| {
                    Error::chain(
                        format!(
                            "Failed to copy {} to {}.",
                            link_from.to_str().unwrap(),
                            link_to.to_str().unwrap(),
                        ),
                        cause,
                    )
                })?;
            } else {
                Warnings::NotInUpstream {
                    path: link_from.to_str().unwrap().to_string(),
                }
                .emit();
            }
        }
    };

    Ok(())
}

/// apply existing patches
pub fn apply_patches(
    rt: &Runtime,
    git: Git,
    package_name: String,
    patch_link: PatchLink,
) -> Result<usize> {
    let patch_dir = match &patch_link.patch_dir {
        Some(patch_dir) => patch_dir,
        None => return Ok(0),
    };

    // Create directory in case it does not already exist
    std::fs::create_dir_all(patch_dir).map_err(|cause| {
        Error::chain(format!("Failed to create directory {patch_dir:?}"), cause)
    })?;

    let mut patches = std::fs::read_dir(patch_dir)?
        .map(move |f| f.unwrap().path())
        .filter(|f| f.extension().is_some())
        .filter(|f| f.extension().unwrap() == "patch")
        .collect::<Vec<_>>();
    patches.sort_by_key(|patch_path| patch_path.to_str().unwrap().to_lowercase());

    rt.block_on(async {
        for patch in &patches {
            git.clone()
                .spawn_with(
                    |c| {
                        let is_file = patch_link
                            .from_prefix
                            .clone()
                            .prefix_paths(git.path)
                            .unwrap()
                            .is_file();

                        let current_patch_target = if is_file {
                            patch_link.from_prefix.parent().unwrap().to_str().unwrap()
                        } else {
                            patch_link.from_prefix.as_path().to_str().unwrap()
                        };

                        c.arg("apply")
                            .arg("--directory")
                            .arg(current_patch_target)
                            .arg("-p1")
                            .arg(patch);

                        // limit to specific file for file links
                        if is_file {
                            let file_path = patch_link.from_prefix.to_str().unwrap();
                            c.arg("--include").arg(file_path);
                        }
                        c
                    },
                    None,
                )
                .await
                .map_err(|cause| {
                    Error::chain(format!("Failed to apply patch {patch:?}."), cause)
                })?;

            stageln!(
                "Patched",
                "{} with {}",
                fmt_pkg!(package_name),
                fmt_path!(patch.display())
            );
        }
        Ok::<(), Error>(())
    })?;
    Ok(patches.len())
}

/// Generate diff
pub fn diff(
    rt: &Runtime,
    git: Git,
    vendor_package: &config::VendorPackage,
    patch_link: PatchLink,
    dep_path: impl AsRef<Path>,
) -> Result<String> {
    // Copy files from local to temporary repo
    let link_from = patch_link
        .from_prefix
        .clone()
        .prefix_paths(dep_path.as_ref())?; // dep_path: path to temporary clone. link_to: targetdir/link.to
    let link_to = patch_link
        .to_prefix
        .clone()
        .prefix_paths(vendor_package.target_dir.as_ref())?;
    if !&link_to.exists() {
        return Err(Error::new(format!(
            "Could not find {}. Did you run bender vendor init?",
            link_to.to_str().unwrap()
        )));
    }
    // Copy src to dst recursively.
    match &link_to.is_dir() {
        true => copy_recursively(
            &link_to,
            &link_from,
            &extend_paths(
                &vendor_package.include_from_upstream,
                &vendor_package.target_dir,
                false,
            )?,
            &vendor_package
                .exclude_from_upstream
                .clone()
                .into_iter()
                .map(|excl| format!("{}/{}", &vendor_package.target_dir.to_str().unwrap(), &excl))
                .collect(),
        )?,
        false => {
            std::fs::copy(&link_to, &link_from).map_err(|cause| {
                Error::chain(
                    format!(
                        "Failed to copy {} to {}.",
                        link_to.to_str().unwrap(),
                        link_from.to_str().unwrap(),
                    ),
                    cause,
                )
            })?;
        }
    };
    // Get diff
    rt.block_on(async {
        git.spawn_with(
            |c| {
                c.arg("diff").arg(format!(
                    "--relative={}",
                    patch_link
                        .from_prefix
                        .to_str()
                        .expect("Failed to convert from_prefix to string.")
                ))
            },
            None,
        )
        .await
    })
}

/// Generate a plain patch from a diff
pub fn gen_plain_patch(diff: String, patch_dir: impl AsRef<Path>, no_patch: bool) -> Result<()> {
    if !diff.is_empty() {
        // if let Some(patch) = patch_dir {
        // Create directory in case it does not already exist
        std::fs::create_dir_all(patch_dir.as_ref()).map_err(|cause| {
            Error::chain(
                format!("Failed to create directory {:?}", patch_dir.as_ref()),
                cause,
            )
        })?;

        let mut patches = std::fs::read_dir(patch_dir.as_ref())?
            .map(move |f| f.unwrap().path())
            .filter(|f| f.extension().unwrap() == "patch")
            .collect::<Vec<_>>();
        patches.sort_by_key(|patch_path| patch_path.to_str().unwrap().to_lowercase());

        let new_patch = if no_patch || patches.is_empty() {
            // Remove all old patches
            for patch_file in patches {
                std::fs::remove_file(patch_file)?;
            }
            "0001-bender-vendor.patch".to_string()
        } else {
            // Get all patch leading numeric keys (0001, ...) and generate new name
            let leading_numbers = patches
                .iter()
                .map(|file_path| file_path.file_name().unwrap().to_str().unwrap())
                .map(|s| &s[..4])
                .collect::<Vec<_>>();
            if !leading_numbers
                .iter()
                .all(|s| s.chars().all(char::is_numeric))
            {
                Err(Error::new(format!(
                    "Please ensure all patches start with four numbers for proper ordering in {}",
                    patch_dir.as_ref().to_str().unwrap()
                )))?;
            }
            let max_number = leading_numbers
                .iter()
                .map(|s| s.parse::<i32>().unwrap())
                .max()
                .unwrap();
            format!("{:04}-bender-vendor.patch", max_number + 1)
        };

        // write patch
        std::fs::write(patch_dir.as_ref().join(new_patch), diff)?;
        // }
    }

    Ok(())
}

/// Commit changes staged in ghost repo and generate format patch
pub fn gen_format_patch(
    rt: &Runtime,
    sess: &Session,
    git: Git,
    patch_link: PatchLink,
    target_dir: impl AsRef<Path>,
    message: Option<&String>,
) -> Result<()> {
    // Local git
    let to_path = patch_link
        .to_prefix
        .clone()
        .prefix_paths(target_dir.as_ref())?;
    if !&to_path.exists() {
        return Err(Error::new(format!(
            "Could not find {}. Did you run bender vendor init?",
            to_path.to_str().unwrap()
        )));
    }
    let git_parent = Git::new(
        if to_path.is_dir() {
            &to_path
        } else {
            to_path.parent().unwrap()
        },
        &sess.config.git,
        sess.git_throttle.clone(),
    );

    // If the patch link maps a file, use the parent directory for the following git operations.
    let from_path_relative = if to_path.is_dir() {
        patch_link.from_prefix.clone()
    } else {
        patch_link.from_prefix.parent().unwrap().to_path_buf()
    };

    // We assume that patch_dir matches Some() was checked outside this function.
    let patch_dir = patch_link.patch_dir.clone().unwrap();

    // If the patch link maps a file, we operate in the file's parent directory
    // Therefore, only get the diff for that file.
    let include_pathspec = if !to_path.is_dir() {
        patch_link
            .to_prefix
            .file_name()
            .unwrap()
            .to_str()
            .unwrap()
            .to_string()
    } else {
        ".".to_string()
    };

    // Build the exclude pathspec to diff only the applicable files
    let exclude_pathspecs: Vec<String> = patch_link
        .exclude
        .iter()
        .map(|path| format!(":!{}", path.to_str().unwrap()))
        .collect();

    let mut diff_args = vec![
        "diff".to_string(),
        "--relative".to_string(),
        "--cached".to_string(),
    ];

    diff_args.push(include_pathspec);
    for exclude_path in exclude_pathspecs {
        diff_args.push(exclude_path);
    }

    // Get staged changes in dependency
    let get_diff_cached = rt
        .block_on(async { git_parent.spawn_with(|c| c.args(&diff_args), None).await })
        .map_err(|cause| Error::chain("Failed to generate diff", cause))?;

    if !get_diff_cached.is_empty() {
        // Write diff into new temp dir. TODO: pipe directly to "git apply"
        let tmp_format_dir = TempDir::new()?;
        let tmp_format_path = tmp_format_dir.keep();
        let diff_cached_path = tmp_format_path.join("staged.diff");
        std::fs::write(diff_cached_path.clone(), get_diff_cached)?;

        // Apply diff and stage changes in ghost repo
        rt.block_on(async {
            git.clone().spawn_with(|c| {
                c.arg("apply")
                    .arg("--directory")
                    .arg(&from_path_relative)
                    .arg("-p1")
                    .arg(&diff_cached_path)
            }, None)
            .and_then(|_| git.clone().spawn_with(|c| c.arg("add").arg("--all"), None))
            .await
        }).map_err(|cause| Error::chain("Could not apply staged changes on top of patched upstream repository. Did you commit all previously patched modifications?", cause))?;

        // Commit all staged changes in ghost repo
        rt.block_on(git.clone().commit(message))?;

        // Create directory in case it does not already exist
        std::fs::create_dir_all(patch_dir.clone()).map_err(|cause| {
            Error::chain(
                format!("Failed to create directory {:?}", patch_dir.clone()),
                cause,
            )
        })?;

        let mut patches = std::fs::read_dir(patch_dir.clone())?
            .map(move |f| f.unwrap().path())
            .filter(|f| f.extension().is_some())
            .filter(|f| f.extension().unwrap() == "patch")
            .collect::<Vec<_>>();
        patches.sort_by_key(|patch_path| patch_path.to_str().unwrap().to_lowercase());

        // Determine max number
        let max_number = if patches.is_empty() {
            0
        } else {
            let leading_numbers = patches
                .iter()
                .map(|file_path| file_path.file_name().unwrap().to_str().unwrap())
                .map(|s| &s[..4])
                .collect::<Vec<_>>();
            if !leading_numbers
                .iter()
                .all(|s| s.chars().all(char::is_numeric))
            {
                Err(Error::new(format!(
                    "Please ensure all patches start with four numbers for proper ordering in {}",
                    patch_dir.to_str().unwrap()
                )))?;
            }
            leading_numbers
                .iter()
                .map(|s| s.parse::<i32>().unwrap())
                .max()
                .unwrap()
        };

        // Generate format-patch
        rt.block_on(async {
            git.spawn_with(
                |c| {
                    c.arg("format-patch")
                        .arg("-o")
                        .arg(patch_dir.to_str().unwrap())
                        .arg("-1")
                        .arg(format!("--start-number={}", max_number + 1))
                        .arg(format!(
                            "--relative={}",
                            from_path_relative.to_str().unwrap()
                        ))
                        .arg("HEAD")
                },
                None,
            )
            .await
        })?;
    }
    Ok(())
}

/// recursive copy function
pub fn copy_recursively(
    source: impl AsRef<Path> + std::fmt::Debug,
    destination: impl AsRef<Path> + std::fmt::Debug,
    includes: &Vec<String>,
    ignore: &Vec<String>,
) -> Result<()> {
    std::fs::create_dir_all(&destination).map_err(|cause| {
        Error::chain(
            format!("Failed to create directory {:?}", &destination),
            cause,
        )
    })?;
    for entry in std::fs::read_dir(source)? {
        let entry = entry?;

        if !includes.iter().any(|include| {
            PathBuf::from(include).ancestors().any(|include_path| {
                Pattern::new(include_path.to_str().unwrap())
                    .unwrap()
                    .matches_path(&entry.path())
            })
        }) || ignore.iter().any(|ignore_path| {
            Pattern::new(ignore_path)
                .unwrap()
                .matches_path(&entry.path())
        }) {
            continue;
        }

        let filetype = entry.file_type()?;
        let canonical_path_filetype =
            std::fs::metadata(std::fs::canonicalize(entry.path()).map_err(|cause| {
                Error::chain(
                    format!(
                        "Failed to canonicalize {:?}.",
                        entry.path().to_str().unwrap()
                    ),
                    cause,
                )
            })?)?
            .file_type();
        if filetype.is_dir() {
            copy_recursively(
                entry.path(),
                destination.as_ref().join(entry.file_name()),
                includes,
                ignore,
            )?;
        } else if filetype.is_symlink() && canonical_path_filetype.is_dir() {
            let orig = std::fs::read_link(entry.path());
            symlink_dir(orig.unwrap(), destination.as_ref().join(entry.file_name()))?;
        } else {
            std::fs::copy(entry.path(), destination.as_ref().join(entry.file_name())).map_err(
                |cause| {
                    Error::chain(
                        format!(
                            "Failed to copy {} to {}.",
                            entry.path().to_str().unwrap(),
                            destination
                                .as_ref()
                                .join(entry.file_name())
                                .to_str()
                                .unwrap()
                        ),
                        cause,
                    )
                },
            )?;
        }
    }
    Ok(())
}

/// Prefix paths with prefix. Append ** to directories.
pub fn extend_paths(
    include_from_upstream: &[String],
    prefix: impl AsRef<Path>,
    dir_only: bool,
) -> Result<Vec<String>> {
    include_from_upstream
        .iter()
        .map(|pattern| {
            let pattern_long = PathBuf::from(pattern).prefix_paths(prefix.as_ref())?;
            if pattern_long.is_dir() && !dir_only {
                Ok(String::from(pattern_long.join("**").to_str().unwrap()))
            } else {
                Ok(String::from(pattern_long.to_str().unwrap()))
            }
        })
        .collect::<Result<_>>()
}

#[cfg(unix)]
fn symlink_dir(p: PathBuf, q: PathBuf) -> Result<()> {
    Ok(std::os::unix::fs::symlink(p, q)?)
}

#[cfg(windows)]
fn symlink_dir(p: PathBuf, q: PathBuf) -> Result<()> {
    Ok(std::os::windows::fs::symlink_dir(p, q)?)
}