Skip to content
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

Fix CredentialHelper::add_command #1137

Merged
merged 2 commits into from
Mar 17, 2025
Merged
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
16 changes: 11 additions & 5 deletions src/cred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl CredentialHelper {

if cmd.starts_with('!') {
self.commands.push(cmd[1..].to_string());
} else if cmd.contains("/") || cmd.contains("\\") {
} else if is_absolute_path(cmd) {
self.commands.push(cmd.to_string());
} else {
self.commands.push(format!("git credential-{}", cmd));
Expand Down Expand Up @@ -481,6 +481,12 @@ impl CredentialHelper {
}
}

fn is_absolute_path(path: &str) -> bool {
path.starts_with('/')
|| path.starts_with('\\')
|| cfg!(windows) && path.chars().nth(1).is_some_and(|x| x == ':')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor note, this is slightly different from https://github.com/git-for-windows/git/blob/cca1f38702730b35f52c29efd62864b85e85ddcc/compat/win32/path-utils.c#L9-L31, but probably not enough to matter here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, git2-rs uses the str type while the git C uses a char* and if I understand the git C code correctly, it just tries to handle the case where the drive letter is a UTF8 character. I'd expect the str::nth function to take care of that already. However, there might indeed be some behaviour differences in corner cases at I'm not aware of.

}

#[cfg(test)]
mod test {
use std::env;
Expand Down Expand Up @@ -578,13 +584,13 @@ echo username=c
return;
} // shell scripts don't work on Windows
let td = TempDir::new().unwrap();
let path = td.path().join("git-credential-script");
let path = td.path().join("git-credential-some-script");
File::create(&path)
.unwrap()
.write(
br"\
#!/bin/sh
echo username=c
echo username=$1
",
)
.unwrap();
Expand All @@ -596,14 +602,14 @@ echo username=c
env::set_var("PATH", &env::join_paths(paths).unwrap());

let cfg = test_cfg! {
"credential.https://example.com.helper" => "script",
"credential.https://example.com.helper" => "some-script \"value/with\\slashes\"",
"credential.helper" => "!f() { echo username=a; echo password=b; }; f"
};
let (u, p) = CredentialHelper::new("https://example.com/foo/bar")
.config(&cfg)
.execute()
.unwrap();
assert_eq!(u, "c");
assert_eq!(u, "value/with\\slashes");
assert_eq!(p, "b");
}

Expand Down