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
|
package cli
import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"codeberg.org/snonux/gitsyncer/internal/codeberg"
"codeberg.org/snonux/gitsyncer/internal/config"
"codeberg.org/snonux/gitsyncer/internal/github"
)
// syncRepoDescriptions ensures both platforms have the canonical description
// Precedence: Codeberg > GitHub; if Codeberg empty and GitHub has one, use GitHub.
// knownCBDesc and knownGHDesc can be empty; the function fetches as needed.
func syncRepoDescriptions(cfg *config.Config, dryRun bool, repoName, knownCBDesc, knownGHDesc string, cache map[string]string) {
// Load orgs
ghOrg := cfg.FindGitHubOrg()
cbOrg := cfg.FindCodebergOrg()
var ghClient *github.Client
var cbClient *codeberg.Client
if ghOrg != nil {
c := github.NewClient(ghOrg.GitHubToken, ghOrg.Name)
ghClient = &c
}
if cbOrg != nil {
c := codeberg.NewClient(cbOrg.Name, cbOrg.CodebergToken)
cbClient = &c
}
// Get current descriptions (use known if provided)
cbDesc := strings.TrimSpace(knownCBDesc)
ghDesc := strings.TrimSpace(knownGHDesc)
var cbExists, ghExists bool
if cbDesc == "" && cbClient != nil {
if repo, exists, err := cbClient.GetRepo(repoName); err == nil {
cbExists = exists
if exists {
cbDesc = strings.TrimSpace(repo.Description)
}
} else {
fmt.Printf(" Warning: Codeberg repo lookup failed: %v\n", err)
}
} else if cbClient != nil {
cbExists = true
}
if ghClient != nil {
if ghDesc == "" || !ghExists {
if repo, exists, err := ghClient.GetRepo(repoName); err == nil {
ghExists = exists
if exists {
ghDesc = strings.TrimSpace(repo.Description)
}
} else {
fmt.Printf(" Warning: GitHub repo lookup failed: %v\n", err)
}
}
}
// Determine canonical description
canonical := cbDesc
if canonical == "" {
canonical = ghDesc
}
canonical = strings.TrimSpace(canonical)
// If nothing to sync, bail
if canonical == "" {
return
}
// Update Codeberg if needed
if cbClient != nil && cbExists {
if cbDesc != canonical {
if dryRun {
fmt.Printf(" [DRY RUN] Would update Codeberg description for %s -> %q\n", repoName, canonical)
} else if cbClient.HasToken() {
if err := cbClient.UpdateRepoDescription(repoName, canonical); err != nil {
fmt.Printf(" Warning: Failed to update Codeberg description: %v\n", err)
} else {
fmt.Printf(" Updated Codeberg description for %s\n", repoName)
}
} else {
fmt.Println(" Warning: No Codeberg token; cannot update description")
}
}
}
// Update GitHub if needed
if ghClient != nil && ghExists {
if ghDesc != canonical {
if dryRun {
fmt.Printf(" [DRY RUN] Would update GitHub description for %s -> %q\n", repoName, canonical)
} else if ghClient.HasToken() {
if err := ghClient.UpdateRepoDescription(repoName, canonical); err != nil {
fmt.Printf(" Warning: Failed to update GitHub description: %v\n", err)
} else {
fmt.Printf(" Updated GitHub description for %s\n", repoName)
}
} else {
fmt.Println(" Warning: No GitHub token; cannot update description")
}
}
}
syncBackupDescriptions(cfg, dryRun, repoName, canonical)
// Update cache
if cache != nil {
cache[repoName] = canonical
}
}
func syncBackupDescriptions(cfg *config.Config, dryRun bool, repoName, canonical string) {
if cfg == nil || canonical == "" {
return
}
for i := range cfg.Organizations {
org := &cfg.Organizations[i]
if !org.BackupLocation {
continue
}
supported, err := syncBackupDescription(org, repoName, canonical, dryRun)
if err != nil {
fmt.Printf(" Warning: Failed to update backup description on %s: %v\n", org.Host, err)
continue
}
if supported && !dryRun {
fmt.Printf(" Updated backup description for %s on %s\n", repoName, org.Host)
}
}
}
func syncBackupDescription(org *config.Organization, repoName, description string, dryRun bool) (bool, error) {
if org == nil || !org.BackupLocation {
return false, nil
}
description = strings.TrimSpace(description)
if description == "" {
return false, nil
}
if strings.HasPrefix(org.Host, "file://") {
descriptionPath := filepath.Join(strings.TrimPrefix(org.Host, "file://"), repoName+".git", "description")
if dryRun {
fmt.Printf(" [DRY RUN] Would update backup description for %s on %s -> %q\n", repoName, org.Host, description)
return true, nil
}
return true, os.WriteFile(descriptionPath, []byte(description+"\n"), 0644)
}
if strings.TrimSpace(org.DescriptionSyncHost) == "" || strings.TrimSpace(org.DescriptionSyncRoot) == "" {
return false, nil
}
descriptionPath := path.Join(org.DescriptionSyncRoot, repoName+".git", "description")
if dryRun {
fmt.Printf(" [DRY RUN] Would update backup description for %s on %s -> %q\n", repoName, org.Host, description)
return true, nil
}
cmd := exec.Command("ssh", org.DescriptionSyncHost, fmt.Sprintf("cat > %s", shellSingleQuote(descriptionPath)))
cmd.Stdin = strings.NewReader(description + "\n")
output, err := cmd.CombinedOutput()
if err != nil {
return true, fmt.Errorf("ssh write failed: %w\n%s", err, strings.TrimSpace(string(output)))
}
return true, nil
}
func shellSingleQuote(value string) string {
return "'" + strings.ReplaceAll(value, "'", `'\''`) + "'"
}
|