Skip to content

Commit 810f8be

Browse files
committed
Don't hide serious errors when using --auto-exclude.
"commit not found in this repo" is normal (and the kind of thing we want to auto-exclude). "Can't open this repo at all" probably indicates a serious problem and we should abort with an error, not just forage onward.
1 parent 3ced493 commit 810f8be

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

git-subtrac.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ func main() {
8282
if err != nil {
8383
fatalf("%v\n", err)
8484
}
85-
fmt.Printf("%v\n", trac.Hash)
85+
if trac != nil {
86+
fmt.Printf("%v\n", trac.Hash)
87+
}
8688
case "dump":
8789
if len(args) < 2 {
8890
usagef("command 'dump' takes at least 1 argument")

subtrac.go

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ func commitPath(path string, sub int) string {
373373
// Recursively open all submodule repositories, starting at c.repo, and
374374
// return a list of them.
375375
func (c *Cache) allSubrepos() (paths []string, repos []*git.Repository, err error) {
376-
var expand func(string, *git.Repository) error
377-
expand = func(path string, r *git.Repository) error {
376+
var recurse func(string, *git.Repository) error
377+
recurse = func(path string, r *git.Repository) error {
378378
wt, err := r.Worktree()
379379
if err != nil {
380380
return fmt.Errorf("git worktree(%s): %v", path, err)
@@ -389,35 +389,51 @@ func (c *Cache) allSubrepos() (paths []string, repos []*git.Repository, err erro
389389
subpath += "/modules/"
390390
}
391391
subpath += sub.Config().Path
392+
393+
ss, err := sub.Status()
394+
if err != nil {
395+
return fmt.Errorf("git status(%v): %v", subpath, err)
396+
}
397+
empty := plumbing.Hash{}
398+
if ss.Current == empty {
399+
// not currently initialized
400+
c.infof("git submodule(%s): not initialized; skipping\n", subpath)
401+
continue
402+
}
403+
392404
subr, err := sub.Repository()
393405
if err != nil {
394406
return fmt.Errorf("git repo(%v): %v", subpath, err)
395407
}
396408
paths = append(paths, subpath)
397409
repos = append(repos, subr)
398-
err = expand(subpath, subr)
410+
err = recurse(subpath, subr)
399411
if err != nil {
400-
return nil
412+
return err
401413
}
402414
}
403415
return nil
404416
}
405417

406-
err = expand("", c.repo)
418+
err = recurse("", c.repo)
407419
if err != nil {
408420
return nil, nil, err
409421
}
410422
return paths, repos, nil
411423
}
412424

425+
type NotPresentError struct{}
426+
427+
var NotPresent = &NotPresentError{}
428+
413429
// Try to find a given commit object in all submodule repositories. If it
414430
// exists, 'git fetch' it into the main repository so we can refer to it
415431
// as a parent of our synthetic commits.
416-
func (c *Cache) tryFetchFromSubmodules(path string, hash plumbing.Hash) error {
432+
func (c *Cache) tryFetchFromSubmodules(path string, hash plumbing.Hash) (*NotPresentError, error) {
417433
c.infof("Searching submodules for: %v\n", path)
418434
paths, repos, err := c.allSubrepos()
419435
if err != nil {
420-
return err
436+
return nil, err
421437
}
422438
for i := range repos {
423439
subpath := paths[i]
@@ -434,21 +450,21 @@ func (c *Cache) tryFetchFromSubmodules(path string, hash plumbing.Hash) error {
434450
err = subr.Storer.SetReference(ref)
435451
defer subr.Storer.RemoveReference(brrefname)
436452
if err != nil {
437-
return fmt.Errorf("submodule %v: create %v: %v", subpath, ref, err)
453+
return nil, fmt.Errorf("submodule %v: create %v: %v", subpath, ref, err)
438454
}
439455
// TODO(apenwarr): go-git should provide this path?
440456
// Maybe it does, but I can't figure out where.
441457
remotename := fmt.Sprintf("%v/.git/modules/%v", c.repoDir, subpath)
442458
absremotename, err := filepath.Abs(remotename)
443459
if err != nil {
444-
return fmt.Errorf("AbsPath(%v): %v", remotename, err)
460+
return nil, fmt.Errorf("AbsPath(%v): %v", remotename, err)
445461
}
446462
remote, err := c.repo.CreateRemoteAnonymous(&config.RemoteConfig{
447463
Name: "anonymous",
448464
URLs: []string{absremotename},
449465
})
450466
if err != nil {
451-
return fmt.Errorf("submodule %v: CreateRemote: %v", absremotename, err)
467+
return nil, fmt.Errorf("submodule %v: CreateRemote: %v", absremotename, err)
452468
}
453469
err = remote.Fetch(&git.FetchOptions{
454470
RemoteName: "anonymous",
@@ -457,16 +473,16 @@ func (c *Cache) tryFetchFromSubmodules(path string, hash plumbing.Hash) error {
457473
},
458474
})
459475
if err != nil {
460-
return fmt.Errorf("submodule %v: fetch: %v", absremotename, err)
476+
return nil, fmt.Errorf("submodule %v: fetch: %v", absremotename, err)
461477
}
462478
// Fetch worked!
463479
err = subr.Storer.RemoveReference(brrefname)
464480
if err != nil {
465-
return fmt.Errorf("submodule %v: remove %v: %v", subpath, ref, err)
481+
return nil, fmt.Errorf("submodule %v: remove %v: %v", subpath, ref, err)
466482
}
467-
return nil
483+
return nil, nil
468484
}
469-
return fmt.Errorf("%v: %v not found.", path, hash)
485+
return NotPresent, fmt.Errorf("%v: %v not found.", path, hash)
470486
}
471487

472488
// Starting from a given git tree object, recursively add all its subtree
@@ -492,13 +508,13 @@ func (c *Cache) tracTree(path string, tree *object.Tree) (*Trac, error) {
492508
subpath := fmt.Sprintf("%s%s@%.10v", path, e.Name, e.Hash)
493509
sc, err := c.repo.CommitObject(e.Hash)
494510
if err != nil {
495-
err = c.tryFetchFromSubmodules(subpath, e.Hash)
511+
npErr, err := c.tryFetchFromSubmodules(subpath, e.Hash)
512+
if npErr != nil && c.autoexclude {
513+
c.infof("Excluding %v\n", e.Hash)
514+
c.exclude(e.Hash)
515+
continue
516+
}
496517
if err != nil {
497-
if c.autoexclude {
498-
c.infof("Excluding %v\n", e.Hash)
499-
c.exclude(e.Hash)
500-
continue
501-
}
502518
return nil, fmt.Errorf("%v (fetch it manually? or try --exclude)", err)
503519
}
504520
}

0 commit comments

Comments
 (0)