commit: give correct advice for empty commit during a rebase

In dcb500dc16 (cherry-pick/revert: advise using --skip, 2019-07-02),
`git commit` learned to suggest to run `git cherry-pick --skip` when
trying to cherry-pick an empty patch.

However, it was overlooked that there are more conditions than just a
`git cherry-pick` when this advice is printed (which originally
suggested the neutral `git reset`): the same can happen during a rebase.

Let's suggest the correct command, even during a rebase.

While at it, we adjust more places in `builtin/commit.c` that
incorrectly assumed that the presence of a `CHERRY_PICK_HEAD` meant that
surely this must be a `cherry-pick` in progress.

Note: we take pains to handle the situation when a user runs a `git
cherry-pick` _during_ a rebase. This is quite valid (e.g. in an `exec`
line in an interactive rebase). On the other hand, it is not possible to
run a rebase during a cherry-pick, meaning: if both `rebase-merge/` and
`sequencer/` exist or CHERRY_PICK_HEAD and REBASE_HEAD point to the same
commit , we still want to advise to use `git cherry-pick --skip`.

Original-patch-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Phillip Wood
2019-12-06 16:06:12 +00:00
committed by Junio C Hamano
parent 901ba7b1ef
commit 430b75f720
5 changed files with 114 additions and 19 deletions

View File

@ -1429,9 +1429,19 @@ out:
return res;
}
static int write_rebase_head(struct object_id *oid)
{
if (update_ref("rebase", "REBASE_HEAD", oid,
NULL, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR))
return error(_("could not update %s"), "REBASE_HEAD");
return 0;
}
static int do_commit(struct repository *r,
const char *msg_file, const char *author,
struct replay_opts *opts, unsigned int flags)
struct replay_opts *opts, unsigned int flags,
struct object_id *oid)
{
int res = 1;
@ -1456,8 +1466,12 @@ static int do_commit(struct repository *r,
return res;
}
}
if (res == 1)
if (res == 1) {
if (is_rebase_i(opts) && oid)
if (write_rebase_head(oid))
return -1;
return run_git_commit(r, msg_file, opts, flags);
}
return res;
}
@ -1945,7 +1959,8 @@ static int do_pick_commit(struct repository *r,
flags |= ALLOW_EMPTY;
if (!opts->no_commit) {
if (author || command == TODO_REVERT || (flags & AMEND_MSG))
res = do_commit(r, msg_file, author, opts, flags);
res = do_commit(r, msg_file, author, opts, flags,
commit? &commit->object.oid : NULL);
else
res = error(_("unable to parse commit author"));
*check_todo = !!(flags & EDIT_MSG);
@ -2964,9 +2979,7 @@ static int make_patch(struct repository *r,
p = short_commit_name(commit);
if (write_message(p, strlen(p), rebase_path_stopped_sha(), 1) < 0)
return -1;
if (update_ref("rebase", "REBASE_HEAD", &commit->object.oid,
NULL, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR))
res |= error(_("could not update %s"), "REBASE_HEAD");
res |= write_rebase_head(&commit->object.oid);
strbuf_addf(&buf, "%s/patch", get_dir(opts));
memset(&log_tree_opt, 0, sizeof(log_tree_opt));
@ -5316,8 +5329,18 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
int sequencer_determine_whence(struct repository *r, enum commit_whence *whence)
{
if (file_exists(git_path_cherry_pick_head(r))) {
*whence = file_exists(git_path_seq_dir()) ?
FROM_CHERRY_PICK_MULTI : FROM_CHERRY_PICK_SINGLE;
struct object_id cherry_pick_head, rebase_head;
if (file_exists(git_path_seq_dir()))
*whence = FROM_CHERRY_PICK_MULTI;
if (file_exists(rebase_path()) &&
!get_oid("REBASE_HEAD", &rebase_head) &&
!get_oid("CHERRY_PICK_HEAD", &cherry_pick_head) &&
oideq(&rebase_head, &cherry_pick_head))
*whence = FROM_REBASE_PICK;
else
*whence = FROM_CHERRY_PICK_SINGLE;
return 1;
}