Skip to content

Commit b68c58b

Browse files
committed
add-patch: add diff.context command line overrides
This patch compliments the previous commit, where builtins that use add-patch infrastructure now respect diff.context and diff.interHunkContext file configurations. In particular, this patch helps users who don't want to set persistent context configurations or just want a way to override them on a one-time basis, by allowing the relevant builtins to accept corresponding command line options that override the file configurations. This mimics commands such as diff and log, which allow for both context file configuration and command line overrides. Signed-off-by: Leon Michalak <[email protected]>
1 parent 1ec8a13 commit b68c58b

20 files changed

+271
-35
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
`-U<n>`::
2+
`--unified=<n>`::
3+
Generate diffs with _<n>_ lines of context. Defaults to `diff.context`
4+
or 3 if the config option is unset.
5+
6+
`--inter-hunk-context=<n>`::
7+
Show the context between diff hunks, up to the specified _<number>_
8+
of lines, thereby fusing hunks that are close to each other.
9+
Defaults to `diff.interHunkContext` or 0 if the config option
10+
is unset.

Documentation/git-add.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ This effectively runs `add --interactive`, but bypasses the
104104
initial command menu and directly jumps to the `patch` subcommand.
105105
See ``Interactive mode'' for details.
106106

107+
include::diff-context-options.adoc[]
108+
107109
`-e`::
108110
`--edit`::
109111
Open the diff vs. the index in an editor and let the user

Documentation/git-checkout.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ section of linkgit:git-add[1] to learn how to operate the `--patch` mode.
289289
Note that this option uses the no overlay mode by default (see also
290290
`--overlay`), and currently doesn't support overlay mode.
291291
292+
include::diff-context-options.adoc[]
293+
292294
`--ignore-other-worktrees`::
293295
`git checkout` refuses when the wanted branch is already checked
294296
out or otherwise in use by another worktree. This option makes

Documentation/git-commit.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ OPTIONS
7676
which changes to commit. See linkgit:git-add[1] for
7777
details.
7878

79+
include::diff-context-options.adoc[]
80+
7981
`-C <commit>`::
8082
`--reuse-message=<commit>`::
8183
Take an existing _<commit>_ object, and reuse the log message

Documentation/git-reset.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ OPTIONS
125125
separated with _NUL_ character and all other characters are taken
126126
literally (including newlines and quotes).
127127

128+
include::diff-context-options.adoc[]
129+
128130
`--`::
129131
Do not interpret any more arguments as options.
130132

Documentation/git-restore.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ leave out at most one of _<rev-A>__ and _<rev-B>_, in which case it defaults to
5252
Mode" section of linkgit:git-add[1] to learn how to operate
5353
the `--patch` mode.
5454

55+
include::diff-context-options.adoc[]
56+
5557
`-W`::
5658
`--worktree`::
5759
`-S`::

Documentation/git-stash.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ to learn how to operate the `--patch` mode.
208208
The `--patch` option implies `--keep-index`. You can use
209209
`--no-keep-index` to override this.
210210
211+
include::diff-context-options.adoc[]
212+
211213
-S::
212214
--staged::
213215
This option is only valid for `push` and `save` commands.

add-interactive.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ static void init_color(struct repository *r, struct add_i_state *s,
3636
free(key);
3737
}
3838

39-
void init_add_i_state(struct add_i_state *s, struct repository *r)
39+
void init_add_i_state(struct add_i_state *s, struct repository *r,
40+
struct add_p_opt *add_p_opt)
4041
{
4142
const char *value;
4243
int context;
@@ -98,6 +99,17 @@ void init_add_i_state(struct add_i_state *s, struct repository *r)
9899
repo_config_get_bool(r, "interactive.singlekey", &s->use_single_key);
99100
if (s->use_single_key)
100101
setbuf(stdin, NULL);
102+
103+
if (add_p_opt->context != -1) {
104+
if (add_p_opt->context < 0)
105+
die(_("%s cannot be negative"), "--unified");
106+
s->context = add_p_opt->context;
107+
}
108+
if (add_p_opt->interhunkcontext != -1) {
109+
if (add_p_opt->interhunkcontext < 0)
110+
die(_("%s cannot be negative"), "--inter-hunk-context");
111+
s->interhunkcontext = add_p_opt->interhunkcontext;
112+
}
101113
}
102114

