Skip to content

Commit 981401a

Browse files
committed
refactor
1 parent f1d3d3a commit 981401a

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

src/git_function.jl

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ julia> run(git(["clone", "https://github.com/JuliaRegistries/General"]))
2222
to bypass the parsing of the command string.
2323
"""
2424
function git(; adjust_PATH::Bool = true, adjust_LIBPATH::Bool = true)
25-
git_cmd = @static if Sys.iswindows()
26-
Git_jll.git(; adjust_PATH, adjust_LIBPATH)::Cmd
27-
else
25+
git_cmd = Git_jll.git(; adjust_PATH, adjust_LIBPATH)::Cmd
26+
env_mapping = Dict{String,String}(
27+
"PATH" => _get_cmd_env(git_cmd, "PATH"),
28+
LIBPATH_env => _get_cmd_env(git_cmd, LIBPATH_env),
29+
)
30+
@static if !Sys.iswindows()
2831
root = Git_jll.artifact_dir
2932

3033
libexec = joinpath(root, "libexec")
@@ -36,7 +39,6 @@ function git(; adjust_PATH::Bool = true, adjust_LIBPATH::Bool = true)
3639

3740
ssl_cert = joinpath(dirname(Sys.BINDIR), "share", "julia", "cert.pem")
3841

39-
env_mapping = Dict{String,String}()
4042
env_mapping["GIT_EXEC_PATH"] = libexec_git_core
4143
env_mapping["GIT_SSL_CAINFO"] = ssl_cert
4244
env_mapping["GIT_TEMPLATE_DIR"] = share_git_core_templates
@@ -47,15 +49,12 @@ function git(; adjust_PATH::Bool = true, adjust_LIBPATH::Bool = true)
4749
# more details.
4850
env_mapping["JLL_DYLD_FALLBACK_LIBRARY_PATH"] = Git_jll.LIBPATH[]
4951
end
50-
51-
original_cmd = Git_jll.git(; adjust_PATH, adjust_LIBPATH)::Cmd
52-
addenv(original_cmd, env_mapping...)::Cmd
5352
end
5453

5554
# Use OpenSSH from the JLL: <https://github.com/JuliaVersionControl/Git.jl/issues/51>.
5655
if !Sys.iswindows() && OpenSSH_jll.is_available()
57-
path = split(get(ENV, "PATH", ""), pathsep)
58-
libpath = split(get(ENV, LIBPATH_env, ""), pathsep)
56+
path = split(get(env_mapping, "PATH", ""), pathsep)
57+
libpath = split(get(env_mapping, LIBPATH_env, ""), pathsep)
5958

6059
path = vcat(dirname(OpenSSH_jll.ssh_path), path)
6160
libpath = vcat(OpenSSH_jll.LIBPATH_list, libpath)
@@ -65,27 +64,36 @@ function git(; adjust_PATH::Bool = true, adjust_LIBPATH::Bool = true)
6564
unique!(filter!(!isempty, path))
6665
unique!(filter!(!isempty, libpath))
6766

68-
git_cmd = addenv(git_cmd, "PATH" => join(path, pathsep), LIBPATH_env => join(libpath, pathsep))
67+
env_mapping["PATH"] = join(path, pathsep)
68+
env_mapping[LIBPATH_env] = join(libpath, pathsep)
6969
end
7070

7171
# Add git-lfs
7272
if Git_LFS_jll.is_available()
73-
# Read path from git_cmd.env as it can be modified above
74-
idx = findfirst(startswith("PATH="), git_cmd.env)
75-
path = if isnothing(idx)
76-
""
77-
else
78-
# dropping the `PATH=` part
79-
git_cmd.env[idx][6:end]
80-
end
81-
path = vcat(dirname(Git_LFS_jll.git_lfs_path), path)
82-
git_cmd = addenv(git_cmd, "PATH" => join(path, pathsep))::Cmd
73+
env_mapping["PATH"] = string(
74+
dirname(Git_LFS_jll.git_lfs_path),
75+
pathsep,
76+
get(env_mapping, "PATH", "")
77+
)
8378
end
84-
return git_cmd
79+
80+
return addenv(git_cmd, env_mapping...)::Cmd
8581
end
8682

8783
function git(args::AbstractVector{<:AbstractString}; kwargs...)
8884
cmd = git(; kwargs...)
8985
append!(cmd.exec, args)
9086
return cmd
9187
end
88+
89+
# The .env field of a Cmd object is an array of strings in the format
90+
# `$(key)=$(value)` for each environment variable.
91+
function _get_cmd_env(cmd::Cmd, key::AbstractString)
92+
idx = findfirst(startswith("$(key)="), cmd.env)
93+
if isnothing(idx)
94+
return ""
95+
else
96+
# dropping the `$(key)=` part
97+
return cmd.env[idx][(ncodeunits(key)+2):end]
98+
end
99+
end

0 commit comments

Comments
 (0)