Skip to content

Commit 4884d21

Browse files
authored
Run git gc on git registries during Pkg.gc. (#4434)
1 parent 5dae2d7 commit 4884d21

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/API.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,34 @@ function gc(ctx::Context = Context(); collect_delay::Union{Period, Nothing} = no
10991099
printpkgstyle(ctx.io, :Deleted, "no artifacts, repos, packages or scratchspaces")
11001100
end
11011101

1102+
# Run git gc on registries if git is available
1103+
if Sys.which("git") !== nothing
1104+
for depot in gc_depots
1105+
reg_dir = joinpath(depot, "registries")
1106+
isdir(reg_dir) || continue
1107+
1108+
for reg_name in readdir(reg_dir)
1109+
reg_path = joinpath(reg_dir, reg_name)
1110+
isdir(reg_path) || continue
1111+
git_dir = joinpath(reg_path, ".git")
1112+
isdir(git_dir) || continue
1113+
1114+
try
1115+
if verbose
1116+
printpkgstyle(ctx.io, :GC, "running git gc on registry $(reg_name)")
1117+
end
1118+
# Run git gc quietly, don't error if it fails
1119+
run(`git -C $reg_path gc --quiet`)
1120+
catch e
1121+
# Silently ignore errors from git gc
1122+
if verbose
1123+
@warn "git gc failed for registry $(reg_name)" exception = e
1124+
end
1125+
end
1126+
end
1127+
end
1128+
end
1129+
11021130
return
11031131
end
11041132

test/registry.jl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,4 +450,48 @@ if Pkg.Registry.registry_use_pkg_server()
450450
end
451451
end
452452

453+
@testset "gc runs git gc on registries" begin
454+
# Only run this test if git is available
455+
if Sys.which("git") !== nothing
456+
temp_pkg_dir() do depot
457+
# Set up a test registry that is a git repository
458+
regdir = mktempdir()
459+
regpath = joinpath(regdir, "TestReg")
460+
mkpath(joinpath(regpath, "TestPkg"))
461+
write(
462+
joinpath(regpath, "Registry.toml"), """
463+
name = "TestReg"
464+
uuid = "$(uuid4())"
465+
repo = "https://github.com/test/test.git"
466+
"""
467+
)
468+
write(
469+
joinpath(regpath, "TestPkg", "Package.toml"), """
470+
name = "TestPkg"
471+
uuid = "$(uuid4())"
472+
repo = "https://github.com/test/TestPkg.git"
473+
"""
474+
)
475+
git_init_and_commit(regpath)
476+
477+
# Install the registry
478+
target_reg_path = joinpath(depot, "registries", "TestReg")
479+
mkpath(dirname(target_reg_path))
480+
cp(regpath, target_reg_path)
481+
482+
# Verify the registry is a git repository
483+
@test isdir(joinpath(target_reg_path, ".git"))
484+
485+
# Run Pkg.gc() - it should run git gc on the registry without errors
486+
# We can't easily verify that git gc was run, but we can verify
487+
# that Pkg.gc() completes without errors
488+
@test_nowarn Pkg.gc(verbose = false)
489+
490+
# The registry should still exist after gc
491+
@test isdir(target_reg_path)
492+
@test isdir(joinpath(target_reg_path, ".git"))
493+
end
494+
end
495+
end
496+
453497
end # module

0 commit comments

Comments
 (0)