File: collect.go

package info (click to toggle)
kitty 0.44.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,540 kB
  • sloc: ansic: 84,052; python: 57,643; objc: 5,365; sh: 1,319; xml: 364; makefile: 144; javascript: 78
file content (402 lines) | stat: -rw-r--r-- 10,233 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
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>

package diff

import (
	"crypto/md5"
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
	"strings"
	"unicode/utf8"

	"github.com/kovidgoyal/kitty/tools/utils"
)

var _ = fmt.Print
var path_name_map, remote_dirs map[string]string

var mimetypes_cache, data_cache, hash_cache *utils.LRUCache[string, string]
var size_cache *utils.LRUCache[string, int64]
var lines_cache *utils.LRUCache[string, []string]
var light_highlighted_lines_cache *utils.LRUCache[string, []string]
var dark_highlighted_lines_cache *utils.LRUCache[string, []string]
var is_text_cache *utils.LRUCache[string, bool]

func init_caches() {
	path_name_map = make(map[string]string, 32)
	remote_dirs = make(map[string]string, 32)
	const sz = 4096
	size_cache = utils.NewLRUCache[string, int64](sz)
	mimetypes_cache = utils.NewLRUCache[string, string](sz)
	data_cache = utils.NewLRUCache[string, string](sz)
	is_text_cache = utils.NewLRUCache[string, bool](sz)
	lines_cache = utils.NewLRUCache[string, []string](sz)
	light_highlighted_lines_cache = utils.NewLRUCache[string, []string](sz)
	dark_highlighted_lines_cache = utils.NewLRUCache[string, []string](sz)
	hash_cache = utils.NewLRUCache[string, string](sz)
}

func add_remote_dir(val string) {
	x := filepath.Base(val)
	idx := strings.LastIndex(x, "-")
	if idx > -1 {
		x = x[idx+1:]
	} else {
		x = ""
	}
	remote_dirs[val] = x
}

func mimetype_for_path(path string) string {
	return mimetypes_cache.MustGetOrCreate(path, func(path string) string {
		mt := utils.GuessMimeTypeWithFileSystemAccess(path)
		if mt == "" {
			mt = "application/octet-stream"
		}
		if utils.KnownTextualMimes[mt] {
			if _, a, found := strings.Cut(mt, "/"); found {
				mt = "text/" + a
			}
		}
		return mt
	})
}

func data_for_path(path string) (string, error) {
	return data_cache.GetOrCreate(path, func(path string) (string, error) {
		ans, err := os.ReadFile(path)
		return utils.UnsafeBytesToString(ans), err
	})
}

func size_for_path(path string) (int64, error) {
	return size_cache.GetOrCreate(path, func(path string) (int64, error) {
		s, err := os.Stat(path)
		if err != nil {
			return 0, err
		}
		return s.Size(), nil
	})
}

func is_image(path string) bool {
	return strings.HasPrefix(mimetype_for_path(path), "image/")
}

func is_path_text(path string) bool {
	return is_text_cache.MustGetOrCreate(path, func(path string) bool {
		if is_image(path) {
			return false
		}
		s1, err := os.Stat(path)
		if err == nil {
			s2, err := os.Stat("/dev/null")
			if err == nil && os.SameFile(s1, s2) {
				return false
			}
		}
		d, err := data_for_path(path)
		if err != nil {
			return false
		}
		return utf8.ValidString(d)
	})
}

func hash_for_path(path string) (string, error) {
	return hash_cache.GetOrCreate(path, func(path string) (string, error) {
		ans, err := data_for_path(path)
		if err != nil {
			return "", err
		}
		hash := md5.Sum(utils.UnsafeStringToBytes(ans))
		return utils.UnsafeBytesToString(hash[:]), err
	})

}

func text_to_lines(text string) []string {
	lines := make([]string, 0, 512)
	splitlines_like_git(text, false, func(line string) { lines = append(lines, line) })
	return lines
}

func sanitize(text string) string { return utils.ReplaceControlCodes(text, conf.Replace_tab_by, "\n") }

func lines_for_path(path string) ([]string, error) {
	return lines_cache.GetOrCreate(path, func(path string) ([]string, error) {
		ans, err := data_for_path(path)
		if err != nil {
			return nil, err
		}
		return text_to_lines(sanitize(ans)), nil
	})
}

func highlighted_lines_for_path(path string) ([]string, error) {
	plain_lines, err := lines_for_path(path)
	if err != nil {
		return nil, err
	}
	var ans []string
	var found bool
	if use_light_colors {
		ans, found = light_highlighted_lines_cache.Get(path)
	} else {
		ans, found = dark_highlighted_lines_cache.Get(path)
	}
	if found && len(ans) == len(plain_lines) {
		return ans, nil
	}
	return plain_lines, nil
}

type Collection struct {
	changes, renames, type_map map[string]string
	adds, removes              *utils.Set[string]
	all_paths                  []string
	paths_to_highlight         *utils.Set[string]
	added_count, removed_count int
}

func (self *Collection) add_change(left, right string) {
	self.changes[left] = right
	self.all_paths = append(self.all_paths, left)
	self.paths_to_highlight.Add(left)
	self.paths_to_highlight.Add(right)
	self.type_map[left] = `diff`
}

func (self *Collection) add_rename(left, right string) {
	self.renames[left] = right
	self.all_paths = append(self.all_paths, left)
	self.type_map[left] = `rename`
}

func (self *Collection) add_add(right string) {
	self.adds.Add(right)
	self.all_paths = append(self.all_paths, right)
	self.paths_to_highlight.Add(right)
	self.type_map[right] = `add`
	if is_path_text(right) {
		num, _ := lines_for_path(right)
		self.added_count += len(num)
	}
}

