File: patch.go

package info (click to toggle)
kitty 0.46.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,064 kB
  • sloc: ansic: 91,642; python: 59,657; objc: 6,273; sh: 1,333; xml: 364; makefile: 144; javascript: 78
file content (642 lines) | stat: -rw-r--r-- 17,261 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>

package diff

import (
	"bytes"
	"errors"
	"fmt"
	"os/exec"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"
	"sync"

	parallel "github.com/kovidgoyal/go-parallel"
	"github.com/kovidgoyal/kitty/tools/simdstring"
	"github.com/kovidgoyal/kitty/tools/utils"
	"github.com/kovidgoyal/kitty/tools/utils/images"
	"github.com/kovidgoyal/kitty/tools/utils/shlex"
)

var _ = fmt.Print

const GIT_DIFF = `git diff --no-color --no-ext-diff --exit-code -U_CONTEXT_ --no-index --`
const DIFF_DIFF = `diff -p -U _CONTEXT_ --`

var diff_cmd []string

var GitExe = sync.OnceValue(func() string {
	return utils.FindExe("git")
})

var DiffExe = sync.OnceValue(func() string {
	return utils.FindExe("diff")
})

func find_differ() {
	if GitExe() != "git" && exec.Command(GitExe(), "--help").Run() == nil {
		diff_cmd, _ = shlex.Split(GIT_DIFF)
	} else if DiffExe() != "diff" && exec.Command(DiffExe(), "--help").Run() == nil {
		diff_cmd, _ = shlex.Split(DIFF_DIFF)
	} else {
		diff_cmd = []string{}
	}
}

func set_diff_command(q string) error {
	switch q {
	case "auto":
		find_differ()
	case "builtin", "":
		diff_cmd = []string{}
	case "diff":
		diff_cmd, _ = shlex.Split(DIFF_DIFF)
	case "git":
		diff_cmd, _ = shlex.Split(GIT_DIFF)
	default:
		c, err := shlex.Split(q)
		if err != nil {
			return err
		}
		diff_cmd = c
	}
	return nil
}

// Region represents a highlighted byte range within a line.
type Region struct{ offset, size int }

// Center holds the highlighted regions for the left (removed) and right (added) sides of a changed line pair.
type Center struct {
	left_regions  []Region
	right_regions []Region
}

type Chunk struct {
	is_context              bool
	left_start, right_start int
	left_count, right_count int
	centers                 []Center
}

func (self *Chunk) add_line() {
	self.right_count++
}

func (self *Chunk) remove_line() {
	self.left_count++
}

func (self *Chunk) context_line() {
	self.left_count++
	self.right_count++
}

// changed_center computes the central changed region of left/right at the byte level.
func changed_center(left, right string) (ans Center) {
	if len(left) > 0 && len(right) > 0 {
		ll, rl := len(left), len(right)
		ml := utils.Min(ll, rl)
		offset := 0
		for ; offset < ml && left[offset] == right[offset]; offset++ {
		}
		suffix_count := 0
		for ; suffix_count < ml && left[ll-1-suffix_count] == right[rl-1-suffix_count]; suffix_count++ {
		}
		left_size := ll - suffix_count - offset
		right_size := rl - suffix_count - offset
		if left_size > 0 {
			ans.left_regions = []Region{{offset: offset, size: left_size}}
		}
		if right_size > 0 {
			ans.right_regions = []Region{{offset: offset, size: right_size}}
		}
	}
	return
}

var word_regexp = sync.OnceValues(func() (*regexp.Regexp, error) {
	pattern := `\S+`
	if conf != nil && conf.Word_regex != "" {
		pattern = conf.Word_regex
	}
	return regexp.Compile(pattern)
})

