Merge branch 'bl/cherry-pick-empty'

Allow git-cherry-pick(1) to automatically drop redundant commits via
a new `--empty` option, similar to the `--empty` options for
git-rebase(1) and git-am(1). Includes a soft deprecation of
`--keep-redundant-commits` as well as some related docs changes and
sequencer code cleanup.

* bl/cherry-pick-empty:
  cherry-pick: add `--empty` for more robust redundant commit handling
  cherry-pick: enforce `--keep-redundant-commits` incompatibility
  sequencer: do not require `allow_empty` for redundant commit options
  sequencer: handle unborn branch with `--allow-empty`
  rebase: update `--empty=ask` to `--empty=stop`
  docs: clean up `--empty` formatting in git-rebase(1) and git-am(1)
  docs: address inaccurate `--empty` default with `--exec`
This commit is contained in:
Junio C Hamano
2024-04-03 10:56:20 -07:00
10 changed files with 285 additions and 67 deletions

View File

@ -787,29 +787,42 @@ static struct object_id *get_cache_tree_oid(struct index_state *istate)
static int is_index_unchanged(struct repository *r)
{
struct object_id head_oid, *cache_tree_oid;
const struct object_id *head_tree_oid;
struct commit *head_commit;
struct index_state *istate = r->index;
const char *head_name;
if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
return error(_("could not resolve HEAD commit"));
if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) {
/* Check to see if this is an unborn branch */
head_name = resolve_ref_unsafe("HEAD",
RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
&head_oid, NULL);
if (!head_name ||
!starts_with(head_name, "refs/heads/") ||
!is_null_oid(&head_oid))
return error(_("could not resolve HEAD commit"));
head_tree_oid = the_hash_algo->empty_tree;
} else {
head_commit = lookup_commit(r, &head_oid);
head_commit = lookup_commit(r, &head_oid);
/*
* If head_commit is NULL, check_commit, called from
* lookup_commit, would have indicated that head_commit is not
* a commit object already. repo_parse_commit() will return failure
* without further complaints in such a case. Otherwise, if
* the commit is invalid, repo_parse_commit() will complain. So
* there is nothing for us to say here. Just return failure.
*/
if (repo_parse_commit(r, head_commit))
return -1;
/*
* If head_commit is NULL, check_commit, called from
* lookup_commit, would have indicated that head_commit is not
* a commit object already. repo_parse_commit() will return failure
* without further complaints in such a case. Otherwise, if
* the commit is invalid, repo_parse_commit() will complain. So
* there is nothing for us to say here. Just return failure.
*/
if (repo_parse_commit(r, head_commit))
return -1;
head_tree_oid = get_commit_tree_oid(head_commit);
}
if (!(cache_tree_oid = get_cache_tree_oid(istate)))
return -1;
return oideq(cache_tree_oid, get_commit_tree_oid(head_commit));
return oideq(cache_tree_oid, head_tree_oid);
}
static int write_author_script(const char *message)
@ -1736,34 +1749,25 @@ static int allow_empty(struct repository *r,
int index_unchanged, originally_empty;
/*
* Four cases:
* For a commit that is initially empty, allow_empty determines if it
* should be kept or not
*
* (1) we do not allow empty at all and error out.
*
* (2) we allow ones that were initially empty, and
* just drop the ones that become empty
*
* (3) we allow ones that were initially empty, but
* halt for the ones that become empty;
*
* (4) we allow both.
* For a commit that becomes empty, keep_redundant_commits and
* drop_redundant_commits determine whether the commit should be kept or
* dropped. If neither is specified, halt.
*/
if (!opts->allow_empty)
return 0; /* let "git commit" barf as necessary */
index_unchanged = is_index_unchanged(r);
if (index_unchanged < 0)
return index_unchanged;
if (!index_unchanged)
return 0; /* we do not have to say --allow-empty */
if (opts->keep_redundant_commits)
return 1;
originally_empty = is_original_commit_empty(commit);
if (originally_empty < 0)
return originally_empty;
if (originally_empty)
return opts->allow_empty;
else if (opts->keep_redundant_commits)
return 1;
else if (opts->drop_redundant_commits)
return 2;
@ -2943,6 +2947,9 @@ static int populate_opts_cb(const char *key, const char *value,
else if (!strcmp(key, "options.allow-empty-message"))
opts->allow_empty_message =
git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
else if (!strcmp(key, "options.drop-redundant-commits"))
opts->drop_redundant_commits =
git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
else if (!strcmp(key, "options.keep-redundant-commits"))
opts->keep_redundant_commits =
git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
@ -3487,6 +3494,9 @@ static int save_opts(struct replay_opts *opts)
if (opts->allow_empty_message)
res |= git_config_set_in_file_gently(opts_file,
"options.allow-empty-message", "true");
if (opts->drop_redundant_commits)
res |= git_config_set_in_file_gently(opts_file,
"options.drop-redundant-commits", "true");
if (opts->keep_redundant_commits)
res |= git_config_set_in_file_gently(opts_file,
"options.keep-redundant-commits", "true");