grim/govcs

handle nested repos correctly
draft
2017-12-27, Gary Kramlich
a8955008648f
Parents 555c1d4bc08b
Children e3e0b8f91448
handle nested repos correctly
  • +27 -1
    govcs.go
  • --- a/govcs.go Wed Dec 27 21:00:20 2017 -0600
    +++ b/govcs.go Wed Dec 27 21:18:55 2017 -0600
    @@ -39,12 +39,38 @@
    // Detect will try to determine what version control system is in use in the
    // given working directory and return a vcs.VCS instance for it.
    func Detect(wd string) (vcs.VCS, error) {
    + var instance vcs.VCS
    +
    for _, detector := range detectors {
    inst := detector(wd)
    +
    + // we found a VCS
    if inst != nil {
    - return inst, nil
    + // if we found one previously, check who's root is longer so that
    + // we return the right vcs when they're nested.
    + // for example if we have
    + //
    + // foo/
    + // .git/
    + // bar/
    + // .hg/
    + //
    + // and we're testing bar, due to the order of the list we'd return
    + // a Git instance since it's first, but it really should be a
    + // mercurial instance.
    + if instance != nil {
    + if len(inst.Root()) > len(instance.Root()) {
    + instance = inst
    + }
    + } else {
    + instance = inst
    + }
    }
    }
    + if instance != nil {
    + return instance, nil
    + }
    +
    return nil, ErrNoRepositoryFound
    }