forked from zaquestion/lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone.go
62 lines (57 loc) · 1.44 KB
/
clone.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
package cmd
import (
"log"
"strings"
"github.com/spf13/cobra"
"github.com/zaquestion/lab/internal/git"
"github.com/zaquestion/lab/internal/gitlab"
)
// cloneCmd represents the clone command
// NOTE: There is special handling for "clone" in cmd/root.go
var cloneCmd = &cobra.Command{
Use: "clone",
Short: "",
Long: `Clone supports these shorthands
- repo
- namespace/repo`,
Run: func(cmd *cobra.Command, args []string) {
project, err := gitlab.FindProject(args[0])
if err == gitlab.ErrProjectNotFound {
err = git.New(append([]string{"clone"}, args...)...).Run()
if err != nil {
log.Fatal(err)
}
return
} else if err != nil {
log.Fatal(err)
}
path := project.SSHURLToRepo
err = git.New(append([]string{"clone", path}, args[1:]...)...).Run()
if err != nil {
log.Fatal(err)
}
// Clone project was a fork belonging to the user so user is
// treating forks as origin. Add upstream as remoted pointing
// to forked from repo
if project.ForkedFromProject != nil &&
strings.Contains(project.PathWithNamespace, gitlab.User()) {
var dir string
if len(args) > 1 {
dir = args[1]
} else {
dir = project.Name
}
ffProject, err := gitlab.FindProject(project.ForkedFromProject.PathWithNamespace)
if err != nil {
log.Fatal(err)
}
err = git.RemoteAdd("upstream", ffProject.SSHURLToRepo, "./"+dir)
if err != nil {
log.Fatal(err)
}
}
},
}
func init() {
RootCmd.AddCommand(cloneCmd)
}