Skip to content

feat(cli): Support -C #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/bin/git-stack/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ pub struct Args {
#[clap(long, group = "mode")]
pub protect: Option<String>,

/// 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 <path> is interpreted relative to the preceding -C <path>. If <path> 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<Vec<std::path::PathBuf>>,

/// Write the current configuration to file with `-` for stdout
#[clap(long, parse(from_os_str), group = "mode")]
pub dump_config: Option<std::path::PathBuf>,
Expand Down
11 changes: 11 additions & 0 deletions src/bin/git-stack/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![allow(clippy::if_same_then_else)]

use clap::Parser;
use proc_exit::WithCodeResultExt;

mod args;
mod config;
Expand Down Expand Up @@ -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() {
Expand Down