Skip to content

Commit

Permalink
feat: support comparing against main on forked repo
Browse files Browse the repository at this point in the history
When doing some open source work I realized I couldn't easily compare my
forked branch against the upstream repos main branch.
  • Loading branch information
wassimk committed May 19, 2024
1 parent cd81ec7 commit b49d4ae
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,33 @@ func main() {
if err != nil {
log.Fatal(err)
}

currentBranch := ref.Name().Short()

var compareArgument string
var forkedRepo bool
var originRepo string

remotes, err := gitRepo.Remotes()
if err != nil {
log.Fatal(err)
}

for _, remote := range remotes {
if remote.Config().Name == "upstream" {
forkedRepo = true
}

if len(os.Args) == 2 {
if remote.Config().Name == "origin" {
originRepo = parseRepoOriginOwnerFromURL(remote.Config().URLs[0])
}
}

if forkedRepo {
compareArgument = fmt.Sprintf("main...%s:%s", originRepo, currentBranch)
} else if len(os.Args) == 2 {
compareArgument = os.Args[1]

if !(strings.Contains(compareArgument, "..") || strings.Contains(compareArgument, "...")) {
if !strings.Contains(compareArgument, "..") && !strings.Contains(compareArgument, "...") {
compareArgument = compareArgument + "..." + currentBranch
}
} else {
Expand All @@ -40,7 +58,6 @@ func main() {
if err != nil {
log.Fatal(err)
}

url := fmt.Sprintf("https://%s/%s/%s/compare/%s", ghRepo.Host, ghRepo.Owner, ghRepo.Name, compareArgument)

browser := browser.New("", os.Stdout, os.Stderr)
Expand All @@ -49,3 +66,8 @@ func main() {
log.Fatal(err)
}
}

func parseRepoOriginOwnerFromURL(url string) string {
parts := strings.Split(url, ":")
return strings.Split(parts[1], "/")[0]
}

0 comments on commit b49d4ae

Please sign in to comment.