@@ -77,11 +77,21 @@ func cloneFunc(cmd *cobra.Command, argz []string) {
77
77
os .Setenv ("GHORG_MATCH_PREFIX" , prefix )
78
78
}
79
79
80
+ if cmd .Flags ().Changed ("exclude-match-prefix" ) {
81
+ prefix := cmd .Flag ("exclude-match-prefix" ).Value .String ()
82
+ os .Setenv ("GHORG_EXCLUDE_MATCH_PREFIX" , prefix )
83
+ }
84
+
80
85
if cmd .Flags ().Changed ("match-regex" ) {
81
86
regex := cmd .Flag ("match-regex" ).Value .String ()
82
87
os .Setenv ("GHORG_MATCH_REGEX" , regex )
83
88
}
84
89
90
+ if cmd .Flags ().Changed ("exclude-match-regex" ) {
91
+ regex := cmd .Flag ("exclude-match-regex" ).Value .String ()
92
+ os .Setenv ("GHORG_EXCLUDE_MATCH_REGEX" , regex )
93
+ }
94
+
85
95
if cmd .Flags ().Changed ("skip-archived" ) {
86
96
os .Setenv ("GHORG_SKIP_ARCHIVED" , "true" )
87
97
}
@@ -287,14 +297,67 @@ func readGhorgIgnore() ([]string, error) {
287
297
return lines , scanner .Err ()
288
298
}
289
299
290
- func filterByRegex (repos []scm.Repo ) []scm.Repo {
300
+ func filterByRegexMatch (repos []scm.Repo ) []scm.Repo {
291
301
filteredRepos := []scm.Repo {}
292
302
regex := fmt .Sprint (os .Getenv ("GHORG_MATCH_REGEX" ))
293
303
294
304
for i , r := range repos {
295
305
re := regexp .MustCompile (regex )
296
- match := re .FindString (getAppNameFromURL (r .URL ))
306
+ match := re .FindString (r .Name )
307
+ if match != "" {
308
+ filteredRepos = append (filteredRepos , repos [i ])
309
+ }
310
+ }
311
+
312
+ return filteredRepos
313
+ }
314
+
315
+ func filterByExcludeRegexMatch (repos []scm.Repo ) []scm.Repo {
316
+ filteredRepos := []scm.Repo {}
317
+ regex := fmt .Sprint (os .Getenv ("GHORG_EXCLUDE_MATCH_REGEX" ))
318
+
319
+ for i , r := range repos {
320
+ exclude := false
321
+ re := regexp .MustCompile (regex )
322
+ match := re .FindString (r .Name )
297
323
if match != "" {
324
+ exclude = true
325
+ }
326
+
327
+ if ! exclude {
328
+ filteredRepos = append (filteredRepos , repos [i ])
329
+ }
330
+ }
331
+
332
+ return filteredRepos
333
+ }
334
+
335
+ func filterByMatchPrefix (repos []scm.Repo ) []scm.Repo {
336
+ filteredRepos := []scm.Repo {}
337
+ for i , r := range repos {
338
+ pfs := strings .Split (os .Getenv ("GHORG_MATCH_PREFIX" ), "," )
339
+ for _ , p := range pfs {
340
+ if strings .HasPrefix (strings .ToLower (r .Name ), strings .ToLower (p )) {
341
+ filteredRepos = append (filteredRepos , repos [i ])
342
+ }
343
+ }
344
+ }
345
+
346
+ return filteredRepos
347
+ }
348
+
349
+ func filterByExcludeMatchPrefix (repos []scm.Repo ) []scm.Repo {
350
+ filteredRepos := []scm.Repo {}
351
+ for i , r := range repos {
352
+ var exclude bool
353
+ pfs := strings .Split (os .Getenv ("GHORG_EXCLUDE_MATCH_PREFIX" ), "," )
354
+ for _ , p := range pfs {
355
+ if strings .HasPrefix (strings .ToLower (r .Name ), strings .ToLower (p )) {
356
+ exclude = true
357
+ }
358
+ }
359
+
360
+ if ! exclude {
298
361
filteredRepos = append (filteredRepos , repos [i ])
299
362
}
300
363
}
@@ -325,12 +388,27 @@ func printDryRun(repos []scm.Repo) {
325
388
326
389
// CloneAllRepos clones all repos
327
390
func CloneAllRepos (git git.Gitter , cloneTargets []scm.Repo ) {
328
- // resc, errc, infoc := make(chan string), make(chan error), make(chan error)
329
391
392
+ // Filter repos that have attributes that don't need specific scm api calls
330
393
if os .Getenv ("GHORG_MATCH_REGEX" ) != "" {
331
- colorlog .PrintInfo ("Filtering repos down by regex that match the provided ..." )
394
+ colorlog .PrintInfo ("Filtering repos down by including regex matches ..." )
332
395
fmt .Println ("" )
333
- cloneTargets = filterByRegex (cloneTargets )
396
+ cloneTargets = filterByRegexMatch (cloneTargets )
397
+ }
398
+ if os .Getenv ("GHORG_EXCLUDE_MATCH_REGEX" ) != "" {
399
+ colorlog .PrintInfo ("Filtering repos down by excluding regex matches..." )
400
+ fmt .Println ("" )
401
+ cloneTargets = filterByExcludeRegexMatch (cloneTargets )
402
+ }
403
+ if os .Getenv ("GHORG_MATCH_PREFIX" ) != "" {
404
+ colorlog .PrintInfo ("Filtering repos down by including prefix matches..." )
405
+ fmt .Println ("" )
406
+ cloneTargets = filterByMatchPrefix (cloneTargets )
407
+ }
408
+ if os .Getenv ("GHORG_EXCLUDE_MATCH_PREFIX" ) != "" {
409
+ colorlog .PrintInfo ("Filtering repos down by excluding prefix matches..." )
410
+ fmt .Println ("" )
411
+ cloneTargets = filterByExcludeMatchPrefix (cloneTargets )
334
412
}
335
413
336
414
// filter repos down based on ghorgignore if one exists
@@ -394,20 +472,15 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
394
472
395
473
var cloneCount , pulledCount int
396
474
397
- for _ , target := range cloneTargets {
398
- appName := getAppNameFromURL (target .URL )
399
-
400
- branch := target .CloneBranch
401
- repo := target
402
-
475
+ for i := range cloneTargets {
476
+ repo := cloneTargets [i ]
477
+ repoSlug := getAppNameFromURL (repo .URL )
403
478
limit .Execute (func () {
404
-
405
- path := appName
406
479
if repo .Path != "" && os .Getenv ("GHORG_PRESERVE_DIRECTORY_STRUCTURE" ) == "true" {
407
480
path = repo .Path
408
481
}
409
482
410
- repo .HostPath = filepath .Join (os .Getenv ("GHORG_ABSOLUTE_PATH_TO_CLONE_TO" ), parentFolder , configs .GetCorrectFilePathSeparator (), path )
483
+ repo .HostPath = filepath .Join (os .Getenv ("GHORG_ABSOLUTE_PATH_TO_CLONE_TO" ), parentFolder , configs .GetCorrectFilePathSeparator (), repoSlug )
411
484
412
485
if repo .IsWiki {
413
486
if ! strings .HasSuffix (repo .HostPath , ".wiki" ) {
@@ -416,7 +489,7 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
416
489
}
417
490
418
491
if os .Getenv ("GHORG_BACKUP" ) == "true" {
419
- repo .HostPath = filepath .Join (os .Getenv ("GHORG_ABSOLUTE_PATH_TO_CLONE_TO" ), parentFolder + "_backup" , configs .GetCorrectFilePathSeparator (), path )
492
+ repo .HostPath = filepath .Join (os .Getenv ("GHORG_ABSOLUTE_PATH_TO_CLONE_TO" ), parentFolder + "_backup" , configs .GetCorrectFilePathSeparator (), repoSlug )
420
493
}
421
494
422
495
action := "cloning"
@@ -521,7 +594,7 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
521
594
if os .Getenv ("GHORG_BRANCH" ) != "" {
522
595
err := git .Checkout (repo )
523
596
if err != nil {
524
- e := fmt .Sprintf ("Could not checkout out %s, branch may not exist, no changes made Repo: %s Error: %v" , branch , repo .URL , err )
597
+ e := fmt .Sprintf ("Could not checkout out %s, branch may not exist, no changes made Repo: %s Error: %v" , repo . CloneBranch , repo .URL , err )
525
598
cloneInfos = append (cloneInfos , e )
526
599
return
527
600
}
@@ -551,7 +624,7 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
551
624
}
552
625
}
553
626
554
- colorlog .PrintSuccess ("Success " + action + " repo: " + repo .URL + " -> branch: " + branch )
627
+ colorlog .PrintSuccess ("Success " + action + " repo: " + repo .URL + " -> branch: " + repo . CloneBranch )
555
628
})
556
629
557
630
}
@@ -614,6 +687,15 @@ func PrintConfigs() {
614
687
if os .Getenv ("GHORG_MATCH_REGEX" ) != "" {
615
688
colorlog .PrintInfo ("* Regex Match : " + os .Getenv ("GHORG_MATCH_REGEX" ))
616
689
}
690
+ if os .Getenv ("GHORG_EXCLUDE_MATCH_REGEX" ) != "" {
691
+ colorlog .PrintInfo ("* Exclude Regex : " + os .Getenv ("GHORG_EXCLUDE_MATCH_REGEX" ))
692
+ }
693
+ if os .Getenv ("GHORG_MATCH_PREFIX" ) != "" {
694
+ colorlog .PrintInfo ("* Prefix Match: " + os .Getenv ("GHORG_MATCH_PREFIX" ))
695
+ }
696
+ if os .Getenv ("GHORG_EXCLUDE_MATCH_PREFIX" ) != "" {
697
+ colorlog .PrintInfo ("* Exclude Prefix: " + os .Getenv ("GHORG_EXCLUDE_MATCH_PREFIX" ))
698
+ }
617
699
if os .Getenv ("GHORG_OUTPUT_DIR" ) != "" {
618
700
colorlog .PrintInfo ("* Output Dir : " + parentFolder )
619
701
}
0 commit comments