forked from zaquestion/lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fork.go
71 lines (63 loc) · 1.44 KB
/
fork.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
package cmd
import (
"log"
"strings"
"github.com/spf13/cobra"
"github.com/tcnksm/go-gitconfig"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)
// forkCmd represents the fork command
var forkCmd = &cobra.Command{
Use: "fork [upstream-to-fork]",
Short: "Fork a remote repository on GitLab and add as remote",
Long: ``,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 1 {
forkToUpstream(cmd, args)
return
}
forkFromOrigin(cmd, args)
},
}
func forkFromOrigin(cmd *cobra.Command, args []string) {
if _, err := gitconfig.Local("remote." + lab.User() + ".url"); err == nil {
log.Println("remote:", lab.User, "already exists")
return
}
remoteURL, err := gitconfig.Local("remote.origin.url")
if err != nil {
log.Fatal(err)
}
if git.IsHub && strings.Contains(remoteURL, "github.com") {
git := git.New("fork")
git.Run()
if err != nil {
log.Fatal(err)
}
return
}
project, err := git.PathWithNameSpace("origin")
if err != nil {
log.Fatal(err)
}
remote, err := lab.Fork(project)
if err != nil {
log.Fatal(err)
}
err = git.RemoteAdd(lab.User(), remote, ".")
if err != nil {
log.Fatal(err)
}
}
func forkToUpstream(cmd *cobra.Command, args []string) {
_, err := lab.Fork(args[0])
if err != nil {
log.Fatal(err)
}
cloneCmd.Run(nil, []string{strings.Split(args[0], "/")[1]})
}
func init() {
RootCmd.AddCommand(forkCmd)
}