/
git.go
151 lines (122 loc) · 3.11 KB
/
git.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
package main
import (
"fmt"
"github.com/urfave/cli"
"os/exec"
"strings"
)
func gitUnits() []cli.Command {
return []cli.Command{
{Name: "init", Usage: "Initial a new repository", Action: gitInit},
{Name: "add", Usage: "Add the changes", Action: gitAdd},
{Name: "commit", Usage: "Commit the changes", Action: gitCommit},
{Name: "push", Usage: "Push to remote server", Action: gitPush},
{Name: "checkout", Usage: "Change the branch", Action: gitCheckout},
{Name: "clone", Usage: "Clone a remote repository", Action: gitClone},
{Name: "merge", Usage: "Merge a branch into the current branch", Action: gitMerge},
{Name: "pull", Usage: "Pull upstream changes into the local repository", Action: gitPull},
{Name: "remote_add", Usage: "Pull upstream changes into the local repository", Action: gitRemoteAdd},
}
}
// initial a new repository
func gitInit(_ *cli.Context) error {
err := exec.Command("git", "init").Run()
checkErr(err)
return nil
}
// add the changes
func gitAdd(c *cli.Context) error {
// inputs
files := c.Args().Get(0)
expected(files)
// execute
err := exec.Command("git", "add", files).Run()
checkErr(err)
return nil
}
// commit the changes
func gitCommit(c *cli.Context) error {
// inputs
message := c.Args().Get(0)
expected(message)
// execute
err := exec.Command("git", "commit", "-m", message).Run()
checkErr(err)
return nil
}
// push to remote repository
func gitPush(c *cli.Context) error {
// inputs
remote, branch := c.Args().Get(0), c.Args().Get(1)
expected(remote, branch)
// execute
err := exec.Command("git", "push", remote, branch).Run()
checkErr(err)
// outputs
fmt.Println("remote:" + remote)
fmt.Println("branch:" + branch)
return nil
}
// add the changes
func gitCheckout(c *cli.Context) error {
// inputs
branch := c.Args().Get(0)
expected(branch)
// execute
err := exec.Command("git", "checkout", branch).Run()
checkErr(err)
fmt.Println("branch:" + branch)
return nil
}
// clone repository
func gitClone(c *cli.Context) error {
// inputs
repo := c.Args().Get(0)
expected(repo, repo)
// execute
err := exec.Command("git", "clone", repo).Run()
checkErr(err)
a := strings.Split(repo, "/")
repoName := strings.Replace(a[len(a)-1], ".git", "", 1)
// outputs
fmt.Println("repository:" + repo)
fmt.Println("local:" + pwd() + "/" + repoName)
return nil
}
// merge a branch
func gitMerge(c *cli.Context) error {
// inputs
branch := c.Args().Get(0)
expected(branch)
// execute
err := exec.Command("git", "merge", branch).Run()
checkErr(err)
// outputs
fmt.Println("branch:" + branch)
return nil
}
// pull changes
func gitPull(c *cli.Context) error {
// inputs
remote := c.Args().Get(0)
expected(remote)
// execute
err := exec.Command("git", "pull", remote).Run()
checkErr(err)
// outputs
fmt.Println("remote:" + remote)
return nil
}
// add new remote
func gitRemoteAdd(c *cli.Context) error {
// inputs
name, url := c.Args().Get(0), c.Args().Get(1)
expected(name, url)
// execute
err := exec.Command("git", "remote", "add", name, url).Run()
checkErr(err)
// outputs
fmt.Println("name:" + name)
fmt.Println("url:" + url)
return nil
}