Skip to content

Commit

Permalink
Fix staging with diff.noprefix and --no-prefix
Browse files Browse the repository at this point in the history
Note that the option `--default-prefix` works only if the installed Git
version supports it (2.41 and onward). The option is just passed through
in order not to break older versions support.
  • Loading branch information
koutcher committed Jun 12, 2024
1 parent de984e2 commit 37ccb67
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ master

Bug fixes:

- Fix parsing of `--no-prefix` argument.
- Fix various issues with `diff.noprefix` and `--no-prefix`.

Improvements:

Expand Down
12 changes: 6 additions & 6 deletions include/tig/git.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
#define GIT_DIFF_STAGED_INITIAL(encoding_arg, context_arg, space_arg, new_name) \
GIT_DIFF_INITIAL(encoding_arg, "--cached", context_arg, space_arg, "", new_name)

#define GIT_DIFF_STAGED(encoding_arg, context_arg, space_arg, word_diff_arg, old_name, new_name) \
#define GIT_DIFF_STAGED(encoding_arg, context_arg, prefix_arg, space_arg, word_diff_arg, old_name, new_name) \
"git", "diff-index", (encoding_arg), "--textconv", "--patch-with-stat", "-C", \
"--cached", "--diff-filter=ACDMRTXB", DIFF_ARGS, "%(cmdlineargs)", (context_arg), \
(space_arg), (word_diff_arg), "HEAD", "--", (old_name), (new_name), NULL
(prefix_arg), (space_arg), (word_diff_arg), "HEAD", "--", (old_name), (new_name), NULL

#define GIT_DIFF_UNSTAGED(encoding_arg, context_arg, space_arg, word_diff_arg, old_name, new_name) \
#define GIT_DIFF_UNSTAGED(encoding_arg, context_arg, prefix_arg, space_arg, word_diff_arg, old_name, new_name) \
"git", "diff-files", (encoding_arg), "--textconv", "--patch-with-stat", "-C", \
DIFF_ARGS, "%(cmdlineargs)", (context_arg), (space_arg), (word_diff_arg), \
DIFF_ARGS, "%(cmdlineargs)", (context_arg), (prefix_arg), (space_arg), (word_diff_arg), \
"--", (old_name), (new_name), NULL

/* Don't show staged unmerged entries. */
Expand All @@ -43,9 +43,9 @@
#define GIT_DIFF_UNSTAGED_FILES(output_arg) \
"git", "diff-files", (output_arg), "%(cmdlineargs)", NULL

#define GIT_DIFF_BLAME(encoding_arg, context_arg, space_arg, word_diff_arg, new_name) \
#define GIT_DIFF_BLAME(encoding_arg, context_arg, prefix_arg, space_arg, word_diff_arg, new_name) \
"git", "diff-files", (encoding_arg), "--textconv", "--patch-with-stat", "-C", \
(context_arg), (space_arg), (word_diff_arg), "--", (new_name), NULL
(context_arg), (prefix_arg), (space_arg), (word_diff_arg), "--", (new_name), NULL

#define GIT_DIFF_BLAME_NO_PARENT(encoding_arg, context_arg, space_arg, new_name) \
GIT_DIFF_INITIAL(encoding_arg, "", context_arg, space_arg, "/dev/null", new_name)
Expand Down
1 change: 1 addition & 0 deletions include/tig/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ const char *commit_order_arg_with_graph(enum graph_display graph_display);
const char *log_custom_pretty_arg();
const char *use_mailmap_arg();
const char *diff_context_arg();
const char *diff_prefix_arg();
const char *word_diff_arg();
const char *show_notes_arg();

Expand Down
1 change: 1 addition & 0 deletions src/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ blame_request(struct view *view, enum request request, struct line *line)
const char *diff_parent_argv[] = {
GIT_DIFF_BLAME(encoding_arg,
diff_context_arg(),
diff_prefix_arg(),
ignore_space_arg(),
word_diff_arg(),
blame->commit->filename)
Expand Down
14 changes: 13 additions & 1 deletion src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ diff_context_arg()
return opt_diff_context_arg;
}

const char *
diff_prefix_arg()
{
return opt_diff_noprefix ? "--no-prefix" : ""; /* --default-prefix did not exist before Git 2.41. */
}

const char *
word_diff_arg()
{
Expand Down Expand Up @@ -260,7 +266,7 @@ update_options_from_argv(const char *argv[])
}

