Skip to content

Commit

Permalink
chore: only assign git env vars if not present
Browse files Browse the repository at this point in the history
  • Loading branch information
Eckhardt-D committed Jun 20, 2024
1 parent 44401d4 commit ffe7f72
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/env_loader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ pub const Map = struct {
}

pub fn remove(this: *Map, key: string) void {
this.map.remove(key);
_ = this.map.swapRemove(key);
}
};

Expand Down
33 changes: 24 additions & 9 deletions src/install/repository.zig
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,31 @@ pub const Repository = extern struct {
env: *DotEnv.Loader,
argv: []const string,
) !string {
// Note: currently if the user sets this to some value that causes
// a prompt for a password, the stdout of the prompt will be masked
// by further output of the rest of the install process.
// A value can still be entered, but we need to find a workaround
// so the user can see what is being prompted. By default the settings
// below will cause no prompt and throw instead.
const originalAskPass = env.map.get("GIT_ASKPASS");
const originalSSHCommand = env.map.get("GIT_SSH_COMMAND");

try env.map.putAllocKeyAndValue(allocator, "GIT_ASKPASS", "echo");
try env.map.putAllocKeyAndValue(allocator, "SSH_ASKPASS", "echo");
try env.map.putAllocKeyAndValue(allocator, "GIT_SSH_COMMAND", "ssh -oStrictHostKeyChecking=accept-new");

var std_map = try env.map.stdEnvMap(allocator);
// Run git clone in non-interactive mode and
// error if ssh or https asks for a password
// so that we can fallback to ssh mode
try std_map.unsafe_map.put("GIT_TERMINAL_PROMPT", "0");
try std_map.unsafe_map.put("GIT_ASKPASS", "echo");
try std_map.unsafe_map.put("SSH_ASKPASS", "echo");
try std_map.unsafe_map.put("GCM_INTERACTIVE", "never");

defer std_map.deinit();

defer {
_ = if (originalAskPass) |value| env.map.putAllocKeyAndValue(allocator, "GIT_ASKPASS", value) catch void
else env.map.remove("GIT_ASKPASS");

_ = if (originalSSHCommand) |value| env.map.putAllocKeyAndValue(allocator, "GIT_SSH_COMMAND", value) catch void
else env.map.remove("GIT_SSH_COMMAND");

// Remove the previous environment variables if they were set
std_map.deinit();
}

const result = if (comptime Environment.isWindows)
try std.process.Child.run(.{
Expand Down

0 comments on commit ffe7f72

Please sign in to comment.