103115
void clear_add_i_state(struct add_i_state *s)
@@ -986,6 +998,10 @@ static int run_patch(struct add_i_state *s, const struct pathspec *ps,
986998
opts->prompt = N_("Patch update");
987999
count = list_and_choose(s, files, opts);
9881000
if (count > 0) {
1001+
struct add_p_opt add_p_opt = {
1002+
.context = s->context,
1003+
.interhunkcontext = s->interhunkcontext,
1004+
};
9891005
struct strvec args = STRVEC_INIT;
9901006
struct pathspec ps_selected = { 0 };
9911007

@@ -996,7 +1012,7 @@ static int run_patch(struct add_i_state *s, const struct pathspec *ps,
9961012
parse_pathspec(&ps_selected,
9971013
PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
9981014
PATHSPEC_LITERAL_PATH, "", args.v);
999-
res = run_add_p(s->r, ADD_P_ADD, NULL, &ps_selected);
1015+
res = run_add_p(s->r, ADD_P_ADD, &add_p_opt, NULL, &ps_selected);
10001016
strvec_clear(&args);
10011017
clear_pathspec(&ps_selected);
10021018
}
@@ -1031,10 +1047,13 @@ static int run_diff(struct add_i_state *s, const struct pathspec *ps,
10311047
if (count > 0) {
10321048
struct child_process cmd = CHILD_PROCESS_INIT;
10331049

1034-
strvec_pushl(&cmd.args, "git", "diff", "-p", "--cached",
1035-
oid_to_hex(!is_initial ? &oid :
1036-
s->r->hash_algo->empty_tree),
1037-
"--", NULL);
1050+
strvec_pushl(&cmd.args, "git", "diff", "-p", "--cached", NULL);
1051+
if (s->context != -1)
1052+
strvec_pushf(&cmd.args, "--unified=%i", s->context);
1053+
if (s->interhunkcontext != -1)
1054+
strvec_pushf(&cmd.args, "--inter-hunk-context=%i", s->interhunkcontext);
1055+
strvec_pushl(&cmd.args, oid_to_hex(!is_initial ? &oid :
1056+
s->r->hash_algo->empty_tree), "--", NULL);
10381057
for (i = 0; i < files->items.nr; i++)
10391058
if (files->selected[i])
10401059
strvec_push(&cmd.args,
@@ -1127,7 +1146,8 @@ static void command_prompt_help(struct add_i_state *s)
11271146
_("(empty) select nothing"));
11281147
}
11291148

1130-
int run_add_i(struct repository *r, const struct pathspec *ps)
1149+
int run_add_i(struct repository *r, const struct pathspec *ps,
1150+
struct add_p_opt *add_p_opt)
11311151
{
11321152
struct add_i_state s = { NULL };
11331153
struct print_command_item_data data = { "[", "]" };
@@ -1170,7 +1190,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
11701190
->util = util;
11711191
}
11721192

1173-
init_add_i_state(&s, r);
1193+
init_add_i_state(&s, r, add_p_opt);
11741194

11751195
/*
11761196
* When color was asked for, use the prompt color for

add-interactive.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
#include "color.h"
55

6+
struct add_p_opt {
7+
int context;
8+
int interhunkcontext;
9+
};
10+
11+
#define ADD_P_OPT_INIT { .context = -1, .interhunkcontext = -1 }
12+
613
struct add_i_state {
714
struct repository *r;
815
int use_color;
@@ -21,12 +28,14 @@ struct add_i_state {
2128
int context, interhunkcontext;
2229
};
2330

24-
void init_add_i_state(struct add_i_state *s, struct repository *r);
31+
void init_add_i_state(struct add_i_state *s, struct repository *r,
32+
struct add_p_opt *add_p_opt);
2533
void clear_add_i_state(struct add_i_state *s);
2634

2735
struct repository;
2836
struct pathspec;
29-
int run_add_i(struct repository *r, const struct pathspec *ps);
37+
int run_add_i(struct repository *r, const struct pathspec *ps,
38+
struct add_p_opt *add_p_opt);
3039

3140
enum add_p_mode {
3241
ADD_P_ADD,
@@ -37,6 +46,7 @@ enum add_p_mode {
3746
};
3847

3948
int run_add_p(struct repository *r, enum add_p_mode mode,
40-
const char *revision, const struct pathspec *ps);
49+
struct add_p_opt *o, const char *revision,
50+
const struct pathspec *ps);
4151

4252
#endif

add-patch.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,14 +1766,15 @@ static int patch_update_file(struct add_p_state *s,
17661766
}
17671767

17681768
int run_add_p(struct repository *r, enum add_p_mode mode,
1769-
const char *revision, const struct pathspec *ps)
1769+
struct add_p_opt *o, const char *revision,
1770+
const struct pathspec *ps)
17701771
{
17711772
struct add_p_state s = {
17721773
{ r }, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
17731774
};
17741775
size_t i, binary_count = 0;
17751776

1776-
init_add_i_state(&s.s, r);
1777+
init_add_i_state(&s.s, r, o);
17771778

17781779
if (mode == ADD_P_STASH)
17791780
s.mode = &patch_mode_stash;

0 commit comments

Comments
 (0)