Skip to content

Commit 0ef8e86

Browse files
The scripts now use LibGit2 throughout
1 parent 22648cd commit 0ef8e86

File tree

4 files changed

+251
-156
lines changed

4 files changed

+251
-156
lines changed

scripts/clone.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using LibGit2
22
using Downloads
3-
using Printf
43

54
repos = [
6-
"https://github.com/JuliaHealth/MedEval3D.jl.git", #Example, can add more repos here
5+
"https://github.com/JuliaHealth/MedEval3D.jl.git",
76
]
87

98
data_dir = "data/exp_raw"
@@ -17,13 +16,13 @@ for repo in repos
1716
repo_path = joinpath(data_dir, replace(repo_name, ".git" => ""))
1817

1918
if isdir(repo_path)
20-
@printf("Skipping %s, already cloned.\n", repo)
19+
println("Skipping $repo, already cloned.")
2120
else
2221
try
2322
LibGit2.clone(repo, repo_path)
24-
@printf("Successfully cloned %s\n", repo)
23+
println("Successfully cloned $repo")
2524
catch e
26-
@printf("Failed to clone %s: %s\n", repo, e)
25+
println("Failed to clone $repo: $e")
2726
end
2827
end
2928
end

scripts/restore_submodules.jl

Lines changed: 75 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,100 @@
1+
using LibGit2
2+
13
function restore_all_submodules()
24
try
3-
# Find the git repository root
4-
git_root = nothing
5+
repo_path = nothing
56
try
6-
git_root = strip(read(`git rev-parse --show-toplevel`, String))
7+
repo = LibGit2.GitRepo(".")
8+
repo_path = LibGit2.path(repo)
9+
LibGit2.close(repo)
710
catch
811
error("Not inside a git repository")
912
end
1013

11-
if isempty(git_root) || !isdir(git_root)
14+
if isnothing(repo_path) || !isdir(repo_path)
1215
error("Could not determine git repository root")
1316
end
1417

15-
# Change to git root directory for operations
16-
cd(git_root) do
17-
println("Working in git repository root: $git_root")
18-
19-
# Get list of submodules from git config
20-
println("Discovering submodules...")
21-
submodules_output = read(`git submodule status --recursive`, String)
22-
if isempty(strip(submodules_output))
23-
println("No submodules found in this repository")
24-
return
18+
println("Working in git repository root: $repo_path")
19+
20+
repo = LibGit2.GitRepo(repo_path)
21+
22+
println("Discovering submodules...")
23+
config = LibGit2.GitConfig(repo)
24+
submodules = String[]
25+
26+
gitmodules_path = joinpath(repo_path, ".gitmodules")
27+
if isfile(gitmodules_path)
28+
try
29+
LibGit2.with(LibGit2.GitConfig(gitmodules_path)) do cfg
30+
i = 1
31+
while true
32+
submod_key = "submodule.$i.path"
33+
try
34+
path = LibGit2.get(cfg, submod_key, "")
35+
if !isempty(path)
36+
push!(submodules, path)
37+
end
38+
i += 1
39+
catch
40+
break
41+
end
42+
end
43+
end
44+
catch e
45+
println("Warning: Could not parse .gitmodules: ", e)
2546
end
26-
27-
# Parse submodule paths
28-
submodule_paths = String[]
29-
for line in split(submodules_output, '\n')
30-
if !isempty(strip(line))
31-
# Extract path (second field in submodule status output)
32-
parts = split(strip(line))
33-
if length(parts) >= 2
34-
push!(submodule_paths, parts[2])
47+
end
48+
49+
if isempty(submodules)
50+
LibGit2.with(config) do cfg
51+
for i in 1:100
52+
try
53+
path = LibGit2.get(cfg, "submodule.sub$i.path", "")
54+
if !isempty(path)
55+
push!(submodules, path)
56+
end
57+
catch
58+
break
3559
end
3660
end
3761
end
62+
end
63+
64+
if isempty(submodules)
65+
println("No submodules found in this repository")
66+
LibGit2.close(repo)
67+
return
68+
end
3869

