Skip to content

Commit

Permalink
Fix staging with diff.noprefix and --no-prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
koutcher committed Jun 11, 2024
1 parent de984e2 commit 79b662a
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 38 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
24 changes: 12 additions & 12 deletions include/tig/git.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
* Argv-style git command macros.
*/

#define GIT_DIFF_INITIAL(encoding_arg, cached_arg, context_arg, space_arg, old_name, new_name) \
#define GIT_DIFF_INITIAL(encoding_arg, cached_arg, context_arg, prefix_arg, space_arg, old_name, new_name) \
"git", "diff", (encoding_arg), "--no-color", "--patch-with-stat", \
(cached_arg), (context_arg), (space_arg), "--", (old_name), (new_name), NULL
(cached_arg), (context_arg), (prefix_arg), (space_arg), "--", (old_name), (new_name), NULL

#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_INITIAL(encoding_arg, context_arg, prefix_arg, space_arg, new_name) \
GIT_DIFF_INITIAL(encoding_arg, "--cached", context_arg, prefix_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,12 +43,12 @@
#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)
#define GIT_DIFF_BLAME_NO_PARENT(encoding_arg, context_arg, prefix_arg, space_arg, new_name) \
GIT_DIFF_INITIAL(encoding_arg, "", context_arg, prefix_arg, space_arg, "/dev/null", new_name)

#define GIT_MAIN_LOG(encoding_arg, commit_order_arg, mainargs, diffargs, revargs, fileargs, show_notes_arg, pretty_arg) \
"git", "log", (encoding_arg), \
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
2 changes: 2 additions & 0 deletions src/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,15 @@ 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)
};
const char *diff_no_parent_argv[] = {
GIT_DIFF_BLAME_NO_PARENT(encoding_arg,
diff_context_arg(),
diff_prefix_arg(),
ignore_space_arg(),
blame->commit->filename)
};
Expand Down
7 changes: 4 additions & 3 deletions src/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ diff_open(struct view *view, enum open_flags flags)
const char *diff_argv[] = {
"git", "show", encoding_arg, "--pretty=fuller", "--root",
"--patch-with-stat", use_mailmap_arg(),
show_notes_arg(), diff_context_arg(), ignore_space_arg(),
DIFF_ARGS, "%(cmdlineargs)", "--no-color", word_diff_arg(),
"%(commit)", "--", "%(fileargs)", NULL
show_notes_arg(), diff_context_arg(), diff_prefix_arg(),
ignore_space_arg(), DIFF_ARGS, "%(cmdlineargs)",
"--no-color", word_diff_arg(), "%(commit)", "--",
"%(fileargs)", NULL
};
enum status_code code;

Expand Down
24 changes: 18 additions & 6 deletions 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";
}

const char *
word_diff_arg()
{
Expand Down Expand Up @@ -259,6 +265,18 @@ update_options_from_argv(const char *argv[])
continue;
}

if (!strcmp(flag, "--no-prefix")) {
opt_diff_noprefix = true;
mark_option_seen(&opt_diff_noprefix);
continue;
}

if (!strcmp(flag, "--default-prefix")) {
/* opt_diff_noprefix = false; */
mark_option_seen(&opt_diff_noprefix);
continue;
}

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

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

argv[flags_pos++] = flag;
}

Expand Down
20 changes: 12 additions & 8 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 @@ -707,23 +709,25 @@ static enum status_code
stage_open(struct view *view, enum open_flags flags)
{
const char *no_head_diff_argv[] = {
GIT_DIFF_STAGED_INITIAL(encoding_arg, diff_context_arg(), ignore_space_arg(),
stage_status.new.name)
GIT_DIFF_STAGED_INITIAL(encoding_arg, diff_context_arg(), diff_prefix_arg(),
ignore_space_arg(), 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
5 changes: 3 additions & 2 deletions src/stash.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ stash_request(struct view *view, enum request request, struct line *line)
const char *diff_argv[] = {
"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
diff_prefix_arg(), ignore_space_arg(),
word_diff_arg(), DIFF_ARGS, "--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 79b662a

Please sign in to comment.