]> git.proxmox.com Git - libgit2.git/commitdiff
reflog: handle the birth of a branch
authorCarlos Martín Nieto <cmn@dwim.me>
Wed, 19 Mar 2014 15:30:37 +0000 (16:30 +0100)
committerCarlos Martín Nieto <cmn@dwim.me>
Wed, 19 Mar 2014 15:52:20 +0000 (16:52 +0100)
The reflog append function was overzealous in its checking. When passed
an old and new ids, it should not do any checking, but just serialize
the data to a reflog entry.

src/refdb_fs.c
tests/repo/head.c

index f2edf0c566f6b1b906a1213e1e821df584bda21d..905113226593e8cdfae318a86920b5fa610d2a41 100644 (file)
@@ -992,18 +992,24 @@ out:
 static int maybe_append_head(refdb_fs_backend *backend, const git_reference *ref, const git_signature *who, const char *message)
 {
        int error;
-       git_oid old_id;
+       git_oid old_id = {{0}};
        git_reference *head;
 
-       error = git_reference_name_to_id(&old_id, backend->repo, ref->name);
-       if (!git_branch_is_head(ref))
-               return 0;
+       /* if we can't resolve, we use {0}*40 as old id */
+       git_reference_name_to_id(&old_id, backend->repo, ref->name);
 
        if ((error = git_reference_lookup(&head, backend->repo, "HEAD")) < 0)
                return error;
 
+       if (git_reference_type(head) == GIT_REF_OID)
+               goto cleanup;
+
+       if (strcmp(git_reference_symbolic_target(head), ref->name))
+               goto cleanup;
+
        error = reflog_append(backend, head, &old_id, git_reference_target(ref), who, message);
 
+cleanup:
        git_reference_free(head);
        return error;
 }
@@ -1595,23 +1601,23 @@ static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, co
                        return error;
        }
 
-       if (is_symbolic) {
-               error = git_reference_name_to_id(&new_id, repo, git_reference_symbolic_target(ref));
-               if (error < 0 && error != GIT_ENOTFOUND)
-                       return error;
-               /* detaching HEAD does not create an entry */
-               if (error == GIT_ENOTFOUND)
-                       return 0;
+       if (new) {
+               git_oid_cpy(&new_id, new);
+       } else {
+               if (!is_symbolic) {
+                       git_oid_cpy(&new_id, git_reference_target(ref));
+               } else {
+                       error = git_reference_name_to_id(&new_id, repo, git_reference_symbolic_target(ref));
+                       if (error < 0 && error != GIT_ENOTFOUND)
+                               return error;
+                       /* detaching HEAD does not create an entry */
+                       if (error == GIT_ENOTFOUND)
+                               return 0;
 
-               giterr_clear();
+                       giterr_clear();
+               }
        }
 
-
-       if (new)
-               git_oid_cpy(&new_id, new);
-       else if (!is_symbolic)
-               git_oid_cpy(&new_id, git_reference_target(ref));
-
        if ((error = serialize_reflog_entry(&buf, &old_id, &new_id, who, message)) < 0)
                goto cleanup;
 
index 130ed858858748eb680ad6c1b15e1710db5de370..d68a5a61e1488f08798e8a326b6b28794468ecf2 100644 (file)
@@ -368,3 +368,51 @@ void test_repo_head__set_to_current_target(void)
        git_signature_free(sig);
 
 }
+
+void test_repo_head__branch_birth(void)
+{
+       git_signature *sig;
+       git_oid id;
+       git_tree *tree;
+       git_reference *ref;
+       const char *msg;
+       git_reflog *log;
+       size_t nentries, nentries_after;
+
+       cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE));
+       nentries = git_reflog_entrycount(log);
+       git_reflog_free(log);
+
+       cl_git_pass(git_signature_now(&sig, "me", "foo@example.com"));
+
+       cl_git_pass(git_repository_head(&ref, repo));
+       cl_git_pass(git_reference_peel((git_object **) &tree, ref, GIT_OBJ_TREE));
+       git_reference_free(ref);
+
+       msg = "message 1";
+       cl_git_pass(git_repository_set_head(repo, "refs/heads/orphan", sig, msg));
+
+       cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE));
+       nentries_after = git_reflog_entrycount(log);
+       git_reflog_free(log);
+
+       cl_assert_equal_i(nentries, nentries_after);
+
+       msg = "message 2";
+       cl_git_pass(git_commit_create(&id, repo, "HEAD", sig, sig, NULL, msg, tree, 0, NULL));
+
+       git_tree_free(tree);
+
+       cl_git_pass(git_reflog_read(&log, repo, "refs/heads/orphan"));
+       cl_assert_equal_i(1, git_reflog_entrycount(log));
+       git_reflog_free(log);
+
+       cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE));
+       nentries_after = git_reflog_entrycount(log);
+       git_reflog_free(log);
+
+       cl_assert_equal_i(nentries + 1, nentries_after);
+
+       git_signature_free(sig);
+
+}