view: ensure that width variable is always initialized
[tig.git] / src / diff.c
blob4b30068302106294dd5ba4e028c23a08cce0e985
1 /* Copyright (c) 2006-2014 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include "tig/argv.h"
15 #include "tig/refdb.h"
16 #include "tig/repo.h"
17 #include "tig/options.h"
18 #include "tig/display.h"
19 #include "tig/parse.h"
20 #include "tig/pager.h"
21 #include "tig/diff.h"
22 #include "tig/draw.h"
24 #define DIFF_LINE_COMMIT_TITLE 1
26 static bool
27 diff_open(struct view *view, enum open_flags flags)
29 const char *diff_argv[] = {
30 "git", "show", encoding_arg, "--pretty=fuller", "--root",
31 "--patch-with-stat",
32 show_notes_arg(), diff_context_arg(), ignore_space_arg(),
33 "%(diffargs)", "%(cmdlineargs)", "--no-color", "%(commit)",
34 "--", "%(fileargs)", NULL
37 return begin_update(view, NULL, diff_argv, flags);
40 bool
41 diff_common_read(struct view *view, const char *data, struct diff_state *state)
43 enum line_type type = get_line_type(data);
45 if (!view->lines && type != LINE_COMMIT)
46 state->reading_diff_stat = TRUE;
48 if (state->combined_diff && !state->after_diff && data[0] == ' ' && data[1] != ' ')
49 state->reading_diff_stat = TRUE;
51 if (state->reading_diff_stat) {
52 size_t len = strlen(data);
53 char *pipe = strchr(data, '|');
54 bool has_histogram = data[len - 1] == '-' || data[len - 1] == '+';
55 bool has_bin_diff = pipe && strstr(pipe, "Bin") && strstr(pipe, "->");
56 bool has_rename = data[len - 1] == '0' && (strstr(data, "=>") || !strncmp(data, " ...", 4));
57 bool has_no_change = pipe && strstr(pipe, " 0");
59 if (pipe && (has_histogram || has_bin_diff || has_rename || has_no_change)) {
60 return add_line_text(view, data, LINE_DIFF_STAT) != NULL;
61 } else {
62 state->reading_diff_stat = FALSE;
65 } else if (!strcmp(data, "---")) {
66 state->reading_diff_stat = TRUE;
69 if (!state->after_commit_title && !prefixcmp(data, " ")) {
70 struct line *line = add_line_text(view, data, LINE_DEFAULT);
72 if (line)
73 line->user_flags |= DIFF_LINE_COMMIT_TITLE;
74 state->after_commit_title = TRUE;
75 return line != NULL;
78 if (type == LINE_DIFF_HEADER) {
79 const int len = STRING_SIZE("diff --");
81 state->after_diff = TRUE;
82 if (!strncmp(data + len, "combined ", strlen("combined ")) ||
83 !strncmp(data + len, "cc ", strlen("cc ")))
84 state->combined_diff = TRUE;
86 } else if (type == LINE_PP_MERGE) {
87 state->combined_diff = TRUE;
90 /* ADD2 and DEL2 are only valid in combined diff hunks */
91 if (!state->combined_diff && (type == LINE_DIFF_ADD2 || type == LINE_DIFF_DEL2))
92 type = LINE_DEFAULT;
94 return pager_common_read(view, data, type);
97 static bool
98 diff_find_stat_entry(struct view *view, struct line *line, enum line_type type)
100 struct line *marker = find_next_line_by_type(view, line, type);
102 return marker &&
103 line == find_prev_line_by_type(view, marker, LINE_DIFF_HEADER);
106 enum request
107 diff_common_enter(struct view *view, enum request request, struct line *line)
109 if (line->type == LINE_DIFF_STAT) {
110 int file_number = 0;
112 while (view_has_line(view, line) && line->type == LINE_DIFF_STAT) {
113 file_number++;
114 line--;
117 for (line = view->line; view_has_line(view, line); line++) {
118 line = find_next_line_by_type(view, line, LINE_DIFF_HEADER);
119 if (!line)
120 break;
122 if (diff_find_stat_entry(view, line, LINE_DIFF_INDEX)
123 || diff_find_stat_entry(view, line, LINE_DIFF_SIMILARITY)) {
124 if (file_number == 1) {
125 break;
127 file_number--;
131 if (!line) {
132 report("Failed to find file diff");
133 return REQ_NONE;
136 select_view_line(view, line - view->line);
137 report_clear();
138 return REQ_NONE;
140 } else {
141 return pager_request(view, request, line);
145 static bool
146 diff_common_draw_part(struct view *view, enum line_type *type, char **text, char c, enum line_type next_type)
148 char *sep = strchr(*text, c);
150 if (sep != NULL) {
151 *sep = 0;
152 draw_text(view, *type, *text);
153 *sep = c;
154 *text = sep;
155 *type = next_type;
158 return sep != NULL;
161 bool
162 diff_common_draw(struct view *view, struct line *line, unsigned int lineno)
164 char *text = line->data;
165 enum line_type type = line->type;
167 if (draw_lineno(view, lineno))
168 return TRUE;
170 if (line->wrapped && draw_text(view, LINE_DELIMITER, "+"))
171 return TRUE;
173 if (type == LINE_DIFF_STAT) {
174 diff_common_draw_part(view, &type, &text, '|', LINE_DEFAULT);
175 if (diff_common_draw_part(view, &type, &text, 'B', LINE_DEFAULT)) {
176 /* Handle binary diffstat: Bin <deleted> -> <added> bytes */
177 diff_common_draw_part(view, &type, &text, ' ', LINE_DIFF_DEL);
178 diff_common_draw_part(view, &type, &text, '-', LINE_DEFAULT);
179 diff_common_draw_part(view, &type, &text, ' ', LINE_DIFF_ADD);
180 diff_common_draw_part(view, &type, &text, 'b', LINE_DEFAULT);
182 } else {
183 diff_common_draw_part(view, &type, &text, '+', LINE_DIFF_ADD);
184 diff_common_draw_part(view, &type, &text, '-', LINE_DIFF_DEL);
188 if (line->user_flags & DIFF_LINE_COMMIT_TITLE)
189 draw_commit_title(view, text, 4);
190 else
191 draw_text(view, type, text);
192 return TRUE;
195 static bool
196 diff_read(struct view *view, char *data)
198 struct diff_state *state = view->private;
200 if (!data) {
201 /* Fall back to retry if no diff will be shown. */
202 if (view->lines == 0 && opt_file_argv) {
203 int pos = argv_size(view->argv)
204 - argv_size(opt_file_argv) - 1;
206 if (pos > 0 && !strcmp(view->argv[pos], "--")) {
207 for (; view->argv[pos]; pos++) {
208 free((void *) view->argv[pos]);
209 view->argv[pos] = NULL;
212 if (view->pipe)
213 io_done(view->pipe);
214 if (io_run(&view->io, IO_RD, view->dir, opt_env, view->argv))
215 return FALSE;
218 return TRUE;
221 return diff_common_read(view, data, state);
224 static bool
225 diff_blame_line(const char *ref, const char *file, unsigned long lineno,
226 struct blame_header *header, struct blame_commit *commit)
228 char author[SIZEOF_STR] = "";
229 char line_arg[SIZEOF_STR];
230 const char *blame_argv[] = {
231 "git", "blame", encoding_arg, "-p", line_arg, ref, "--", file, NULL
233 struct io io;
234 bool ok = FALSE;
235 char *buf;
237 if (!string_format(line_arg, "-L%ld,+1", lineno))
238 return FALSE;
240 if (!io_run(&io, IO_RD, repo.cdup, opt_env, blame_argv))
241 return FALSE;
243 while ((buf = io_get(&io, '\n', TRUE))) {
244 if (header) {
245 if (!parse_blame_header(header, buf, 9999999))
246 break;
247 header = NULL;
249 } else if (parse_blame_info(commit, author, buf)) {
250 ok = commit->filename != NULL;
251 break;
255 if (io_error(&io))
256 ok = FALSE;
258 io_done(&io);
259 return ok;
262 unsigned int
263 diff_get_lineno(struct view *view, struct line *line)
265 const struct line *header, *chunk;
266 unsigned int lineno;
267 struct chunk_header chunk_header;
269 /* Verify that we are after a diff header and one of its chunks */
270 header = find_prev_line_by_type(view, line, LINE_DIFF_HEADER);
271 chunk = find_prev_line_by_type(view, line, LINE_DIFF_CHUNK);
272 if (!header || !chunk || chunk < header)
273 return 0;
276 * In a chunk header, the number after the '+' sign is the number of its
277 * following line, in the new version of the file. We increment this
278 * number for each non-deletion line, until the given line position.
280 if (!parse_chunk_header(&chunk_header, chunk->data))
281 return 0;
283 lineno = chunk_header.new.position;
285 for (chunk++; chunk < line; chunk++)
286 if (chunk->type != LINE_DIFF_DEL &&
287 chunk->type != LINE_DIFF_DEL2)
288 lineno++;
290 return lineno;
293 static enum request
294 diff_trace_origin(struct view *view, struct line *line)
296 struct line *diff = find_prev_line_by_type(view, line, LINE_DIFF_HEADER);
297 struct line *chunk = find_prev_line_by_type(view, line, LINE_DIFF_CHUNK);
298 const char *chunk_data;
299 int chunk_marker = line->type == LINE_DIFF_DEL ? '-' : '+';
300 unsigned long lineno = 0;
301 const char *file = NULL;
302 char ref[SIZEOF_REF];
303 struct blame_header header;
304 struct blame_commit commit;
306 if (!diff || !chunk || chunk == line) {
307 report("The line to trace must be inside a diff chunk");
308 return REQ_NONE;
311 for (; diff < line && !file; diff++) {
312 const char *data = diff->data;
314 if (!prefixcmp(data, "--- a/")) {
315 file = data + STRING_SIZE("--- a/");
316 break;
320 if (diff == line || !file) {
321 report("Failed to read the file name");
322 return REQ_NONE;
325 chunk_data = chunk->data;
327 if (!parse_chunk_lineno(&lineno, chunk_data, chunk_marker)) {
328 report("Failed to read the line number");
329 return REQ_NONE;
332 if (lineno == 0) {
333 report("This is the origin of the line");
334 return REQ_NONE;
337 for (chunk += 1; chunk < line; chunk++) {
338 if (chunk->type == LINE_DIFF_ADD) {
339 lineno += chunk_marker == '+';
340 } else if (chunk->type == LINE_DIFF_DEL) {
341 lineno += chunk_marker == '-';
342 } else {
343 lineno++;
347 if (chunk_marker == '+')
348 string_copy(ref, view->vid);
349 else
350 string_format(ref, "%s^", view->vid);
352 if (string_rev_is_null(ref)) {
353 string_ncopy(view->env->file, file, strlen(file));
354 string_copy(view->env->ref, "");
355 view->env->lineno = lineno - 1;
357 } else {
358 if (!diff_blame_line(ref, file, lineno, &header, &commit)) {
359 report("Failed to read blame data");
360 return REQ_NONE;
363 string_ncopy(view->env->file, commit.filename, strlen(commit.filename));
364 string_copy(view->env->ref, header.id);
365 view->env->lineno = header.orig_lineno - 1;
368 return REQ_VIEW_BLAME;
371 const char *
372 diff_get_pathname(struct view *view, struct line *line)
374 const struct line *header;
375 const char *dst = NULL;
376 const char *prefixes[] = { " b/", "cc ", "combined " };
377 int i;
379 header = find_prev_line_by_type(view, line, LINE_DIFF_HEADER);
380 if (!header)
381 return NULL;
383 for (i = 0; i < ARRAY_SIZE(prefixes) && !dst; i++)
384 dst = strstr(header->data, prefixes[i]);
386 return dst ? dst + strlen(prefixes[--i]) : NULL;
389 enum request
390 diff_common_edit(struct view *view, enum request request, struct line *line)
392 const char *file = diff_get_pathname(view, line);
393 char path[SIZEOF_STR];
394 bool has_path = file && string_format(path, "%s%s", repo.cdup, file);
396 if (has_path && access(path, R_OK)) {
397 report("Failed to open file: %s", file);
398 return REQ_NONE;
401 open_editor(file, diff_get_lineno(view, line));
402 return REQ_NONE;
405 static enum request
406 diff_request(struct view *view, enum request request, struct line *line)
408 switch (request) {
409 case REQ_VIEW_BLAME:
410 return diff_trace_origin(view, line);
412 case REQ_EDIT:
413 return diff_common_edit(view, request, line);
415 case REQ_ENTER:
416 return diff_common_enter(view, request, line);
418 case REQ_REFRESH:
419 if (string_rev_is_null(view->vid))
420 refresh_view(view);
421 else
422 reload_view(view);
423 return REQ_NONE;
425 default:
426 return pager_request(view, request, line);
430 static void
431 diff_select(struct view *view, struct line *line)
433 if (line->type == LINE_DIFF_STAT) {
434 string_format(view->ref, "Press '%s' to jump to file diff",
435 get_view_key(view, REQ_ENTER));
436 } else {
437 const char *file = diff_get_pathname(view, line);
439 if (file) {
440 string_format(view->ref, "Changes to '%s'", file);
441 string_format(view->env->file, "%s", file);
442 view->env->blob[0] = 0;
443 } else {
444 string_ncopy(view->ref, view->ops->id, strlen(view->ops->id));
445 pager_select(view, line);
450 static struct view_ops diff_ops = {
451 "line",
452 argv_env.commit,
453 VIEW_DIFF_LIKE | VIEW_ADD_DESCRIBE_REF | VIEW_ADD_PAGER_REFS | VIEW_FILE_FILTER | VIEW_REFRESH,
454 sizeof(struct diff_state),
455 diff_open,
456 diff_read,
457 diff_common_draw,
458 diff_request,
459 pager_grep,
460 diff_select,
463 DEFINE_VIEW(diff);
465 /* vim: set ts=8 sw=8 noexpandtab: */