// word_diff_center computes highlighted regions for changed words between left and right.
func word_diff_center(left, right string, re *regexp.Regexp) Center {
	left_matches := re.FindAllStringIndex(left, -1)
	right_matches := re.FindAllStringIndex(right, -1)

	type word struct {
		text   string
		offset int
		size   int
	}
	left_words := make([]word, len(left_matches))
	right_words := make([]word, len(right_matches))
	for i, m := range left_matches {
		left_words[i] = word{text: left[m[0]:m[1]], offset: m[0], size: m[1] - m[0]}
	}
	for i, m := range right_matches {
		right_words[i] = word{text: right[m[0]:m[1]], offset: m[0], size: m[1] - m[0]}
	}

	// Strip common prefix and suffix words so LCS only runs on the differing middle.
	prefix := 0
	for prefix < len(left_words) && prefix < len(right_words) && left_words[prefix].text == right_words[prefix].text {
		prefix++
	}
	suffix := 0
	for suffix < len(left_words)-prefix && suffix < len(right_words)-prefix &&
		left_words[len(left_words)-1-suffix].text == right_words[len(right_words)-1-suffix].text {
		suffix++
	}
	lw := left_words[prefix : len(left_words)-suffix]
	rw := right_words[prefix : len(right_words)-suffix]

	m, n := len(lw), len(rw)
	// LCS dynamic programming table
	dp := make([][]int, m+1)
	for i := range dp {
		dp[i] = make([]int, n+1)
	}
	for i := 1; i <= m; i++ {
		for j := 1; j <= n; j++ {
			if lw[i-1].text == rw[j-1].text {
				dp[i][j] = dp[i-1][j-1] + 1
			} else if dp[i-1][j] > dp[i][j-1] {
				dp[i][j] = dp[i-1][j]
			} else {
				dp[i][j] = dp[i][j-1]
			}
		}
	}

	// Backtrack to find changed words within the middle slice.
	left_changed := make([]bool, m)
	right_changed := make([]bool, n)
	i, j := m, n
	for i > 0 && j > 0 {
		if lw[i-1].text == rw[j-1].text {
			i--
			j--
		} else if dp[i-1][j] > dp[i][j-1] {
			left_changed[i-1] = true
			i--
		} else {
			right_changed[j-1] = true
			j--
		}
	}
	for i > 0 {
		left_changed[i-1] = true
		i--
	}
	for j > 0 {
		right_changed[j-1] = true
		j--
	}

	// Remap changed flags to absolute indices within the full words arrays.
	left_changed_abs := make([]bool, len(left_words))
	right_changed_abs := make([]bool, len(right_words))
	for k, c := range left_changed {
		left_changed_abs[prefix+k] = c
	}
	for k, c := range right_changed {
		right_changed_abs[prefix+k] = c
	}

	// Verify that every changed word at index i has a corresponding changed
	// word at the same index i on the other side.  If any position is changed
	// on exactly one side, the lines differ in word count and it makes more
	// sense to highlight the single changed central region of the whole line.
	max_idx := max(len(left_words), len(right_words))
	for idx := 0; idx < max_idx; idx++ {
		lc := idx < len(left_words) && left_changed_abs[idx]
		rc := idx < len(right_words) && right_changed_abs[idx]
		if lc != rc {
			return changed_center(left, right)
		}
	}

	// All changed words are positionally paired.  Apply character-level
	// prefix/suffix trimming to each pair so only the differing central bytes
	// are highlighted.
	var ans Center
	for idx := range left_words {
		if !left_changed_abs[idx] {
			continue
		}
		lword := left_words[idx]
		rword := right_words[idx]
		lt := left[lword.offset : lword.offset+lword.size]
		rt := right[rword.offset : rword.offset+rword.size]
		ll, rl := len(lt), len(rt)
		ml := min(ll, rl)
		cpfx := 0
		for cpfx < ml && lt[cpfx] == rt[cpfx] {
			cpfx++
		}
		csfx := 0
		for csfx < ml-cpfx && lt[ll-1-csfx] == rt[rl-1-csfx] {
			csfx++
		}
		lsize := ll - cpfx - csfx
		rsize := rl - cpfx - csfx
		if lsize > 0 {
			ans.left_regions = append(ans.left_regions, Region{offset: lword.offset + cpfx, size: lsize})
		}
		if rsize > 0 {
			ans.right_regions = append(ans.right_regions, Region{offset: rword.offset + cpfx, size: rsize})
		}
	}
	return ans
}

