diff --git a/README.md b/README.md index 10042d1..335821b 100644 --- a/README.md +++ b/README.md @@ -68,10 +68,12 @@ by real Git Bash) passes 100%. Supported: - Paths: `/c/foo` ↔ `C:\foo`, `/tmp` → `%TEMP%`, `/usr/bin`, `/bin`, `/ucrt64` → Git's install dir, `/dev/null`, `/dev/stdin|stdout|stderr|fd/N`, `/dev/tty`. Windows paths - (`C:\x`, `C:/x`) work everywhere too. `$PWD` and `pwd` show the MSYS form. + (`C:\x`, `C:/x`) work everywhere too. `$PWD` and `pwd` show the MSYS form, `pwd -W` the + Windows one (`C:/...`). - PATH is kept in MSYS form inside the shell and converted when launching programs. - Arguments to native programs get MSYS2-style path conversion (`/c/x` → `C:/x`), - honoring `MSYS_NO_PATHCONV` and `MSYS2_ARG_CONV_EXCL`. + honoring `MSYS_NO_PATHCONV` and `MSYS2_ARG_CONV_EXCL` when they are exported or given as + a command prefix (`MSYS_NO_PATHCONV=1 cmd ...`), as in Git Bash. - Arguments to MSYS programs are quoted so the MSYS runtime doesn't glob/brace/tilde-expand or `@file`-expand them. - Executables are found with or without `.exe`/`.com`/`.bat`/`.cmd`; extensionless files with a diff --git a/src/builtins.rs b/src/builtins.rs index 70a1d0e..339e8d6 100644 --- a/src/builtins.rs +++ b/src/builtins.rs @@ -393,25 +393,46 @@ fn cd(sh: &mut Shell, args: &[String]) -> R { } pub fn physical_pwd(sh: &Shell) -> String { + path::to_posix(&physical_cwd(sh)) +} + +/// The current directory with symlinks resolved, as a Windows path. +fn physical_cwd(sh: &Shell) -> std::path::PathBuf { match std::fs::canonicalize(&sh.cwd) { Ok(c) => { let s = c.to_string_lossy().into_owned(); - let s = s.strip_prefix(r"\\?\").unwrap_or(&s).to_string(); - path::to_posix(std::path::Path::new(&s)) + std::path::PathBuf::from(s.strip_prefix(r"\\?\").unwrap_or(&s)) } - Err(_) => path::to_posix(&sh.cwd), + Err(_) => sh.cwd.clone(), } } fn pwd(sh: &mut Shell, args: &[String]) -> R { - let physical = args.iter().any(|a| a == "-P") || (sh.o[O_PHYSICAL] && !args.iter().any(|a| a == "-L")); - let p = if physical { - physical_pwd(sh) - } else { - match sh.get_str("PWD") { + // -L, -P and MSYS bash's -W (the physical directory in Windows form, `C:/...`) each choose a + // form; as in Git Bash, the last one given wins. + let mut form = if sh.o[O_PHYSICAL] { 'P' } else { 'L' }; + for a in args { + if a == "--" || a == "-" || !a.starts_with('-') { + break; + } + for c in a[1..].chars() { + match c { + 'L' | 'P' | 'W' => form = c, + _ => { + sh.eprint(&format!("pwd: -{}: invalid option", c)); + sh.eprint("pwd: usage: pwd [-LPW]"); + return Ok(2); + } + } + } + } + let p = match form { + 'P' => physical_pwd(sh), + 'W' => path::win_fwd(&physical_cwd(sh)), + _ => match sh.get_str("PWD") { Some(p) if !p.is_empty() && path::to_win(&p, &sh.cwd).is_dir() => p, _ => path::to_posix(&sh.cwd), - } + }, }; sh.out(1, format!("{}\n", p).as_bytes())?; Ok(0) diff --git a/src/exec.rs b/src/exec.rs index a2fd83e..636f800 100644 --- a/src/exec.rs +++ b/src/exec.rs @@ -1494,9 +1494,22 @@ impl Shell { let mut cmd; let native; let mut msys_child = false; - let no_conv = self.get_str("MSYS_NO_PATHCONV").map(|v| !v.is_empty()).unwrap_or(false); - let excl: Vec = self - .get_str("MSYS2_ARG_CONV_EXCL") + // As in Git Bash, these count where the MSYS runtime would see them: in the environment + // the program starts with, so when exported or given as a prefix (`MSYS_NO_PATHCONV=1 cmd`). + let env_var = |name: &str| -> Option { + match assigns.iter().rev().find(|(k, _)| k == name) { + Some((_, v)) => Some(v.clone()), + None => match self.vars.get(name) { + Some(v) if v.has(EXPORT) => match &v.value { + Some(Value::Str(s)) => Some(s.clone()), + _ => None, + }, + _ => None, + }, + } + }; + let no_conv = env_var("MSYS_NO_PATHCONV").map(|v| !v.is_empty()).unwrap_or(false); + let excl: Vec = env_var("MSYS2_ARG_CONV_EXCL") .map(|v| v.split(';').filter(|s| !s.is_empty()).map(|s| s.to_string()).collect()) .unwrap_or_default(); let conv = |a: &str| -> String { diff --git a/tests/scripts/072_pathconv_env_pwd_w.code b/tests/scripts/072_pathconv_env_pwd_w.code new file mode 100644 index 0000000..c227083 --- /dev/null +++ b/tests/scripts/072_pathconv_env_pwd_w.code @@ -0,0 +1 @@ +0 \ No newline at end of file diff --git a/tests/scripts/072_pathconv_env_pwd_w.out b/tests/scripts/072_pathconv_env_pwd_w.out new file mode 100644 index 0000000..6064870 --- /dev/null +++ b/tests/scripts/072_pathconv_env_pwd_w.out @@ -0,0 +1,12 @@ + 'C:/x' + '/c/x' + 'C:/x' + '/c/x' + '/c/x' 'D:/y' + 'C:/x' '/d/y' '/e/z' +C:/Windows/System32 +C:/Windows/System32 +/c/Windows/System32 +/c/Windows/System32 +/c/Windows/System32 +pwd -X: rc=2 diff --git a/tests/scripts/072_pathconv_env_pwd_w.sh b/tests/scripts/072_pathconv_env_pwd_w.sh new file mode 100644 index 0000000..f2b1f21 --- /dev/null +++ b/tests/scripts/072_pathconv_env_pwd_w.sh @@ -0,0 +1,24 @@ +# MSYS_NO_PATHCONV and MSYS2_ARG_CONV_EXCL count when exported or given as a command prefix, +# as in Git Bash. pwd -W prints the current directory in Windows form. +# Start from no conversion settings, whatever the environment exports (Claude Code sets one). +unset MSYS_NO_PATHCONV MSYS2_ARG_CONV_EXCL +git rev-parse --sq-quote /c/x +MSYS_NO_PATHCONV=1 git rev-parse --sq-quote /c/x +MSYS_NO_PATHCONV=1 +git rev-parse --sq-quote /c/x +export MSYS_NO_PATHCONV +git rev-parse --sq-quote /c/x +unset MSYS_NO_PATHCONV +MSYS2_ARG_CONV_EXCL=/c git rev-parse --sq-quote /c/x /d/y +export MSYS2_ARG_CONV_EXCL='/d;/e' +git rev-parse --sq-quote /c/x /d/y /e/z +unset MSYS2_ARG_CONV_EXCL + +cd /c/Windows/System32 || exit 1 +pwd -W +pwd -LW +pwd -WP +pwd -P +pwd -L +pwd -X 2>/dev/null +echo "pwd -X: rc=$?"