-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
279 lines (244 loc) · 8.22 KB
/
main.go
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
271
272
273
274
275
276
277
278
279
package main
import (
"flag"
"fmt"
"os/exec"
"strings"
"github.com/cli/go-gh/v2/pkg/api"
"github.com/cli/safeexec"
)
// AppConfig holds the configuration for the fork-sync command
type AppConfig struct {
UpstreamBranch string // Branch to sync from upstream (default: current branch)
OriginBranch string // Local branch to update (default: current branch)
Rebase bool // Rebase instead of merge
ForcePush bool // Force push to origin
DryRun bool // Print commands without executing them
}
// RepoInfo holds information about a GitHub repository
type RepoInfo struct {
FullName string `json:"full_name"`
Fork bool `json:"fork"`
Parent struct {
FullName string `json:"full_name"`
CloneURL string `json:"clone_url"`
} `json:"parent"`
}
// GitCommand represents a git command to be executed
type GitCommand struct {
Args []string
Description string
}
// parseFlags parses command line flags and returns the configuration
func parseFlags() *AppConfig {
config := &AppConfig{}
flag.StringVar(&config.UpstreamBranch, "upstream-branch", "main", "Branch to sync from upstream (default: main)")
flag.StringVar(&config.OriginBranch, "origin-branch", "main", "Local branch to update (default: main)")
flag.BoolVar(&config.Rebase, "rebase", false, "Rebase instead of merge")
flag.BoolVar(&config.ForcePush, "force", false, "Force push to origin")
flag.BoolVar(&config.DryRun, "dry-run", false, "Print commands without executing them")
// Add custom usage message
flag.Usage = func() {
fmt.Println("gh fork-sync - GitHub CLI extension to sync your fork with the upstream repository")
fmt.Println("\nUsage:")
fmt.Println(" gh fork-sync [flags]")
fmt.Println("\nFlags:")
flag.PrintDefaults()
fmt.Println("\nExamples:")
fmt.Println(" # Sync main branch with upstream")
fmt.Println(" $ gh fork-sync")
fmt.Println("\n # Sync a specific branch")
fmt.Println(" $ gh fork-sync --upstream-branch develop --origin-branch develop")
fmt.Println("\n # Rebase instead of merge")
fmt.Println(" $ gh fork-sync --rebase")
fmt.Println("\n # Force push the changes")
fmt.Println(" $ gh fork-sync --force")
fmt.Println("\n # Preview the commands without executing them")
fmt.Println(" $ gh fork-sync --dry-run")
}
flag.Parse()
return config
}
// GetOriginRepo returns the owner and repo name of the "origin" remote.
func GetOriginRepo() (owner, repo string, err error) {
// Get the origin remote URL
cmd := exec.Command("git", "remote", "get-url", "origin")
output, err := cmd.CombinedOutput()
if err != nil {
return "", "", fmt.Errorf("failed to get origin remote: %v", err)
}
url := strings.TrimSpace(string(output))
// Parse SSH or HTTPS URL
var parts []string
if strings.HasPrefix(url, "[email protected]:") {
// SSH format: [email protected]:owner/repo.git
path := strings.TrimPrefix(url, "[email protected]:")
parts = strings.SplitN(path, "/", 2)
} else if strings.Contains(url, "github.com/") {
// HTTPS format: https://github.com/owner/repo.git
path := strings.SplitN(url, "github.com/", 2)[1]
parts = strings.SplitN(path, "/", 2)
} else {
return "", "", fmt.Errorf("unsupported origin URL format: %s", url)
}
if len(parts) < 2 {
return "", "", fmt.Errorf("failed to parse owner/repo from URL: %s", url)
}
owner = parts[0]
repo = strings.TrimSuffix(parts[1], ".git") // Remove .git suffix if present
return owner, repo, nil
}
// getRepoInfo fetches repository information from GitHub API
func getRepoInfo(client *api.RESTClient, owner, repoName string) (*RepoInfo, error) {
info := &RepoInfo{}
err := client.Get(fmt.Sprintf("repos/%s/%s", owner, repoName), info)
if err != nil {
return nil, fmt.Errorf("failed to get repo info: %v", err)
}
return info, nil
}
// validateFork checks if the repository is a fork
func validateFork(info *RepoInfo) error {
if !info.Fork {
return fmt.Errorf("repository %s isn't a fork", info.FullName)
}
return nil
}
// runGitCommand executes a git command and returns any error
func runGitCommand(gitBin string, cmd GitCommand) error {
output, err := exec.Command(gitBin, cmd.Args...).CombinedOutput()
if err != nil {
return fmt.Errorf("%s: %v\nOutput: %s", cmd.Description, err, output)
}
return nil
}
// getSyncCommand returns the appropriate sync command (merge or rebase)
func getSyncCommand(config *AppConfig, upstreamBranch string) GitCommand {
if config.Rebase {
args := []string{"rebase", "upstream"}
if upstreamBranch != "" {
args = append(args, fmt.Sprintf("upstream/%s", upstreamBranch))
}
return GitCommand{
Args: args,
Description: "rebasing onto upstream",
}
}
args := []string{"merge", "upstream"}
if upstreamBranch != "" {
args = append(args, fmt.Sprintf("upstream/%s", upstreamBranch))
}
return GitCommand{
Args: args,
Description: "merging upstream",
}
}
// printDryRun prints commands that would be executed
func printDryRun(config *AppConfig, parentCloneURL string) {
fmt.Println("Note: The following commands are examples. The actual upstream URL will be taken from your fork's parent repository.")
fmt.Println("Dry run mode - commands that would be executed:")
fmt.Printf("Would run: git remote add upstream %s\n", parentCloneURL)
fmt.Printf("Would run: git fetch upstream\n")
if config.Rebase {
fmt.Printf("Would run: git rebase upstream/%s\n", config.UpstreamBranch)
} else {
fmt.Printf("Would run: git merge upstream/%s\n", config.UpstreamBranch)
}
pushCmd := "push"
if config.ForcePush {
pushCmd += " -f"
}
fmt.Printf("Would run: git %s origin HEAD:%s\n", pushCmd, config.OriginBranch)
}
func main() {
config := parseFlags()
// Handle dry run first, before any API calls or git operations
if config.DryRun {
// Use placeholder URL for dry run
printDryRun(config, "https://github.com/upstream/repo.git")
return
}
// Initialize the GitHub API client
client, err := api.DefaultRESTClient()
if err != nil {
fmt.Printf("✗ Error: %v\n", err)
return
}
// Get repository information
owner, repoName, err := GetOriginRepo()
if err != nil {
fmt.Printf("✗ Error: %v\n", err)
return
}
// Get and validate repository information
repoInfo, err := getRepoInfo(client, owner, repoName)
if err != nil {
fmt.Printf("✗ Error: %v\n", err)
return
}
if err := validateFork(repoInfo); err != nil {
fmt.Printf("✗ %v\n", err)
return
}
fmt.Printf("✓ Detected fork: %s (parent: %s)\n", repoInfo.FullName, repoInfo.Parent.FullName)
// Find git executable
gitBin, err := safeexec.LookPath("git")
if err != nil {
fmt.Printf("✗ Error while looking for git: %v\n", err)
return
}
// Add upstream remote
cmd := GitCommand{
Args: []string{"remote", "add", "upstream", repoInfo.Parent.CloneURL},
Description: "adding upstream remote",
}
if err := runGitCommand(gitBin, cmd); err != nil {
if !strings.Contains(err.Error(), "remote upstream already exists") {
fmt.Printf("✗ Error while %v\n", err)
return
}
}
// Fetch upstream
cmd = GitCommand{
Args: []string{"fetch", "upstream"},
Description: "fetching upstream",
}
if err := runGitCommand(gitBin, cmd); err != nil {
fmt.Printf("✗ Error while %v\n", err)
return
}
fmt.Println("✓ Fetched upstream")
// Sync with upstream
cmd = getSyncCommand(config, config.UpstreamBranch)
if err := runGitCommand(gitBin, cmd); err != nil {
fmt.Printf("✗ Error while %v\n", err)
if config.Rebase {
fmt.Println("To abort the rebase, run: git rebase --abort")
} else {
fmt.Println("To abort the merge, run: git merge --abort")
}
return
}
if config.Rebase {
fmt.Printf("✓ Rebased onto upstream/%s\n", config.UpstreamBranch)
} else {
fmt.Printf("✓ Merged upstream/%s\n", config.UpstreamBranch)
}
// Push changes
pushArgs := []string{"push"}
if config.ForcePush {
pushArgs = append(pushArgs, "-f")
}
pushArgs = append(pushArgs, "origin", fmt.Sprintf("HEAD:%s", config.OriginBranch))
cmd = GitCommand{
Args: pushArgs,
Description: fmt.Sprintf("pushing to origin/%s", config.OriginBranch),
}
if err := runGitCommand(gitBin, cmd); err != nil {
fmt.Printf("✗ Error while %v\n", err)
return
}
fmt.Printf("✓ Pushed to origin/%s\n", config.OriginBranch)
}
// For more examples of using go-gh, see:
// https://github.com/cli/go-gh/blob/trunk/example_gh_test.go