-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (117 loc) · 3.66 KB
/
main.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
125
126
127
128
129
130
package main
import (
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/fatih/color"
)
func main() {
dirA, dirB := parseFlags()
gitRootA, gitRootB := gitRoot(dirA), gitRoot(dirB)
dirAHierarchy, dirBHierarchy := scanDir(gitRootA, dirA, "."), scanDir(gitRootB, dirB, ".")
comparisons := compareHierarchies(dirAHierarchy, dirBHierarchy)
logComparisons(gitRootA, dirA, dirB, comparisons)
}
type comparison struct {
hashA string
hashB string
}
func logComparisons(repoRootA, dirA, dirB string, comparisons map[string]comparison) {
dupeCount, diffCount, riggedCount := 0, 0, 0
for relPath, comp := range comparisons {
if comp.hashA != comp.hashB {
if comp.hashA != "" && comp.hashB != "" {
labelText, labelColor := "Diff", color.FgYellow
commitA, commitB := commitForSha(repoRootA, dirA, relPath, comp.hashA), commitForSha(repoRootA, dirA, relPath, comp.hashB)
if commitA == "" || commitB == "" {
riggedCount++
labelText, labelColor = "Diff", color.FgRed
}
commitAStr, commitBStr := richCommit(commitA, color.FgHiBlack), richCommit(commitB, color.FgYellow)
fmt.Printf("%s: %s: %s %s vs %s %s\n", colorize(labelText, labelColor, true), relPath,
colorize(comp.hashA[:10], color.FgHiBlack, true), commitAStr, colorize(comp.hashB[:10], color.FgHiBlack, true), commitBStr)
diffCount++
}
} else {
fmt.Printf("%s: %s @ %s\n", colorize("Dupe", color.FgHiWhite, true), relPath, colorize(comp.hashA[:10], color.FgHiBlack, true))
dupeCount++
}
}
fmt.Printf("There are %d coinciding paths. Out of these, %d have matching files and %d have differing files.\n", dupeCount+diffCount, dupeCount, diffCount)
if riggedCount > 0 {
fmt.Printf("Out of the %d different files, "+colorize("%d files have modifications unknown to the repository at %s", color.FgRed, true)+".\n",
diffCount, riggedCount, repoRootA)
}
}
func richCommit(commit string, col color.Attribute) string {
if commit == "" {
return colorize("(NO MATCHING COMMIT)", color.FgRed, true)
}
return fmt.Sprintf("(Commit: %s)", colorize(commit[:10], col, true))
}
func compareHierarchies(a, b map[string]string) map[string]comparison {
m := make(map[string]comparison)
for relPath, hashA := range a {
m[relPath] = comparison{hashA: hashA}
}
for relPath, hashB := range b {
comp, _ := m[relPath]
comp.hashB = hashB
m[relPath] = comp
}
return m
}
func parseFlags() (dirA, dirB string) {
flag.Parse()
if flag.NArg() != 2 {
fatalf("Usage: duperig <dirA> <dirB>")
}
return flag.Arg(0), flag.Arg(1)
}
func scanDir(repoRoot, rootPath, relPath string) map[string]string {
m := make(map[string]string)
absPath := filepath.Join(abs(rootPath), relPath)
if filepath.Join(repoRoot, ".git") == absPath {
return m
}
infos, err := ioutil.ReadDir(absPath)
if err != nil {
fatalf("Could not read dir \"%s\": %v", absPath, err)
}
for _, info := range infos {
if info.IsDir() {
subDirMap := scanDir(repoRoot, rootPath, filepath.Join(relPath, info.Name()))
insertAll(subDirMap, m)
} else {
m[filepath.Join(relPath, info.Name())] = hash(filepath.Join(absPath, info.Name()))
}
}
return m
}
func hash(filePath string) string {
hash := sha256.New()
f, err := os.Open(filePath)
if err != nil {
fatalf("Could not open file \"%s\" for hashing: %v", filePath, err)
}
defer f.Close()
_, err = io.Copy(hash, f)
if err != nil {
fatalf("Could not hash file \"%s\": %v", filePath, err)
}
return hex.EncodeToString(hash.Sum(nil))
}
func insertAll(from, to map[string]string) {
for k, v := range from {
to[k] = v
}
}
func fatalf(formatMessage string, args ...interface{}) {
fmt.Printf(formatMessage+"\n", args...)
os.Exit(1)
}