commit: avoid race when creating orphan commits
authorMichael Haggerty <mhagger@alum.mit.edu>
Tue, 17 Feb 2015 17:00:18 +0000 (18:00 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 17 Feb 2015 19:24:20 +0000 (11:24 -0800)
If HEAD doesn't point at anything during the initial check, then we
should make sure that it *still* doesn't point at anything when we are
ready to update the reference. Otherwise, another process might commit
while we are working (e.g., while we are waiting for the user to edit
the commit message) and we will silently overwrite it.

This fixes a failing test in t7516.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Reviewed-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/commit.c
t/t7516-commit-races.sh
index 8afb0ff5e044719b1d2266231d1d2881e4d93f21..682f922c736dbaaa918a9f27c07487e3f15ec286 100644 (file)
@@ -1766,7 +1766,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
        if (!transaction ||
            ref_transaction_update(transaction, "HEAD", sha1,
                                   current_head
-                                  ? current_head->object.sha1 : NULL,
+                                  ? current_head->object.sha1 : null_sha1,
                                   0, sb.buf, &err) ||
            ref_transaction_commit(transaction, &err)) {
                rollback_index_files();
index ed04d1c425e2a9ca5812e5ad7c8b2e3f45755fab..f2ce14e9071c0a3b071f53186c2d311103883443 100755 (executable)
@@ -3,7 +3,7 @@
 test_description='git commit races'
 . ./test-lib.sh
 
-test_expect_failure 'race to create orphan commit' '
+test_expect_success 'race to create orphan commit' '
        write_script hare-editor <<-\EOF &&
        git commit --allow-empty -m hare
        EOF