git_diff_live 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +33 -0
- data/LICENSE.txt +21 -0
- data/README.md +128 -0
- data/app/assets/builds/git_diff_live.css +2 -0
- data/app/controllers/git_diff_live/application_controller.rb +27 -0
- data/app/controllers/git_diff_live/changes_controller.rb +73 -0
- data/app/helpers/git_diff_live/changes_helper.rb +134 -0
- data/app/javascript/git_diff_live/application.js +9 -0
- data/app/javascript/git_diff_live/controllers/changes_controller.js +114 -0
- data/app/views/git_diff_live/changes/_empty.html.slim +3 -0
- data/app/views/git_diff_live/changes/_file.html.slim +106 -0
- data/app/views/git_diff_live/changes/_nav.html.slim +13 -0
- data/app/views/git_diff_live/changes/_summary.html.slim +17 -0
- data/app/views/git_diff_live/changes/file.turbo_stream.slim +2 -0
- data/app/views/git_diff_live/changes/index.html.slim +42 -0
- data/app/views/git_diff_live/changes/refresh.turbo_stream.slim +29 -0
- data/app/views/layouts/git_diff_live/application.html.slim +17 -0
- data/config/importmap.rb +6 -0
- data/config/routes.rb +12 -0
- data/exe/git_diff_live +15 -0
- data/lib/git_diff_live/cli.rb +177 -0
- data/lib/git_diff_live/comments.rb +181 -0
- data/lib/git_diff_live/diff.rb +535 -0
- data/lib/git_diff_live/engine.rb +45 -0
- data/lib/git_diff_live/version.rb +5 -0
- data/lib/git_diff_live.rb +88 -0
- data/skills/explain-changes/SKILL.md +53 -0
- data/skills/show-changes/SKILL.md +37 -0
- metadata +171 -0
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
require "digest"
|
|
2
|
+
require "open3"
|
|
3
|
+
require "pathname"
|
|
4
|
+
|
|
5
|
+
require_relative "comments"
|
|
6
|
+
|
|
7
|
+
module GitDiffLive
|
|
8
|
+
|
|
9
|
+
# A repository's working tree compared with a commit, as side-by-side rows:
|
|
10
|
+
# what the changes page draws. Git computes the diff, renames and binary files
|
|
11
|
+
# included; this class only reads its output. Staged and unstaged changes are
|
|
12
|
+
# shown together, and untracked files are listed as added.
|
|
13
|
+
#
|
|
14
|
+
# diff = GitDiffLive::Diff.new(root: Rails.root, base: "HEAD")
|
|
15
|
+
# diff.files.each do | file |
|
|
16
|
+
# file.status # :modified, :added, :deleted, :renamed, :copied or :unmerged
|
|
17
|
+
# file.hunks.each do | hunk |
|
|
18
|
+
# hunk.rows.each { | row | row.old&.number; row.new&.segments }
|
|
19
|
+
# end
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# With a +head+ it compares two commits instead, and the working tree is not
|
|
23
|
+
# read at all.
|
|
24
|
+
#
|
|
25
|
+
# Each file carries the comments written about its changes within the
|
|
26
|
+
# comparison (Comments); the store they are kept in is not listed as a file.
|
|
27
|
+
#
|
|
28
|
+
# +context+ is the number of unchanged lines kept around each change;
|
|
29
|
+
# WHOLE_FILE keeps them all, so a file comes back complete.
|
|
30
|
+
#
|
|
31
|
+
# Plain Ruby: it needs git on the PATH and nothing from Rails.
|
|
32
|
+
class Diff
|
|
33
|
+
|
|
34
|
+
# Raised for a +base+ that does not name a commit.
|
|
35
|
+
class UnknownRevision < StandardError; end
|
|
36
|
+
|
|
37
|
+
# Raised when git itself fails.
|
|
38
|
+
class GitError < StandardError; end
|
|
39
|
+
|
|
40
|
+
DEFAULT_CONTEXT = 3
|
|
41
|
+
|
|
42
|
+
# More context lines than any file has.
|
|
43
|
+
WHOLE_FILE = 1_000_000
|
|
44
|
+
|
|
45
|
+
# A file with more changed lines than this is not drawn until asked for.
|
|
46
|
+
LARGE_FILE = 1_500
|
|
47
|
+
|
|
48
|
+
# An untracked file bigger than this (1 MB) is listed without its contents.
|
|
49
|
+
MAX_UNTRACKED_BYTES = 1_048_576
|
|
50
|
+
|
|
51
|
+
# Two paired lines with more tokens between them than this (as a product) are
|
|
52
|
+
# not compared word by word.
|
|
53
|
+
MAX_TOKEN_PRODUCT = 20_000
|
|
54
|
+
|
|
55
|
+
STATUSES = { "M" => :modified, "T" => :modified, "A" => :added, "D" => :deleted, "R" => :renamed, "C" => :copied, "U" => :unmerged }.freeze
|
|
56
|
+
|
|
57
|
+
# One side of a row: its line number in that version of the file, its kind
|
|
58
|
+
# (:context, :removed or :added) and its text as [ text, changed ] segments,
|
|
59
|
+
# where a changed segment is the part of a paired line that differs.
|
|
60
|
+
Line = Data.define(:number, :kind, :segments) do
|
|
61
|
+
|
|
62
|
+
def text
|
|
63
|
+
segments.sum("") { it.first }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# A row of the comparison. +old+ is nil beside an added line, +new+ is nil
|
|
69
|
+
# beside a removed one.
|
|
70
|
+
Row = Data.define(:old, :new) do
|
|
71
|
+
|
|
72
|
+
def changed?
|
|
73
|
+
old&.kind == :removed || new&.kind == :added
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# A run of rows, headed by git's "@@ -12,7 +12,9 @@ def create" line.
|
|
79
|
+
Hunk = Data.define(:heading, :rows)
|
|
80
|
+
|
|
81
|
+
# One file of the comparison. The patch is parsed on first use, so listing
|
|
82
|
+
# many files to show one costs little.
|
|
83
|
+
class ChangedFile
|
|
84
|
+
|
|
85
|
+
# +comments+ are the ones written about this file within the comparison:
|
|
86
|
+
# those on the file itself (file_comments) and those on its lines, placed
|
|
87
|
+
# or not (placed_comments).
|
|
88
|
+
attr_reader :status, :path, :old_path, :comments
|
|
89
|
+
|
|
90
|
+
def initialize(status:, path:, old_path: nil, patch: "", untracked: false, unreadable: false, comments: [])
|
|
91
|
+
@status = status
|
|
92
|
+
@path = path
|
|
93
|
+
@old_path = old_path
|
|
94
|
+
@patch = patch
|
|
95
|
+
@untracked = untracked
|
|
96
|
+
@unreadable = unreadable
|
|
97
|
+
@comments = comments
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def untracked?
|
|
101
|
+
@untracked
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Drawn complete, every unchanged line included (Diff#whole).
|
|
105
|
+
def whole?
|
|
106
|
+
@whole == true
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# What this file's change is, in twelve characters: the page holds one
|
|
110
|
+
# per file it shows and sends them back when the tree changes, so only
|
|
111
|
+
# the files that are no longer the same are drawn again. A new comment
|
|
112
|
+
# is such a change.
|
|
113
|
+
def digest
|
|
114
|
+
@digest ||= Digest::SHA1.hexdigest([ status, old_path, @unreadable, @patch, *comments.flat_map(&:deconstruct) ].join("\0"))[0, 12]
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# This file as the whole-file version of +file+: it stands in for it on
|
|
118
|
+
# the page, so it answers with its digest. The two patches differ only in
|
|
119
|
+
# how much unchanged text surrounds the same change.
|
|
120
|
+
def as_whole_of(file)
|
|
121
|
+
@whole = true
|
|
122
|
+
@digest = file.digest
|
|
123
|
+
self
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Git could not show a text diff (an image, a font, a PDF), or the file is
|
|
127
|
+
# an untracked one too big to read.
|
|
128
|
+
def binary?
|
|
129
|
+
@unreadable || @patch.match?(/^(Binary files .* differ|GIT binary patch)$/)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def hunks
|
|
133
|
+
@hunks ||= parse
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Counted off the patch itself, not the parsed rows: the list of files
|
|
137
|
+
# shows every file's counts, and should not have to parse every file.
|
|
138
|
+
def additions
|
|
139
|
+
@additions ||= body.scan(/^\+/).size
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def deletions
|
|
143
|
+
@deletions ||= body.scan(/^-/).size
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Renamed or re-moded with no change to the content.
|
|
147
|
+
def unchanged_content?
|
|
148
|
+
!binary? && hunks.empty?
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def large?
|
|
152
|
+
additions + deletions > LARGE_FILE
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Where the first change sits in the file as it is now: the line an editor
|
|
156
|
+
# should open it at. A removal has no line of its own there, so it counts
|
|
157
|
+
# as the line after the one above it.
|
|
158
|
+
def first_changed_line
|
|
159
|
+
hunks.each do | hunk |
|
|
160
|
+
above = nil
|
|
161
|
+
|
|
162
|
+
hunk.rows.each do | row |
|
|
163
|
+
return row.new&.number || above.to_i + 1 if row.old&.kind == :removed || row.new&.kind == :added
|
|
164
|
+
|
|
165
|
+
above = row.new.number
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
1
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# The removed lines (:old) or the added lines (:new): the lines a comment
|
|
173
|
+
# can be about.
|
|
174
|
+
def changed_lines(side)
|
|
175
|
+
kind = side == :old ? :removed : :added
|
|
176
|
+
|
|
177
|
+
hunks.flat_map { | hunk | hunk.rows.filter_map { it.public_send(side) } }.select { it.kind == kind }
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# The comments on the file as a whole, drawn under its heading.
|
|
181
|
+
def file_comments
|
|
182
|
+
comments.select(&:file?)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# The file itself is the change, more than any line of it: it was added
|
|
186
|
+
# or deleted, or it has no changed lines at all (moved, binary). A
|
|
187
|
+
# comment on the file is then comment enough.
|
|
188
|
+
def whole_change?
|
|
189
|
+
hunks.empty? || [ :added, :deleted ].include?(status)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# The comments that still have their line here, by where they go:
|
|
193
|
+
# { [ :new, 12 ] => [ comment ] }. A comment goes beside the changed line
|
|
194
|
+
# whose text it was written about, the nearest to its own line number
|
|
195
|
+
# when that text is there more than once. One whose line is gone, or
|
|
196
|
+
# changed again, has no place.
|
|
197
|
+
def placed_comments
|
|
198
|
+
@placed_comments ||= begin
|
|
199
|
+
numbers = [ :old, :new ].to_h { | side | [ side, changed_lines(side).group_by(&:text).transform_values { it.map(&:number) } ] }
|
|
200
|
+
|
|
201
|
+
comments.reject(&:file?).each_with_object({}) do | comment, placed |
|
|
202
|
+
number = numbers[comment.side][comment.code]&.min_by { (it - comment.line).abs }
|
|
203
|
+
|
|
204
|
+
(placed[[ comment.side, number ]] ||= []) << comment if number
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# The comments to draw beside +row+: those about its removed line, then
|
|
210
|
+
# those about its added one.
|
|
211
|
+
def comments_beside(row)
|
|
212
|
+
return [] if comments.empty?
|
|
213
|
+
|
|
214
|
+
placed_comments.values_at(([ :old, row.old.number ] if row.old), ([ :new, row.new.number ] if row.new)).compact.flatten
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# A stable id for the file's element on the page.
|
|
218
|
+
def dom_id
|
|
219
|
+
self.class.dom_id(path)
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def self.dom_id(path)
|
|
223
|
+
"file-#{ Digest::SHA1.hexdigest(path)[0, 12] }"
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
private
|
|
227
|
+
|
|
228
|
+
# The patch from its first hunk on: the lines above it (index, ---, +++)
|
|
229
|
+
# are not changes.
|
|
230
|
+
def body
|
|
231
|
+
@patch[/^@@ .*/m].to_s
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# Reads the hunks out of the unified patch. A change is a run of "-" lines
|
|
235
|
+
# followed by a run of "+" lines: they are paired off in order, first
|
|
236
|
+
# removed with first added, and whatever is left over stands alone.
|
|
237
|
+
def parse
|
|
238
|
+
hunks = []
|
|
239
|
+
removed, added = [], []
|
|
240
|
+
old_number = new_number = 0
|
|
241
|
+
|
|
242
|
+
flush = lambda do
|
|
243
|
+
hunks.last.rows.concat(pair(removed, added)) if hunks.any?
|
|
244
|
+
removed, added = [], []
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
@patch.each_line(chomp: true) do | line |
|
|
248
|
+
if (header = line.match(/\A@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/))
|
|
249
|
+
flush.call
|
|
250
|
+
hunks << Hunk.new(heading: line, rows: [])
|
|
251
|
+
old_number, new_number = header[1].to_i, header[2].to_i
|
|
252
|
+
elsif hunks.empty?
|
|
253
|
+
# The lines above the first hunk: index, mode, rename from / to, ---, +++.
|
|
254
|
+
next
|
|
255
|
+
elsif line.start_with?("-")
|
|
256
|
+
removed << [ old_number, line[1..] ]
|
|
257
|
+
old_number += 1
|
|
258
|
+
elsif line.start_with?("+")
|
|
259
|
+
added << [ new_number, line[1..] ]
|
|
260
|
+
new_number += 1
|
|
261
|
+
elsif line.start_with?("\\")
|
|
262
|
+
# "" belongs to the line above it.
|
|
263
|
+
next
|
|
264
|
+
else
|
|
265
|
+
flush.call
|
|
266
|
+
text = [ [ line[1..].to_s, false ] ]
|
|
267
|
+
hunks.last.rows << Row.new(old: Line.new(number: old_number, kind: :context, segments: text), new: Line.new(number: new_number, kind: :context, segments: text))
|
|
268
|
+
old_number += 1
|
|
269
|
+
new_number += 1
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
flush.call
|
|
274
|
+
hunks
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def pair(removed, added)
|
|
278
|
+
Array.new([ removed.size, added.size ].max) do | index |
|
|
279
|
+
old_number, old_text = removed[index]
|
|
280
|
+
new_number, new_text = added[index]
|
|
281
|
+
|
|
282
|
+
old_segments, new_segments =
|
|
283
|
+
if old_text && new_text
|
|
284
|
+
Diff.segments(old_text, new_text)
|
|
285
|
+
else
|
|
286
|
+
[ [ [ old_text, false ] ], [ [ new_text, false ] ] ]
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
Row.new(
|
|
290
|
+
old: (Line.new(number: old_number, kind: :removed, segments: old_segments) if old_text),
|
|
291
|
+
new: (Line.new(number: new_number, kind: :added, segments: new_segments) if new_text)
|
|
292
|
+
)
|
|
293
|
+
end
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
attr_reader :base, :head, :context, :root
|
|
299
|
+
|
|
300
|
+
# +head+ is the commit to compare +base+ with; nil for the working tree.
|
|
301
|
+
def initialize(root: Dir.pwd, base: "HEAD", head: nil, context: DEFAULT_CONTEXT)
|
|
302
|
+
@root = Pathname.new(root)
|
|
303
|
+
@base = base.to_s
|
|
304
|
+
@head = head.to_s.empty? ? nil : head.to_s
|
|
305
|
+
@context = context
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
# The commit compared with, as a full SHA.
|
|
309
|
+
def base_sha
|
|
310
|
+
@base_sha ||= commit(base)
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
# The commit compared, as a full SHA; nil when it is the working tree.
|
|
314
|
+
def head_sha
|
|
315
|
+
@head_sha ||= (commit(head) if head)
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def working_tree?
|
|
319
|
+
head.nil?
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
def branch
|
|
323
|
+
@branch ||= git("rev-parse", "--abbrev-ref", "HEAD").first.strip
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
# Every changed file, tracked or not, by path, each with the comments
|
|
327
|
+
# written about it. The comments' own store is not one of them.
|
|
328
|
+
def files
|
|
329
|
+
@files ||= begin
|
|
330
|
+
written = comments.group_by(&:path)
|
|
331
|
+
|
|
332
|
+
entries.reject { Comments.store?(it[:path]) }
|
|
333
|
+
.map { ChangedFile.new(**it, comments: written.values_at(it[:path], it[:old_path]).compact.flatten) }
|
|
334
|
+
.sort_by(&:path)
|
|
335
|
+
end
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
# The comments written within the comparison: the lines their store gained
|
|
339
|
+
# between its two ends, read off the same diff as the files.
|
|
340
|
+
def comments
|
|
341
|
+
@comments ||= entries.select { it[:path] == Comments::PATH }.flat_map { Comments.added(ChangedFile.new(**it)) }
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def file(path)
|
|
345
|
+
files.find { it.path == path }
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
# +file+ again with every unchanged line, for drawing it whole. One more
|
|
349
|
+
# git call, made the first time a whole file is asked for.
|
|
350
|
+
def whole(file)
|
|
351
|
+
@complete ||= self.class.new(root: root, base: base, head: head, context: WHOLE_FILE)
|
|
352
|
+
|
|
353
|
+
@complete.file(file.path)&.as_whole_of(file) || file
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
# A digest that changes whenever the page would: the commits compared, the
|
|
357
|
+
# patch of every tracked change (a comment is one, to its store), and the
|
|
358
|
+
# size and modification time of every untracked file. The page polls it to
|
|
359
|
+
# know when to redraw itself.
|
|
360
|
+
def fingerprint
|
|
361
|
+
Digest::SHA1.hexdigest([ base_sha, head_sha, tracked_output, *untracked_paths.flat_map { | path | [ path, *stamp(path) ] } ].join("\0"))
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
# Marks what differs between a removed line and the added line it is paired
|
|
365
|
+
# with, word by word. Returns the two lines as [ text, changed ] segments.
|
|
366
|
+
# Lines with too little in common come back unmarked: the whole row is
|
|
367
|
+
# already coloured, and marking every word of it says nothing more.
|
|
368
|
+
def self.segments(old_text, new_text)
|
|
369
|
+
old_tokens, new_tokens = [ old_text, new_text ].map { it.scan(/\w+|\s+|[^\w\s]/) }
|
|
370
|
+
|
|
371
|
+
shortest = [ old_tokens.size, new_tokens.size ].min
|
|
372
|
+
prefix = 0
|
|
373
|
+
prefix += 1 while prefix < shortest && old_tokens[prefix] == new_tokens[prefix]
|
|
374
|
+
suffix = 0
|
|
375
|
+
suffix += 1 while suffix < shortest - prefix && old_tokens[-1 - suffix] == new_tokens[-1 - suffix]
|
|
376
|
+
|
|
377
|
+
old_middle = old_tokens[prefix, old_tokens.size - prefix - suffix]
|
|
378
|
+
new_middle = new_tokens[prefix, new_tokens.size - prefix - suffix]
|
|
379
|
+
|
|
380
|
+
old_changed, new_changed =
|
|
381
|
+
if old_middle.size * new_middle.size > MAX_TOKEN_PRODUCT
|
|
382
|
+
[ Array.new(old_middle.size, true), Array.new(new_middle.size, true) ]
|
|
383
|
+
else
|
|
384
|
+
common_subsequence(old_middle, new_middle)
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
old_flags = Array.new(prefix, false) + old_changed + Array.new(suffix, false)
|
|
388
|
+
new_flags = Array.new(prefix, false) + new_changed + Array.new(suffix, false)
|
|
389
|
+
|
|
390
|
+
if alike?(old_tokens, old_flags) && alike?(new_tokens, new_flags)
|
|
391
|
+
[ join(old_tokens, old_flags), join(new_tokens, new_flags) ]
|
|
392
|
+
else
|
|
393
|
+
[ [ [ old_text, false ] ], [ [ new_text, false ] ] ]
|
|
394
|
+
end
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
# Which tokens of each list fall outside their longest common subsequence.
|
|
398
|
+
def self.common_subsequence(old_tokens, new_tokens)
|
|
399
|
+
lengths = Array.new(old_tokens.size + 1) { Array.new(new_tokens.size + 1, 0) }
|
|
400
|
+
|
|
401
|
+
(old_tokens.size - 1).downto(0) do | i |
|
|
402
|
+
(new_tokens.size - 1).downto(0) do | j |
|
|
403
|
+
lengths[i][j] = old_tokens[i] == new_tokens[j] ? lengths[i + 1][j + 1] + 1 : [ lengths[i + 1][j], lengths[i][j + 1] ].max
|
|
404
|
+
end
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
old_changed = Array.new(old_tokens.size, true)
|
|
408
|
+
new_changed = Array.new(new_tokens.size, true)
|
|
409
|
+
i = j = 0
|
|
410
|
+
|
|
411
|
+
while i < old_tokens.size && j < new_tokens.size
|
|
412
|
+
if old_tokens[i] == new_tokens[j]
|
|
413
|
+
old_changed[i] = new_changed[j] = false
|
|
414
|
+
i += 1
|
|
415
|
+
j += 1
|
|
416
|
+
elsif lengths[i + 1][j] >= lengths[i][j + 1]
|
|
417
|
+
i += 1
|
|
418
|
+
else
|
|
419
|
+
j += 1
|
|
420
|
+
end
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
[ old_changed, new_changed ]
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
# At least a third of the line's visible characters are unchanged.
|
|
427
|
+
def self.alike?(tokens, flags)
|
|
428
|
+
visible = tokens.each_index.reject { tokens[it].match?(/\A\s+\z/) }
|
|
429
|
+
unchanged = visible.reject { flags[it] }.sum { tokens[it].size }
|
|
430
|
+
|
|
431
|
+
unchanged * 3 >= visible.sum { tokens[it].size } && unchanged.positive?
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
# Tokens to segments. A space left unchanged between two changed words is
|
|
435
|
+
# marked with them, so a rewritten phrase reads as one stretch.
|
|
436
|
+
def self.join(tokens, flags)
|
|
437
|
+
flags = flags.each_with_index.map do | changed, index |
|
|
438
|
+
changed || (tokens[index].match?(/\A\s+\z/) && index.positive? && flags[index - 1] && flags[index + 1] == true)
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
tokens.zip(flags).chunk_while { | (_, a), (_, b) | a == b }.map { | run | [ run.sum("") { it.first }, run.first.last ] }
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
private_class_method :common_subsequence, :alike?, :join
|
|
445
|
+
|
|
446
|
+
private
|
|
447
|
+
|
|
448
|
+
# +revision+ comes from the query string: it has to look like a revision
|
|
449
|
+
# (so it can never be read as an option) and has to name a commit.
|
|
450
|
+
def commit(revision)
|
|
451
|
+
raise UnknownRevision, "#{ revision.inspect } is not a revision" unless revision.match?(/\A[\w.\/~^@{}][\w.\/~^@{}-]*\z/)
|
|
452
|
+
|
|
453
|
+
sha, status = git("rev-parse", "--verify", "--quiet", "--end-of-options", "#{ revision }^{commit}", allow_failure: true)
|
|
454
|
+
raise UnknownRevision, "#{ revision.inspect } does not name a commit" unless status.success?
|
|
455
|
+
|
|
456
|
+
sha.strip
|
|
457
|
+
end
|
|
458
|
+
|
|
459
|
+
# What ChangedFile.new takes for every changed path, the comments' store
|
|
460
|
+
# included: files and comments are both made from these.
|
|
461
|
+
def entries
|
|
462
|
+
@entries ||= tracked_entries + untracked_entries
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
# One git call gives the file list and the patches from the same snapshot:
|
|
466
|
+
# NUL-separated raw records (exact paths, whatever characters they hold),
|
|
467
|
+
# an empty record, then the patch, one "diff --git" block per record in the
|
|
468
|
+
# same order.
|
|
469
|
+
def tracked_output
|
|
470
|
+
@tracked_output ||= git("diff", "--patch-with-raw", "-z", "--no-color", "--no-ext-diff", "--no-textconv", "--find-renames", "--unified=#{ context }", base_sha, *head_sha, "--").first
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
def tracked_entries
|
|
474
|
+
return [] if tracked_output.empty?
|
|
475
|
+
|
|
476
|
+
raw, patch = tracked_output.split("\0\0", 2)
|
|
477
|
+
fields = raw.split("\0")
|
|
478
|
+
records = []
|
|
479
|
+
|
|
480
|
+
while (meta = fields.shift)
|
|
481
|
+
letter = meta.split(" ").last[0]
|
|
482
|
+
# A rename or copy record carries two paths, the old one first. The nil matters: the variable outlives the iteration.
|
|
483
|
+
old_path = [ "R", "C" ].include?(letter) ? fields.shift : nil
|
|
484
|
+
|
|
485
|
+
records << { status: STATUSES.fetch(letter, :modified), path: fields.shift, old_path: old_path }
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
patches = patch.to_s.split(/^(?=diff --(?:git|cc|combined) )/)
|
|
489
|
+
raise GitError, "git listed #{ records.size } files but sent #{ patches.size } patches" unless records.size == patches.size
|
|
490
|
+
|
|
491
|
+
records.zip(patches).map { | record, patch | { **record, patch: patch } }
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
# Two commits have no untracked files between them.
|
|
495
|
+
def untracked_paths
|
|
496
|
+
@untracked_paths ||= working_tree? ? git("ls-files", "--others", "--exclude-standard", "-z").first.split("\0") : []
|
|
497
|
+
end
|
|
498
|
+
|
|
499
|
+
def untracked_entries
|
|
500
|
+
untracked_paths.map do | path |
|
|
501
|
+
full_path = root.join(path)
|
|
502
|
+
content = full_path.binread if full_path.file? && full_path.size <= MAX_UNTRACKED_BYTES
|
|
503
|
+
|
|
504
|
+
if content.nil? || content.include?("\0")
|
|
505
|
+
{ status: :added, path: path, untracked: true, unreadable: true }
|
|
506
|
+
else
|
|
507
|
+
lines = content.force_encoding(Encoding::UTF_8).scrub.lines(chomp: true)
|
|
508
|
+
patch = "@@ -0,0 +1,#{ lines.size } @@\n#{ lines.map { "+#{ it }\n" }.join }"
|
|
509
|
+
|
|
510
|
+
{ status: :added, path: path, untracked: true, patch: lines.any? ? patch : "" }
|
|
511
|
+
end
|
|
512
|
+
end
|
|
513
|
+
end
|
|
514
|
+
|
|
515
|
+
# A file's size and modification time, or nothing if it has just gone.
|
|
516
|
+
def stamp(path)
|
|
517
|
+
stat = root.join(path).stat
|
|
518
|
+
|
|
519
|
+
[ stat.size, stat.mtime.to_f ]
|
|
520
|
+
rescue SystemCallError
|
|
521
|
+
[]
|
|
522
|
+
end
|
|
523
|
+
|
|
524
|
+
# Runs git in the repository, never through a shell, and without taking the
|
|
525
|
+
# index lock (a page load must not get in the way of a commit in progress).
|
|
526
|
+
def git(*arguments, allow_failure: false)
|
|
527
|
+
output, error, status = Open3.capture3({ "GIT_OPTIONAL_LOCKS" => "0" }, "git", "-C", root.to_s, *arguments)
|
|
528
|
+
raise GitError, (error.empty? ? "git #{ arguments.first } failed" : error) unless status.success? || allow_failure
|
|
529
|
+
|
|
530
|
+
[ output.force_encoding(Encoding::UTF_8).scrub, status ]
|
|
531
|
+
end
|
|
532
|
+
|
|
533
|
+
end
|
|
534
|
+
|
|
535
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Gems a host app's Bundler.require does not load for us: they are this gem's
|
|
2
|
+
# dependencies, not lines of the host's Gemfile.
|
|
3
|
+
require "fontawesome_subsetter"
|
|
4
|
+
require "importmap-rails"
|
|
5
|
+
require "slim"
|
|
6
|
+
require "stimulus-rails"
|
|
7
|
+
require "turbo-rails"
|
|
8
|
+
|
|
9
|
+
module GitDiffLive
|
|
10
|
+
|
|
11
|
+
# The mountable engine. It brings its own layout, stylesheet (prebuilt, in
|
|
12
|
+
# app/assets/builds) and import map, so the host only mounts it:
|
|
13
|
+
#
|
|
14
|
+
# mount GitDiffLive::Engine => "/changes" if Rails.env.development?
|
|
15
|
+
#
|
|
16
|
+
# The one thing it takes from the host is the Font Awesome icon font, through
|
|
17
|
+
# fontawesome_subsetter.
|
|
18
|
+
class Engine < ::Rails::Engine
|
|
19
|
+
|
|
20
|
+
isolate_namespace GitDiffLive
|
|
21
|
+
|
|
22
|
+
# The page's JavaScript is served as it is written, through the host's
|
|
23
|
+
# asset pipeline. (app/assets/builds is on the asset paths already, as
|
|
24
|
+
# every engine's app/assets/* is.)
|
|
25
|
+
initializer "git_diff_live.assets" do | app |
|
|
26
|
+
app.config.assets.paths << root.join("app/javascript") if app.config.respond_to?(:assets)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
initializer "git_diff_live.importmap" do
|
|
30
|
+
GitDiffLive.importmap.draw root.join("config/importmap.rb")
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# fontawesome_subsetter builds the icon font from the icon(:fas, :name)
|
|
34
|
+
# calls it finds in the host's views; these are the engine's, so its icons
|
|
35
|
+
# are in the font too. After the host's initializers, in case one of them
|
|
36
|
+
# sets scan_globs.
|
|
37
|
+
initializer "git_diff_live.icons" do | app |
|
|
38
|
+
app.config.after_initialize do
|
|
39
|
+
FontawesomeSubsetter.configuration.scan_globs |= [ root.join("app/views/**/*.slim").to_s, root.join("app/helpers/**/*.rb").to_s ]
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
require_relative "git_diff_live/version"
|
|
2
|
+
require_relative "git_diff_live/diff"
|
|
3
|
+
require_relative "git_diff_live/comments"
|
|
4
|
+
|
|
5
|
+
# A live, side-by-side page of the working tree's git changes, as a mountable
|
|
6
|
+
# Rails engine.
|
|
7
|
+
#
|
|
8
|
+
# == Quick Start
|
|
9
|
+
#
|
|
10
|
+
# # Gemfile
|
|
11
|
+
# gem "git_diff_live", group: :development
|
|
12
|
+
#
|
|
13
|
+
# # config/routes.rb
|
|
14
|
+
# mount GitDiffLive::Engine => "/changes" if Rails.env.development?
|
|
15
|
+
#
|
|
16
|
+
# See Configuration for the options, and Comments for the comments drawn
|
|
17
|
+
# beside the changes.
|
|
18
|
+
module GitDiffLive
|
|
19
|
+
|
|
20
|
+
class << self
|
|
21
|
+
|
|
22
|
+
attr_writer :configuration
|
|
23
|
+
|
|
24
|
+
# Returns the current configuration, initializing with defaults if needed.
|
|
25
|
+
def configuration
|
|
26
|
+
@configuration ||= Configuration.new
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Yields the current Configuration for modification.
|
|
30
|
+
#
|
|
31
|
+
# GitDiffLive.configure do | config |
|
|
32
|
+
# config.poll_interval = 5
|
|
33
|
+
# end
|
|
34
|
+
def configure
|
|
35
|
+
yield(configuration)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The engine's own import map (config/importmap.rb), kept apart from the
|
|
39
|
+
# host's so the page loads the same JavaScript in every app.
|
|
40
|
+
def importmap
|
|
41
|
+
@importmap ||= Importmap::Map.new
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Holds all configuration options.
|
|
47
|
+
#
|
|
48
|
+
# == Attributes
|
|
49
|
+
#
|
|
50
|
+
# [root] The repository to show. Default: +Rails.root+.
|
|
51
|
+
# [enabled] Whether the page answers at all; anything else is a 404.
|
|
52
|
+
# Default: only in the development and test environments.
|
|
53
|
+
# [poll_interval] Seconds between the page's checks for new changes. +nil+
|
|
54
|
+
# or 0 turns live reload off. Default: 2.
|
|
55
|
+
# [context] Unchanged lines drawn around each change. Default: 3.
|
|
56
|
+
# [stylesheets] The host's stylesheets the page links before its own: the
|
|
57
|
+
# icon font. Default: <tt>["fontawesome"]</tt>, the file
|
|
58
|
+
# fontawesome_subsetter builds.
|
|
59
|
+
# [editor_url] Where a file's "Open" link goes, as a format string given
|
|
60
|
+
# +path+ (absolute, URL-escaped) and +line+. Default: +nil+,
|
|
61
|
+
# which opens VS Code, through its Remote - WSL window when
|
|
62
|
+
# the server runs under WSL. For another editor:
|
|
63
|
+
# <tt>"cursor://file%{path}:%{line}"</tt>.
|
|
64
|
+
class Configuration
|
|
65
|
+
|
|
66
|
+
attr_accessor :poll_interval, :context, :stylesheets, :editor_url
|
|
67
|
+
|
|
68
|
+
attr_writer :root, :enabled
|
|
69
|
+
|
|
70
|
+
def initialize
|
|
71
|
+
@poll_interval = 2
|
|
72
|
+
@context = Diff::DEFAULT_CONTEXT
|
|
73
|
+
@stylesheets = [ "fontawesome" ]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def root
|
|
77
|
+
@root || Rails.root
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def enabled?
|
|
81
|
+
@enabled.nil? ? Rails.env.local? : @enabled
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
require_relative "git_diff_live/engine" if defined?(Rails)
|