Repository navigation
Expand file tree
/
Copy pathdiff.go
More file actions
146 lines (133 loc) · 3.44 KB
/
Copy pathdiff.go
File metadata and controls
146 lines (133 loc) · 3.44 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"cmp"
"context"
"fmt"
"golang.org/x/sync/errgroup"
"slices"
"strings"
)
const (
statusIdentical = "IDENTICAL"
statusChanged = "CHANGED"
statusMoved = "MOVED"
statusAdded = "ADDED"
statusDeleted = "DELETED"
)
type TreeDiff struct {
Files []*FileDiff
}
type FileDiff struct {
Status string
*FileDsc
}
func DiffDirs(dir1, dir2 string) (*TreeDiff, error) {
g, _ := errgroup.WithContext(context.Background())
var t1, t2 *FileTree
g.Go(func() error {
var err error
t1, err = Tree(dir1)
return err
})
g.Go(func() error {
var err error
t2, err = Tree(dir2)
return err
})
err := g.Wait()
if err != nil {
return nil, err
}
t1.RemovePrefix()
t2.RemovePrefix()
return DiffTrees(t1, t2), nil
}
func DiffTrees(t1, t2 *FileTree) *TreeDiff {
res := &TreeDiff{}
dir1ByHash := map[string][]*FileDsc{}
dir1ByPath := map[string]*FileDsc{}
matched := map[string]int{}
for _, dsc1 := range t1.Files {
if dsc1.Hash != "" {
dir1ByHash[dsc1.Hash] = append(dir1ByHash[dsc1.Hash], dsc1)
}
dir1ByPath[dsc1.Path] = dsc1
}
t2loop:
for _, dsc2 := range t2.Files {
if dsc2.IsDir {
continue
}
if dsc1, ok := dir1ByPath[dsc2.Path]; ok {
if dsc1.Hash != dsc2.Hash || dsc1.Size != dsc2.Size {
res.Files = append(res.Files, &FileDiff{Status: statusChanged, FileDsc: dsc1})
} else {
res.Files = append(res.Files, &FileDiff{Status: statusIdentical, FileDsc: dsc1})
}
matched[dsc1.Hash]++
continue
}
// Let's try to find the file somewhere else:
for _, dsc1 := range dir1ByHash[dsc2.Hash] {
// Ignore accidentally equal hashes if the files are of different sizes
if dsc1.Size != dsc2.Size {
continue
}
r := &FileDiff{Status: statusMoved, FileDsc: dsc2}
r.Path = dsc1.Path + " -> " + dsc2.Path
res.Files = append(res.Files, r)
matched[dsc2.Hash]++
continue t2loop // Once we found one match we continue with the outer loop.
}
res.Files = append(res.Files, &FileDiff{Status: statusAdded, FileDsc: dsc2})
}
for _, dsc1 := range t1.Files {
if dsc1.IsDir {
continue
}
num := matched[dsc1.Hash]
if num == 0 {
res.Files = append(res.Files, &FileDiff{Status: statusDeleted, FileDsc: dsc1})
} else {
matched[dsc1.Hash]--
}
}
slices.SortStableFunc(res.Files, func(a, b *FileDiff) int {
return cmp.Compare(a.Path, b.Path)
})
return res
}
func (d *TreeDiff) String() string {
var sb strings.Builder
counts := map[string]int{}
for _, f := range d.Files {
counts[f.Status]++
if f.Status == statusIdentical {
continue
}
sb.WriteString(fmt.Sprintf("%s: %s\n", f.FormatStatus(), f.Path))
}
sb.WriteString(colored(fmt.Sprintf("\nTotal : %d \n", len(d.Files)), colorWhite, modeFont, styleOverline))
sb.WriteString(fmt.Sprintf("%s : %d\n", green(statusAdded), counts[statusAdded]))
sb.WriteString(fmt.Sprintf("%s : %d\n", red(statusDeleted), counts[statusDeleted]))
sb.WriteString(fmt.Sprintf("%s : %d\n", blue(statusMoved), counts[statusMoved]))
sb.WriteString(fmt.Sprintf("%s : %d\n", yellow(statusChanged), counts[statusChanged]))
sb.WriteString(fmt.Sprintf("%s : %d\n", statusIdentical, counts[statusIdentical]))
return sb.String()
}
func (d *FileDiff) FormatStatus() string {
st := fmt.Sprintf("%7s ", d.Status)
switch d.Status {
case statusIdentical:
return st
case statusChanged:
return yellow(st)
case statusMoved:
return blue(st)
case statusAdded:
return green(st)
case statusDeleted:
return red(st)
}
return st
}