-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
61 lines (55 loc) · 1.81 KB
/
diff.go
File metadata and controls
61 lines (55 loc) · 1.81 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
package main
import "fmt"
const diffContextLines = 3
// printWriteResult prints the result of a write or append to stdout.
// Context lines (unchanged) are shown with their existing hashes.
//
// Two block types are used depending on what happened in the replaced range:
// - <NewLines> — new or replacement content, shown with freshly allocated hashes.
// - <DeletedLines> — pure deletion (empty new content), shown with the old hashes that were removed.
func printWriteResult(oldLines, oldHashes []string, startIdx, endIdx int, newLines, newHashes []string) {
ctxStart := startIdx - diffContextLines
if ctxStart < 0 {
ctxStart = 0
}
// Context above — unchanged lines with their existing hashes.
for i := ctxStart; i < startIdx && i < len(oldLines); i++ {
hash := ""
if i < len(oldHashes) {
hash = oldHashes[i] + " "
}
fmt.Printf(" %s%s\n", hash, oldLines[i])
}
if len(newLines) == 0 {
// Pure deletion — show the lines that were removed with their old hashes.
fmt.Println("<DeletedLines>")
endCap := endIdx + 1
if endCap > len(oldLines) {
endCap = len(oldLines)
}
for _, line := range oldLines[startIdx:endCap] {
fmt.Printf(" %s\n", line)
}
fmt.Println("</DeletedLines>")
} else {
// New or replacement content — show incoming lines with their fresh hashes.
fmt.Println("<NewLines>")
for i, line := range newLines {
fmt.Printf(" %s %s\n", newHashes[i], line)
}
fmt.Println("</NewLines>")
}
// Context below — lines after the replaced region, with their existing hashes.
afterStart := endIdx + 1
afterEnd := afterStart + diffContextLines
if afterEnd > len(oldLines) {
afterEnd = len(oldLines)
}
for i := afterStart; i < afterEnd; i++ {
hash := ""
if i < len(oldHashes) {
hash = oldHashes[i] + " "
}
fmt.Printf(" %s%s\n", hash, oldLines[i])
}
}