diff options
author | Carlos Martín Nieto <cmn@dwim.me> | 2014-04-09 14:08:22 +0200 |
---|---|---|
committer | Carlos Martín Nieto <cmn@dwim.me> | 2014-04-30 00:41:37 +0200 |
commit | 217c029b54e8f1574ae6bc71c4b25533ecff3b6a (patch) | |
tree | f8caf68f1027883b5370e057b29946433d100dd8 /tests/object | |
parent | 041336e6bd9dbd22db29f2294e5db8645efdd46b (diff) | |
download | libgit2-217c029b54e8f1574ae6bc71c4b25533ecff3b6a.tar.gz |
commit: safer commit creation with reference updatecmn/commit-create-safe
The current version of the commit creation and amend function are unsafe
to use when passing the update_ref parameter, as they do not check that
the reference at the moment of update points to what the user expects.
Make sure that we're moving history forward when we ask the library to
update the reference for us by checking that the first parent of the new
commit is the current value of the reference. We also make sure that the
ref we're updating hasn't moved between the read and the write.
Similarly, when amending a commit, make sure that the current tip of the
branch is the commit we're amending.
Diffstat (limited to 'tests/object')
-rw-r--r-- | tests/object/commit/commitstagedfile.c | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/tests/object/commit/commitstagedfile.c b/tests/object/commit/commitstagedfile.c index 3e7b3c02c..9758ea9a2 100644 --- a/tests/object/commit/commitstagedfile.c +++ b/tests/object/commit/commitstagedfile.c @@ -175,6 +175,10 @@ void test_object_commit_commitstagedfile__amend_commit(void) cl_git_pass(git_commit_amend( &new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", NULL)); + /* fail because the commit isn't the tip of the branch anymore */ + cl_git_fail(git_commit_amend( + &new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", NULL)); + cl_git_pass(git_commit_lookup(&new_commit, repo, &new_oid)); cl_assert_equal_i(0, git_commit_parentcount(new_commit)); @@ -182,6 +186,7 @@ void test_object_commit_commitstagedfile__amend_commit(void) assert_commit_is_head(new_commit); git_commit_free(old_commit); + old_commit = new_commit; /* let's amend the tree of that last commit */ @@ -192,6 +197,10 @@ void test_object_commit_commitstagedfile__amend_commit(void) cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid)); cl_assert_equal_i(2, git_tree_entrycount(tree)); + /* fail to amend on a ref which does not exist */ + cl_git_fail_with(GIT_ENOTFOUND, git_commit_amend( + &new_oid, old_commit, "refs/heads/nope", NULL, NULL, NULL, "Initial commit", tree)); + cl_git_pass(git_commit_amend( &new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", tree)); git_tree_free(tree); |