]> git.proxmox.com Git - libgit2.git/commitdiff
revwalk: refuse push of non-commit objects
authorMichael Schubert <schu@schu.io>
Mon, 27 Aug 2012 08:51:01 +0000 (10:51 +0200)
committerMichael Schubert <schu@schu.io>
Mon, 27 Aug 2012 09:52:32 +0000 (11:52 +0200)
Check the type of the pushed object immediately instead of starting the
walk and failing in between.

src/revwalk.c
tests-clar/revwalk/basic.c

index 9dff283f5ea27deddbfde58f59c49878145e16ec..8b0e93baf4be5289c27a33bc74e89ef5b7ae5d61 100644 (file)
@@ -264,12 +264,7 @@ static int commit_parse(git_revwalk *walk, commit_object *commit)
 
        if ((error = git_odb_read(&obj, walk->odb, &commit->oid)) < 0)
                return error;
-
-       if (obj->raw.type != GIT_OBJ_COMMIT) {
-               git_odb_object_free(obj);
-               giterr_set(GITERR_INVALID, "Failed to parse commit. Object is no commit object");
-               return -1;
-       }
+       assert(obj->raw.type == GIT_OBJ_COMMIT);
 
        error = commit_quick_parse(walk, commit, &obj->raw);
        git_odb_object_free(obj);
@@ -515,8 +510,21 @@ static int process_commit_parents(git_revwalk *walk, commit_object *commit)
 
 static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting)
 {
+       git_object *obj;
+       git_otype type;
        commit_object *commit;
 
+       if (git_object_lookup(&obj, walk->repo, oid, GIT_OBJ_ANY) < 0)
+               return -1;
+
+       type = git_object_type(obj);
+       git_object_free(obj);
+
+       if (type != GIT_OBJ_COMMIT) {
+               giterr_set(GITERR_INVALID, "Object is no commit object");
+               return -1;
+       }
+
        commit = commit_lookup(walk, oid);
        if (commit == NULL)
                return -1; /* error already reported by failed lookup */
index 6f3c1c06d1d4a7345dc710c23696aa7ca83af1e6..126ca7d9f848fb279f081f796893af2ba5c4e11f 100644 (file)
@@ -179,3 +179,11 @@ void test_revwalk_basic__push_head_hide_ref_nobase(void)
        /* git log HEAD --oneline --not refs/heads/packed | wc -l => 7 */
        cl_assert(i == 7);
 }
+
+void test_revwalk_basic__disallow_non_commit(void)
+{
+       git_oid oid;
+
+       cl_git_pass(git_oid_fromstr(&oid, "521d87c1ec3aef9824daf6d96cc0ae3710766d91"));
+       cl_git_fail(git_revwalk_push(_walk, &oid));
+}