summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2014-12-18 09:18:30 +0100
committerCarlos Martín Nieto <cmn@dwim.me>2018-03-22 16:23:20 +0100
commitc3d0633800bb6edcd88f4a042dbeffd235253eeb (patch)
treec4b2c8c24fb01711d8dead3707b934c5e8730308
parent8f212f64a0cb01abab35b3333b0d405cb9a1639c (diff)
downloadlibgit2-c3d0633800bb6edcd88f4a042dbeffd235253eeb.tar.gz
commit: provide functions with and without a reference name
Currently git_commit_create() takes on creating a commit and updating a reference. Provide a better interface by splitting up each of the concerns into named functions for each. git_commit_create() will only create the commit, it will not modify any reference. git_commit_create_on() takes a reference name to update and git_commit_create_on_head() is a convenience function to update HEAD.
-rw-r--r--include/git2/commit.h44
-rw-r--r--src/commit.c40
-rw-r--r--src/notes.c8
-rw-r--r--src/rebase.c2
-rw-r--r--src/stash.c3
-rw-r--r--tests/checkout/tree.c2
-rw-r--r--tests/cherrypick/workdir.c2
-rw-r--r--tests/commit/commit.c12
-rw-r--r--tests/diff/rename.c2
-rw-r--r--tests/odb/freshen.c2
-rw-r--r--tests/refs/reflog/messages.c6
-rw-r--r--tests/reset/hard.c4
-rw-r--r--tests/revert/workdir.c6
-rw-r--r--tests/revwalk/basic.c2
-rw-r--r--tests/status/submodules.c2
15 files changed, 97 insertions, 40 deletions
diff --git a/include/git2/commit.h b/include/git2/commit.h
index e184e1cc6..356b24688 100644
--- a/include/git2/commit.h
+++ b/include/git2/commit.h
@@ -294,14 +294,6 @@ GIT_EXTERN(int) git_commit_extract_signature(git_buf *signature, git_buf *signed
*
* @param repo Repository where to store the commit
*
- * @param update_ref If not NULL, name of the reference that
- * will be updated to point to this commit. If the reference
- * is not direct, it will be resolved to a direct reference.
- * Use "HEAD" to update the HEAD of the current branch and
- * make it point to this commit. If the reference doesn't
- * exist yet, it will be created. If it does exist, the first
- * parent must be the tip of this branch.
- *
* @param author Signature with author and author time of commit
*
* @param committer Signature with committer and * commit time of commit
@@ -331,6 +323,26 @@ GIT_EXTERN(int) git_commit_extract_signature(git_buf *signature, git_buf *signed
GIT_EXTERN(int) git_commit_create(
git_oid *id,
git_repository *repo,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message_encoding,
+ const char *message,
+ const git_tree *tree,
+ size_t parent_count,
+ const git_commit *parents[]);
+
+/**
+ * Create a commit and update a reference
+ *
+ * @param update_ref reference to update with the new commit. The
+ * current tip of the reference must be the first commit in the parent
+ * list.
+ *
+ * @see git_commit_create
+ */
+GIT_EXTERN(int) git_commit_create_on(
+ git_oid *id,
+ git_repository *repo,
const char *update_ref,
const git_signature *author,
const git_signature *committer,
@@ -341,6 +353,22 @@ GIT_EXTERN(int) git_commit_create(
const git_commit *parents[]);
/**
+ * Create a commit and update the current branch
+ *
+ * @see git_commit_create
+ */
+GIT_EXTERN(int) git_commit_create_on_head(
+ git_oid *id,
+ git_repository *repo,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message_encoding,
+ const char *message,
+ const git_tree *tree,
+ size_t parent_count,
+ const git_commit *parents[]);
+
+/**
* Create new commit in the repository using a variable argument list.
*
* The message will **not** be cleaned up automatically. You can do that
diff --git a/src/commit.c b/src/commit.c
index 8f923c336..724e014c9 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -291,7 +291,7 @@ static const git_oid *commit_parent_from_array(size_t curr, void *payload)
return git_commit_id(commit);
}
-int git_commit_create(
+int git_commit_create_on(
git_oid *id,
git_repository *repo,
const char *update_ref,
@@ -313,6 +313,38 @@ int git_commit_create(
commit_parent_from_array, &data, false);
}
+int git_commit_create_on_head(
+ git_oid *id,
+ git_repository *repo,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message_encoding,
+ const char *message,
+ const git_tree *tree,
+ size_t parent_count,
+ const git_commit *parents[])
+{
+ return git_commit_create_on(id, repo, GIT_HEAD_FILE,
+ author, committer, message_encoding, message,
+ tree, parent_count, parents);
+}
+
+int git_commit_create(
+ git_oid *id,
+ git_repository *repo,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message_encoding,
+ const char *message,
+ const git_tree *tree,
+ size_t parent_count,
+ const git_commit *parents[])
+{
+ return git_commit_create_on(id, repo, NULL,
+ author, committer, message_encoding, message,
+ tree, parent_count, parents);
+}
+
static const git_oid *commit_parent_for_amend(size_t curr, void *payload)
{
const git_commit *commit_to_amend = payload;
@@ -454,9 +486,9 @@ int git_commit_create_fromstate(
if ((error = git_tree_lookup(&tree, repo, &tree_id)) < 0)
goto cleanup;
- error = git_commit_create(id, repo, GIT_HEAD_FILE,
- author, committer, message_encoding, message,
- tree, commits.length, (const git_commit **) commits.contents);
+ error = git_commit_create_on(id, repo, GIT_HEAD_FILE,
+ author, committer, message_encoding, message,
+ tree, commits.length, (const git_commit **) commits.contents);
cleanup:
git_tree_free(tree);
diff --git a/src/notes.c b/src/notes.c
index f63ef3667..2386bfd6d 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -300,9 +300,9 @@ static int note_write(
git_oid_cpy(notes_blob_out, &oid);
- error = git_commit_create(&oid, repo, notes_ref, author, committer,
- NULL, GIT_NOTES_DEFAULT_MSG_ADD,
- tree, *parents == NULL ? 0 : 1, (const git_commit **) parents);
+ error = git_commit_create_on(&oid, repo, notes_ref, author, committer,
+ NULL, GIT_NOTES_DEFAULT_MSG_ADD,
+ tree, *parents == NULL ? 0 : 1, (const git_commit **) parents);
if (notes_commit_out)
git_oid_cpy(notes_commit_out, &oid);
@@ -385,7 +385,7 @@ static int note_remove(
remove_note_in_tree_eexists_cb, remove_note_in_tree_enotfound_cb)) < 0)
goto cleanup;
- error = git_commit_create(&oid, repo, notes_ref, author, committer,
+ error = git_commit_create_on(&oid, repo, notes_ref, author, committer,
NULL, GIT_NOTES_DEFAULT_MSG_RM,
tree_after_removal,
*parents == NULL ? 0 : 1,
diff --git a/src/rebase.c b/src/rebase.c
index 3be751254..f8ab0df9b 100644
--- a/src/rebase.c
+++ b/src/rebase.c
@@ -978,7 +978,7 @@ static int rebase_commit__create(
message = git_commit_message(current_commit);
}
- if ((error = git_commit_create(&commit_id, rebase->repo, NULL, author,
+ if ((error = git_commit_create(&commit_id, rebase->repo, author,
committer, message_encoding, message, tree, 1,
(const git_commit **)&parent_commit)) < 0 ||
(error = git_commit_lookup(&commit, rebase->repo, &commit_id)) < 0)
diff --git a/src/stash.c b/src/stash.c
index 8a8c57a83..2505d7da0 100644
--- a/src/stash.c
+++ b/src/stash.c
@@ -135,7 +135,6 @@ static int commit_index(
if ((error = git_commit_create(
&i_commit_oid,
git_index_owner(index),
- NULL,
stasher,
stasher,
NULL,
@@ -277,7 +276,6 @@ static int commit_untracked(
if ((error = git_commit_create(
&u_commit_oid,
git_index_owner(index),
- NULL,
stasher,
stasher,
NULL,
@@ -380,7 +378,6 @@ static int commit_worktree(
error = git_commit_create(
w_commit_oid,
git_index_owner(index),
- NULL,
stasher,
stasher,
NULL,
diff --git a/tests/checkout/tree.c b/tests/checkout/tree.c
index a7e29b3db..fb61b4ec4 100644
--- a/tests/checkout/tree.c
+++ b/tests/checkout/tree.c
@@ -1231,7 +1231,7 @@ void test_checkout_tree__case_changing_rename(void)
cl_git_pass(git_signature_new(&signature, "Renamer", "rename@contoso.com", time(NULL), 0));
- cl_git_pass(git_commit_create(&commit_id, g_repo, "refs/heads/dir", signature, signature, NULL, "case-changing rename", tree, 1, (const git_commit **)&dir_commit));
+ cl_git_pass(git_commit_create_on(&commit_id, g_repo, "refs/heads/dir", signature, signature, NULL, "case-changing rename", tree, 1, (const git_commit **)&dir_commit));
cl_assert(git_path_isfile("testrepo/readme"));
if (case_sensitive)
diff --git a/tests/cherrypick/workdir.c b/tests/cherrypick/workdir.c
index 2b45f5a33..9da0d5624 100644
--- a/tests/cherrypick/workdir.c
+++ b/tests/cherrypick/workdir.c
@@ -77,7 +77,7 @@ void test_cherrypick_workdir__automerge(void)
cl_git_pass(git_index_write_tree(&cherrypicked_tree_oid, repo_index));
cl_git_pass(git_tree_lookup(&cherrypicked_tree, repo, &cherrypicked_tree_oid));
- cl_git_pass(git_commit_create(&cherrypicked_oid, repo, "HEAD", signature, signature, NULL,
+ cl_git_pass(git_commit_create_on(&cherrypicked_oid, repo, "HEAD", signature, signature, NULL,
"Cherry picked!", cherrypicked_tree, 1, (const git_commit **)&head));
cl_assert(merge_test_index(repo_index, merge_index_entries + i * 3, 3));
diff --git a/tests/commit/commit.c b/tests/commit/commit.c
index b03169bc8..152b88f1a 100644
--- a/tests/commit/commit.c
+++ b/tests/commit/commit.c
@@ -35,12 +35,12 @@ void test_commit_commit__create_unexisting_update_ref(void)
cl_git_pass(git_signature_now(&s, "alice", "alice@example.com"));
cl_git_fail(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
- cl_git_pass(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s,
- NULL, "some msg", tree, 1, (const git_commit **) &commit));
+ cl_git_pass(git_commit_create_on(&oid, _repo, "refs/heads/foo/bar", s, s,
+ NULL, "some msg", tree, 1, (const git_commit **) &commit));
/* fail because the parent isn't the tip of the branch anymore */
- cl_git_fail(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s,
- NULL, "some msg", tree, 1, (const git_commit **) &commit));
+ cl_git_fail(git_commit_create_on(&oid, _repo, "refs/heads/foo/bar", s, s,
+ NULL, "some msg", tree, 1, (const git_commit **) &commit));
cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
cl_assert_equal_oid(&oid, git_reference_target(ref));
@@ -68,7 +68,7 @@ void test_commit_commit__create_initial_commit(void)
cl_git_pass(git_signature_now(&s, "alice", "alice@example.com"));
cl_git_fail(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
- cl_git_pass(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s,
+ cl_git_pass(git_commit_create_on(&oid, _repo, "refs/heads/foo/bar", s, s,
NULL, "initial commit", tree, 0, NULL));
cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
@@ -99,7 +99,7 @@ void test_commit_commit__create_initial_commit_parent_not_current(void)
cl_git_pass(git_reference_name_to_id(&original_oid, _repo, "HEAD"));
- cl_git_fail(git_commit_create(&oid, _repo, "HEAD", s, s,
+ cl_git_fail(git_commit_create_on(&oid, _repo, "HEAD", s, s,
NULL, "initial commit", tree, 0, NULL));
cl_git_pass(git_reference_name_to_id(&oid, _repo, "HEAD"));
diff --git a/tests/diff/rename.c b/tests/diff/rename.c
index ddc1d5d78..99000d156 100644
--- a/tests/diff/rename.c
+++ b/tests/diff/rename.c
@@ -423,7 +423,7 @@ void test_diff_rename__test_small_files(void)
cl_git_pass(git_index_write_tree(&oid, index));
cl_git_pass(git_tree_lookup(&commit_tree, g_repo, &oid));
cl_git_pass(git_signature_new(&signature, "Rename", "rename@example.com", 1404157834, 0));
- cl_git_pass(git_commit_create(&oid, g_repo, "HEAD", signature, signature, NULL, "Test commit", commit_tree, 1, (const git_commit**)&head_commit));
+ cl_git_pass(git_commit_create_on(&oid, g_repo, "HEAD", signature, signature, NULL, "Test commit", commit_tree, 1, (const git_commit**)&head_commit));
cl_git_mkfile("renames/copy.txt", "Hello World!\n");
cl_git_rmfile("renames/small.txt");
diff --git a/tests/odb/freshen.c b/tests/odb/freshen.c
index 9d3cf51dc..55070659a 100644
--- a/tests/odb/freshen.c
+++ b/tests/odb/freshen.c
@@ -123,7 +123,7 @@ void test_odb_freshen__tree_during_commit(void)
cl_git_pass(git_signature_new(&signature,
"Refresher", "refresher@example.com", 1488547083, 0));
- cl_git_pass(git_commit_create(&commit_id, repo, NULL,
+ cl_git_pass(git_commit_create(&commit_id, repo,
signature, signature, NULL, "New commit pointing to old tree",
tree, 1, (const git_commit **)&parent));
diff --git a/tests/refs/reflog/messages.c b/tests/refs/reflog/messages.c
index a7a77666b..055376110 100644
--- a/tests/refs/reflog/messages.c
+++ b/tests/refs/reflog/messages.c
@@ -164,7 +164,7 @@ void test_refs_reflog_messages__branch_birth(void)
cl_assert_equal_i(nentries, nentries_after);
msg = "message 2";
- cl_git_pass(git_commit_create(&id, g_repo, "HEAD", sig, sig, NULL, msg, tree, 0, NULL));
+ cl_git_pass(git_commit_create_on(&id, g_repo, "HEAD", sig, sig, NULL, msg, tree, 0, NULL));
cl_assert_equal_i(1, reflog_entrycount(g_repo, "refs/heads/orphan"));
@@ -203,7 +203,7 @@ void test_refs_reflog_messages__commit_on_symbolic_ref_updates_head_reflog(void)
cl_assert_equal_i(nentries_master, reflog_entrycount(g_repo, "refs/heads/master"));
msg = "message 2";
- cl_git_pass(git_commit_create(&id, g_repo, "HEAD", sig, sig, NULL, msg, tree, 0, NULL));
+ cl_git_pass(git_commit_create_on(&id, g_repo, "HEAD", sig, sig, NULL, msg, tree, 0, NULL));
cl_assert_equal_i(1, reflog_entrycount(g_repo, "refs/heads/foo"));
cl_assert_equal_i(nentries_head + 1, reflog_entrycount(g_repo, GIT_HEAD_FILE));
@@ -239,7 +239,7 @@ void test_refs_reflog_messages__show_merge_for_merge_commits(void)
cl_git_pass(git_commit_tree(&tree, b1_commit));
- cl_git_pass(git_commit_create(&merge_commit_oid,
+ cl_git_pass(git_commit_create_on(&merge_commit_oid,
g_repo, "HEAD", s, s, NULL,
"Merge commit", tree,
2, (const struct git_commit **) parent_commits));
diff --git a/tests/reset/hard.c b/tests/reset/hard.c
index 69ef41e50..ed902abae 100644
--- a/tests/reset/hard.c
+++ b/tests/reset/hard.c
@@ -265,7 +265,7 @@ void test_reset_hard__switch_file_to_dir(void)
cl_git_pass(git_index_clear(idx));
cl_git_pass(git_tree_lookup(&tree, repo, &src_tree_id));
- cl_git_pass(git_commit_create(&src_id, repo, NULL, sig, sig, NULL, "foo", tree, 0, NULL));
+ cl_git_pass(git_commit_create(&src_id, repo, sig, sig, NULL, "foo", tree, 0, NULL));
git_tree_free(tree);
/* Create the new tree */
@@ -276,7 +276,7 @@ void test_reset_hard__switch_file_to_dir(void)
cl_git_pass(git_index_write_tree_to(&tgt_tree_id, idx, repo));
cl_git_pass(git_tree_lookup(&tree, repo, &tgt_tree_id));
- cl_git_pass(git_commit_create(&tgt_id, repo, NULL, sig, sig, NULL, "foo", tree, 0, NULL));
+ cl_git_pass(git_commit_create(&tgt_id, repo, sig, sig, NULL, "foo", tree, 0, NULL));
git_tree_free(tree);
git_index_free(idx);
git_signature_free(sig);
diff --git a/tests/revert/workdir.c b/tests/revert/workdir.c
index 802819c75..ebb26f19e 100644
--- a/tests/revert/workdir.c
+++ b/tests/revert/workdir.c
@@ -189,7 +189,7 @@ void test_revert_workdir__again(void)
cl_git_pass(git_tree_lookup(&reverted_tree, repo, &reverted_tree_oid));
cl_git_pass(git_signature_new(&signature, "Reverter", "reverter@example.org", time(NULL), 0));
- cl_git_pass(git_commit_create(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&orig_head));
+ cl_git_pass(git_commit_create_on(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&orig_head));
cl_git_pass(git_revert(repo, orig_head, NULL));
cl_assert(merge_test_index(repo_index, merge_index_entries, 4));
@@ -239,7 +239,7 @@ void test_revert_workdir__again_after_automerge(void)
cl_git_pass(git_tree_lookup(&reverted_tree, repo, &reverted_tree_oid));
cl_git_pass(git_signature_new(&signature, "Reverter", "reverter@example.org", time(NULL), 0));
- cl_git_pass(git_commit_create(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&head));
+ cl_git_pass(git_commit_create_on(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&head));
cl_git_pass(git_revert(repo, commit, NULL));
cl_assert(merge_test_index(repo_index, second_revert_entries, 6));
@@ -288,7 +288,7 @@ void test_revert_workdir__again_after_edit(void)
cl_git_pass(git_tree_lookup(&reverted_tree, repo, &reverted_tree_oid));
cl_git_pass(git_signature_new(&signature, "Reverter", "reverter@example.org", time(NULL), 0));
- cl_git_pass(git_commit_create(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&orig_head));
+ cl_git_pass(git_commit_create_on(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&orig_head));
cl_git_pass(git_revert(repo, commit, NULL));
cl_assert(merge_test_index(repo_index, merge_index_entries, 4));
diff --git a/tests/revwalk/basic.c b/tests/revwalk/basic.c
index 6a23701f3..2ccf7eeb5 100644
--- a/tests/revwalk/basic.c
+++ b/tests/revwalk/basic.c
@@ -490,7 +490,7 @@ void test_revwalk_basic__big_timestamp(void)
cl_git_pass(git_signature_new(&sig, "Joe", "joe@example.com", 2399662595, 0));
cl_git_pass(git_commit_tree(&tree, tip));
- cl_git_pass(git_commit_create(&id, _repo, "HEAD", sig, sig, NULL, "some message", tree, 1,
+ cl_git_pass(git_commit_create_on(&id, _repo, "HEAD", sig, sig, NULL, "some message", tree, 1,
(const git_commit **)&tip));
cl_git_pass(git_revwalk_push_head(_walk));
diff --git a/tests/status/submodules.c b/tests/status/submodules.c
index 33c9e5ab4..412c227db 100644
--- a/tests/status/submodules.c
+++ b/tests/status/submodules.c
@@ -502,7 +502,7 @@ void test_status_submodules__entry_but_dir_tracked(void)
cl_git_pass(git_index_write_tree(&tree_id, index));
cl_git_pass(git_signature_now(&sig, "Sloppy Submoduler", "sloppy@example.com"));
cl_git_pass(git_tree_lookup(&tree, repo, &tree_id));
- cl_git_pass(git_commit_create(&commit_id, repo, NULL, sig, sig, NULL, "message", tree, 0, NULL));
+ cl_git_pass(git_commit_create(&commit_id, repo, sig, sig, NULL, "message", tree, 0, NULL));
cl_git_pass(git_reference_create(&ref, repo, "refs/heads/master", &commit_id, 1, "commit: foo"));
git_reference_free(ref);
git_signature_free(sig);