@matheusbsbs / matheus-bagliano / commits / cf10f9fdd5
Fork 0

builtin-commit: use reduce_heads() only when appropriate

Since commit 6bb6b034 (builtin-commit: use commit_tree(), 2008-09-10), builtin-commit performs a reduce_heads() unconditionally. However, it's not always needed, and in some cases even harmful. reduce_heads() is not needed for the initial commit or for an "ordinary" commit, because they don't have any or have only one parent, respectively. reduce_heads() must be avoided when 'git commit' is run after a 'git merge --no-ff --no-commit', otherwise it will turn the non-fast-forward merge into fast-forward. For the same reason, reduce_heads() must be avoided when amending such a merge commit. To resolve this issue, 'git merge' will write info about whether fast-forward is allowed or not to $GIT_DIR/MERGE_MODE. Based on this info, 'git commit' will only perform reduce_heads() when it's committing a merge and fast-forward is enabled. Also add test cases to ensure that non-fast-forward merges are committed and amended properly. Signed-off-by: Miklos Vajna <vmiklos@frugalware.org> Signed-off-by: SZEDER Gábor <szeder@ira.uka.de> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Miklos Vajna committed Oct 3, 2008 at 14:04 UTC cf10f9fdd5e673a163847f0e931ce9731507e03b
4 files changed +41 -2
branch.c
+1
@@ -168,5 +168,6 @@ void remove_branch_state(void)
168 unlink(git_path("MERGE_HEAD"));
169 unlink(git_path("MERGE_RR"));
170 unlink(git_path("MERGE_MSG"));
171 + unlink(git_path("MERGE_MODE"));
172 unlink(git_path("SQUASH_MSG"));
173 }
builtin-commit.c
+14 -2
@@ -937,6 +937,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
937 unsigned char commit_sha1[20];
938 struct ref_lock *ref_lock;
939 struct commit_list *parents = NULL, **pptr = &parents;
940 + struct stat statbuf;
941 + int allow_fast_forward = 1;
942
943 git_config(git_commit_config, NULL);
944
@@ -951,6 +953,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
953 return 1;
954 }
955
956 + strbuf_init(&sb, 0);
957 /* Determine parents */
958 if (initial_commit) {
959 reflog_msg = "commit (initial)";
@@ -984,14 +987,22 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
987 }
988 fclose(fp);
989 strbuf_release(&m);
990 + if (!stat(git_path("MERGE_MODE"), &statbuf)) {
991 + if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
992 + die("could not read MERGE_MODE: %s",
993 + strerror(errno));
994 + if (!strcmp(sb.buf, "no-ff"))
995 + allow_fast_forward = 0;
996 + }
997 + if (allow_fast_forward)
998 + parents = reduce_heads(parents);
999 } else {
1000 reflog_msg = "commit";
1001 pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
1002 }
991 - parents = reduce_heads(parents);
1003
1004 /* Finally, get the commit message */
994 - strbuf_init(&sb, 0);
1005 + strbuf_reset(&sb);
1006 if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
1007 rollback_index_files();
1008 die("could not read commit message");
@@ -1040,6 +1051,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1051
1052 unlink(git_path("MERGE_HEAD"));
1053 unlink(git_path("MERGE_MSG"));
1054 + unlink(git_path("MERGE_MODE"));
1055 unlink(git_path("SQUASH_MSG"));
1056
1057 if (commit_index_files())
builtin-merge.c
+10
@@ -180,6 +180,7 @@ static void drop_save(void)
180 {
181 unlink(git_path("MERGE_HEAD"));
182 unlink(git_path("MERGE_MSG"));
183 + unlink(git_path("MERGE_MODE"));
184 }
185
186 static void save_state(void)
@@ -1210,6 +1211,15 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1211 merge_msg.len)
1212 die("Could not write to %s", git_path("MERGE_MSG"));
1213 close(fd);
1214 + fd = open(git_path("MERGE_MODE"), O_WRONLY | O_CREAT | O_TRUNC, 0666);
1215 + if (fd < 0)
1216 + die("Could open %s for writing", git_path("MERGE_MODE"));
1217 + strbuf_reset(&buf);
1218 + if (!allow_fast_forward)
1219 + strbuf_addf(&buf, "no-ff");
1220 + if (write_in_full(fd, buf.buf, buf.len) != buf.len)
1221 + die("Could not write to %s", git_path("MERGE_MODE"));
1222 + close(fd);
1223 }
1224
1225 if (merge_was_ok) {
t/t7600-merge.sh
+16
@@ -511,4 +511,20 @@ test_expect_success 'in-index merge' '
511
512 test_debug 'gitk --all'
513
514 +test_expect_success 'merge --no-ff --no-commit && commit' '
515 + git reset --hard c0 &&
516 + git merge --no-ff --no-commit c1 &&
517 + EDITOR=: git commit &&
518 + verify_parents $c0 $c1
519 +'
520 +
521 +test_debug 'gitk --all'
522 +
523 +test_expect_success 'amending no-ff merge commit' '
524 + EDITOR=: git commit --amend &&
525 + verify_parents $c0 $c1
526 +'
527 +
528 +test_debug 'gitk --all'
529 +
530 test_done