39-
if isempty(submodule_paths)
40-
println("No active submodules found")
41-
return
70+
for submod_path in submodules
71+
println("\nProcessing submodule at: $submod_path")
72+
73+
if !isdir(joinpath(repo_path, submod_path))
74+
println("Warning: Submodule directory $submod_path not found, initializing...")
4275
end
43-
44-
# Restore each submodule
45-
for path in submodule_paths
46-
println("\nProcessing submodule at: $path")
47-
if !isdir(path)
48-
println("Warning: Submodule directory $path not found, initializing...")
76+
77+
try
78+
submod = LibGit2.GitSubmodule(repo, submod_path)
79+
80+
if !LibGit2.isinit(submod)
81+
LibGit2.set_init(submod)
4982
end
5083

51-
# Initialize and update each submodule
52-
run(`git submodule init $path`)
53-
run(`git submodule update --recursive $path`)
84+
LibGit2.update(submod; recursive=true)
85+
86+
println("Current status of $submod_path:")
87+
submod_head = LibGit2.head_oid(submod)
88+
println("HEAD at: ", string(submod_head))
5489

55-
# Show status for this submodule
56-
println("Current status of $path:")
57-
run(`git submodule status $path`)
90+
catch e
91+
println("Failed to process submodule $submod_path: ", e)
5892
end
59-
60-
println("\nAll submodules successfully restored to recorded commits!")
6193
end
6294

95+
println("\nAll submodules successfully restored to recorded commits!")
96+
LibGit2.close(repo)
97+
6398
catch e
6499
println("Error occurred: ", e)
65100
println("Failed to restore submodules. Please ensure:")

scripts/update_and_record.jl

Lines changed: 94 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,118 @@
1+
using LibGit2
12
using Dates
23

34
function update_and_record_submodules()
45
try
5-
git_root = nothing
6+
repo_path = nothing
67
try
7-
git_root = strip(read(`git rev-parse --show-toplevel`, String))
8+
repo = LibGit2.GitRepo(".")
9+
repo_path = LibGit2.path(repo)
10+
LibGit2.close(repo)
811
catch
912
error("Not inside a git repository")
1013
end
1114

12-
if isempty(git_root) || !isdir(git_root)
15+
if isnothing(repo_path) || !isdir(repo_path)
1316
error("Could not determine git repository root")
1417
end
1518

16-
output_file = joinpath(git_root, "submodule_hashes.txt")
19+
output_file = joinpath(repo_path, "submodule_hashes.txt")
1720

18-
cd(git_root) do
19-
println("Working in git repository root: $git_root")
20-
21-
println("Initializing submodules...")
22-
run(`git submodule init`)
23-
24-
println("\nUpdating submodules to latest commits...")
25-
run(`git submodule update --remote --recursive`)
26-
27-
println("\nRecording submodule states...")
28-
submodules_output = read(`git submodule status --recursive`, String)
29-
if isempty(strip(submodules_output))
30-
println("No submodules found in this repository")
31-
return
21+
repo = LibGit2.GitRepo(repo_path)
22+
println("Working in git repository root: $repo_path")
23+
24+
config = LibGit2.GitConfig(repo)
25+
submodules = Dict{String,String}()
26+
27+
println("Discovering submodules...")
28+
gitmodules_path = joinpath(repo_path, ".gitmodules")
29+
if isfile(gitmodules_path)
30+
LibGit2.with(LibGit2.GitConfig(gitmodules_path)) do cfg
31+
for i in 1:100
32+
try
33+
path = LibGit2.get(cfg, "submodule.sub$i.path", "")
34+
if !isempty(path)
35+
submodules[path] = ""
36+
end
37+
catch
38+
break
39+
end
40+
end
3241
end
42+
end
43+
44+
if isempty(submodules)
45+
println("No submodules found in this repository")
46+
LibGit2.close(repo)
47+
return
48+
end
3349

