mirror of
https://github.com/git/git.git
synced 2025-12-12 20:36:24 +01:00
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>
This commit is contained in:
committed by
Junio C Hamano
parent
6b099bdd4d
commit
c63d6347c9
@@ -926,7 +926,7 @@ static int run_patch(struct add_i_state *s, const struct pathspec *ps,
|
|||||||
parse_pathspec(&ps_selected,
|
parse_pathspec(&ps_selected,
|
||||||
PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
|
PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
|
||||||
PATHSPEC_LITERAL_PATH, "", args.v);
|
PATHSPEC_LITERAL_PATH, "", args.v);
|
||||||
res = run_add_p(s->r, ADD_P_ADD, &opts, NULL, &ps_selected);
|
res = run_add_p(s->r, ADD_P_ADD, &opts, NULL, &ps_selected, 0);
|
||||||
strvec_clear(&args);
|
strvec_clear(&args);
|
||||||
clear_pathspec(&ps_selected);
|
clear_pathspec(&ps_selected);
|
||||||
}
|
}
|
||||||
|
|||||||
21
add-patch.c
21
add-patch.c
@@ -1565,7 +1565,8 @@ static bool get_first_undecided(const struct file_diff *file_diff, size_t *idx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int patch_update_file(struct add_p_state *s,
|
static int patch_update_file(struct add_p_state *s,
|
||||||
struct file_diff *file_diff)
|
struct file_diff *file_diff,
|
||||||
|
unsigned flags)
|
||||||
{
|
{
|
||||||
size_t hunk_index = 0;
|
size_t hunk_index = 0;
|
||||||
ssize_t i, undecided_previous, undecided_next, rendered_hunk_index = -1;
|
ssize_t i, undecided_previous, undecided_next, rendered_hunk_index = -1;
|
||||||
@@ -1666,7 +1667,8 @@ static int patch_update_file(struct add_p_state *s,
|
|||||||
permitted |= ALLOW_SPLIT;
|
permitted |= ALLOW_SPLIT;
|
||||||
strbuf_addstr(&s->buf, ",s");
|
strbuf_addstr(&s->buf, ",s");
|
||||||
}
|
}
|
||||||
if (hunk_index + 1 > file_diff->mode_change &&
|
if (!(flags & ADD_P_DISALLOW_EDIT) &&
|
||||||
|
hunk_index + 1 > file_diff->mode_change &&
|
||||||
!file_diff->deleted) {
|
!file_diff->deleted) {
|
||||||
permitted |= ALLOW_EDIT;
|
permitted |= ALLOW_EDIT;
|
||||||
strbuf_addstr(&s->buf, ",e");
|
strbuf_addstr(&s->buf, ",e");
|
||||||
@@ -1932,7 +1934,8 @@ soft_increment:
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int run_add_p_common(struct add_p_state *state,
|
static int run_add_p_common(struct add_p_state *state,
|
||||||
const struct pathspec *ps)
|
const struct pathspec *ps,
|
||||||
|
unsigned flags)
|
||||||
{
|
{
|
||||||
size_t binary_count = 0;
|
size_t binary_count = 0;
|
||||||
|
|
||||||
@@ -1942,7 +1945,7 @@ static int run_add_p_common(struct add_p_state *state,
|
|||||||
for (size_t i = 0; i < state->file_diff_nr; i++) {
|
for (size_t i = 0; i < state->file_diff_nr; i++) {
|
||||||
if (state->file_diff[i].binary && !state->file_diff[i].hunk_nr)
|
if (state->file_diff[i].binary && !state->file_diff[i].hunk_nr)
|
||||||
binary_count++;
|
binary_count++;
|
||||||
else if (patch_update_file(state, state->file_diff + i))
|
else if (patch_update_file(state, state->file_diff + i, flags))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1956,7 +1959,8 @@ static int run_add_p_common(struct add_p_state *state,
|
|||||||
|
|
||||||
int run_add_p(struct repository *r, enum add_p_mode mode,
|
int run_add_p(struct repository *r, enum add_p_mode mode,
|
||||||
struct interactive_options *opts, const char *revision,
|
struct interactive_options *opts, const char *revision,
|
||||||
const struct pathspec *ps)
|
const struct pathspec *ps,
|
||||||
|
unsigned flags)
|
||||||
{
|
{
|
||||||
struct add_p_state s = {
|
struct add_p_state s = {
|
||||||
.r = r,
|
.r = r,
|
||||||
@@ -2005,7 +2009,7 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = run_add_p_common(&s, ps);
|
ret = run_add_p_common(&s, ps, flags);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
@@ -2021,7 +2025,8 @@ int run_add_p_index(struct repository *r,
|
|||||||
const char *index_file,
|
const char *index_file,
|
||||||
struct interactive_options *opts,
|
struct interactive_options *opts,
|
||||||
const char *revision,
|
const char *revision,
|
||||||
const struct pathspec *ps)
|
const struct pathspec *ps,
|
||||||
|
unsigned flags)
|
||||||
{
|
{
|
||||||
struct patch_mode mode = {
|
struct patch_mode mode = {
|
||||||
.apply_args = { "--cached", NULL },
|
.apply_args = { "--cached", NULL },
|
||||||
@@ -2079,7 +2084,7 @@ int run_add_p_index(struct repository *r,
|
|||||||
|
|
||||||
interactive_config_init(&s.cfg, r, opts);
|
interactive_config_init(&s.cfg, r, opts);
|
||||||
|
|
||||||
ret = run_add_p_common(&s, ps);
|
ret = run_add_p_common(&s, ps, flags);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
|
|||||||
11
add-patch.h
11
add-patch.h
@@ -50,15 +50,22 @@ enum add_p_mode {
|
|||||||
ADD_P_WORKTREE,
|
ADD_P_WORKTREE,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum add_p_flags {
|
||||||
|
/* Disallow "editing" hunks. */
|
||||||
|
ADD_P_DISALLOW_EDIT = (1 << 0),
|
||||||
|
};
|
||||||
|
|
||||||
int run_add_p(struct repository *r, enum add_p_mode mode,
|
int run_add_p(struct repository *r, enum add_p_mode mode,
|
||||||
struct interactive_options *opts, const char *revision,
|
struct interactive_options *opts, const char *revision,
|
||||||
const struct pathspec *ps);
|
const struct pathspec *ps,
|
||||||
|
unsigned flags);
|
||||||
|
|
||||||
int run_add_p_index(struct repository *r,
|
int run_add_p_index(struct repository *r,
|
||||||
struct index_state *index,
|
struct index_state *index,
|
||||||
const char *index_file,
|
const char *index_file,
|
||||||
struct interactive_options *opts,
|
struct interactive_options *opts,
|
||||||
const char *revision,
|
const char *revision,
|
||||||
const struct pathspec *ps);
|
const struct pathspec *ps,
|
||||||
|
unsigned flags);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ int interactive_add(struct repository *repo,
|
|||||||
prefix, argv);
|
prefix, argv);
|
||||||
|
|
||||||
if (patch)
|
if (patch)
|
||||||
ret = !!run_add_p(repo, ADD_P_ADD, interactive_opts, NULL, &pathspec);
|
ret = !!run_add_p(repo, ADD_P_ADD, interactive_opts, NULL, &pathspec, 0);
|
||||||
else
|
else
|
||||||
ret = !!run_add_i(repo, &pathspec, interactive_opts);
|
ret = !!run_add_i(repo, &pathspec, interactive_opts);
|
||||||
|
|
||||||
|
|||||||
@@ -576,7 +576,7 @@ static int checkout_paths(const struct checkout_opts *opts,
|
|||||||
BUG("either flag must have been set, worktree=%d, index=%d",
|
BUG("either flag must have been set, worktree=%d, index=%d",
|
||||||
opts->checkout_worktree, opts->checkout_index);
|
opts->checkout_worktree, opts->checkout_index);
|
||||||
return !!run_add_p(the_repository, patch_mode, &interactive_opts,
|
return !!run_add_p(the_repository, patch_mode, &interactive_opts,
|
||||||
rev, &opts->pathspec);
|
rev, &opts->pathspec, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
|
repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
|
||||||
|
|||||||
@@ -436,7 +436,7 @@ int cmd_reset(int argc,
|
|||||||
die(_("options '%s' and '%s' cannot be used together"), "--patch", "--{hard,mixed,soft}");
|
die(_("options '%s' and '%s' cannot be used together"), "--patch", "--{hard,mixed,soft}");
|
||||||
trace2_cmd_mode("patch-interactive");
|
trace2_cmd_mode("patch-interactive");
|
||||||
update_ref_status = !!run_add_p(the_repository, ADD_P_RESET,
|
update_ref_status = !!run_add_p(the_repository, ADD_P_RESET,
|
||||||
&interactive_opts, rev, &pathspec);
|
&interactive_opts, rev, &pathspec, 0);
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
} else {
|
} else {
|
||||||
if (interactive_opts.context != -1)
|
if (interactive_opts.context != -1)
|
||||||
|
|||||||
@@ -1331,7 +1331,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
|
|||||||
old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
|
old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
|
||||||
setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
|
setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
|
||||||
|
|
||||||
ret = !!run_add_p(the_repository, ADD_P_STASH, interactive_opts, NULL, ps);
|
ret = !!run_add_p(the_repository, ADD_P_STASH, interactive_opts, NULL, ps, 0);
|
||||||
|
|
||||||
the_repository->index_file = old_repo_index_file;
|
the_repository->index_file = old_repo_index_file;
|
||||||
if (old_index_env && *old_index_env)
|
if (old_index_env && *old_index_env)
|
||||||
|
|||||||
Reference in New Issue
Block a user