func (self *Chunk) finalize(_ []string, _ []string) {
	// Center computation is performed in parallel by Patch.compute_centers
}

type Hunk struct {
	left_start, left_count     int
	right_start, right_count   int
	title                      string
	added_count, removed_count int
	chunks                     []*Chunk
	current_chunk              *Chunk
	largest_line_number        int
}

func (self *Hunk) new_chunk(is_context bool) *Chunk {
	left_start, right_start := self.left_start, self.right_start
	if len(self.chunks) > 0 {
		c := self.chunks[len(self.chunks)-1]
		left_start = c.left_start + c.left_count
		right_start = c.right_start + c.right_count
	}
	return &Chunk{is_context: is_context, left_start: left_start, right_start: right_start}
}

func (self *Hunk) ensure_diff_chunk() {
	if self.current_chunk == nil || self.current_chunk.is_context {
		if self.current_chunk != nil {
			self.chunks = append(self.chunks, self.current_chunk)
		}
		self.current_chunk = self.new_chunk(false)
	}
}

func (self *Hunk) ensure_context_chunk() {
	if self.current_chunk == nil || !self.current_chunk.is_context {
		if self.current_chunk != nil {
			self.chunks = append(self.chunks, self.current_chunk)
		}
		self.current_chunk = self.new_chunk(true)
	}
}

func (self *Hunk) add_line() {
	self.ensure_diff_chunk()
	self.current_chunk.add_line()
	self.added_count++
}

func (self *Hunk) remove_line() {
	self.ensure_diff_chunk()
	self.current_chunk.remove_line()
	self.removed_count++
}

func (self *Hunk) context_line() {
	self.ensure_context_chunk()
	self.current_chunk.context_line()
}

func (self *Hunk) finalize(left_lines, right_lines []string) error {
	if self.current_chunk != nil {
		self.chunks = append(self.chunks, self.current_chunk)
	}
	// Sanity check
	c := self.chunks[len(self.chunks)-1]
	if c.left_start+c.left_count != self.left_start+self.left_count {
		return fmt.Errorf("Left side line mismatch %d != %d", c.left_start+c.left_count, self.left_start+self.left_count)
	}
	if c.right_start+c.right_count != self.right_start+self.right_count {
		return fmt.Errorf("Right side line mismatch %d != %d", c.right_start+c.right_count, self.right_start+self.right_count)
	}
	for _, c := range self.chunks {
		c.finalize(left_lines, right_lines)
	}
	return nil
}

type Patch struct {
	all_hunks                                       []*Hunk
	largest_line_number, added_count, removed_count int
	left_moved_lines, right_moved_lines             *utils.Set[int]
}

func (self *Patch) Len() int { return len(self.all_hunks) }

func splitlines_like_git(raw string, strip_trailing_lines bool, process_line func(string)) {
	sz := len(raw)
	if strip_trailing_lines {
		for sz > 0 && (raw[sz-1] == '\n' || raw[sz-1] == '\r') {
			sz--
		}
	}
	start := 0
	for i := 0; i < sz; i++ {
		switch raw[i] {
		case '\n':
			process_line(raw[start:i])
			start = i + 1
		case '\r':
			process_line(raw[start:i])
			start = i + 1
			if start < sz && raw[start] == '\n' {
				i++
				start++
			}
		}
	}
	if start < sz {
		process_line(raw[start:sz])
	}
}

func parse_range(x string) (start, count int) {
	s, c, found := strings.Cut(x, ",")
	start, _ = strconv.Atoi(s)
	if start < 0 {
		start = -start
	}
	count = 1
	if found {
		count, _ = strconv.Atoi(c)
	}
	return
}

