Skip to content

Commit ec65760

Browse files
committed
Merge branch 'pw/rebase-i-show-HEAD-to-reword' into jch
"git rebase -i" showed a wrong HEAD while "reword" open the editor. * pw/rebase-i-show-HEAD-to-reword: sequencer: simplify root commit creation rebase -i: check for updated todo after squash and reword rebase -i: always update HEAD before rewording
2 parents 4302c69 + b0a3186 commit ec65760

5 files changed

+75
-103
lines changed

sequencer.c

Lines changed: 39 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -868,34 +868,6 @@ static char *get_author(const char *message)
868868
return NULL;
869869
}
870870

871-
/* Read author-script and return an ident line (author <email> timestamp) */
872-
static const char *read_author_ident(struct strbuf *buf)
873-
{
874-
struct strbuf out = STRBUF_INIT;
875-
char *name, *email, *date;
876-
877-
if (read_author_script(rebase_path_author_script(),
878-
&name, &email, &date, 0))
879-
return NULL;
880-
881-
/* validate date since fmt_ident() will die() on bad value */
882-
if (parse_date(date, &out)){
883-
warning(_("invalid date format '%s' in '%s'"),
884-
date, rebase_path_author_script());
885-
strbuf_release(&out);
886-
return NULL;
887-
}
888-
889-
strbuf_reset(&out);
890-
strbuf_addstr(&out, fmt_ident(name, email, WANT_AUTHOR_IDENT, date, 0));
891-
strbuf_swap(buf, &out);
892-
strbuf_release(&out);
893-
free(name);
894-
free(email);
895-
free(date);
896-
return buf->buf;
897-
}
898-
899871
static const char staged_changes_advice[] =
900872
N_("you have staged changes in your working tree\n"
901873
"If these changes are meant to be squashed into the previous commit, run:\n"
@@ -953,47 +925,6 @@ static int run_git_commit(struct repository *r,
953925
{
954926
struct child_process cmd = CHILD_PROCESS_INIT;
955927

956-
if ((flags & CREATE_ROOT_COMMIT) && !(flags & AMEND_MSG)) {
957-
struct strbuf msg = STRBUF_INIT, script = STRBUF_INIT;
958-
const char *author = NULL;
959-
struct object_id root_commit, *cache_tree_oid;
960-
int res = 0;
961-
962-
if (is_rebase_i(opts)) {
963-
author = read_author_ident(&script);
964-
if (!author) {
965-
strbuf_release(&script);
966-
return -1;
967-
}
968-
}
969-
970-
if (!defmsg)
971-
BUG("root commit without message");
972-
973-
if (!(cache_tree_oid = get_cache_tree_oid(r->index)))
974-
res = -1;
975-
976-
if (!res)
977-
res = strbuf_read_file(&msg, defmsg, 0);
978-
979-
if (res <= 0)
980-
res = error_errno(_("could not read '%s'"), defmsg);
981-
else
982-
res = commit_tree(msg.buf, msg.len, cache_tree_oid,
983-
NULL, &root_commit, author,
984-
opts->gpg_sign);
985-
986-
strbuf_release(&msg);
987-
strbuf_release(&script);
988-
if (!res) {
989-
update_ref(NULL, "CHERRY_PICK_HEAD", &root_commit, NULL,
990-
REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR);
991-
res = update_ref(NULL, "HEAD", &root_commit, NULL, 0,
992-
UPDATE_REFS_MSG_ON_ERR);
993-
}
994-
return res < 0 ? error(_("writing root commit")) : 0;
995-
}
996-
997928
cmd.git_cmd = 1;
998929

999930
if (is_rebase_i(opts) && read_env_script(&cmd.env_array)) {
@@ -1377,7 +1308,7 @@ static int try_to_commit(struct repository *r,
13771308
struct object_id *oid)
13781309
{
13791310
struct object_id tree;
1380-
struct commit *current_head;
1311+
struct commit *current_head = NULL;
13811312
struct commit_list *parents = NULL;
13821313
struct commit_extra_header *extra = NULL;
13831314
struct strbuf err = STRBUF_INIT;
@@ -1412,7 +1343,8 @@ static int try_to_commit(struct repository *r,
14121343
}
14131344
parents = copy_commit_list(current_head->parents);
14141345
extra = read_commit_extra_headers(current_head, exclude_gpgsig);
1415-
} else if (current_head) {
1346+
} else if (current_head &&
1347+
(!(flags & CREATE_ROOT_COMMIT) || (flags & AMEND_MSG))) {
14161348
commit_list_insert(current_head, &parents);
14171349
}
14181350

@@ -1489,8 +1421,7 @@ static int do_commit(struct repository *r,
14891421
{
14901422
int res = 1;
14911423

1492-
if (!(flags & EDIT_MSG) && !(flags & VERIFY_MSG) &&
1493-
!(flags & CREATE_ROOT_COMMIT)) {
1424+
if (!(flags & EDIT_MSG) && !(flags & VERIFY_MSG)) {
14941425
struct object_id oid;
14951426
struct strbuf sb = STRBUF_INIT;
14961427

@@ -1774,7 +1705,7 @@ static int do_pick_commit(struct repository *r,
17741705
enum todo_command command,
17751706
struct commit *commit,
17761707
struct replay_opts *opts,
1777-
int final_fixup)
1708+
int final_fixup, int *check_todo)
17781709
{
17791710
unsigned int flags = opts->edit ? EDIT_MSG : 0;
17801711
const char *msg_file = opts->edit ? NULL : git_path_merge_msg(r);
@@ -1784,7 +1715,7 @@ static int do_pick_commit(struct repository *r,
17841715
char *author = NULL;
17851716
struct commit_message msg = { NULL, NULL, NULL, NULL };
17861717
struct strbuf msgbuf = STRBUF_INIT;
1787-
int res, unborn = 0, allow;
1718+
int res, unborn = 0, reword = 0, allow;
17881719

17891720
if (opts->no_commit) {
17901721
/*
@@ -1854,7 +1785,7 @@ static int do_pick_commit(struct repository *r,
18541785
opts);
18551786
if (res || command != TODO_REWORD)
18561787
goto leave;
1857-
flags |= EDIT_MSG | AMEND_MSG | VERIFY_MSG;
1788+
reword = 1;
18581789
msg_file = NULL;
18591790
goto fast_forward_edit;
18601791
}
@@ -1912,7 +1843,7 @@ static int do_pick_commit(struct repository *r,
19121843
}
19131844

19141845
if (command == TODO_REWORD)
1915-
flags |= EDIT_MSG | VERIFY_MSG;
1846+
reword = 1;
19161847
else if (is_fixup(command)) {
19171848
if (update_squash_messages(r, command, commit, opts))
19181849
return -1;
@@ -1996,13 +1927,21 @@ static int do_pick_commit(struct repository *r,
19961927
} else if (allow)
19971928
flags |= ALLOW_EMPTY;
19981929
if (!opts->no_commit) {
1999-
fast_forward_edit:
20001930
if (author || command == TODO_REVERT || (flags & AMEND_MSG))
20011931
res = do_commit(r, msg_file, author, opts, flags);
20021932
else
20031933
res = error(_("unable to parse commit author"));
1934+
*check_todo = !!(flags & EDIT_MSG);
1935+
if (!res && reword) {
1936+
fast_forward_edit:
1937+
res = run_git_commit(r, NULL, opts, EDIT_MSG |
1938+
VERIFY_MSG | AMEND_MSG |
1939+
(flags & ALLOW_EMPTY));
1940+
*check_todo = 1;
1941+
}
20041942
}
20051943

1944+
20061945
if (!res && final_fixup) {
20071946
unlink(rebase_path_fixup_msg());
20081947
unlink(rebase_path_squash_msg());
@@ -3827,6 +3766,7 @@ static int pick_commits(struct repository *r,
38273766
while (todo_list->current < todo_list->nr) {
38283767
struct todo_item *item = todo_list->items + todo_list->current;
38293768
const char *arg = todo_item_get_arg(todo_list, item);
3769+
int check_todo = 0;
38303770

38313771
if (save_todo(todo_list, opts))
38323772
return -1;
@@ -3865,7 +3805,8 @@ static int pick_commits(struct repository *r,
38653805
command_to_string(item->command), NULL),
38663806
1);
38673807
res = do_pick_commit(r, item->command, item->commit,
3868-
opts, is_final_fixup(todo_list));
3808+
opts, is_final_fixup(todo_list),
3809+
&check_todo);
38693810
if (is_rebase_i(opts) && res < 0) {
38703811
/* Reschedule */
38713812
advise(_(rescheduled_advice),
@@ -3922,7 +3863,6 @@ static int pick_commits(struct repository *r,
39223863
} else if (item->command == TODO_EXEC) {
39233864
char *end_of_arg = (char *)(arg + item->arg_len);
39243865
int saved = *end_of_arg;
3925-
struct stat st;
39263866

39273867
if (!opts->verbose)
39283868
term_clear_line();
@@ -3933,17 +3873,8 @@ static int pick_commits(struct repository *r,
39333873
if (res) {
39343874
if (opts->reschedule_failed_exec)
39353875
reschedule = 1;
3936-
} else if (stat(get_todo_path(opts), &st))
3937-
res = error_errno(_("could not stat '%s'"),
3938-
get_todo_path(opts));
3939-
else if (match_stat_data(&todo_list->stat, &st)) {
3940-
/* Reread the todo file if it has changed. */
3941-
todo_list_release(todo_list);
3942-
if (read_populate_todo(r, todo_list, opts))
3943-
res = -1; /* message was printed */
3944-
/* `current` will be incremented below */
3945-
todo_list->current = -1;
39463876
}
3877+
check_todo = 1;
39473878
} else if (item->command == TODO_LABEL) {
39483879
if ((res = do_label(r, arg, item->arg_len)))
39493880
reschedule = 1;
@@ -3979,6 +3910,20 @@ static int pick_commits(struct repository *r,
39793910
item->commit,
39803911
arg, item->arg_len,
39813912
opts, res, 0);
3913+
} else if (check_todo && !res) {
3914+
struct stat st;
3915+
3916+
if (stat(get_todo_path(opts), &st)) {
3917+
res = error_errno(_("could not stat '%s'"),
3918+
get_todo_path(opts));
3919+
} else if (match_stat_data(&todo_list->stat, &st)) {
3920+
/* Reread the todo file if it has changed. */
3921+
todo_list_release(todo_list);
3922+
if (read_populate_todo(r, todo_list, opts))
3923+
res = -1; /* message was printed */
3924+
/* `current` will be incremented below */
3925+
todo_list->current = -1;
3926+
}
39823927
}
39833928

39843929
todo_list->current++;
@@ -4305,9 +4250,12 @@ static int single_pick(struct repository *r,
43054250
struct commit *cmit,
43064251
struct replay_opts *opts)
43074252
{
4253+
int check_todo;
4254+
43084255
setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
43094256
return do_pick_commit(r, opts->action == REPLAY_PICK ?
4310-
TODO_PICK : TODO_REVERT, cmit, opts, 0);
4257+
TODO_PICK : TODO_REVERT, cmit, opts, 0,
4258+
&check_todo);
43114259
}
43124260

43134261
int sequencer_pick_revisions(struct repository *r,

t/t3404-rebase-interactive.sh

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,16 +1014,26 @@ test_expect_success 'rebase -i --root fixup root commit' '
10141014
test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
10151015
'
10161016

1017-
test_expect_success 'rebase -i --root reword root commit' '
1017+
test_expect_success 'rebase -i --root reword original root commit' '
10181018
test_when_finished "test_might_fail git rebase --abort" &&
1019-
git checkout -b reword-root-branch master &&
1019+
git checkout -b reword-original-root-branch master &&
10201020
set_fake_editor &&
10211021
FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
10221022
git rebase -i --root &&
10231023
git show HEAD^ | grep "A changed" &&
10241024
test -z "$(git show -s --format=%p HEAD^)"
10251025
'
10261026

1027+
test_expect_success 'rebase -i --root reword new root commit' '
1028+
test_when_finished "test_might_fail git rebase --abort" &&
1029+
git checkout -b reword-now-root-branch master &&
1030+
set_fake_editor &&
1031+
FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \
1032+
git rebase -i --root &&
1033+
git show HEAD^ | grep "C changed" &&
1034+
test -z "$(git show -s --format=%p HEAD^)"
1035+
'
1036+
10271037
test_expect_success 'rebase -i --root when root has untracked file conflict' '
10281038
test_when_finished "reset_rebase" &&
10291039
git checkout -b failing-root-pick A &&
@@ -1052,7 +1062,7 @@ test_expect_success 'rebase -i --root reword root when root has untracked file c
10521062
'
10531063

10541064
test_expect_success C_LOCALE_OUTPUT 'rebase --edit-todo does not work on non-interactive rebase' '
1055-
git checkout reword-root-branch &&
1065+
git checkout reword-original-root-branch &&
10561066
git reset --hard &&
10571067
git checkout conflict-branch &&
10581068
set_fake_editor &&

t/t3429-rebase-edit-todo.sh

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
test_description='rebase should reread the todo file if an exec modifies it'
44

55
. ./test-lib.sh
6+
. "$TEST_DIRECTORY"/lib-rebase.sh
7+
8+
test_expect_success 'setup' '
9+
test_commit first file &&
10+
test_commit second file &&
11+
test_commit third file
12+
'
613

714
test_expect_success 'rebase exec modifies rebase-todo' '
8-
test_commit initial &&
915
todo=.git/rebase-merge/git-rebase-todo &&
1016
git rebase HEAD -x "echo exec touch F >>$todo" &&
1117
test -e F
@@ -33,4 +39,17 @@ test_expect_success SHA1 'loose object cache vs re-reading todo list' '
3339
git rebase HEAD -x "./append-todo.sh 5 6"
3440
'
3541

42+
test_expect_success 'todo is re-read after reword and squash' '
43+
write_script reword-editor.sh <<-\EOS &&
44+
GIT_SEQUENCE_EDITOR="echo \"exec echo $(cat file) >>actual\" >>" \
45+
git rebase --edit-todo
46+
EOS
47+
48+
test_write_lines first third >expected &&
49+
set_fake_editor &&
50+
GIT_SEQUENCE_EDITOR="$EDITOR" FAKE_LINES="reword 1 squash 2 fixup 3" \
51+
GIT_EDITOR=./reword-editor.sh git rebase -i --root third &&
52+
test_cmp expected actual
53+
'
54+
3655
test_done

t/t7505-prepare-commit-msg-hook.sh

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,7 @@ test_rebase () {
241241
git add b &&
242242
git rebase --continue
243243
) &&
244-
if test "$mode" = -p # reword amended after pick
245-
then
246-
n=18
247-
else
248-
n=17
249-
fi &&
250-
git log --pretty=%s -g -n$n HEAD@{1} >actual &&
244+
git log --pretty=%s -g -n18 HEAD@{1} >actual &&
251245
test_cmp "$TEST_DIRECTORY/t7505/expected-rebase${mode:--i}" actual
252246
'
253247
}

t/t7505/expected-rebase-i

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ message (no editor) [edit rebase-10]
77
message [fixup rebase-9]
88
message (no editor) [fixup rebase-8]
99
message (no editor) [squash rebase-7]
10-
message [reword rebase-6]
10+
HEAD [reword rebase-6]
11+
message (no editor) [reword rebase-6]
1112
message [squash rebase-5]
1213
message (no editor) [fixup rebase-4]
1314
message (no editor) [pick rebase-3]

0 commit comments

Comments
 (0)