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"
25 diff_open(struct view
*view
, enum open_flags flags
)
27 const char *diff_argv
[] = {
28 "git", "show", encoding_arg
, "--pretty=fuller", "--root",
30 show_notes_arg(), diff_context_arg(), ignore_space_arg(),
31 "%(diffargs)", "%(cmdlineargs)", "--no-color", "%(commit)",
32 "--", "%(fileargs)", NULL
35 return begin_update(view
, NULL
, diff_argv
, flags
);
39 diff_common_add_diff_stat(struct view
*view
, const char *text
, size_t offset
)
41 const char *start
= text
+ offset
;
42 const char *data
= start
+ strspn(start
, " ");
43 size_t len
= strlen(data
);
44 char *pipe
= strchr(data
, '|');
46 /* Ensure that '|' is present and the file name part contains
47 * non-space characters. */
48 if (!pipe
|| pipe
== data
|| strcspn(data
, " ") == 0)
51 /* Detect remaining part of a diff stat line:
53 * added | 40 +++++++++++
54 * remove | 124 --------------------------
56 * rename.from => rename.to | 0
57 * .../truncated file name | 11 ++---
58 * binary add | Bin 0 -> 1234 bytes
59 * binary update | Bin 1234 -> 2345 bytes
62 if ((data
[len
- 1] == '-' || data
[len
- 1] == '+') ||
64 (strstr(pipe
, "Bin") && strstr(pipe
, "->")) ||
65 strstr(pipe
, "Unmerged") ||
66 (data
[len
- 1] == '0' && (strstr(data
, "=>") || !prefixcmp(data
, "..."))))
67 return add_line_text(view
, text
, LINE_DIFF_STAT
);
72 diff_common_read(struct view
*view
, const char *data
, struct diff_state
*state
)
74 enum line_type type
= get_line_type(data
);
76 if (!view
->lines
&& type
!= LINE_COMMIT
)
77 state
->reading_diff_stat
= TRUE
;
79 if (state
->combined_diff
&& !state
->after_diff
&& data
[0] == ' ' && data
[1] != ' ')
80 state
->reading_diff_stat
= TRUE
;
82 if (state
->reading_diff_stat
) {
83 if (diff_common_add_diff_stat(view
, data
, 0))
85 state
->reading_diff_stat
= FALSE
;
87 } else if (!strcmp(data
, "---")) {
88 state
->reading_diff_stat
= TRUE
;
91 if (!state
->after_commit_title
&& !prefixcmp(data
, " ")) {
92 struct line
*line
= add_line_text(view
, data
, LINE_DEFAULT
);
95 line
->commit_title
= 1;
96 state
->after_commit_title
= TRUE
;
100 if (type
== LINE_DIFF_HEADER
) {
101 const int len
= STRING_SIZE("diff --");
103 state
->after_diff
= TRUE
;
104 if (!strncmp(data
+ len
, "combined ", strlen("combined ")) ||
105 !strncmp(data
+ len
, "cc ", strlen("cc ")))
106 state
->combined_diff
= TRUE
;
108 } else if (type
== LINE_PP_MERGE
) {
109 state
->combined_diff
= TRUE
;
112 /* ADD2 and DEL2 are only valid in combined diff hunks */
113 if (!state
->combined_diff
&& (type
== LINE_DIFF_ADD2
|| type
== LINE_DIFF_DEL2
))
116 return pager_common_read(view
, data
, type
, NULL
);
120 diff_find_stat_entry(struct view
*view
, struct line
*line
, enum line_type type
)
122 struct line
*marker
= find_next_line_by_type(view
, line
, type
);
125 line
== find_prev_line_by_type(view
, marker
, LINE_DIFF_HEADER
);
129 diff_common_enter(struct view
*view
, enum request request
, struct line
*line
)
131 if (line
->type
== LINE_DIFF_STAT
) {
134 while (view_has_line(view
, line
) && line
->type
== LINE_DIFF_STAT
) {
139 for (line
= view
->line
; view_has_line(view
, line
); line
++) {
140 line
= find_next_line_by_type(view
, line
, LINE_DIFF_HEADER
);
144 if (diff_find_stat_entry(view
, line
, LINE_DIFF_INDEX
)
145 || diff_find_stat_entry(view
, line
, LINE_DIFF_SIMILARITY
)) {
146 if (file_number
== 1) {
154 report("Failed to find file diff");
158 select_view_line(view
, line
- view
->line
);
163 return pager_request(view
, request
, line
);
168 diff_read(struct view
*view
, char *data
)
170 struct diff_state
*state
= view
->private;
173 /* Fall back to retry if no diff will be shown. */
174 if (view
->lines
== 0 && opt_file_argv
) {
175 int pos
= argv_size(view
->argv
)
176 - argv_size(opt_file_argv
) - 1;
178 if (pos
> 0 && !strcmp(view
->argv
[pos
], "--")) {
179 for (; view
->argv
[pos
]; pos
++) {
180 free((void *) view
->argv
[pos
]);
181 view
->argv
[pos
] = NULL
;
186 if (io_run(&view
->io
, IO_RD
, view
->dir
, opt_env
, view
->argv
))
193 return diff_common_read(view
, data
, state
);
197 diff_blame_line(const char *ref
, const char *file
, unsigned long lineno
,
198 struct blame_header
*header
, struct blame_commit
*commit
)
200 char author
[SIZEOF_STR
] = "";
201 char line_arg
[SIZEOF_STR
];
202 const char *blame_argv
[] = {
203 "git", "blame", encoding_arg
, "-p", line_arg
, ref
, "--", file
, NULL
209 if (!string_format(line_arg
, "-L%ld,+1", lineno
))
212 if (!io_run(&io
, IO_RD
, repo
.cdup
, opt_env
, blame_argv
))
215 while ((buf
= io_get(&io
, '\n', TRUE
))) {
217 if (!parse_blame_header(header
, buf
, 9999999))
221 } else if (parse_blame_info(commit
, author
, buf
)) {
222 ok
= commit
->filename
!= NULL
;
235 diff_get_lineno(struct view
*view
, struct line
*line
)
237 const struct line
*header
, *chunk
;
239 struct chunk_header chunk_header
;
241 /* Verify that we are after a diff header and one of its chunks */
242 header
= find_prev_line_by_type(view
, line
, LINE_DIFF_HEADER
);
243 chunk
= find_prev_line_by_type(view
, line
, LINE_DIFF_CHUNK
);
244 if (!header
|| !chunk
|| chunk
< header
)
248 * In a chunk header, the number after the '+' sign is the number of its
249 * following line, in the new version of the file. We increment this
250 * number for each non-deletion line, until the given line position.
252 if (!parse_chunk_header(&chunk_header
, chunk
->data
))
255 lineno
= chunk_header
.new.position
;
257 for (chunk
++; chunk
< line
; chunk
++)
258 if (chunk
->type
!= LINE_DIFF_DEL
&&
259 chunk
->type
!= LINE_DIFF_DEL2
)
266 diff_trace_origin(struct view
*view
, struct line
*line
)
268 struct line
*diff
= find_prev_line_by_type(view
, line
, LINE_DIFF_HEADER
);
269 struct line
*chunk
= find_prev_line_by_type(view
, line
, LINE_DIFF_CHUNK
);
270 const char *chunk_data
;
271 int chunk_marker
= line
->type
== LINE_DIFF_DEL
? '-' : '+';
272 unsigned long lineno
= 0;
273 const char *file
= NULL
;
274 char ref
[SIZEOF_REF
];
275 struct blame_header header
;
276 struct blame_commit commit
;
278 if (!diff
|| !chunk
|| chunk
== line
) {
279 report("The line to trace must be inside a diff chunk");
283 for (; diff
< line
&& !file
; diff
++) {
284 const char *data
= diff
->data
;
286 if (!prefixcmp(data
, "--- a/")) {
287 file
= data
+ STRING_SIZE("--- a/");
292 if (diff
== line
|| !file
) {
293 report("Failed to read the file name");
297 chunk_data
= chunk
->data
;
299 if (!parse_chunk_lineno(&lineno
, chunk_data
, chunk_marker
)) {
300 report("Failed to read the line number");
305 report("This is the origin of the line");
309 for (chunk
+= 1; chunk
< line
; chunk
++) {
310 if (chunk
->type
== LINE_DIFF_ADD
) {
311 lineno
+= chunk_marker
== '+';
312 } else if (chunk
->type
== LINE_DIFF_DEL
) {
313 lineno
+= chunk_marker
== '-';
319 if (chunk_marker
== '+')
320 string_copy(ref
, view
->vid
);
322 string_format(ref
, "%s^", view
->vid
);
324 if (string_rev_is_null(ref
)) {
325 string_ncopy(view
->env
->file
, file
, strlen(file
));
326 string_copy(view
->env
->ref
, "");
327 view
->env
->lineno
= lineno
- 1;
330 if (!diff_blame_line(ref
, file
, lineno
, &header
, &commit
)) {
331 report("Failed to read blame data");
335 string_ncopy(view
->env
->file
, commit
.filename
, strlen(commit
.filename
));
336 string_copy(view
->env
->ref
, header
.id
);
337 view
->env
->lineno
= header
.orig_lineno
- 1;
340 return REQ_VIEW_BLAME
;
344 diff_get_pathname(struct view
*view
, struct line
*line
)
346 const struct line
*header
;
347 const char *dst
= NULL
;
348 const char *prefixes
[] = { " b/", "cc ", "combined " };
351 header
= find_prev_line_by_type(view
, line
, LINE_DIFF_HEADER
);
355 for (i
= 0; i
< ARRAY_SIZE(prefixes
) && !dst
; i
++)
356 dst
= strstr(header
->data
, prefixes
[i
]);
358 return dst
? dst
+ strlen(prefixes
[--i
]) : NULL
;
362 diff_common_edit(struct view
*view
, enum request request
, struct line
*line
)
364 const char *file
= diff_get_pathname(view
, line
);
365 char path
[SIZEOF_STR
];
366 bool has_path
= file
&& string_format(path
, "%s%s", repo
.cdup
, file
);
368 if (has_path
&& access(path
, R_OK
)) {
369 report("Failed to open file: %s", file
);
373 open_editor(file
, diff_get_lineno(view
, line
));
378 diff_request(struct view
*view
, enum request request
, struct line
*line
)
382 return diff_trace_origin(view
, line
);
385 return diff_common_edit(view
, request
, line
);
388 return diff_common_enter(view
, request
, line
);
391 if (string_rev_is_null(view
->vid
))
398 return pager_request(view
, request
, line
);
403 diff_select(struct view
*view
, struct line
*line
)
405 if (line
->type
== LINE_DIFF_STAT
) {
406 string_format(view
->ref
, "Press '%s' to jump to file diff",
407 get_view_key(view
, REQ_ENTER
));
409 const char *file
= diff_get_pathname(view
, line
);
412 string_format(view
->ref
, "Changes to '%s'", file
);
413 string_format(view
->env
->file
, "%s", file
);
414 view
->env
->blob
[0] = 0;
416 string_ncopy(view
->ref
, view
->ops
->id
, strlen(view
->ops
->id
));
417 pager_select(view
, line
);
422 static struct view_ops diff_ops
= {
425 VIEW_DIFF_LIKE
| VIEW_ADD_DESCRIBE_REF
| VIEW_ADD_PAGER_REFS
| VIEW_FILE_FILTER
| VIEW_REFRESH
,
426 sizeof(struct diff_state
),
434 view_column_bit(LINE_NUMBER
) | view_column_bit(TEXT
),
435 pager_get_column_data
,
440 /* vim: set ts=8 sw=8 noexpandtab: */