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.
15 #include "tig/refdb.h"
17 #include "tig/options.h"
18 #include "tig/display.h"
19 #include "tig/parse.h"
20 #include "tig/pager.h"
24 #define DIFF_LINE_COMMIT_TITLE 1
27 diff_open(struct view
*view
, enum open_flags flags
)
29 const char *diff_argv
[] = {
30 "git", "show", encoding_arg
, "--pretty=fuller", "--root",
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
);
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
;
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
);
73 line
->user_flags
|= DIFF_LINE_COMMIT_TITLE
;
74 state
->after_commit_title
= TRUE
;
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
))
94 return pager_common_read(view
, data
, type
);
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
);
103 line
== find_prev_line_by_type(view
, marker
, LINE_DIFF_HEADER
);
107 diff_common_enter(struct view
*view
, enum request request
, struct line
*line
)
109 if (line
->type
== LINE_DIFF_STAT
) {
112 while (view_has_line(view
, line
) && line
->type
== LINE_DIFF_STAT
) {
117 for (line
= view
->line
; view_has_line(view
, line
); line
++) {
118 line
= find_next_line_by_type(view
, line
, LINE_DIFF_HEADER
);
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) {
132 report("Failed to find file diff");
136 select_view_line(view
, line
- view
->line
);
141 return pager_request(view
, request
, line
);
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
);
152 draw_text(view
, *type
, *text
);
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
))
170 if (line
->wrapped
&& draw_text(view
, LINE_DELIMITER
, "+"))
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
);
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);
191 draw_text(view
, type
, text
);
196 diff_read(struct view
*view
, char *data
)
198 struct diff_state
*state
= view
->private;
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
;
214 if (io_run(&view
->io
, IO_RD
, view
->dir
, opt_env
, view
->argv
))
221 return diff_common_read(view
, data
, state
);
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
237 if (!string_format(line_arg
, "-L%ld,+1", lineno
))
240 if (!io_run(&io
, IO_RD
, repo
.cdup
, opt_env
, blame_argv
))
243 while ((buf
= io_get(&io
, '\n', TRUE
))) {
245 if (!parse_blame_header(header
, buf
, 9999999))
249 } else if (parse_blame_info(commit
, author
, buf
)) {
250 ok
= commit
->filename
!= NULL
;
263 diff_get_lineno(struct view
*view
, struct line
*line
)
265 const struct line
*header
, *chunk
;
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
)
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
))
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
)
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");
311 for (; diff
< line
&& !file
; diff
++) {
312 const char *data
= diff
->data
;
314 if (!prefixcmp(data
, "--- a/")) {
315 file
= data
+ STRING_SIZE("--- a/");
320 if (diff
== line
|| !file
) {
321 report("Failed to read the file name");
325 chunk_data
= chunk
->data
;
327 if (!parse_chunk_lineno(&lineno
, chunk_data
, chunk_marker
)) {
328 report("Failed to read the line number");
333 report("This is the origin of the line");
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
== '-';
347 if (chunk_marker
== '+')
348 string_copy(ref
, view
->vid
);
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;
358 if (!diff_blame_line(ref
, file
, lineno
, &header
, &commit
)) {
359 report("Failed to read blame data");
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
;
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 " };
379 header
= find_prev_line_by_type(view
, line
, LINE_DIFF_HEADER
);
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
;
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
);
401 open_editor(file
, diff_get_lineno(view
, line
));
406 diff_request(struct view
*view
, enum request request
, struct line
*line
)
410 return diff_trace_origin(view
, line
);
413 return diff_common_edit(view
, request
, line
);
416 return diff_common_enter(view
, request
, line
);
419 if (string_rev_is_null(view
->vid
))
426 return pager_request(view
, request
, line
);
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
));
437 const char *file
= diff_get_pathname(view
, line
);
440 string_format(view
->ref
, "Changes to '%s'", file
);
441 string_format(view
->env
->file
, "%s", file
);
442 view
->env
->blob
[0] = 0;
444 string_ncopy(view
->ref
, view
->ops
->id
, strlen(view
->ops
->id
));
445 pager_select(view
, line
);
450 static struct view_ops diff_ops
= {
453 VIEW_DIFF_LIKE
| VIEW_ADD_DESCRIBE_REF
| VIEW_ADD_PAGER_REFS
| VIEW_FILE_FILTER
| VIEW_REFRESH
,
454 sizeof(struct diff_state
),
465 /* vim: set ts=8 sw=8 noexpandtab: */