/
git_operation.go
59 lines (51 loc) · 1.29 KB
/
git_operation.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
package gitopt
import (
"context"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"gopkg.in/src-d/go-git.v4/config"
)
// git init operation
func GitInit(path string) (*git.Repository, error) {
repo, err := git.PlainInit(path, false)
return repo, err
}
// git add operation
func GitAdd(w *git.Worktree, glob string) error {
err := w.AddGlob(glob)
return err
}
// git commit operation
func GitCommit(wt *git.Worktree, msg string, opt *git.CommitOptions) (plumbing.Hash, error) {
ph, err := wt.Commit(msg, opt)
return ph, err
}
// git 'remote add' operation
func GitCreateRemote(r git.Repository, cloneUrl string) (*git.Remote, error) {
remote, err := r.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{cloneUrl},
})
return remote, err
}
// create 'master' branch
func GitCreateBranch(r git.Repository) error {
err := r.CreateBranch(&config.Branch{
Name: "master",
Remote: "origin",
Merge: "refs/heads/master",
})
return err
}
// git 'push' operation
func GitPush(r *git.Remote, ctx context.Context, userName string, password string) error {
err := r.Push(&git.PushOptions{
RemoteName: r.Config().Name,
Auth: &http.BasicAuth{
Username: userName,
Password: password,
},
})
return err
}