func parse_hunk_header(line string) *Hunk {
	parts := strings.SplitN(line, "@@", 3)
	linespec := strings.TrimSpace(parts[1])
	title := ""
	if len(parts) == 3 {
		title = strings.TrimSpace(parts[2])
	}
	left, right, _ := strings.Cut(linespec, " ")
	ls, lc := parse_range(left)
	rs, rc := parse_range(right)
	return &Hunk{
		title: title, left_start: ls - 1, left_count: lc, right_start: rs - 1, right_count: rc,
		largest_line_number: utils.Max(ls-1+lc, rs-1+rc),
	}
}

func (self *Patch) compute_centers(left_lines, right_lines []string) error {
	word_mode := conf != nil && conf.Word_diff_mode == Word_diff_mode_words
	var re *regexp.Regexp
	if word_mode {
		var err error
		re, err = word_regexp()
		if err != nil {
			return fmt.Errorf("Failed to compile word_regex %q: %w", conf.Word_regex, err)
		}
	}

	type pair struct {
		chunk *Chunk
		idx   int
	}
	var pairs []pair
	for _, hunk := range self.all_hunks {
		for _, chunk := range hunk.chunks {
			if !chunk.is_context && chunk.left_count == chunk.right_count {
				for i := 0; i < chunk.left_count; i++ {
					pairs = append(pairs, pair{chunk, i})
				}
			}
		}
	}
	if len(pairs) == 0 {
		return nil
	}
	centers := make([]Center, len(pairs))
	if err := parallel.Run_in_parallel_over_range(0, func(start, end int) {
		for i := start; i < end; i++ {
			p := pairs[i]
			left := left_lines[p.chunk.left_start+p.idx]
			right := right_lines[p.chunk.right_start+p.idx]
			if word_mode {
				centers[i] = word_diff_center(left, right, re)
			} else {
				centers[i] = changed_center(left, right)
			}
		}
	}, 0, len(pairs)); err != nil {
		return err
	}
	ci := 0
	for _, hunk := range self.all_hunks {
		for _, chunk := range hunk.chunks {
			if !chunk.is_context && chunk.left_count == chunk.right_count {
				chunk.centers = centers[ci : ci+chunk.left_count]
				ci += chunk.left_count
			}
		}
	}
	return nil
}

// Use SIMD to efficiently find non-blank lines: a line is non-blank if it
// contains at least one character that is not a space or tab.
func is_non_blank(text string) bool {
	return simdstring.NotIndexByte2String(text, ' ', '\t') >= 0
}

func (self *Patch) detect_moved_lines(left_lines, right_lines []string) {
	// Build maps from line text to lists of line numbers for removed and added lines.
	removed := make(map[string][]int, len(left_lines)) // text -> left line numbers
	added := make(map[string][]int, len(right_lines))  // text -> right line numbers
	for _, hunk := range self.all_hunks {
		for _, chunk := range hunk.chunks {
			if !chunk.is_context {
				for i := 0; i < chunk.left_count; i++ {
					lnum := chunk.left_start + i
					text := left_lines[lnum]
					if is_non_blank(text) {
						removed[text] = append(removed[text], lnum)
					}
				}
				for i := 0; i < chunk.right_count; i++ {
					rnum := chunk.right_start + i
					text := right_lines[rnum]
					if is_non_blank(text) {
						added[text] = append(added[text], rnum)
					}
				}
			}
		}
	}
	// Lines that appear in both removed and added sets are moved lines. When a
	// line appears multiple times on each side, only min(left_count,
	// right_count) occurrences are marked as moved.
	self.left_moved_lines = utils.NewSet[int]()
	self.right_moved_lines = utils.NewSet[int]()
	for text, lnums := range removed {
		if rnums, ok := added[text]; ok {
			count := min(len(lnums), len(rnums))
			for _, lnum := range lnums[:count] {
				self.left_moved_lines.Add(lnum)
			}
			for _, rnum := range rnums[:count] {
				self.right_moved_lines.Add(rnum)
			}
		}
	}
}

