/
check.go
124 lines (100 loc) · 2.38 KB
/
check.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"fmt"
"os"
"github.com/vito/gocart/command_runner"
"github.com/vito/gocart/dependency"
"github.com/vito/gocart/repository"
"github.com/vito/gocart/set"
)
type VersionMismatch struct {
Expected string
Status DependencyStatus
}
func (self VersionMismatch) Error() string {
want := indent(1, "want "+red(self.Expected))
have := indent(1, "have "+green(self.Status.CurrentVersion))
log := indent(1, self.Status.DeltaLog)
if self.Status.Delta > 0 {
have = have + " (" + bold(fmt.Sprintf("%d", self.Status.Delta)) + " ahead)"
have = have + "\n" + indent(1, "extra commits:\n"+log)
} else if self.Status.Delta == 0 {
have = have + " (? behind)"
} else {
have = have + " (" + bold(fmt.Sprintf("%d", -self.Status.Delta)) + " behind)"
have = have + "\n" + indent(1, "missing commits:\n"+log)
}
return fmt.Sprintf(
"version mismatch:\n%s\n%s\n",
want,
have,
)
}
type DirtyState struct {
Output string
}
func (self DirtyState) Error() string {
return fmt.Sprintf("dirty state:\n%s", indent(1, self.Output))
}
func check(root string) {
cartridge, err := set.LoadFrom(root)
if err != nil {
fatal(err)
}
dirty := checkDependencies(cartridge, 0)
if dirty {
os.Exit(1)
}
}
func checkDependencies(deps *set.Set, depth int) bool {
dirty := false
for _, dep := range deps.Dependencies {
err := checkForDirtyState(dep)
if err != nil {
dirty = true
fmt.Println(indent(depth, bold(dep.Path)))
fmt.Println(indent(depth+1, err.Error()))
} else {
fmt.Println(indent(depth, bold(dep.Path)), green("OK"))
}
nextDeps, err := set.LoadFrom(dep.FullPath(GOPATH))
if err == set.NoCartridgeError {
continue
} else if err != nil {
fatal(err)
}
if checkDependencies(nextDeps, depth+1) {
dirty = true
}
}
return dirty
}
func checkForDirtyState(dep dependency.Dependency) error {
repoPath := dep.FullPath(GOPATH)
if _, err := os.Stat(repoPath); os.IsNotExist(err) {
return nil
}
repo, err := repository.New(repoPath, command_runner.New(false))
if err != nil {
fatal(err)
}
statusOut, err := repo.Status()
if err != nil {
fatal(err)
}
if len(statusOut) != 0 {
return DirtyState{
Output: statusOut,
}
}
currentStatus := getDependencyStatus(dep)
if currentStatus != nil {
if !currentStatus.VersionMatches {
return VersionMismatch{
Expected: dep.Version,
Status: *currentStatus,
}
}
}
return nil
}