Skip to content

Commit

Permalink
feat(import): Add --directory option
Browse files Browse the repository at this point in the history
This option maps to `git apply --directory`.

Fixes: #36
  • Loading branch information
jpgrayson committed Dec 12, 2022
1 parent c3abc79 commit ad4e0dd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions completion/stgit.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
Expand Down
10 changes: 9 additions & 1 deletion src/cmd/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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,
)
}
}
15 changes: 15 additions & 0 deletions src/cmd/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <root> to all filenames")
.long_help(
"Prepend <root> 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")
Expand Down Expand Up @@ -640,6 +652,9 @@ fn create_patch<'repo>(
matches.get_flag("reject"),
matches.get_flag("3way"),
strip_level,
matches
.get_one::<PathBuf>("directory")
.map(|path_buf| path_buf.as_path()),
matches.get_one::<usize>("context-lines").copied(),
)?;

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;

Expand Down
5 changes: 5 additions & 0 deletions src/stupid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl<'repo, 'index> StupidContext<'repo, 'index> {
reject: bool,
threeway: bool,
strip_level: Option<usize>,
directory: Option<&Path>,
context_lines: Option<usize>,
) -> Result<()> {
let mut command = self.git_in_work_root();
Expand All @@ -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}"));
}
Expand Down

0 comments on commit ad4e0dd

Please sign in to comment.