Skip to content

Commit 61b20af

Browse files
committed
fixed a bug
1 parent b4f5583 commit 61b20af

7 files changed

+265
-14
lines changed

bzr.go

+10
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ func (s BzrRepo) Vcs() Type {
6969
return Bzr
7070
}
7171

72+
// Sets the branch of the current clone on the repository.
73+
func (s *BzrRepo) SetCloneBranch(branch string) {
74+
s.setBranch(branch)
75+
}
76+
77+
// Sets the import package on the repository.
78+
func (s *BzrRepo) SetPkg(pkg string) {
79+
s.setRawPkg(pkg)
80+
}
81+
7282
// Get is used to perform an initial clone of a repository.
7383
func (s *BzrRepo) Get() error {
7484

git.go

+163-12
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@ package vcs
33
import (
44
"bytes"
55
"encoding/xml"
6+
"fmt"
67
"io/ioutil"
78
"os"
89
"os/exec"
910
"path/filepath"
1011
"runtime"
1112
"strings"
13+
"sync"
1214
"time"
15+
16+
"github.com/sirupsen/logrus"
17+
)
18+
19+
var (
20+
repoMap sync.Map
1321
)
1422

1523
// NewGitRepo creates a new instance of GitRepo. The remote and local directories
@@ -64,36 +72,142 @@ type GitRepo struct {
6472
RemoteLocation string
6573
}
6674

75+
type TempRepo struct {
76+
fun string
77+
rawPkg string
78+
remote string
79+
local string
80+
branch string
81+
RemoteLocation string
82+
err error
83+
out string
84+
buf []int
85+
}
86+
6787
// Vcs retrieves the underlying VCS being implemented.
6888
func (s GitRepo) Vcs() Type {
6989
return Git
7090
}
7191

92+
// Sets the branch of the current clone on the repository.
93+
func (s *GitRepo) SetCloneBranch(branch string) {
94+
s.setBranch(branch)
95+
}
96+
97+
// Sets the import package on the repository.
98+
func (s *GitRepo) SetPkg(pkg string) {
99+
s.setRawPkg(pkg)
100+
}
101+
72102
// Get is used to perform an initial clone of a repository.
73103
func (s *GitRepo) Get() error {
74-
out, err := s.run("git", "clone", "--recursive", s.Remote(), s.LocalPath())
104+
remote := s.Remote()
105+
local := s.LocalPath()
106+
branch := s.Branch()
107+
108+
logrus.Infoln("0----------->", remote)
109+
110+
repoValue := &TempRepo{
111+
fun: "[GET]",
112+
rawPkg: s.rawPkg,
113+
remote: s.remote,
114+
local: s.local,
115+
branch: s.branch,
116+
RemoteLocation: s.RemoteLocation,
117+
}
118+
119+
var out []byte
120+
var err error
121+
122+
if branch == "" {
123+
for i := 1; i < 6; i++ {
124+
err = nil
125+
repoValue.err = nil
126+
repoValue.out = ""
127+
out, err = s.run("git", "clone", "--recursive", remote, local)
128+
if err == nil {
129+
break
130+
}
131+
132+
repoValue.err = err
133+
repoValue.out = string(out)
134+
logrus.Warnfp("", i, s.value, repoValue)
135+
}
136+
} else {
137+
ok := false
138+
for i := 1; i < 6; i++ {
139+
err = nil
140+
repoValue.err = nil
141+
repoValue.out = ""
142+
if !ok {
143+
out, err = s.run("git", "clone", "--recursive", "-b", branch, remote, local)
144+
if err == nil {
145+
break
146+
}
147+
repoValue.err = err
148+
repoValue.out = string(out)
149+
}
150+
151+
outMsg := fmt.Sprintf("Remote branch %s not found in upstream", branch)
152+
if strings.Contains(repoValue.out, outMsg) {
153+
ok = true
154+
}
155+
156+
if ok {
157+
out, err = s.run("git", "clone", "--recursive", remote, local)
158+
if err == nil {
159+
break
160+
}
161+
162+
repoValue.err = err
163+
repoValue.out = string(out)
164+
}
165+
logrus.Warnfp("", i, s.value, repoValue)
166+
}
167+
}
75168

76169
// There are some windows cases where Git cannot create the parent directory,
77170
// if it does not already exist, to the location it's trying to create the
78171
// repo. Catch that error and try to handle it.
79172
if err != nil && s.isUnableToCreateDir(err) {
80-
173+
logrus.Infoln("1----------->", remote)
81174
basePath := filepath.Dir(filepath.FromSlash(s.LocalPath()))
82175
if _, err := os.Stat(basePath); os.IsNotExist(err) {
83176
err = os.MkdirAll(basePath, 0755)
84177
if err != nil {
85-
return NewLocalError("Unable to create directory", err, "")
178+
repoValue.err = err
179+
repoValue.out = string(out)
180+
logrus.Errorfp("", s.value, repoValue)
181+
return NewLocalError("[0] Unable to create directory", err, "")
182+
}
183+
if branch == "" {
184+
out, err = s.run("git", "clone", remote, local)
185+
} else {
186+
out, err = s.run("git", "clone", "-b", branch, remote, local)
86187
}
87188

88-
out, err = s.run("git", "clone", s.Remote(), s.LocalPath())
89189
if err != nil {
90-
return NewRemoteError("Unable to get repository", err, string(out))
190+
repoValue.err = err
191+
repoValue.out = string(out)
192+
logrus.Errorfp("", s.value, repoValue)
193+
return NewRemoteError("[1] Unable to get repository", err, string(out))
91194
}
92195
return err
93196
}
94197

95198
} else if err != nil {
96-
return NewRemoteError("Unable to get repository", err, string(out))
199+
repoValue.err = err
200+
repoValue.out = string(out)
201+
logrus.Errorfp("", s.value, repoValue)
202+
return NewRemoteError("[2] Unable to get repository", err, string(out))
203+
}
204+
205+
if _, ok := repoMap.Load(remote); ok {
206+
logrus.Infoln("2----------->", remote)
207+
repoMap.Store(remote, true)
208+
} else {
209+
logrus.Infoln("3----------->", remote)
210+
repoMap.Store(remote, true)
97211
}
98212

99213
return nil
@@ -103,6 +217,15 @@ func (s *GitRepo) Get() error {
103217
func (s *GitRepo) Init() error {
104218
out, err := s.run("git", "init", s.LocalPath())
105219

220+
repoValue := &TempRepo{
221+
fun: "[INIT]",
222+
rawPkg: s.rawPkg,
223+
remote: s.remote,
224+
local: s.local,
225+
branch: s.branch,
226+
RemoteLocation: s.RemoteLocation,
227+
}
228+
106229
// There are some windows cases where Git cannot create the parent directory,
107230
// if it does not already exist, to the location it's trying to create the
108231
// repo. Catch that error and try to handle it.
@@ -112,36 +235,61 @@ func (s *GitRepo) Init() error {
112235
if _, err := os.Stat(basePath); os.IsNotExist(err) {
113236
err = os.MkdirAll(basePath, 0755)
114237
if err != nil {
115-
return NewLocalError("Unable to initialize repository", err, "")
238+
repoValue.err = err
239+
repoValue.out = string(out)
240+
logrus.Errorfp("", s.value, repoValue)
241+
return NewLocalError("[0] Unable to initialize repository", err, "")
116242
}
117243

118244
out, err = s.run("git", "init", s.LocalPath())
119245
if err != nil {
120-
return NewLocalError("Unable to initialize repository", err, string(out))
246+
repoValue.err = err
247+
repoValue.out = string(out)
248+
logrus.Errorfp("", s.value, repoValue)
249+
return NewLocalError("[1] Unable to initialize repository", err, string(out))
121250
}
122251
return nil
123252
}
124253

125254
} else if err != nil {
126-
return NewLocalError("Unable to initialize repository", err, string(out))
255+
repoValue.err = err
256+
repoValue.out = string(out)
257+
logrus.Errorfp("", s.value, repoValue)
258+
return NewLocalError("[2] Unable to initialize repository", err, string(out))
127259
}
128260

129261
return nil
130262
}
131263

132264
// Update performs an Git fetch and pull to an existing checkout.
133265
func (s *GitRepo) Update() error {
266+
267+
repoValue := &TempRepo{
268+
fun: "[UPDATE]",
269+
rawPkg: s.rawPkg,
270+
remote: s.remote,
271+
local: s.local,
272+
branch: s.branch,
273+
RemoteLocation: s.RemoteLocation,
274+
}
275+
134276
// Perform a fetch to make sure everything is up to date.
135277
out, err := s.RunFromDir("git", "fetch", "--tags", s.RemoteLocation)
136278
if err != nil {
137-
return NewRemoteError("Unable to update repository", err, string(out))
279+
repoValue.err = err
280+
repoValue.out = string(out)
281+
logrus.Errorfp("", s.value, repoValue)
282+
return NewRemoteError("[0] Unable to update repository", err, string(out))
138283
}
139284

140285
// When in a detached head state, such as when an individual commit is checked
141286
// out do not attempt a pull. It will cause an error.
142287
detached, err := isDetachedHead(s.LocalPath())
143288
if err != nil {
144-
return NewLocalError("Unable to update repository", err, "")
289+
repoValue.err = err
290+
repoValue.out = string(out)
291+
logrus.Errorfp("", s.value, repoValue)
292+
return NewLocalError("[1] Unable to update repository", err, "")
145293
}
146294

147295
if detached {
@@ -150,7 +298,10 @@ func (s *GitRepo) Update() error {
150298

151299
out, err = s.RunFromDir("git", "pull")
152300
if err != nil {
153-
return NewRemoteError("Unable to update repository", err, string(out))
301+
repoValue.err = err
302+
repoValue.out = string(out)
303+
logrus.Errorfp("", s.value, repoValue)
304+
return NewRemoteError("[2] Unable to update repository", err, string(out))
154305
}
155306

156307
return s.defendAgainstSubmodules()

hg.go

+10
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ func (s HgRepo) Vcs() Type {
7070
return Hg
7171
}
7272

73+
// Sets the branch of the current clone on the repository.
74+
func (s *HgRepo) SetCloneBranch(branch string) {
75+
s.setBranch(branch)
76+
}
77+
78+
// Sets the import package on the repository.
79+
func (s *HgRepo) SetPkg(pkg string) {
80+
s.setRawPkg(pkg)
81+
}
82+
7383
// Get is used to perform an initial clone of a repository.
7484
func (s *HgRepo) Get() error {
7585
out, err := s.run("hg", "clone", s.Remote(), s.LocalPath())

repo.go

+40-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import (
3434
"regexp"
3535
"strings"
3636
"time"
37+
38+
"github.com/sirupsen/logrus"
3739
)
3840

3941
// Logger is where you can provide a logger, implementing the log.Logger interface,
@@ -136,6 +138,12 @@ type Repo interface {
136138

137139
// ExportDir exports the current revision to the passed in directory.
138140
ExportDir(string) error
141+
142+
// Sets the branch of the current clone on the repository.
143+
SetCloneBranch(string)
144+
145+
// Sets the import package on the repository.
146+
SetPkg(string)
139147
}
140148

141149
// NewRepo returns a Repo based on trying to detect the source control from the
@@ -188,9 +196,21 @@ type CommitInfo struct {
188196
Message string
189197
}
190198

199+
type value struct {
200+
rawPkg_ string `json:"rawPkg"`
201+
remote_ string `json:"remote"`
202+
local_ string `json:"local"`
203+
branch_ string `json:"branch"`
204+
buf []int
205+
}
206+
191207
type base struct {
192-
remote, local string
193-
Logger *log.Logger
208+
value
209+
rawPkg string
210+
remote string
211+
local string
212+
branch string
213+
Logger *log.Logger
194214
}
195215

196216
func (b *base) log(v interface{}) {
@@ -207,15 +227,32 @@ func (b *base) LocalPath() string {
207227
return b.local
208228
}
209229

230+
func (b *base) Branch() string {
231+
return b.branch
232+
}
233+
210234
func (b *base) setRemote(remote string) {
235+
b.value.remote_ = remote
211236
b.remote = remote
212237
}
213238

214239
func (b *base) setLocalPath(local string) {
240+
b.value.local_ = local
215241
b.local = local
216242
}
217243

244+
func (b *base) setBranch(branch string) {
245+
b.value.branch_ = branch
246+
b.branch = branch
247+
}
248+
249+
func (b *base) setRawPkg(pkg string) {
250+
b.value.rawPkg_ = pkg
251+
b.rawPkg = pkg
252+
}
253+
218254
func (b base) run(cmd string, args ...string) ([]byte, error) {
255+
logrus.Debugfp("run:", b.value, cmd, args)
219256
out, err := exec.Command(cmd, args...).CombinedOutput()
220257
b.log(out)
221258
if err != nil {
@@ -225,6 +262,7 @@ func (b base) run(cmd string, args ...string) ([]byte, error) {
225262
}
226263

227264
func (b *base) CmdFromDir(cmd string, args ...string) *exec.Cmd {
265+
logrus.Debugfp("CmdFromDir:", b.value, cmd, args)
228266
c := exec.Command(cmd, args...)
229267
c.Dir = b.local
230268
c.Env = envForDir(c.Dir)

0 commit comments

Comments
 (0)