func (self *Collection) add_removal(left string) {
	self.removes.Add(left)
	self.all_paths = append(self.all_paths, left)
	self.paths_to_highlight.Add(left)
	self.type_map[left] = `removal`
	if is_path_text(left) {
		num, _ := lines_for_path(left)
		self.removed_count += len(num)
	}
}

func (self *Collection) finalize() {
	utils.StableSortWithKey(self.all_paths, func(path string) string {
		return path_name_map[path]
	})
}

func (self *Collection) Len() int { return len(self.all_paths) }

func (self *Collection) Items() int { return len(self.all_paths) }

func (self *Collection) Apply(f func(path, typ, changed_path string) error) error {
	for _, path := range self.all_paths {
		typ := self.type_map[path]
		changed_path := ""
		switch typ {
		case "diff":
			changed_path = self.changes[path]
		case "rename":
			changed_path = self.renames[path]
		}
		if err := f(path, typ, changed_path); err != nil {
			return err
		}
	}
	return nil
}

func allowed(path string, patterns ...string) bool {
	name := filepath.Base(path)
	for _, pat := range patterns {
		if matched, err := filepath.Match(pat, name); err == nil && matched {
			return false
		}
	}
	return true
}

func remote_hostname(path string) (string, string) {
	for q, val := range remote_dirs {
		if strings.HasPrefix(path, q) {
			return q, val
		}
	}
	return "", ""
}

func resolve_remote_name(path, defval string) string {
	remote_dir, rh := remote_hostname(path)
	if remote_dir != "" && rh != "" {
		r, err := filepath.Rel(remote_dir, path)
		if err == nil {
			return rh + ":" + r
		}
	}
	return defval
}

func walk(base string, patterns []string, names *utils.Set[string], pmap, path_name_map map[string]string) error {
	base, err := filepath.Abs(base)
	if err != nil {
		return err
	}
	return filepath.WalkDir(base, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		is_allowed := allowed(path, patterns...)
		if !is_allowed {
			if d.IsDir() {
				return fs.SkipDir
			}
			return nil
		}
		if d.IsDir() {
			return nil
		}
		path, err = filepath.Abs(path)
		if err != nil {
			return err
		}
		name, err := filepath.Rel(base, path)
		if err != nil {
			return err
		}
		if name != "." {
			path_name_map[path] = name
			names.Add(name)
			pmap[name] = path
		}
		return nil
	})
}

func (self *Collection) collect_files(left, right string) error {
	left_names, right_names := utils.NewSet[string](16), utils.NewSet[string](16)
	left_path_map, right_path_map := make(map[string]string, 16), make(map[string]string, 16)
	err := walk(left, conf.Ignore_name, left_names, left_path_map, path_name_map)
	if err != nil {
		return err
	}
	if err = walk(right, conf.Ignore_name, right_names, right_path_map, path_name_map); err != nil {
		return err
	}
	common_names := left_names.Intersect(right_names)
	changed_names := utils.NewSet[string](common_names.Len())
	for n := range common_names.Iterable() {
		ld, err := data_for_path(left_path_map[n])
		var rd string
		if err == nil {
			rd, err = data_for_path(right_path_map[n])
		}
		if err != nil {
			return err
		}
		if ld != rd {
			changed_names.Add(n)
			self.add_change(left_path_map[n], right_path_map[n])
		} else {
			if lstat, err := os.Stat(left_path_map[n]); err == nil {
				if rstat, err := os.Stat(right_path_map[n]); err == nil {
					if lstat.Mode() != rstat.Mode() {
						// identical files with only a mode change
						changed_names.Add(n)
						self.add_change(left_path_map[n], right_path_map[n])
					}
				}
			}
		}
	}
	removed := left_names.Subtract(common_names)
	added := right_names.Subtract(common_names)
	ahash, rhash := make(map[string]string, added.Len()), make(map[string]string, removed.Len())
	for a := range added.Iterable() {
		ahash[a], err = hash_for_path(right_path_map[a])
		if err != nil {
			return err
		}
	}
	for r := range removed.Iterable() {
		rhash[r], err = hash_for_path(left_path_map[r])
		if err != nil {
			return err
		}
	}
	for name, rh := range rhash {
		found := false
		for n, ah := range ahash {
			if ah == rh {
				ld, _ := data_for_path(left_path_map[name])
				rd, _ := data_for_path(right_path_map[n])
				if ld == rd {
					self.add_rename(left_path_map[name], right_path_map[n])
					added.Discard(n)
					found = true
					break
				}
			}
		}
		if !found {
			self.add_removal(left_path_map[name])
		}
	}
	for name := range added.Iterable() {
		self.add_add(right_path_map[name])
	}
	return nil
}

func create_collection(left, right string) (ans *Collection, err error) {
	ans = &Collection{
		changes:            make(map[string]string),
		renames:            make(map[string]string),
		type_map:           make(map[string]string),
		adds:               utils.NewSet[string](32),
		removes:            utils.NewSet[string](32),
		paths_to_highlight: utils.NewSet[string](32),
		all_paths:          make([]string, 0, 32),
	}
	left_stat, err := os.Stat(left)
	if err != nil {
		return nil, err
	}
	if left_stat.IsDir() {
		err = ans.collect_files(left, right)
		if err != nil {
			return nil, err
		}
	} else {
		pl, err := filepath.Abs(left)
		if err != nil {
			return nil, err
		}
		pr, err := filepath.Abs(right)
		if err != nil {
			return nil, err
		}
		path_name_map[pl] = resolve_remote_name(pl, left)
		path_name_map[pr] = resolve_remote_name(pr, right)
		ans.add_change(pl, pr)
	}
	ans.finalize()
	return ans, err
}