diff --git a/src/bin/git-stack/args.rs b/src/bin/git-stack/args.rs index 6174ee9..f402538 100644 --- a/src/bin/git-stack/args.rs +++ b/src/bin/git-stack/args.rs @@ -58,6 +58,20 @@ pub struct Args { #[clap(long, group = "mode")] pub protect: Option, + /// Run as if git was started in `PATH` instead of the current working directory. + /// + /// When multiple -C options are given, each subsequent + /// non-absolute -C is interpreted relative to the preceding -C . If is present but empty, e.g. -C "", then the + /// current working directory is left unchanged. + /// + /// This option affects options that expect path name like --git-dir and --work-tree in that their interpretations of the path names + /// would be made relative to the working directory caused by the -C option. For example the following invocations are equivalent: + /// + /// git --git-dir=a.git --work-tree=b -C c status + /// git --git-dir=c/a.git --work-tree=c/b status + #[clap(short = 'C', hide = true, value_name = "PATH", parse(from_os_str))] + pub current_dir: Option>, + /// Write the current configuration to file with `-` for stdout #[clap(long, parse(from_os_str), group = "mode")] pub dump_config: Option, diff --git a/src/bin/git-stack/main.rs b/src/bin/git-stack/main.rs index e00ecb9..6a9166a 100644 --- a/src/bin/git-stack/main.rs +++ b/src/bin/git-stack/main.rs @@ -3,6 +3,7 @@ #![allow(clippy::if_same_then_else)] use clap::Parser; +use proc_exit::WithCodeResultExt; mod args; mod config; @@ -35,6 +36,16 @@ fn run() -> proc_exit::ExitResult { logger::init_logging(args.verbose.clone(), colored_stderr); + if let Some(current_dir) = args.current_dir.as_deref() { + let current_dir = current_dir + .iter() + .fold(std::path::PathBuf::new(), |current, next| { + current.join(next) + }); + log::trace!("CWD={}", current_dir.display()); + std::env::set_current_dir(current_dir).with_code(proc_exit::Code::USAGE_ERR)?; + } + if let Some(output_path) = args.dump_config.as_deref() { config::dump_config(&args, output_path)?; } else if let Some(ignore) = args.protect.as_deref() {