From ad4e0dd1d88d9d07a5259044bc9611e1c9c71651 Mon Sep 17 00:00:00 2001 From: Peter Grayson Date: Sun, 11 Dec 2022 23:31:02 -0500 Subject: [PATCH] feat(import): Add --directory option This option maps to `git apply --directory`. Fixes: #36 --- completion/stgit.zsh | 1 + src/cmd/fold.rs | 10 +++++++++- src/cmd/import.rs | 15 +++++++++++++++ src/cmd/sync.rs | 2 +- src/stupid/mod.rs | 5 +++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/completion/stgit.zsh b/completion/stgit.zsh index 2a11e007..05688390 100644 --- a/completion/stgit.zsh +++ b/completion/stgit.zsh @@ -709,6 +709,7 @@ _stg-import() { subcmd_args+=( '(-n --name)'{-n,--name}'[name for imported patch]' '(-p --strip)'{-p+,--strip=}'[remove N leading directories from diff paths]:num' + '--directory[prepend root to all filenames]:root:_directories' '(-t --stripname)'{-t,--stripname}'[strip number and extension from patch name]' '-C=[ensure N lines of surrounding context for each change]:num' '(-3 --3way)'{-3,--3way}'[attempt three-way merge]' diff --git a/src/cmd/fold.rs b/src/cmd/fold.rs index a40d9d4c..3152d5dd 100644 --- a/src/cmd/fold.rs +++ b/src/cmd/fold.rs @@ -124,6 +124,7 @@ fn run(matches: &clap::ArgMatches) -> Result<()> { reject_flag, false, strip_level, + None, context_lines, ) { stupid.read_tree_checkout_hard(orig_head_tree_id)?; @@ -137,6 +138,13 @@ fn run(matches: &clap::ArgMatches) -> Result<()> { Err(crate::stack::Error::CausedConflicts("Merge conflicts".to_string()).into()) } } else { - stupid.apply_to_worktree_and_index(&diff, reject_flag, false, strip_level, context_lines) + stupid.apply_to_worktree_and_index( + &diff, + reject_flag, + false, + strip_level, + None, + context_lines, + ) } } diff --git a/src/cmd/import.rs b/src/cmd/import.rs index b5a4b6ef..dd2aca0a 100644 --- a/src/cmd/import.rs +++ b/src/cmd/import.rs @@ -132,6 +132,18 @@ fn make() -> clap::Command { .value_name("n") .value_parser(crate::argset::parse_usize), ) + .arg( + Arg::new("directory") + .long("directory") + .help("Prepend to all filenames") + .long_help( + "Prepend to all filenames. If a \"-p\" argument is also \ + passed, it is applied before prepending the new root.", + ) + .value_name("root") + .value_parser(clap::value_parser!(PathBuf)) + .value_hint(clap::ValueHint::DirPath), + ) .arg( Arg::new("stripname") .long("stripname") @@ -640,6 +652,9 @@ fn create_patch<'repo>( matches.get_flag("reject"), matches.get_flag("3way"), strip_level, + matches + .get_one::("directory") + .map(|path_buf| path_buf.as_path()), matches.get_one::("context-lines").copied(), )?; diff --git a/src/cmd/sync.rs b/src/cmd/sync.rs index a27b490a..8fa12470 100644 --- a/src/cmd/sync.rs +++ b/src/cmd/sync.rs @@ -297,7 +297,7 @@ fn series_merge_patch( stupid.update_index_refresh()?; stupid.read_tree_checkout(trans.get_branch_head().tree_id(), parent.tree_id())?; stupid - .apply_to_worktree_and_index(&diff, false, false, None, None) + .apply_to_worktree_and_index(&diff, false, false, None, None, None) .with_context(|| format!("applying {patchname} from series"))?; stupid.update_index_refresh()?; diff --git a/src/stupid/mod.rs b/src/stupid/mod.rs index 638242b9..795b9edb 100644 --- a/src/stupid/mod.rs +++ b/src/stupid/mod.rs @@ -141,6 +141,7 @@ impl<'repo, 'index> StupidContext<'repo, 'index> { reject: bool, threeway: bool, strip_level: Option, + directory: Option<&Path>, context_lines: Option, ) -> Result<()> { let mut command = self.git_in_work_root(); @@ -154,6 +155,10 @@ impl<'repo, 'index> StupidContext<'repo, 'index> { if let Some(strip_level) = strip_level { command.arg(format!("-p{strip_level}")); } + if let Some(directory) = directory { + command.arg("--directory"); + command.arg(directory); + } if let Some(context_lines) = context_lines { command.arg(format!("-C{context_lines}")); }