forked from kardianos/govendor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcs.go
79 lines (68 loc) · 1.63 KB
/
vcs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// package vcs gets version control information from the file system.
package vcs
import (
"path/filepath"
"sync"
"time"
"github.com/kardianos/govendor/internal/pathos"
)
// VcsInfo returns information about a given repo.
type VcsInfo struct {
Dirty bool
Revision string
RevisionTime *time.Time
}
// Vcs represents a version control system.
type Vcs interface {
// Return nil VcsInfo if unable to determine VCS from directory.
Find(dir string) (*VcsInfo, error)
}
var vcsRegistry = []Vcs{
VcsGit{},
VcsHg{},
VcsSvn{},
VcsBzr{},
}
var registerSync = sync.Mutex{}
// RegisterVCS adds a new VCS to use.
func RegisterVCS(vcs Vcs) {
registerSync.Lock()
defer registerSync.Unlock()
vcsRegistry = append(vcsRegistry, vcs)
}
const looplimit = 10000
// FindVcs determines the version control information given a package dir and
// lowest root dir.
func FindVcs(root, packageDir string) (info *VcsInfo, err error) {
if !filepath.IsAbs(root) {
return nil, nil
}
if !filepath.IsAbs(packageDir) {
return nil, nil
}
path := packageDir
for i := 0; i <= looplimit; i++ {
for _, vcs := range vcsRegistry {
info, err = vcs.Find(path)
if err != nil {
return nil, err
}
if info != nil {
return info, nil
}
}
nextPath := filepath.Clean(filepath.Join(path, ".."))
// Check for root.
if nextPath == path {
return nil, nil
}
if pathos.FileHasPrefix(nextPath, root) == false {
return nil, nil
}
path = nextPath
}
panic("loop limit")
}