-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.c
440 lines (364 loc) · 11 KB
/
diff.c
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
/* Copyright (c) 2006-2014 Jonas Fonseca <jonas.fonseca@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "tig/argv.h"
#include "tig/refdb.h"
#include "tig/repo.h"
#include "tig/options.h"
#include "tig/display.h"
#include "tig/parse.h"
#include "tig/pager.h"
#include "tig/diff.h"
#include "tig/draw.h"
static bool
diff_open(struct view *view, enum open_flags flags)
{
const char *diff_argv[] = {
"git", "show", encoding_arg, "--pretty=fuller", "--root",
"--patch-with-stat",
show_notes_arg(), diff_context_arg(), ignore_space_arg(),
"%(diffargs)", "%(cmdlineargs)", "--no-color", "%(commit)",
"--", "%(fileargs)", NULL
};
return begin_update(view, NULL, diff_argv, flags);
}
struct line *
diff_common_add_diff_stat(struct view *view, const char *text, size_t offset)
{
const char *start = text + offset;
const char *data = start + strspn(start, " ");
size_t len = strlen(data);
char *pipe = strchr(data, '|');
/* Ensure that '|' is present and the file name part contains
* non-space characters. */
if (!pipe || pipe == data || strcspn(data, " ") == 0)
return NULL;
/* Detect remaining part of a diff stat line:
*
* added | 40 +++++++++++
* remove | 124 --------------------------
* updated | 14 +----
* rename.from => rename.to | 0
* .../truncated file name | 11 ++---
* binary add | Bin 0 -> 1234 bytes
* binary update | Bin 1234 -> 2345 bytes
* unmerged | Unmerged
*/
if ((data[len - 1] == '-' || data[len - 1] == '+') ||
strstr(pipe, " 0") ||
(strstr(pipe, "Bin") && strstr(pipe, "->")) ||
strstr(pipe, "Unmerged") ||
(data[len - 1] == '0' && (strstr(data, "=>") || !prefixcmp(data, "..."))))
return add_line_text(view, text, LINE_DIFF_STAT);
return NULL;
}
bool
diff_common_read(struct view *view, const char *data, struct diff_state *state)
{
enum line_type type = get_line_type(data);
if (!view->lines && type != LINE_COMMIT)
state->reading_diff_stat = TRUE;
if (state->combined_diff && !state->after_diff && data[0] == ' ' && data[1] != ' ')
state->reading_diff_stat = TRUE;
if (state->reading_diff_stat) {
if (diff_common_add_diff_stat(view, data, 0))
return TRUE;
state->reading_diff_stat = FALSE;
} else if (!strcmp(data, "---")) {
state->reading_diff_stat = TRUE;
}
if (!state->after_commit_title && !prefixcmp(data, " ")) {
struct line *line = add_line_text(view, data, LINE_DEFAULT);
if (line)
line->commit_title = 1;
state->after_commit_title = TRUE;
return line != NULL;
}
if (type == LINE_DIFF_HEADER) {
const int len = STRING_SIZE("diff --");
state->after_diff = TRUE;
if (!strncmp(data + len, "combined ", strlen("combined ")) ||
!strncmp(data + len, "cc ", strlen("cc ")))
state->combined_diff = TRUE;
} else if (type == LINE_PP_MERGE) {
state->combined_diff = TRUE;
}
/* ADD2 and DEL2 are only valid in combined diff hunks */
if (!state->combined_diff && (type == LINE_DIFF_ADD2 || type == LINE_DIFF_DEL2))
type = LINE_DEFAULT;
return pager_common_read(view, data, type, NULL);
}
static bool
diff_find_stat_entry(struct view *view, struct line *line, enum line_type type)
{
struct line *marker = find_next_line_by_type(view, line, type);
return marker &&
line == find_prev_line_by_type(view, marker, LINE_DIFF_HEADER);
}
enum request
diff_common_enter(struct view *view, enum request request, struct line *line)
{
if (line->type == LINE_DIFF_STAT) {
int file_number = 0;
while (view_has_line(view, line) && line->type == LINE_DIFF_STAT) {
file_number++;
line--;
}
for (line = view->line; view_has_line(view, line); line++) {
line = find_next_line_by_type(view, line, LINE_DIFF_HEADER);
if (!line)
break;
if (diff_find_stat_entry(view, line, LINE_DIFF_INDEX)
|| diff_find_stat_entry(view, line, LINE_DIFF_SIMILARITY)) {
if (file_number == 1) {
break;
}
file_number--;
}
}
if (!line) {
report("Failed to find file diff");
return REQ_NONE;
}
select_view_line(view, line - view->line);
report_clear();
return REQ_NONE;
} else {
return pager_request(view, request, line);
}
}
static bool
diff_read(struct view *view, struct buffer *buf)
{
struct diff_state *state = view->private;
if (!buf) {
/* Fall back to retry if no diff will be shown. */
if (view->lines == 0 && opt_file_argv) {
int pos = argv_size(view->argv)
- argv_size(opt_file_argv) - 1;
if (pos > 0 && !strcmp(view->argv[pos], "--")) {
for (; view->argv[pos]; pos++) {
free((void *) view->argv[pos]);
view->argv[pos] = NULL;
}
if (view->pipe)
io_done(view->pipe);
if (io_run(&view->io, IO_RD, view->dir, opt_env, view->argv))
return FALSE;
}
}
return TRUE;
}
return diff_common_read(view, buf->data, state);
}
static bool
diff_blame_line(const char *ref, const char *file, unsigned long lineno,
struct blame_header *header, struct blame_commit *commit)
{
char author[SIZEOF_STR] = "";
char line_arg[SIZEOF_STR];
const char *blame_argv[] = {
"git", "blame", encoding_arg, "-p", line_arg, ref, "--", file, NULL
};
struct io io;
bool ok = FALSE;
struct buffer buf;
if (!string_format(line_arg, "-L%ld,+1", lineno))
return FALSE;
if (!io_run(&io, IO_RD, repo.cdup, opt_env, blame_argv))
return FALSE;
while (io_get(&io, &buf, '\n', TRUE)) {
if (header) {
if (!parse_blame_header(header, buf.data, 9999999))
break;
header = NULL;
} else if (parse_blame_info(commit, author, buf.data)) {
ok = commit->filename != NULL;
break;
}
}
if (io_error(&io))
ok = FALSE;
io_done(&io);
return ok;
}
unsigned int
diff_get_lineno(struct view *view, struct line *line)
{
const struct line *header, *chunk;
unsigned int lineno;
struct chunk_header chunk_header;
/* Verify that we are after a diff header and one of its chunks */
header = find_prev_line_by_type(view, line, LINE_DIFF_HEADER);
chunk = find_prev_line_by_type(view, line, LINE_DIFF_CHUNK);
if (!header || !chunk || chunk < header)
return 0;
/*
* In a chunk header, the number after the '+' sign is the number of its
* following line, in the new version of the file. We increment this
* number for each non-deletion line, until the given line position.
*/
if (!parse_chunk_header(&chunk_header, chunk->data))
return 0;
lineno = chunk_header.new.position;
for (chunk++; chunk < line; chunk++)
if (chunk->type != LINE_DIFF_DEL &&
chunk->type != LINE_DIFF_DEL2)
lineno++;
return lineno;
}
static enum request
diff_trace_origin(struct view *view, struct line *line)
{
struct line *diff = find_prev_line_by_type(view, line, LINE_DIFF_HEADER);
struct line *chunk = find_prev_line_by_type(view, line, LINE_DIFF_CHUNK);
const char *chunk_data;
int chunk_marker = line->type == LINE_DIFF_DEL ? '-' : '+';
unsigned long lineno = 0;
const char *file = NULL;
char ref[SIZEOF_REF];
struct blame_header header;
struct blame_commit commit;
if (!diff || !chunk || chunk == line) {
report("The line to trace must be inside a diff chunk");
return REQ_NONE;
}
for (; diff < line && !file; diff++) {
const char *data = diff->data;
if (!prefixcmp(data, "--- a/")) {
file = data + STRING_SIZE("--- a/");
break;
}
}
if (diff == line || !file) {
report("Failed to read the file name");
return REQ_NONE;
}
chunk_data = chunk->data;
if (!parse_chunk_lineno(&lineno, chunk_data, chunk_marker)) {
report("Failed to read the line number");
return REQ_NONE;
}
if (lineno == 0) {
report("This is the origin of the line");
return REQ_NONE;
}
for (chunk += 1; chunk < line; chunk++) {
if (chunk->type == LINE_DIFF_ADD) {
lineno += chunk_marker == '+';
} else if (chunk->type == LINE_DIFF_DEL) {
lineno += chunk_marker == '-';
} else {
lineno++;
}
}
if (chunk_marker == '+')
string_copy(ref, view->vid);
else
string_format(ref, "%s^", view->vid);
if (string_rev_is_null(ref)) {
string_ncopy(view->env->file, file, strlen(file));
string_copy(view->env->ref, "");
view->env->lineno = lineno - 1;
} else {
if (!diff_blame_line(ref, file, lineno, &header, &commit)) {
report("Failed to read blame data");
return REQ_NONE;
}
string_ncopy(view->env->file, commit.filename, strlen(commit.filename));
string_copy(view->env->ref, header.id);
view->env->lineno = header.orig_lineno - 1;
}
return REQ_VIEW_BLAME;
}
const char *
diff_get_pathname(struct view *view, struct line *line)
{
const struct line *header;
const char *dst = NULL;
const char *prefixes[] = { " b/", "cc ", "combined " };
int i;
header = find_prev_line_by_type(view, line, LINE_DIFF_HEADER);
if (!header)
return NULL;
for (i = 0; i < ARRAY_SIZE(prefixes) && !dst; i++)
dst = strstr(header->data, prefixes[i]);
return dst ? dst + strlen(prefixes[--i]) : NULL;
}
enum request
diff_common_edit(struct view *view, enum request request, struct line *line)
{
const char *file = diff_get_pathname(view, line);
char path[SIZEOF_STR];
bool has_path = file && string_format(path, "%s%s", repo.cdup, file);
if (has_path && access(path, R_OK)) {
report("Failed to open file: %s", file);
return REQ_NONE;
}
open_editor(file, diff_get_lineno(view, line));
return REQ_NONE;
}
static enum request
diff_request(struct view *view, enum request request, struct line *line)
{
switch (request) {
case REQ_VIEW_BLAME:
return diff_trace_origin(view, line);
case REQ_EDIT:
return diff_common_edit(view, request, line);
case REQ_ENTER:
return diff_common_enter(view, request, line);
case REQ_REFRESH:
if (string_rev_is_null(view->vid))
refresh_view(view);
else
reload_view(view);
return REQ_NONE;
default:
return pager_request(view, request, line);
}
}
static void
diff_select(struct view *view, struct line *line)
{
if (line->type == LINE_DIFF_STAT) {
string_format(view->ref, "Press '%s' to jump to file diff",
get_view_key(view, REQ_ENTER));
} else {
const char *file = diff_get_pathname(view, line);
if (file) {
string_format(view->ref, "Changes to '%s'", file);
string_format(view->env->file, "%s", file);
view->env->blob[0] = 0;
} else {
string_ncopy(view->ref, view->ops->id, strlen(view->ops->id));
pager_select(view, line);
}
}
}
static struct view_ops diff_ops = {
"line",
argv_env.commit,
VIEW_DIFF_LIKE | VIEW_ADD_DESCRIBE_REF | VIEW_ADD_PAGER_REFS | VIEW_FILE_FILTER | VIEW_REFRESH,
sizeof(struct diff_state),
diff_open,
diff_read,
view_column_draw,
diff_request,
view_column_grep,
diff_select,
NULL,
view_column_bit(LINE_NUMBER) | view_column_bit(TEXT),
pager_get_column_data,
};
DEFINE_VIEW(diff);
/* vim: set ts=8 sw=8 noexpandtab: */