func parse_patch(raw string, left_lines, right_lines []string) (ans *Patch, err error) {
	ans = &Patch{all_hunks: make([]*Hunk, 0, 32)}
	var current_hunk *Hunk
	splitlines_like_git(raw, true, func(line string) {
		if strings.HasPrefix(line, "@@ ") {
			current_hunk = parse_hunk_header(line)
			ans.all_hunks = append(ans.all_hunks, current_hunk)
		} else if current_hunk != nil {
			var ch byte
			if len(line) > 0 {
				ch = line[0]
			}
			switch ch {
			case '+':
				current_hunk.add_line()
			case '-':
				current_hunk.remove_line()
			case '\\':
			default:
				current_hunk.context_line()
			}
		}
	})
	for _, h := range ans.all_hunks {
		err = h.finalize(left_lines, right_lines)
		if err != nil {
			return
		}
		ans.added_count += h.added_count
		ans.removed_count += h.removed_count
	}
	if len(ans.all_hunks) > 0 {
		ans.largest_line_number = ans.all_hunks[len(ans.all_hunks)-1].largest_line_number
	}
	err = ans.compute_centers(left_lines, right_lines)
	if err == nil && conf.Mark_moved_lines {
		ans.detect_moved_lines(left_lines, right_lines)
	}
	return
}

func run_diff(file1, file2 string, num_of_context_lines int) (ok, is_different bool, patch string, err error) {
	// we resolve symlinks because git diff does not follow symlinks, while diff
	// does. We want consistent behavior, also for integration with git difftool
	// we always want symlinks to be followed.
	path1, err := filepath.EvalSymlinks(file1)
	if err != nil {
		return
	}
	path2, err := filepath.EvalSymlinks(file2)
	if err != nil {
		return
	}
	if len(diff_cmd) == 0 {
		data1, err := data_for_path(path1)
		if err != nil {
			return false, false, "", err
		}
		data2, err := data_for_path(path2)
		if err != nil {
			return false, false, "", err
		}
		patchb := Diff(path1, data1, path2, data2, num_of_context_lines)
		if patchb == nil {
			return true, false, "", nil
		}
		return true, len(patchb) > 0, utils.UnsafeBytesToString(patchb), nil
	} else {
		context := strconv.Itoa(num_of_context_lines)
		cmd := utils.Map(func(x string) string {
			return strings.ReplaceAll(x, "_CONTEXT_", context)
		}, diff_cmd)

		cmd = append(cmd, path1, path2)
		c := exec.Command(cmd[0], cmd[1:]...)
		stdout, stderr := bytes.Buffer{}, bytes.Buffer{}
		c.Stdout, c.Stderr = &stdout, &stderr
		err = c.Run()
		if err != nil {
			var e *exec.ExitError
			if errors.As(err, &e) && e.ExitCode() == 1 {
				return true, true, stdout.String(), nil
			}
			return false, false, stderr.String(), err
		}
		return true, false, stdout.String(), nil
	}
}

func do_diff(file1, file2 string, context_count int) (ans *Patch, err error) {
	ok, _, raw, err := run_diff(file1, file2, context_count)
	if !ok {
		return nil, fmt.Errorf("Failed to diff %s vs. %s with errors:\n%s", file1, file2, raw)
	}
	if err != nil {
		return
	}
	left_lines, err := lines_for_path(file1)
	if err != nil {
		return
	}
	right_lines, err := lines_for_path(file2)
	if err != nil {
		return
	}
	ans, err = parse_patch(raw, left_lines, right_lines)
	return
}

type diff_job struct{ file1, file2 string }

func diff(jobs []diff_job, context_count int) (ans map[string]*Patch, err error) {
	ans = make(map[string]*Patch)
	ctx := images.Context{}
	type result struct {
		file1, file2 string
		err          error
		patch        *Patch
	}
	results := make(chan result, len(jobs))
	if err := ctx.SafeParallel(0, len(jobs), func(nums <-chan int) {
		for i := range nums {
			job := jobs[i]
			r := result{file1: job.file1, file2: job.file2}
			r.patch, r.err = do_diff(job.file1, job.file2, context_count)
			results <- r
		}
	}); err != nil {
		panic(err)
	}
	close(results)
	for r := range results {
		if r.err != nil {
			return nil, r.err
		}
		ans[r.file1] = r.patch
	}
	return ans, nil
}