-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
145 lines (131 loc) · 3.68 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
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/client"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
)
func (pkgs PackagesStr) install() {
for i := 0; i < len(pkgs); i++ {
for j := 0; j < len(pkgs[i].Packages); j++ {
pkg := pkgs[i].Packages[j]
pkg.cloneOrUpdate()
pkg.install()
}
}
}
func (pkg *Package) cloneOrUpdate() {
client.InstallProtocol("https", githttp.NewClient(CLIENT))
pkgDir, _ := filepath.Abs(filepath.Join(RIME_DIR, "package", pkg.User, strings.TrimPrefix(pkg.Repo, "rime-")))
pkg.WorkingDirectory = pkgDir
localBranch := plumbing.NewBranchReferenceName(pkg.Branch)
if _, err := os.Stat(pkgDir); os.IsNotExist(err) {
fmt.Printf("Cloning %s to %s\n", pkg.Repo, pkgDir)
_, err := git.PlainClone(pkgDir, false, &git.CloneOptions{
URL: pkg.URL,
ReferenceName: localBranch,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
Depth: 1})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
fmt.Printf("Updating %s\n", pkgDir)
repo, err := git.PlainOpen(pkgDir)
if err != nil {
fmt.Printf("%s is not a valid git repository\n", pkgDir)
os.Exit(1)
}
// validate r.Branch
remotes, err := repo.Remotes()
if err != nil {
fmt.Printf("failed to get remotes %s\n", err)
os.Exit(1)
}
remoteName := remotes[0].Config().Name
_, err = repo.Branch(pkg.Branch)
if err != nil {
refs, _ := remotes[0].List(&git.ListOptions{})
var hasBranch bool
for _, ref := range refs {
if ref.Name() == localBranch {
hasBranch = true
break
}
}
if !hasBranch {
fmt.Printf("branch %s can not be found on local and remote\n", pkg.Branch)
os.Exit(1)
}
err = repo.CreateBranch(&config.Branch{Name: pkg.Branch, Remote: remoteName, Merge: localBranch})
if err != nil {
fmt.Printf("failed to create branch %s\n", pkg.Branch)
os.Exit(1)
}
remoteBranch := plumbing.NewRemoteReferenceName(remoteName, pkg.Branch)
newReference := plumbing.NewSymbolicReference(localBranch, remoteBranch)
err = repo.Storer.SetReference(newReference)
if err != nil {
fmt.Printf("failed to set reference %s->%s\n", localBranch, remoteBranch)
os.Exit(1)
}
}
w, err := repo.Worktree()
if err != nil {
fmt.Printf("%s doesn't contains any worktree\n", pkgDir)
os.Exit(1)
}
err = w.Checkout(&git.CheckoutOptions{
Branch: localBranch,
Force: true,
})
if err != nil {
fmt.Printf("failed to checkout branch %s\n", pkg.Branch)
}
err = w.Pull(&git.PullOptions{
RemoteName: remoteName,
ReferenceName: localBranch,
})
if err != nil {
switch err.Error() {
case "already up-to-date":
fmt.Printf("%s already up-to-date\n", pkg.URL)
case "empty git-upload-pack given":
fmt.Printf("empty git-upload-pack given, %s already up-to-date\n", pkg.URL)
default:
fmt.Printf("failed to pull repository %s: %s\n", pkg.URL, err.Error())
os.Exit(1)
}
}
}
}
func commit(dir, file string) {
repo, err := git.PlainOpen(dir)
if err != nil {
fmt.Printf("%s is not a valid git repository\n", dir)
os.Exit(1)
}
w, err := repo.Worktree()
if err != nil {
fmt.Printf("%s doesn't contains any worktree\n", dir)
os.Exit(1)
}
_, err = w.Add(filepath.Base(file))
if err != nil {
fmt.Printf("can not git add %s\n", file)
os.Exit(1)
}
_, err = w.Commit(fmt.Sprintf("keep downloaded %s.%s", file, strconv.Itoa(int(time.Now().Unix()))), &git.CommitOptions{})
if err != nil {
fmt.Printf("failed to commit: %s", err)
os.Exit(1)
}
}