Skip to content

Commit e5102ca

Browse files
committed
Use the login shell when invoking the signing binary (gitbutlerapp#5839)
That way it should pick up all configuration just as it does when invoking it from the terminal.
1 parent 44690b5 commit e5102ca

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

crates/gitbutler-repo/src/repository_ext.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use gix::objs::WriteTo;
1515
use gix::status::index_worktree;
1616
use std::borrow::Cow;
1717
use std::collections::HashSet;
18-
use std::ffi::OsStr;
18+
use std::ffi::OsString;
1919
#[cfg(unix)]
2020
use std::os::unix::fs::PermissionsExt;
2121
#[cfg(windows)]
@@ -417,17 +417,15 @@ impl RepositoryExt for git2::Repository {
417417
signature_storage.write_all(buffer)?;
418418
let buffer_file_to_sign_path = signature_storage.into_temp_path();
419419

420-
let gpg_program = config.trusted_program("gpg.ssh.program");
421-
let mut gpg_program = gpg_program.unwrap_or(Cow::Borrowed(OsStr::new("ssh-keygen")));
422-
// if cmd is "", use gpg
423-
if gpg_program.is_empty() {
424-
gpg_program = Cow::Borrowed(OsStr::new("ssh-keygen"));
425-
}
420+
let gpg_program = config
421+
.trusted_program("gpg.ssh.program")
422+
.filter(|program| !program.is_empty())
423+
.map_or_else(
424+
|| Path::new("ssh-keygen").into(),
425+
|program| Cow::Owned(program.into_owned().into()),
426+
);
426427

427-
let mut cmd_string = format!(
428-
"{} -Y sign -n git -f ",
429-
gix::path::os_str_into_bstr(gpg_program.as_ref())?
430-
);
428+
let mut cmd_string = format!("{} -Y sign -n git -f ", gpg_program.display());
431429

432430
let buffer_file_to_sign_path_str = buffer_file_to_sign_path
433431
.to_str()
@@ -453,16 +451,14 @@ impl RepositoryExt for git2::Repository {
453451
let args = format!(
454452
"{} -U {}",
455453
key_file_path.to_string_lossy(),
456-
buffer_file_to_sign_path.to_string_lossy()
454+
buffer_file_to_sign_path_str,
457455
);
458456
cmd_string += &args;
459457
} else {
460458
let args = format!("{} {}", signing_key, buffer_file_to_sign_path_str);
461459
cmd_string += &args;
462460
};
463-
let mut signing_cmd: std::process::Command = gix::command::prepare(cmd_string)
464-
.with_shell_disallow_manual_argument_splitting()
465-
.into();
461+
let mut signing_cmd: std::process::Command = command_with_login_shell(cmd_string);
466462
let output = signing_cmd
467463
.stderr(Stdio::piped())
468464
.stdout(Stdio::piped())
@@ -484,20 +480,21 @@ impl RepositoryExt for git2::Repository {
484480
} else {
485481
let gpg_program = config
486482
.trusted_program("gpg.program")
487-
.map(|program| Cow::Owned(program.into_owned().into()))
488-
.unwrap_or_else(|| Path::new("gpg").into());
483+
.filter(|program| !program.is_empty())
484+
.map_or_else(
485+
|| Path::new("gpg").into(),
486+
|program| Cow::Owned(program.into_owned().into()),
487+
);
489488

490-
let mut cmd = std::process::Command::new(gpg_program.as_ref());
489+
let mut cmd = command_with_login_shell(format!(
490+
"{gpg_program} --status-fd=2 -bsau {signing_key} -",
491+
gpg_program = gpg_program.display(),
492+
));
491493

492-
cmd.args(["--status-fd=2", "-bsau", signing_key])
493-
.arg("-")
494-
.stdout(Stdio::piped())
494+
cmd.stdout(Stdio::piped())
495495
.stderr(Stdio::piped())
496496
.stdin(Stdio::piped());
497497

498-
#[cfg(windows)]
499-
cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW
500-
501498
let mut child = match cmd.spawn() {
502499
Ok(child) => child,
503500
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
@@ -582,6 +579,13 @@ impl RepositoryExt for git2::Repository {
582579
}
583580
}
584581

582+
pub fn command_with_login_shell(shell_cmd: impl Into<OsString>) -> std::process::Command {
583+
gix::command::prepare(shell_cmd)
584+
.with_shell_disallow_manual_argument_splitting()
585+
.with_shell_program(gix::path::env::login_shell().unwrap_or("bash".as_ref()))
586+
.into()
587+
}
588+
585589
/// Signs the buffer with the configured gpg key, returning the signature.
586590
pub fn is_literal_ssh_key(string: &str) -> (bool, &str) {
587591
if let Some(key) = string.strip_prefix("key::") {

0 commit comments

Comments
 (0)