Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 93ea970

Browse files
authoredJan 11, 2019
Merge pull request #877 from koutcher/master
Fix diff highlighting of removed lines starting with -- and added lines starting with ++
2 parents d75c510 + 060c755 commit 93ea970

File tree

17 files changed

+168
-15
lines changed

17 files changed

+168
-15
lines changed
 

‎doc/tigrc.5.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ The following variables can be set:
225225
226226
Whether to show staged and unstaged changes in the main view.
227227
228+
'show-untracked' (bool)::
229+
230+
Whether to show also untracked changes in the main view.
231+
228232
'vertical-split' (mixed) [auto|<bool>]::
229233
230234
Whether to split the view horizontally or vertically.

‎include/tig/diff.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
struct diff_state {
2020
bool after_commit_title;
2121
bool after_diff;
22+
bool reading_diff_chunk;
2223
bool reading_diff_stat;
2324
bool combined_diff;
2425
bool adding_describe_ref;

‎include/tig/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct main_state {
3939
bool first_parent;
4040
bool add_changes_staged;
4141
bool add_changes_unstaged;
42+
bool add_changes_untracked;
4243
};
4344

4445
bool main_get_column_data(struct view *view, const struct line *line, struct view_column_data *column_data);

‎include/tig/options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ typedef struct view_column *view_settings;
6969
_(send_child_enter, bool, VIEW_NO_FLAGS) \
7070
_(show_changes, bool, VIEW_NO_FLAGS) \
7171
_(show_notes, bool, VIEW_NO_FLAGS) \
72+
_(show_untracked, bool, VIEW_NO_FLAGS) \
7273
_(split_view_height, double, VIEW_RESET_DISPLAY) \
7374
_(split_view_width, double, VIEW_RESET_DISPLAY) \
7475
_(stage_view, view_settings, VIEW_NO_FLAGS) \

‎include/tig/watch.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ enum watch_trigger {
3030
WATCH_INDEX_STAGED_NO = 1 << 1,
3131
WATCH_INDEX_UNSTAGED_YES = 1 << 2,
3232
WATCH_INDEX_UNSTAGED_NO = 1 << 3,
33-
WATCH_HEAD = 1 << 4,
34-
WATCH_STASH = 1 << 5,
35-
WATCH_REFS = 1 << 6,
33+
WATCH_INDEX_UNTRACKED_YES = 1 << 4,
34+
WATCH_INDEX_UNTRACKED_NO = 1 << 5,
35+
WATCH_HEAD = 1 << 6,
36+
WATCH_STASH = 1 << 7,
37+
WATCH_REFS = 1 << 8,
3638

3739
WATCH_INDEX_STAGED = WATCH_INDEX_STAGED_YES | WATCH_INDEX_STAGED_NO,
3840
WATCH_INDEX_UNSTAGED = WATCH_INDEX_UNSTAGED_YES | WATCH_INDEX_UNSTAGED_NO,
39-
WATCH_INDEX = WATCH_INDEX_STAGED | WATCH_INDEX_UNSTAGED,
41+
WATCH_INDEX_UNTRACKED = WATCH_INDEX_UNTRACKED_YES | WATCH_INDEX_UNTRACKED_NO,
42+
WATCH_INDEX = WATCH_INDEX_STAGED | WATCH_INDEX_UNSTAGED | WATCH_INDEX_UNTRACKED,
4043
};
4144

4245
struct watch {

‎src/blame.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,23 @@ blame_open(struct view *view, enum open_flags flags)
126126

127127
blame_update_file_name_visibility(view);
128128

129-
if (!view->env->file[0])
130-
return error("No file chosen, press %s to open tree view",
131-
get_view_key(view, REQ_VIEW_TREE));
129+
if (!view->env->file[0]) {
130+
struct stat statbuf;
131+
132+
if (opt_file_args && !opt_file_args[1] &&
133+
stat(opt_file_args[0], &statbuf) == 0 && !S_ISDIR(statbuf.st_mode)) {
134+
const char *ls_files_argv[] = {
135+
"git", "ls-files", "-z", "--full-name", "--", opt_file_args[0], NULL
136+
};
137+
char name[SIZEOF_STR] = "";
138+
139+
io_run_buf(ls_files_argv, name, sizeof(name), NULL, false);
140+
string_ncopy(view->env->file, name, strlen(name));
141+
} else {
142+
return error("No file chosen, press %s to open tree view",
143+
get_view_key(view, REQ_VIEW_TREE));
144+
}
145+
}
132146

133147
if (!view->prev && *repo.prefix && !(flags & (OPEN_RELOAD | OPEN_REFRESH))) {
134148
string_copy(path, view->env->file);

‎src/diff.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ diff_common_read(struct view *view, const char *data, struct diff_state *state)
263263
if (!strncmp(data + len, "combined ", strlen("combined ")) ||
264264
!strncmp(data + len, "cc ", strlen("cc ")))
265265
state->combined_diff = true;
266+
state->reading_diff_chunk = false;
266267

267268
} else if (type == LINE_DIFF_CHUNK) {
268269
const char *context = strstr(data + STRING_SIZE("@@"), "@@");
@@ -278,6 +279,7 @@ diff_common_read(struct view *view, const char *data, struct diff_state *state)
278279
box->cell[0].length = (context + 2) - data;
279280
box->cell[1].length = strlen(context + 2);
280281
box->cell[box->cells++].type = LINE_DIFF_STAT;
282+
state->reading_diff_chunk = true;
281283
return true;
282284

283285
} else if (state->highlight && strchr(data, 0x1b)) {
@@ -291,6 +293,14 @@ diff_common_read(struct view *view, const char *data, struct diff_state *state)
291293
if (!state->combined_diff && (type == LINE_DIFF_ADD2 || type == LINE_DIFF_DEL2))
292294
type = LINE_DEFAULT;
293295

296+
/* DEL_FILE, ADD_FILE and START are only valid outside diff chunks */
297+
if (state->reading_diff_chunk) {
298+
if (type == LINE_DIFF_DEL_FILE || type == LINE_DIFF_START)
299+
type = LINE_DIFF_DEL;
300+
else if (type == LINE_DIFF_ADD_FILE)
301+
type = LINE_DIFF_ADD;
302+
}
303+
294304
return pager_common_read(view, data, type, NULL);
295305
}
296306

‎src/draw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ draw_view_line_search_result(struct view *view, unsigned int lineno)
593593
regoff_t end = pmatch[0].rm_eo;
594594

595595
if (start == -1 || end <= 0 || end <= start)
596-
continue;
596+
break;
597597

598598
mvwchgat(view->win, lineno,
599599
utf8_width_of(buf, bufpos + start, -1),

‎src/main.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ main_status_exists(struct view *view, enum line_type type)
4545
return true;
4646
if (type == LINE_STAT_UNSTAGED && state->add_changes_unstaged)
4747
return true;
48+
if (type == LINE_STAT_UNTRACKED && state->add_changes_untracked)
49+
return true;
4850

4951
return false;
5052
}
@@ -60,9 +62,9 @@ main_register_commit(struct view *view, struct commit *commit, const char *ids,
6062
string_copy_rev(commit->id, ids);
6163

6264
/* FIXME: lazily check index state here instead of in main_open. */
63-
if ((state->add_changes_unstaged || state->add_changes_staged) && is_head_commit(commit->id)) {
65+
if ((state->add_changes_untracked || state->add_changes_unstaged || state->add_changes_staged) && is_head_commit(commit->id)) {
6466
main_add_changes(view, state, ids);
65-
state->add_changes_unstaged = state->add_changes_staged = false;
67+
state->add_changes_untracked = state->add_changes_unstaged = state->add_changes_staged = false;
6668
}
6769

6870
if (state->with_graph)
@@ -147,9 +149,16 @@ main_check_index(struct view *view, struct main_state *state)
147149
{
148150
struct index_diff diff;
149151

150-
if (!index_diff(&diff, false, false))
152+
if (!index_diff(&diff, opt_show_untracked, false))
151153
return false;
152154

155+
if (!diff.untracked) {
156+
watch_apply(&view->watch, WATCH_INDEX_UNTRACKED_NO);
157+
} else {
158+
watch_apply(&view->watch, WATCH_INDEX_UNTRACKED_YES);
159+
state->add_changes_untracked = true;
160+
}
161+
153162
if (!diff.unstaged) {
154163
watch_apply(&view->watch, WATCH_INDEX_UNSTAGED_NO);
155164
} else {
@@ -172,6 +181,7 @@ main_add_changes(struct view *view, struct main_state *state, const char *parent
172181
{
173182
const char *staged_parent = parent;
174183
const char *unstaged_parent = NULL_ID;
184+
const char *untracked_parent = NULL_ID;
175185

176186
if (!state->add_changes_staged) {
177187
staged_parent = NULL;
@@ -180,9 +190,16 @@ main_add_changes(struct view *view, struct main_state *state, const char *parent
180190

181191
if (!state->add_changes_unstaged) {
182192
unstaged_parent = NULL;
193+
if (!state->add_changes_staged)
194+
untracked_parent = parent;
195+
}
196+
197+
if (!state->add_changes_untracked) {
198+
untracked_parent = NULL;
183199
}
184200

185-
return main_add_changes_commit(view, LINE_STAT_UNSTAGED, unstaged_parent, "Unstaged changes")
201+
return main_add_changes_commit(view, LINE_STAT_UNTRACKED, untracked_parent, "Untracked changes")
202+
&& main_add_changes_commit(view, LINE_STAT_UNSTAGED, unstaged_parent, "Unstaged changes")
186203
&& main_add_changes_commit(view, LINE_STAT_STAGED, staged_parent, "Staged changes");
187204
}
188205

@@ -528,6 +545,8 @@ main_request(struct view *view, enum request request, struct line *line)
528545
if (line->type == LINE_STAT_UNSTAGED
529546
|| line->type == LINE_STAT_STAGED)
530547
open_stage_view(view, NULL, line->type, flags);
548+
else if (line->type == LINE_STAT_UNTRACKED)
549+
open_status_view(view, flags);
531550
else
532551
open_diff_view(view, flags);
533552
break;
@@ -558,7 +577,7 @@ main_select(struct view *view, struct line *line)
558577
{
559578
struct commit *commit = line->data;
560579

561-
if (line->type == LINE_STAT_STAGED || line->type == LINE_STAT_UNSTAGED) {
580+
if (line->type == LINE_STAT_STAGED || line->type == LINE_STAT_UNSTAGED || line->type == LINE_STAT_UNTRACKED) {
562581
string_ncopy(view->ref, commit->title, strlen(commit->title));
563582
status_stage_info(view->env->status, line->type, NULL);
564583
} else {

‎src/pager.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,39 @@ pager_wrap_line(struct view *view, const char *data, enum line_type type)
101101
bool
102102
pager_common_read(struct view *view, const char *data, enum line_type type, struct line **line_ptr)
103103
{
104+
struct diff_state *state = view->private;
104105
struct line *line;
105106

106107
if (!data)
107108
return true;
108109

110+
if (type == LINE_DIFF_HEADER) {
111+
const int len = STRING_SIZE("diff --");
112+
113+
if (!strncmp(data + len, "combined ", strlen("combined ")) ||
114+
!strncmp(data + len, "cc ", strlen("cc ")))
115+
state->combined_diff = true;
116+
state->reading_diff_chunk = false;
117+
118+
} else if (type == LINE_DIFF_CHUNK) {
119+
state->reading_diff_chunk = true;
120+
121+
} else if (type == LINE_PP_MERGE) {
122+
state->combined_diff = true;
123+
}
124+
125+
/* ADD2 and DEL2 are only valid in combined diff hunks */
126+
if (!state->combined_diff && (type == LINE_DIFF_ADD2 || type == LINE_DIFF_DEL2))
127+
type = LINE_DEFAULT;
128+
129+
/* DEL_FILE, ADD_FILE and START are only valid outside diff chunks */
130+
if (state->reading_diff_chunk) {
131+
if (type == LINE_DIFF_DEL_FILE || type == LINE_DIFF_START)
132+
type = LINE_DIFF_DEL;
133+
else if (type == LINE_DIFF_ADD_FILE)
134+
type = LINE_DIFF_ADD;
135+
}
136+
109137
if (opt_wrap_lines) {
110138
line = pager_wrap_line(view, data, type);
111139
} else {
@@ -188,7 +216,7 @@ static struct view_ops pager_ops = {
188216
"line",
189217
"",
190218
VIEW_OPEN_DIFF | VIEW_NO_REF | VIEW_NO_GIT_DIR,
191-
0,
219+
sizeof(struct diff_state),
192220
pager_open,
193221
pager_read,
194222
view_column_draw,

‎src/repo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ index_diff(struct index_diff *diff, bool untracked, bool count_all)
150150
/* Ignore staged but unmerged entries. */
151151
else if (buf.data[0] != ' ' && buf.data[0] != 'U')
152152
diff->staged++;
153-
if (buf.data[1] != ' ')
153+
if (buf.data[1] != ' ' && buf.data[1] != '?')
154154
diff->unstaged++;
155155
if (!count_all && diff->staged && diff->unstaged &&
156156
(!untracked || diff->untracked))

‎test/diff/editor-test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
. libgit.sh
55

66
tigrc <<EOF
7+
set show-untracked = no
78
set line-graphics = ascii
89
set diff-view-line-number = yes
910
set stage-view-line-number = yes

‎test/diff/submodule-editor-test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ executable exec-env <<EOF
1616
EOF
1717

1818
tigrc <<EOF
19+
set show-untracked = no
1920
set line-graphics = ascii
2021
set diff-view-line-number = yes
2122
set stage-view-line-number = yes

‎test/diff/worktree-editor-test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ executable exec-env <<EOF
1616
EOF
1717

1818
tigrc <<EOF
19+
set show-untracked = no
1920
set line-graphics = ascii
2021
set diff-view-line-number = yes
2122
set stage-view-line-number = yes

‎test/main/mailmap-test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Full Throttle <f.throttle@example.org> Max Power <power123@example.org>"
1717
EOF
1818

1919
tigrc <<EOF
20+
set show-untracked = no
2021
set mailmap = yes
2122
# set diff-options = --use-mailmap
2223
# set main-options = --use-mailmap

‎test/main/untracked-test

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/sh
2+
3+
. libtest.sh
4+
. libgit.sh
5+
. "$source_dir/util.sh"
6+
7+
export LINES=20
8+
9+
steps '
10+
:save-display start.screen
11+
<Enter>
12+
:save-display result.screen
13+
'
14+
15+
tigrc <<EOF
16+
set vertical-split = no
17+
EOF
18+
19+
in_work_dir create_dirty_workdir
20+
in_work_dir git add .j
21+
in_work_dir touch z
22+
23+
test_tig
24+
25+
assert_equals 'start.screen' <<EOF
26+
$YYY_MM_DD_HH_MM +0000 Unknown o Untracked changes
27+
$YYY_MM_DD_HH_MM +0000 Unknown o Unstaged changes
28+
$YYY_MM_DD_HH_MM +0000 Unknown o Staged changes
29+
2009-02-13 23:31 +0000 A. U. Thor I [master] Initial commit
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
[main] Untracked changes 100%
45+
EOF
46+
47+
assert_equals 'result.screen' <<EOF
48+
$YYY_MM_DD_HH_MM +0000 Unknown o Untracked changes
49+
$YYY_MM_DD_HH_MM +0000 Unknown o Unstaged changes
50+
$YYY_MM_DD_HH_MM +0000 Unknown o Staged changes
51+
2009-02-13 23:31 +0000 A. U. Thor I [master] Initial commit
52+
53+
54+
[main] Untracked changes 100%
55+
On branch master
56+
Changes to be committed:
57+
M .j
58+
Changes not staged for commit:
59+
M a
60+
M b.c
61+
M e/f
62+
M g h
63+
Untracked files:
64+
? z
65+
66+
[status] Nothing to update 100%
67+
EOF

‎tigrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ set diff-view = line-number:no,interval=5 text:yes,commit-title-overflow=no
8484

8585
# UI display settings
8686
set show-changes = yes # Show changes commits in the main view?
87+
set show-untracked = yes # Show also untracked changes?
8788
set wrap-lines = no # Wrap long lines in pager views?
8889
set tab-size = 8 # Number of spaces to use when expanding tabs
8990
set line-graphics = default # Enum: ascii, default, utf-8

0 commit comments

Comments
 (0)
Please sign in to comment.