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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
package manage
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/ebisu/mugi/internal/config"
"gopkg.in/yaml.v3"
)
type RepoInfo struct {
Name string
Path string
Remotes map[string]string
}
func Add(path, configPath string, remoteDefs map[string]config.RemoteDefinition) error {
absPath, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("invalid path: %w", err)
}
if !isGitRepo(absPath) {
return fmt.Errorf("not a git repository: %s", absPath)
}
info, err := extractRepoInfo(absPath, remoteDefs)
if err != nil {
return err
}
return appendToConfig(configPath, info)
}
func Remove(name, configPath string) error {
cfg, err := config.Load(configPath)
if err != nil {
return err
}
fullName, _, found := cfg.FindRepo(name)
if !found {
return fmt.Errorf("repository not found: %s", name)
}
return removeFromConfig(configPath, fullName)
}
func List(configPath string) ([]RepoInfo, error) {
cfg, err := config.Load(configPath)
if err != nil {
return nil, err
}
var repos []RepoInfo
for name, repo := range cfg.Repos {
repos = append(repos, RepoInfo{
Name: name,
Path: repo.ExpandPath(),
Remotes: repo.Remotes,
})
}
return repos, nil
}
func isGitRepo(path string) bool {
cmd := exec.Command("git", "rev-parse", "--git-dir")
cmd.Dir = path
return cmd.Run() == nil
}
func extractRepoInfo(path string, remoteDefs map[string]config.RemoteDefinition) (RepoInfo, error) {
info := RepoInfo{
Path: path,
Remotes: make(map[string]string),
}
cmd := exec.Command("git", "remote", "-v")
cmd.Dir = path
out, err := cmd.Output()
if err != nil {
return info, fmt.Errorf("failed to get remotes: %w", err)
}
remoteURLs := parseRemotes(string(out))
for remoteName, url := range remoteURLs {
knownRemote := matchRemoteURL(url, remoteDefs)
if knownRemote != "" {
info.Remotes[knownRemote] = url
} else {
info.Remotes[remoteName] = url
}
}
info.Name = inferRepoName(path, remoteURLs)
return info, nil
}
func parseRemotes(output string) map[string]string {
remotes := make(map[string]string)
for line := range strings.SplitSeq(output, "\n") {
if !strings.Contains(line, "(fetch)") {
continue
}
parts := strings.Fields(line)
if len(parts) >= 2 {
remotes[parts[0]] = parts[1]
}
}
return remotes
}
func matchRemoteURL(url string, remoteDefs map[string]config.RemoteDefinition) string {
for name, def := range remoteDefs {
template := def.URL
if template == "" {
continue
}
pattern := strings.ReplaceAll(template, "${user}", "")
pattern = strings.ReplaceAll(pattern, "${repo}", "")
base := strings.Split(pattern, ":")[0]
if strings.Contains(url, base) || strings.Contains(url, name) {
return name
}
}
return ""
}
func inferRepoName(path string, remotes map[string]string) string {
for _, url := range remotes {
name := extractRepoNameFromURL(url)
if name != "" {
return name
}
}
return filepath.Base(path)
}
func extractRepoNameFromURL(url string) string {
url = strings.TrimSuffix(url, ".git")
if strings.Contains(url, ":") {
parts := strings.Split(url, ":")
if len(parts) == 2 {
return strings.TrimPrefix(parts[1], "~")
}
}
if strings.Contains(url, "/") {
parts := strings.Split(url, "/")
if len(parts) >= 2 {
return parts[len(parts)-2] + "/" + parts[len(parts)-1]
}
}
return ""
}
func appendToConfig(configPath string, info RepoInfo) error {
data, err := os.ReadFile(configPath)
if err != nil {
return err
}
var raw map[string]yaml.Node
if err := yaml.Unmarshal(data, &raw); err != nil {
return err
}
reposNode, ok := raw["repos"]
if !ok {
return fmt.Errorf("repos section not found in config")
}
repoEntry := map[string]any{
"path": info.Path,
"remotes": info.Remotes,
}
entryBytes, err := yaml.Marshal(map[string]any{info.Name: repoEntry})
if err != nil {
return err
}
var entryNode yaml.Node
if err := yaml.Unmarshal(entryBytes, &entryNode); err != nil {
return err
}
if reposNode.Kind == yaml.MappingNode && len(entryNode.Content) > 0 && len(entryNode.Content[0].Content) >= 2 {
reposNode.Content = append(reposNode.Content, entryNode.Content[0].Content...)
raw["repos"] = reposNode
}
output, err := yaml.Marshal(raw)
if err != nil {
return err
}
return os.WriteFile(configPath, output, 0o644)
}
func removeFromConfig(configPath, name string) error {
data, err := os.ReadFile(configPath)
if err != nil {
return err
}
var raw map[string]yaml.Node
if err := yaml.Unmarshal(data, &raw); err != nil {
return err
}
reposNode, ok := raw["repos"]
if !ok {
return fmt.Errorf("repos section not found in config")
}
if reposNode.Kind != yaml.MappingNode {
return fmt.Errorf("repos section is not a mapping")
}
var newContent []*yaml.Node
for i := 0; i < len(reposNode.Content); i += 2 {
if i+1 >= len(reposNode.Content) {
break
}
if reposNode.Content[i].Value != name {
newContent = append(newContent, reposNode.Content[i], reposNode.Content[i+1])
}
}
reposNode.Content = newContent
raw["repos"] = reposNode
output, err := yaml.Marshal(raw)
if err != nil {
return err
}
return os.WriteFile(configPath, output, 0o644)
}
|