Skip to content

Commit 1e1687e

Browse files
authored
Merge pull request #1137 from aibaars/fix-credential-helper
Fix CredentialHelper::add_command
2 parents 258d3b6 + c776e8d commit 1e1687e

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/cred.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl CredentialHelper {
299299

300300
if cmd.starts_with('!') {
301301
self.commands.push(cmd[1..].to_string());
302-
} else if cmd.contains("/") || cmd.contains("\\") {
302+
} else if is_absolute_path(cmd) {
303303
self.commands.push(cmd.to_string());
304304
} else {
305305
self.commands.push(format!("git credential-{}", cmd));
@@ -481,6 +481,12 @@ impl CredentialHelper {
481481
}
482482
}
483483

484+
fn is_absolute_path(path: &str) -> bool {
485+
path.starts_with('/')
486+
|| path.starts_with('\\')
487+
|| cfg!(windows) && path.chars().nth(1).is_some_and(|x| x == ':')
488+
}
489+
484490
#[cfg(test)]
485491
mod test {
486492
use std::env;
@@ -578,13 +584,13 @@ echo username=c
578584
return;
579585
} // shell scripts don't work on Windows
580586
let td = TempDir::new().unwrap();
581-
let path = td.path().join("git-credential-script");
587+
let path = td.path().join("git-credential-some-script");
582588
File::create(&path)
583589
.unwrap()
584590
.write(
585591
br"\
586592
#!/bin/sh
587-
echo username=c
593+
echo username=$1
588594
",
589595
)
590596
.unwrap();
@@ -596,14 +602,14 @@ echo username=c
596602
env::set_var("PATH", &env::join_paths(paths).unwrap());
597603

598604
let cfg = test_cfg! {
599-
"credential.https://example.com.helper" => "script",
605+
"credential.https://example.com.helper" => "some-script \"value/with\\slashes\"",
600606
"credential.helper" => "!f() { echo username=a; echo password=b; }; f"
601607
};
602608
let (u, p) = CredentialHelper::new("https://example.com/foo/bar")
603609
.config(&cfg)
604610
.execute()
605611
.unwrap();
606-
assert_eq!(u, "c");
612+
assert_eq!(u, "value/with\\slashes");
607613
assert_eq!(p, "b");
608614
}
609615

0 commit comments

Comments
 (0)