if (!strcmp(flag, "--word-diff=none")) {
/* opt_word_diff = false; */
opt_word_diff = false;
mark_option_seen(&opt_word_diff);
continue;
}
Expand All @@ -284,6 +290,12 @@ update_options_from_argv(const char *argv[])
/* Keep the flag in argv. */
}

if (!strcmp(flag, "--default-prefix")) {
opt_diff_noprefix = false;
mark_option_seen(&opt_diff_noprefix);
/* Keep the flag in argv. */
}

argv[flags_pos++] = flag;
}

Expand Down
16 changes: 10 additions & 6 deletions src/stage.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ stage_apply_chunk(struct view *view, struct line *chunk, struct line *single,
if (!diff_hdr)
return false;

if (opt_diff_noprefix)
apply_argv[argc++] = "-p0";
if (!revert)
apply_argv[argc++] = "--cached";
if (revert || stage_line_type == LINE_STAT_STAGED)
Expand Down Expand Up @@ -711,19 +713,21 @@ stage_open(struct view *view, enum open_flags flags)
stage_status.new.name)
};
const char *index_show_argv[] = {
GIT_DIFF_STAGED(encoding_arg, diff_context_arg(), ignore_space_arg(),
word_diff_arg(), stage_status.old.name, stage_status.new.name)
GIT_DIFF_STAGED(encoding_arg, diff_context_arg(), diff_prefix_arg(),
ignore_space_arg(), word_diff_arg(), stage_status.old.name,
stage_status.new.name)
};
const char *files_show_argv[] = {
GIT_DIFF_UNSTAGED(encoding_arg, diff_context_arg(), ignore_space_arg(),
word_diff_arg(), stage_status.old.name, stage_status.new.name)
GIT_DIFF_UNSTAGED(encoding_arg, diff_context_arg(), diff_prefix_arg(),
ignore_space_arg(), word_diff_arg(), stage_status.old.name,
stage_status.new.name)
};
/* Diffs for unmerged entries are empty when passing the new
* path, so leave out the new path. */
const char *files_unmerged_argv[] = {
"git", "diff-files", encoding_arg, "--textconv", "--patch-with-stat",
DIFF_ARGS, diff_context_arg(), ignore_space_arg(), "--",
stage_status.old.name, NULL
DIFF_ARGS, diff_context_arg(), diff_prefix_arg(),
ignore_space_arg(), "--", stage_status.old.name, NULL
};
static const char *file_argv[] = { repo.exec_dir, stage_status.new.name, NULL };
const char **argv = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/stash.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ stash_request(struct view *view, enum request request, struct line *line)
"git", "stash", "show", encoding_arg, "--pretty=fuller",
"--patch-with-stat", diff_context_arg(),
ignore_space_arg(), word_diff_arg(), DIFF_ARGS,
"--no-color", "%(stash)", NULL
"%(cmdlineargs)", "--no-color", "%(stash)", NULL
};

if (!argv_format(diff_view.env, &diff_view.argv, diff_argv, 0))
Expand Down
4 changes: 2 additions & 2 deletions test/diff/editor-test
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ test_case noprefix-conflict \
' << EOF
1| diff --cc conflict-file
| index 86c5a05,b4c3de6..0000000
| --- a/conflict-file
| +++ b/conflict-file
| --- conflict-file
| +++ conflict-file
5| @@@ -1,1 -1,1 +1,5 @@@
| ++<<<<<<< HEAD
| +c'
Expand Down
4 changes: 2 additions & 2 deletions test/diff/submodule-editor-test
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ test_case noprefix-conflict \
' << EOF
1| diff --cc conflict-file
| index 86c5a05,b4c3de6..0000000
| --- a/conflict-file
| +++ b/conflict-file
| --- conflict-file
| +++ conflict-file
5| @@@ -1,1 -1,1 +1,5 @@@
| ++<<<<<<< HEAD
| +c'
Expand Down
4 changes: 2 additions & 2 deletions test/diff/worktree-editor-test
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ test_case noprefix-conflict \
' << EOF
1| diff --cc conflict-file
| index 86c5a05,b4c3de6..0000000
| --- a/conflict-file
| +++ b/conflict-file
| --- conflict-file
| +++ conflict-file
5| @@@ -1,1 -1,1 +1,5 @@@
| ++<<<<<<< HEAD
| +c'
Expand Down

0 comments on commit 37ccb67

Please sign in to comment.