|
| 1 | +using LibGit2 |
1 | 2 | using Dates |
2 | 3 |
|
3 | 4 | function update_and_record_submodules() |
4 | 5 | try |
5 | | - git_root = nothing |
| 6 | + repo_path = nothing |
6 | 7 | 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) |
8 | 11 | catch |
9 | 12 | error("Not inside a git repository") |
10 | 13 | end |
11 | 14 |
|
12 | | - if isempty(git_root) || !isdir(git_root) |
| 15 | + if isnothing(repo_path) || !isdir(repo_path) |
13 | 16 | error("Could not determine git repository root") |
14 | 17 | end |
15 | 18 |
|
16 | | - output_file = joinpath(git_root, "submodule_hashes.txt") |
| 19 | + output_file = joinpath(repo_path, "submodule_hashes.txt") |
17 | 20 |
|
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 |
32 | 41 | end |
| 42 | + end |
| 43 | + |
| 44 | + if isempty(submodules) |
| 45 | + println("No submodules found in this repository") |
| 46 | + LibGit2.close(repo) |
| 47 | + return |
| 48 | + end |
33 | 49 |
|
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) |
37 | 56 |
|
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) |
52 | 59 | 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) |
53 | 68 | 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, "=========================================") |
54 | 75 |
|
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 |
66 | 83 | end |
67 | | - |
68 | | - println("\nSubmodule updates completed!") |
69 | | - println("Hashes recorded in: $output_file") |
70 | | - println("Current status:") |
71 | | - run(`git submodule status --recursive`) |
72 | 84 | 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) |
73 | 116 |
|
74 | 117 | catch e |
75 | 118 | println("Error occurred: ", e) |
|
0 commit comments