@@ -3,13 +3,21 @@ package vcs
3
3
import (
4
4
"bytes"
5
5
"encoding/xml"
6
+ "fmt"
6
7
"io/ioutil"
7
8
"os"
8
9
"os/exec"
9
10
"path/filepath"
10
11
"runtime"
11
12
"strings"
13
+ "sync"
12
14
"time"
15
+
16
+ "github.com/sirupsen/logrus"
17
+ )
18
+
19
+ var (
20
+ repoMap sync.Map
13
21
)
14
22
15
23
// NewGitRepo creates a new instance of GitRepo. The remote and local directories
@@ -64,36 +72,142 @@ type GitRepo struct {
64
72
RemoteLocation string
65
73
}
66
74
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
+
67
87
// Vcs retrieves the underlying VCS being implemented.
68
88
func (s GitRepo ) Vcs () Type {
69
89
return Git
70
90
}
71
91
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
+
72
102
// Get is used to perform an initial clone of a repository.
73
103
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
+ }
75
168
76
169
// There are some windows cases where Git cannot create the parent directory,
77
170
// if it does not already exist, to the location it's trying to create the
78
171
// repo. Catch that error and try to handle it.
79
172
if err != nil && s .isUnableToCreateDir (err ) {
80
-
173
+ logrus . Infoln ( "1----------->" , remote )
81
174
basePath := filepath .Dir (filepath .FromSlash (s .LocalPath ()))
82
175
if _ , err := os .Stat (basePath ); os .IsNotExist (err ) {
83
176
err = os .MkdirAll (basePath , 0755 )
84
177
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 )
86
187
}
87
188
88
- out , err = s .run ("git" , "clone" , s .Remote (), s .LocalPath ())
89
189
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 ))
91
194
}
92
195
return err
93
196
}
94
197
95
198
} 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 )
97
211
}
98
212
99
213
return nil
@@ -103,6 +217,15 @@ func (s *GitRepo) Get() error {
103
217
func (s * GitRepo ) Init () error {
104
218
out , err := s .run ("git" , "init" , s .LocalPath ())
105
219
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
+
106
229
// There are some windows cases where Git cannot create the parent directory,
107
230
// if it does not already exist, to the location it's trying to create the
108
231
// repo. Catch that error and try to handle it.
@@ -112,36 +235,61 @@ func (s *GitRepo) Init() error {
112
235
if _ , err := os .Stat (basePath ); os .IsNotExist (err ) {
113
236
err = os .MkdirAll (basePath , 0755 )
114
237
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 , "" )
116
242
}
117
243
118
244
out , err = s .run ("git" , "init" , s .LocalPath ())
119
245
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 ))
121
250
}
122
251
return nil
123
252
}
124
253
125
254
} 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 ))
127
259
}
128
260
129
261
return nil
130
262
}
131
263
132
264
// Update performs an Git fetch and pull to an existing checkout.
133
265
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
+
134
276
// Perform a fetch to make sure everything is up to date.
135
277
out , err := s .RunFromDir ("git" , "fetch" , "--tags" , s .RemoteLocation )
136
278
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 ))
138
283
}
139
284
140
285
// When in a detached head state, such as when an individual commit is checked
141
286
// out do not attempt a pull. It will cause an error.
142
287
detached , err := isDetachedHead (s .LocalPath ())
143
288
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 , "" )
145
293
}
146
294
147
295
if detached {
@@ -150,7 +298,10 @@ func (s *GitRepo) Update() error {
150
298
151
299
out , err = s .RunFromDir ("git" , "pull" )
152
300
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 ))
154
305
}
155
306
156
307
return s .defendAgainstSubmodules ()
0 commit comments