Skip to content

Commit 48f6d92

Browse files
pks-tgitster
authored andcommitted
add-patch: allow disabling editing of hunks
The "add-patch" mode allows the user to edit hunks to apply custom changes. This is incompatible with a new `git history split` command that we're about to introduce in a subsequent commit, so we need a way to disable this mode. Add a new flag to disable editing hunks. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 0c5583a commit 48f6d92

7 files changed

Lines changed: 28 additions & 15 deletions

File tree

add-interactive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ static int run_patch(struct add_i_state *s, const struct pathspec *ps,
927927
parse_pathspec(&ps_selected,
928928
PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
929929
PATHSPEC_LITERAL_PATH, "", args.v);
930-
res = run_add_p(s->r, ADD_P_ADD, &opts, NULL, &ps_selected);
930+
res = run_add_p(s->r, ADD_P_ADD, &opts, NULL, &ps_selected, 0);
931931
strvec_clear(&args);
932932
clear_pathspec(&ps_selected);
933933
}

add-patch.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,9 @@ static bool get_first_undecided(const struct file_diff *file_diff, size_t *idx)
16041604
return false;
16051605
}
16061606

1607-
static size_t patch_update_file(struct add_p_state *s, size_t idx)
1607+
static size_t patch_update_file(struct add_p_state *s,
1608+
size_t idx,
1609+
unsigned flags)
16081610
{
16091611
size_t hunk_index = 0;
16101612
ssize_t i, undecided_previous, undecided_next, rendered_hunk_index = -1;
@@ -1715,7 +1717,8 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
17151717
permitted |= ALLOW_SPLIT;
17161718
strbuf_addstr(&s->buf, ",s");
17171719
}
1718-
if (hunk_index + 1 > file_diff->mode_change &&
1720+
if (!(flags & ADD_P_DISALLOW_EDIT) &&
1721+
hunk_index + 1 > file_diff->mode_change &&
17191722
!file_diff->deleted) {
17201723
permitted |= ALLOW_EDIT;
17211724
strbuf_addstr(&s->buf, ",e");
@@ -2003,7 +2006,8 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
20032006
}
20042007

20052008
static int run_add_p_common(struct add_p_state *state,
2006-
const struct pathspec *ps)
2009+
const struct pathspec *ps,
2010+
unsigned flags)
20072011
{
20082012
size_t binary_count = 0;
20092013
size_t i;
@@ -2017,7 +2021,7 @@ static int run_add_p_common(struct add_p_state *state,
20172021
i++;
20182022
continue;
20192023
}
2020-
if ((i = patch_update_file(state, i)) == state->file_diff_nr)
2024+
if ((i = patch_update_file(state, i, flags)) == state->file_diff_nr)
20212025
break;
20222026
}
20232027

@@ -2035,7 +2039,8 @@ static int run_add_p_common(struct add_p_state *state,
20352039

20362040
int run_add_p(struct repository *r, enum add_p_mode mode,
20372041
struct interactive_options *opts, const char *revision,
2038-
const struct pathspec *ps)
2042+
const struct pathspec *ps,
2043+
unsigned flags)
20392044
{
20402045
struct add_p_state s = {
20412046
.r = r,
@@ -2084,7 +2089,7 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
20842089
goto out;
20852090
}
20862091

2087-
ret = run_add_p_common(&s, ps);
2092+
ret = run_add_p_common(&s, ps, flags);
20882093
if (ret < 0)
20892094
goto out;
20902095

@@ -2100,7 +2105,8 @@ int run_add_p_index(struct repository *r,
21002105
const char *index_file,
21012106
struct interactive_options *opts,
21022107
const char *revision,
2103-
const struct pathspec *ps)
2108+
const struct pathspec *ps,
2109+
unsigned flags)
21042110
{
21052111
struct patch_mode mode = {
21062112
.apply_args = { "--cached", NULL },
@@ -2156,7 +2162,7 @@ int run_add_p_index(struct repository *r,
21562162
mode.diff_cmd[1] = "-r";
21572163
mode.diff_cmd[2] = parent_tree_oid;
21582164

2159-
ret = run_add_p_common(&s, ps);
2165+
ret = run_add_p_common(&s, ps, flags);
21602166
if (ret < 0)
21612167
goto out;
21622168

add-patch.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,22 @@ enum add_p_mode {
5353
ADD_P_WORKTREE,
5454
};
5555

56+
enum add_p_flags {
57+
/* Disallow "editing" hunks. */
58+
ADD_P_DISALLOW_EDIT = (1 << 0),
59+
};
60+
5661
int run_add_p(struct repository *r, enum add_p_mode mode,
5762
struct interactive_options *opts, const char *revision,
58-
const struct pathspec *ps);
63+
const struct pathspec *ps,
64+
unsigned flags);
5965

6066
int run_add_p_index(struct repository *r,
6167
struct index_state *index,
6268
const char *index_file,
6369
struct interactive_options *opts,
6470
const char *revision,
65-
const struct pathspec *ps);
71+
const struct pathspec *ps,
72+
unsigned flags);
6673

6774
#endif

builtin/add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ int interactive_add(struct repository *repo,
172172
prefix, argv);
173173

174174
if (patch)
175-
ret = !!run_add_p(repo, ADD_P_ADD, interactive_opts, NULL, &pathspec);
175+
ret = !!run_add_p(repo, ADD_P_ADD, interactive_opts, NULL, &pathspec, 0);
176176
else
177177
ret = !!run_add_i(repo, &pathspec, interactive_opts);
178178

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ static int checkout_paths(const struct checkout_opts *opts,
563563
BUG("either flag must have been set, worktree=%d, index=%d",
564564
opts->checkout_worktree, opts->checkout_index);
565565
return !!run_add_p(the_repository, patch_mode, &interactive_opts,
566-
rev, &opts->pathspec);
566+
rev, &opts->pathspec, 0);
567567
}
568568

569569
repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);

builtin/reset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ int cmd_reset(int argc,
438438
die(_("options '%s' and '%s' cannot be used together"), "--patch", "--{hard,mixed,soft}");
439439
trace2_cmd_mode("patch-interactive");
440440
update_ref_status = !!run_add_p(the_repository, ADD_P_RESET,
441-
&interactive_opts, rev, &pathspec);
441+
&interactive_opts, rev, &pathspec, 0);
442442
goto cleanup;
443443
} else {
444444
if (interactive_opts.context != -1)

builtin/stash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
13311331
old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
13321332
setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
13331333

1334-
ret = !!run_add_p(the_repository, ADD_P_STASH, interactive_opts, NULL, ps);
1334+
ret = !!run_add_p(the_repository, ADD_P_STASH, interactive_opts, NULL, ps, 0);
13351335

13361336
the_repository->index_file = old_repo_index_file;
13371337
if (old_index_env && *old_index_env)

0 commit comments

Comments
 (0)