Repository: gitbutlerapp/gitbutler
File: crates/gitbutler-git/src/repository.rs (Rust)

Code before:
    extra: Extra,
) -> Result<(usize, String, String), Error<E>>
where
    P: AsRef<Path>,
    E: GitExecutor,
    F: FnMut(String, Extra) -> Fut,
    Fut: std::future::Future<Output = Option<String>>,
    Extra: Send + Clone,
{
    let mut current_exe = std::env::current_exe().map_err(Error::<E>::NoSelfExe)?;
    current_exe = current_exe.canonicalize().unwrap_or(current_exe);

    // TODO(qix-): Get parent PID of connecting processes to make sure they're us.
    // let our_pid = std::process::id();

    // This is a bit of a hack. Under a test environment, Cargo is running a
    // test runner with a quasi-random suffix. The actual executables live in
    // the parent directory. Thus, we have to do this under test. It's not
    // ideal, but it works for now.
    //
    // This logic is used in downstream tests like gitbutler-branch-actions.
    if current_exe.iter().nth_back(1) == Some("deps".as_ref()) {
        current_exe = current_exe.parent().unwrap().to_path_buf();
    }

    let askpath_path = current_exe
        .with_file_name("gitbutler-git-askpass")
        .with_extension(std::env::consts::EXE_EXTENSION);

    #[cfg(unix)]
    let setsid_path = current_exe.with_file_name("gitbutler-git-setsid");

    let res = executor.stat(&askpath_path).await.map_err(Error::<E>::Exec);
    if res.is_err() {
        let (path, prefix) = if let Some(workdir) = std::env::current_dir().ok().and_then(|cwd| {
            gix::discover::upwards(&cwd)
                .ok()
                .and_then(|p| p.0.into_repository_and_work_tree_directories().1)
        }) {
            let prefix = std::env::var_os("CARGO_TARGET_DIR")
                .map(std::path::PathBuf::from)
                .map(|path| {
                    format!(
                        "CARGO_TARGET_DIR={path} ",
                        path = path.strip_prefix(&workdir).unwrap_or(&path).display()
                    )
                })
                .unwrap_or_default();
            (
                askpath_path.strip_prefix(&workdir).unwrap_or(&askpath_path),
                prefix,

Review comment (suggestion):
This is a wonderful improvement. If memory serves, it should even work without without the `.exe` suffix if the path is executed via `std::process::Command`, but let's keep it fit for `.exists()` checks as well.

Code after:
    extra: Extra,
) -> Result<(usize, String, String), Error<E>>
where
    P: AsRef<Path>,
    E: GitExecutor,
    F: FnMut(String, Extra) -> Fut,
    Fut: std::future::Future<Output = Option<String>>,
    Extra: Send + Clone,
{
    let mut current_exe = std::env::current_exe().map_err(Error::<E>::NoSelfExe)?;
    current_exe = current_exe.canonicalize().unwrap_or(current_exe);

    // TODO(qix-): Get parent PID of connecting processes to make sure they're us.
    // This is a bit of a hack. Under a test environment, Cargo is running a
    // test runner with a quasi-random suffix. The actual executables live in
    // the parent directory. Thus, we have to do this under test. It's not
    // ideal, but it works for now.
    //
    // TODO: remove this special case once `gitbutler-branch-actions` is gone.
    if current_exe.iter().nth_back(1) == Some("deps".as_ref()) {
        current_exe = current_exe.parent().unwrap().to_path_buf();
    }

    let askpath_path = current_exe
        .with_file_name("gitbutler-git-askpass")
        .with_extension(std::env::consts::EXE_EXTENSION);

    #[cfg(unix)]
    let setsid_path = current_exe.with_file_name("gitbutler-git-setsid");

    let res = executor.stat(&askpath_path).await.map_err(Error::<E>::Exec);
    if res.is_err() {
        let (path, prefix) = if let Some(workdir) = std::env::current_dir().ok().and_then(|cwd| {
            gix::discover::upwards(&cwd)
                .ok()
                .and_then(|p| p.0.into_repository_and_work_tree_directories().1)
        }) {
            let prefix = std::env::var_os("CARGO_TARGET_DIR")
                .map(std::path::PathBuf::from)
                .map(|path| {
                    format!(
                        "CARGO_TARGET_DIR={path} ",
                        path = path.strip_prefix(&workdir).unwrap_or(&path).display()
                    )
                })
                .unwrap_or_default();
            (
                askpath_path.strip_prefix(&workdir).unwrap_or(&askpath_path),
                prefix,
            )
        } else {