-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (59 loc) · 1.48 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
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/cli/go-gh/v2/pkg/browser"
"github.com/cli/go-gh/v2/pkg/repository"
"github.com/go-git/go-git/v5"
)
func main() {
gitRepo, err := git.PlainOpen(".")
if err != nil {
log.Fatal(err)
}
ref, err := gitRepo.Head()
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 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, "...") {
compareArgument = compareArgument + "..." + currentBranch
}
} else {
compareArgument = currentBranch
}
ghRepo, err := repository.Current()
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)
if err = browser.Browse(url); err != nil {
log.Fatal(err)
}
}
func parseRepoOriginOwnerFromURL(url string) string {
parts := strings.Split(url, ":")
return strings.Split(parts[1], "/")[0]
}