-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore_submodules.jl
137 lines (116 loc) · 4.34 KB
/
restore_submodules.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
restore_all_submodules()
Restores all Git submodules in the current repository to their recorded commits.
This function:
1. Detects if the current directory is within a Git repository
2. Locates and parses the repository's submodules from either `.gitmodules` file or Git config
3. For each submodule:
- Initializes it if not already initialized
- Updates it to its recorded commit
- Reports the current HEAD status
4. Handles errors gracefully with appropriate feedback
# Throws
- Error if not inside a Git repository
- Error if repository root cannot be determined
- Prints warnings for missing submodules or parsing issues
- Exits with status 1 on fatal errors
# Examples
```julia
julia> cd("path/to/git/repo")
julia> restore_all_submodules()
Working in git repository root: /path/to/git/repo
Discovering submodules...
Processing submodule at: submod1
Current status of submod1:
HEAD at: abc123...
All submodules successfully restored to recorded commits!
```
"""
function restore_all_submodules()
try
repo_path = nothing
try
repo = LibGit2.GitRepo(".")
repo_path = LibGit2.path(repo)
LibGit2.close(repo)
catch
error("Not inside a git repository")
end
if isnothing(repo_path) || !isdir(repo_path)
error("Could not determine git repository root")
end
println("Working in git repository root: $repo_path")
repo = LibGit2.GitRepo(repo_path)
println("Discovering submodules...")
config = LibGit2.GitConfig(repo)
submodules = String[]
gitmodules_path = joinpath(repo_path, ".gitmodules")
if isfile(gitmodules_path)
try
LibGit2.with(LibGit2.GitConfig(gitmodules_path)) do cfg
i = 1
while true
submod_key = "submodule.$i.path"
try
path = LibGit2.get(cfg, submod_key, "")
if !isempty(path)
push!(submodules, path)
end
i += 1
catch
break
end
end
end
catch e
println("Warning: Could not parse .gitmodules: ", e)
end
end
if isempty(submodules)
LibGit2.with(config) do cfg
for i in 1:100
try
path = LibGit2.get(cfg, "submodule.sub$i.path", "")
if !isempty(path)
push!(submodules, path)
end
catch
break
end
end
end
end
if isempty(submodules)
println("No submodules found in this repository")
LibGit2.close(repo)
return
end
for submod_path in submodules
println("\nProcessing submodule at: $submod_path")
if !isdir(joinpath(repo_path, submod_path))
println("Warning: Submodule directory $submod_path not found, initializing...")
end
try
submod = LibGit2.GitSubmodule(repo, submod_path)
if !LibGit2.isinit(submod)
LibGit2.set_init(submod)
end
LibGit2.update(submod; recursive=true)
println("Current status of $submod_path:")
submod_head = LibGit2.head_oid(submod)
println("HEAD at: ", string(submod_head))
catch e
println("Failed to process submodule $submod_path: ", e)
end
end
println("\nAll submodules successfully restored to recorded commits!")
LibGit2.close(repo)
catch e
println("Error occurred: ", e)
println("Failed to restore submodules. Please ensure:")
println("1. Git is installed and accessible")
println("2. You have permission to access the submodules")
println("3. You're inside a git repository (any subdirectory)")
exit(1)
end
end