34-
open(output_file, "w") do io
35-
println(io, "Submodule Commit Hashes - Recorded on: ", Dates.now())
36-
println(io, "=========================================")
50+
println("Initializing submodules...")
51+
println("\nUpdating submodules to latest commits...")
52+
53+
for (submod_path, _) in submodules
54+
try
55+
submod = LibGit2.GitSubmodule(repo, submod_path)
3756

38-
for line in split(submodules_output, '\n')
39-
if !isempty(strip(line))
40-
parts = split(strip(line))
41-
if length(parts) >= 2
42-
commit_hash = parts[1]
43-
commit_hash = replace(commit_hash, r"^[+-]" => "")
44-
path = parts[2]
45-
46-
println("Recording: $path @ $commit_hash")
47-
println(io, "Path: $path")
48-
println(io, "Commit: $commit_hash")
49-
println(io, "")
50-
end
51-
end
57+
if !LibGit2.isinit(submod)
58+
LibGit2.set_init(submod)
5259
end
60+
61+
LibGit2.update(submod; recursive=true, fetch=true)
62+
63+
commit_hash = string(LibGit2.head_oid(submod))
64+
submodules[submod_path] = commit_hash
65+
66+
catch e
67+
println("Failed to update submodule $submod_path: ", e)
5368
end
69+
end
70+
71+
println("\nRecording submodule states...")
72+
open(output_file, "w") do io
73+
println(io, "Submodule Commit Hashes - Recorded on: ", Dates.now())
74+
println(io, "=========================================")
5475

55-
println("\nChecking for changes to commit...")
56-
status_output = read(`git status --porcelain`, String)
57-
58-
if !isempty(strip(status_output))
59-
println("Committing submodule updates...")
60-
run(`git add .gitmodules`)
61-
run(`git add $output_file`)
62-
run(`git commit -m "Updated submodules to latest commits and recorded hashes"`)
63-
println("Changes committed successfully")
64-
else
65-
println("No changes to commit - submodules were already up to date")
76+
for (path, commit_hash) in submodules
77+
if !isempty(commit_hash)
78+
println("Recording: $path @ $commit_hash")
79+
println(io, "Path: $path")
80+
println(io, "Commit: $commit_hash")
81+
println(io, "")
82+
end
6683
end
67-
68-
println("\nSubmodule updates completed!")
69-
println("Hashes recorded in: $output_file")
70-
println("Current status:")
71-
run(`git submodule status --recursive`)
7284
end
85+
86+
println("\nChecking for changes to commit...")
87+
status = LibGit2.status(repo)
88+
89+
if !isempty(status)
90+
println("Committing submodule updates...")
91+
idx = LibGit2.GitIndex(repo)
92+
LibGit2.add!(idx, ".gitmodules")
93+
LibGit2.add!(idx, "submodule_hashes.txt")
94+
LibGit2.write!(idx)
95+
96+
tree_id = LibGit2.write_tree!(idx)
97+
parents = LibGit2.isdetached(repo) ? [] : [LibGit2.head_oid(repo)]
98+
sig = LibGit2.default_signature(repo)
99+
LibGit2.commit(repo, "Updated submodules to latest commits and recorded hashes";
100+
tree=tree_id, parents=parents, author=sig, committer=sig)
101+
println("Changes committed successfully")
102+
else
103+
println("No changes to commit - submodules were already up to date")
104+
end
105+
106+
println("\nSubmodule updates completed!")
107+
println("Hashes recorded in: $output_file")
108+
println("Current status:")
109+
for (submod_path, commit_hash) in submodules
110+
if !isempty(commit_hash)
111+
println("$submod_path $commit_hash")
112+
end
113+
end
114+
115+
LibGit2.close(repo)
73116

74117
catch e
75118
println("Error occurred: ", e)

0 commit comments

Comments
 (0)