Skip to content

Commit

Permalink
feat(cli): Support -C
Browse files Browse the repository at this point in the history
This helps when running outside of `git`.  Because of the limited case,
its hidden.

This supersedes gitext-rs#195
  • Loading branch information
epage committed Mar 14, 2022
1 parent 62b4301 commit 72a1a5f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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

0 comments on commit 72a1a5f

Please sign in to comment.