-
Notifications
You must be signed in to change notification settings - Fork 1
/
go-repository.go
156 lines (130 loc) · 3.53 KB
/
go-repository.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
152
153
154
155
156
package main
import (
"errors"
"fmt"
"io/fs"
"os"
"regexp"
"strings"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"go101.org/gotv/internal/util"
)
func (gotv *gotv) ensureGoRepository(pullOnExist bool) (pulled bool, err error) {
_, err = os.Stat(gotv.repositoryDir)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return
}
} else {
var okay = true
_, err = gitWorktree(gotv.repositoryDir)
if err != nil {
okay = false
}
if okay {
if pullOnExist {
pulled = true
fmt.Println("[Run]: git pull -a (in " + gotv.replaceHomeDir(gotv.repositoryDir) + ")")
err = gitPull(gotv.repositoryDir)
}
return
} else {
err = os.RemoveAll(gotv.repositoryDir)
if err != nil {
return
}
}
}
// clone it
pulled = true
defer func() {
if err != nil {
os.RemoveAll(gotv.repositoryDir)
}
}()
err = os.MkdirAll(gotv.cacheDir, 0700)
if err != nil {
return
}
fmt.Println(`Please specify the Go project repositry git address.
Generally, it should be one of the following ones:
* https://go.googlesource.com/go
* https://github.com/golang/go.git
* git@github.com:golang/go.git`)
fmt.Println()
var repoAddr string
for repoAddr == "" {
fmt.Print(`Specify it here: `)
_, err = fmt.Scanln(&repoAddr)
if err != nil && !strings.Contains(err.Error(), "unexpected newline") {
return
}
repoAddr = strings.TrimSpace(repoAddr)
}
fmt.Println("[Run]: git clone", gotv.replaceHomeDir(repoAddr), gotv.replaceHomeDir(gotv.repositoryDir))
err = gitClone(repoAddr, gotv.repositoryDir)
if err != nil {
return
}
return
}
func (gotv *gotv) copyBranchShallowly(tv toolchainVersion, toDir string) error {
var repoDir = gotv.repositoryDir
switch tv.kind {
case kind_Tag, kind_Branch, kind_Revision:
fmt.Println("[Run]: cp -r", gotv.replaceHomeDir(repoDir), gotv.replaceHomeDir(toDir))
var err = util.CopyDir(repoDir, toDir)
if err != nil {
return err
}
fmt.Println("[Run]: cd", gotv.replaceHomeDir(toDir))
var o = git.CheckoutOptions{Force: true, Keep: false}
if tv.kind == kind_Revision {
o.Hash = plumbing.NewHash(tv.version)
} else if tv.kind == kind_Tag {
o.Branch = plumbing.NewTagReferenceName(tv.version)
} else { // tv.kind == kind_Branch
o.Branch = plumbing.NewRemoteReferenceName("origin", tv.version)
// o.Create = true
// ToDo: not work. how to create local branches using go-git?
// Use a walkaround to avoid creating local branches now.
// But this is a question needing an answer.
}
fmt.Println("[Run]: git checkout", tv.version)
err = gitCheckout(toDir, &o)
if err != nil {
return err
}
default:
panic("unsupoorted version kinds")
}
return nil
}
var (
releaseTagRegexp = regexp.MustCompile(`^go([1-9]([0-9]*).*)`)
releaseBranchRegexp = regexp.MustCompile(`^(release-branch.go([1-9]([0-9]*).*))`)
)
func collectRepositoryInfo(repoDir string) (repoInfo repoInfo, err error) {
tags, branches, err := gitListTagsAndRemoteBranches(repoDir)
if err != nil {
return
}
repoInfo.allTags = tags
repoInfo.releaseTags = make(map[string]string, len(tags))
for t := range tags {
if ms := releaseTagRegexp.FindAllStringSubmatch(t, 1); len(ms) > 0 {
repoInfo.releaseTags[ms[0][1]] = t
}
}
repoInfo.allBranches = branches
repoInfo.versionBranches = make(map[string]string, len(branches))
for b, hash := range branches {
if ms := releaseBranchRegexp.FindAllStringSubmatch(b, 1); len(ms) > 0 {
repoInfo.versionBranches[ms[0][2]] = ms[0][1]
} else if b == "master" {
repoInfo.tipHash = hash
}
}
return
}