Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9855a1b

Browse files
committed
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.
1 parent a059009 commit 9855a1b

File tree

12 files changed

+90
-33
lines changed

12 files changed

+90
-33
lines changed

include/git2/commit.h

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,6 @@ GIT_EXTERN(int) git_commit_header_field(git_buf *out, const git_commit *commit,
260260
*
261261
* @param repo Repository where to store the commit
262262
*
263-
* @param update_ref If not NULL, name of the reference that
264-
* will be updated to point to this commit. If the reference
265-
* is not direct, it will be resolved to a direct reference.
266-
* Use "HEAD" to update the HEAD of the current branch and
267-
* make it point to this commit. If the reference doesn't
268-
* exist yet, it will be created. If it does exist, the first
269-
* parent must be the tip of this branch.
270-
*
271263
* @param author Signature with author and author time of commit
272264
*
273265
* @param committer Signature with committer and * commit time of commit
@@ -295,6 +287,26 @@ GIT_EXTERN(int) git_commit_header_field(git_buf *out, const git_commit *commit,
295287
* the given reference will be updated to point to it
296288
*/
297289
GIT_EXTERN(int) git_commit_create(
290+
git_oid *id,
291+
git_repository *repo,
292+
const git_signature *author,
293+
const git_signature *committer,
294+
const char *message_encoding,
295+
const char *message,
296+
const git_tree *tree,
297+
size_t parent_count,
298+
const git_commit *parents[]);
299+
300+
/**
301+
* Create a commit and update a reference
302+
*
303+
* @param update_ref reference to update with the new commit. The
304+
* current tip of the reference must be the first commit in the parent
305+
* list.
306+
*
307+
* @see git_commit_create
308+
*/
309+
GIT_EXTERN(int) git_commit_create_on(
298310
git_oid *id,
299311
git_repository *repo,
300312
const char *update_ref,
@@ -306,6 +318,22 @@ GIT_EXTERN(int) git_commit_create(
306318
size_t parent_count,
307319
const git_commit *parents[]);
308320

321+
/**
322+
* Create a commit and update the current branch
323+
*
324+
* @see git_commit_create
325+
*/
326+
GIT_EXTERN(int) git_commit_create_on_head(
327+
git_oid *id,
328+
git_repository *repo,
329+
const git_signature *author,
330+
const git_signature *committer,
331+
const char *message_encoding,
332+
const char *message,
333+
const git_tree *tree,
334+
size_t parent_count,
335+
const git_commit *parents[]);
336+
309337
/**
310338
* Create new commit in the repository using a variable argument list.
311339
*

src/commit.c

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ static const git_oid *commit_parent_from_array(size_t curr, void *payload)
211211
return git_commit_id(commit);
212212
}
213213

214-
int git_commit_create(
214+
int git_commit_create_on(
215215
git_oid *id,
216216
git_repository *repo,
217217
const char *update_ref,
@@ -233,6 +233,38 @@ int git_commit_create(
233233
commit_parent_from_array, &data);
234234
}
235235

236+
int git_commit_create_on_head(
237+
git_oid *id,
238+
git_repository *repo,
239+
const git_signature *author,
240+
const git_signature *committer,
241+
const char *message_encoding,
242+
const char *message,
243+
const git_tree *tree,
244+
size_t parent_count,
245+
const git_commit *parents[])
246+
{
247+
return git_commit_create_on(id, repo, GIT_HEAD_FILE,
248+
author, committer, message_encoding, message,
249+
tree, parent_count, parents);
250+
}
251+
252+
int git_commit_create(
253+
git_oid *id,
254+
git_repository *repo,
255+
const git_signature *author,
256+
const git_signature *committer,
257+
const char *message_encoding,
258+
const char *message,
259+
const git_tree *tree,
260+
size_t parent_count,
261+
const git_commit *parents[])
262+
{
263+
return git_commit_create_on(id, repo, NULL,
264+
author, committer, message_encoding, message,
265+
tree, parent_count, parents);
266+
}
267+
236268
static const git_oid *commit_parent_for_amend(size_t curr, void *payload)
237269
{
238270
const git_commit *commit_to_amend = payload;
@@ -364,9 +396,9 @@ int git_commit_create_fromstate(
364396
if ((error = git_tree_lookup(&tree, repo, &tree_id)) < 0)
365397
goto cleanup;
366398

367-
error = git_commit_create(id, repo, GIT_HEAD_FILE,
368-
author, committer, message_encoding, message,
369-
tree, commits.length, (const git_commit **) commits.contents);
399+
error = git_commit_create_on(id, repo, GIT_HEAD_FILE,
400+
author, committer, message_encoding, message,
401+
tree, commits.length, (const git_commit **) commits.contents);
370402

371403
cleanup:
372404
git_tree_free(tree);

src/notes.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,9 @@ static int note_write(git_oid *out,
297297
if (out)
298298
git_oid_cpy(out, &oid);
299299

300-
error = git_commit_create(&oid, repo, notes_ref, author, committer,
301-
NULL, GIT_NOTES_DEFAULT_MSG_ADD,
302-
tree, *parents == NULL ? 0 : 1, (const git_commit **) parents);
300+
error = git_commit_create_on(&oid, repo, notes_ref, author, committer,
301+
NULL, GIT_NOTES_DEFAULT_MSG_ADD,
302+
tree, *parents == NULL ? 0 : 1, (const git_commit **) parents);
303303

304304
cleanup:
305305
git_tree_free(tree);
@@ -377,7 +377,7 @@ static int note_remove(git_repository *repo,
377377
remove_note_in_tree_eexists_cb, remove_note_in_tree_enotfound_cb)) < 0)
378378
goto cleanup;
379379

380-
error = git_commit_create(&oid, repo, notes_ref, author, committer,
380+
error = git_commit_create_on(&oid, repo, notes_ref, author, committer,
381381
NULL, GIT_NOTES_DEFAULT_MSG_RM,
382382
tree_after_removal,
383383
*parents == NULL ? 0 : 1,

src/rebase.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ static int rebase_commit_merge(
888888
message = git_commit_message(current_commit);
889889
}
890890

891-
if ((error = git_commit_create(commit_id, rebase->repo, NULL, author,
891+
if ((error = git_commit_create(commit_id, rebase->repo, author,
892892
committer, message_encoding, message, tree, 1,
893893
(const git_commit **)&head_commit)) < 0 ||
894894
(error = git_commit_lookup(&commit, rebase->repo, commit_id)) < 0 ||

src/stash.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ static int commit_index(
133133
if ((error = git_commit_create(
134134
&i_commit_oid,
135135
git_index_owner(index),
136-
NULL,
137136
stasher,
138137
stasher,
139138
NULL,
@@ -275,7 +274,6 @@ static int commit_untracked(
275274
if ((error = git_commit_create(
276275
&u_commit_oid,
277276
git_index_owner(index),
278-
NULL,
279277
stasher,
280278
stasher,
281279
NULL,
@@ -378,7 +376,6 @@ static int commit_worktree(
378376
error = git_commit_create(
379377
w_commit_oid,
380378
git_index_owner(index),
381-
NULL,
382379
stasher,
383380
stasher,
384381
NULL,

tests/checkout/tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ void test_checkout_tree__case_changing_rename(void)
11841184

11851185
cl_git_pass(git_signature_new(&signature, "Renamer", "[email protected]", time(NULL), 0));
11861186

1187-
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));
1187+
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));
11881188

11891189
cl_assert(git_path_isfile("testrepo/readme"));
11901190
if (case_sensitive)

tests/cherrypick/workdir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void test_cherrypick_workdir__automerge(void)
7777

7878
cl_git_pass(git_index_write_tree(&cherrypicked_tree_oid, repo_index));
7979
cl_git_pass(git_tree_lookup(&cherrypicked_tree, repo, &cherrypicked_tree_oid));
80-
cl_git_pass(git_commit_create(&cherrypicked_oid, repo, "HEAD", signature, signature, NULL,
80+
cl_git_pass(git_commit_create_on(&cherrypicked_oid, repo, "HEAD", signature, signature, NULL,
8181
"Cherry picked!", cherrypicked_tree, 1, (const git_commit **)&head));
8282

8383
cl_assert(merge_test_index(repo_index, merge_index_entries + i * 3, 3));

tests/commit/commit.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ void test_commit_commit__create_unexisting_update_ref(void)
3535
cl_git_pass(git_signature_now(&s, "alice", "[email protected]"));
3636

3737
cl_git_fail(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
38-
cl_git_pass(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s,
39-
NULL, "some msg", tree, 1, (const git_commit **) &commit));
38+
cl_git_pass(git_commit_create_on(&oid, _repo, "refs/heads/foo/bar", s, s,
39+
NULL, "some msg", tree, 1, (const git_commit **) &commit));
4040

4141
/* fail because the parent isn't the tip of the branch anymore */
42-
cl_git_fail(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s,
43-
NULL, "some msg", tree, 1, (const git_commit **) &commit));
42+
cl_git_fail(git_commit_create_on(&oid, _repo, "refs/heads/foo/bar", s, s,
43+
NULL, "some msg", tree, 1, (const git_commit **) &commit));
4444

4545
cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
4646
cl_assert_equal_oid(&oid, git_reference_target(ref));

tests/diff/rename.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ void test_diff_rename__test_small_files(void)
406406
cl_git_pass(git_index_write_tree(&oid, index));
407407
cl_git_pass(git_tree_lookup(&commit_tree, g_repo, &oid));
408408
cl_git_pass(git_signature_new(&signature, "Rename", "[email protected]", 1404157834, 0));
409-
cl_git_pass(git_commit_create(&oid, g_repo, "HEAD", signature, signature, NULL, "Test commit", commit_tree, 1, (const git_commit**)&head_commit));
409+
cl_git_pass(git_commit_create_on(&oid, g_repo, "HEAD", signature, signature, NULL, "Test commit", commit_tree, 1, (const git_commit**)&head_commit));
410410

411411
cl_git_mkfile("renames/copy.txt", "Hello World!\n");
412412
cl_git_rmfile("renames/small.txt");

tests/repo/head.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ void test_repo_head__branch_birth(void)
391391
cl_assert_equal_i(nentries, nentries_after);
392392

393393
msg = "message 2";
394-
cl_git_pass(git_commit_create(&id, repo, "HEAD", sig, sig, NULL, msg, tree, 0, NULL));
394+
cl_git_pass(git_commit_create_on(&id, repo, "HEAD", sig, sig, NULL, msg, tree, 0, NULL));
395395

396396
git_tree_free(tree);
397397

@@ -449,7 +449,7 @@ void test_repo_head__symref_chain(void)
449449
cl_assert_equal_i(nentries_master, entrycount(repo, "refs/heads/master"));
450450

451451
msg = "message 2";
452-
cl_git_pass(git_commit_create(&id, repo, "HEAD", sig, sig, NULL, msg, tree, 0, NULL));
452+
cl_git_pass(git_commit_create_on(&id, repo, "HEAD", sig, sig, NULL, msg, tree, 0, NULL));
453453
git_tree_free(tree);
454454

455455
cl_assert_equal_i(1, entrycount(repo, "refs/heads/foo"));

tests/revert/workdir.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void test_revert_workdir__again(void)
189189
cl_git_pass(git_tree_lookup(&reverted_tree, repo, &reverted_tree_oid));
190190

191191
cl_git_pass(git_signature_new(&signature, "Reverter", "[email protected]", time(NULL), 0));
192-
cl_git_pass(git_commit_create(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&orig_head));
192+
cl_git_pass(git_commit_create_on(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&orig_head));
193193

194194
cl_git_pass(git_revert(repo, orig_head, NULL));
195195
cl_assert(merge_test_index(repo_index, merge_index_entries, 4));
@@ -239,7 +239,7 @@ void test_revert_workdir__again_after_automerge(void)
239239
cl_git_pass(git_tree_lookup(&reverted_tree, repo, &reverted_tree_oid));
240240

241241
cl_git_pass(git_signature_new(&signature, "Reverter", "[email protected]", time(NULL), 0));
242-
cl_git_pass(git_commit_create(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&head));
242+
cl_git_pass(git_commit_create_on(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&head));
243243

244244
cl_git_pass(git_revert(repo, commit, NULL));
245245
cl_assert(merge_test_index(repo_index, second_revert_entries, 6));
@@ -288,7 +288,7 @@ void test_revert_workdir__again_after_edit(void)
288288
cl_git_pass(git_tree_lookup(&reverted_tree, repo, &reverted_tree_oid));
289289

290290
cl_git_pass(git_signature_new(&signature, "Reverter", "[email protected]", time(NULL), 0));
291-
cl_git_pass(git_commit_create(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&orig_head));
291+
cl_git_pass(git_commit_create_on(&reverted_commit_oid, repo, "HEAD", signature, signature, NULL, "Reverted!", reverted_tree, 1, (const git_commit **)&orig_head));
292292

293293
cl_git_pass(git_revert(repo, commit, NULL));
294294
cl_assert(merge_test_index(repo_index, merge_index_entries, 4));

tests/status/submodules.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ void test_status_submodules__entry_but_dir_tracked(void)
502502
cl_git_pass(git_index_write_tree(&tree_id, index));
503503
cl_git_pass(git_signature_now(&sig, "Sloppy Submoduler", "[email protected]"));
504504
cl_git_pass(git_tree_lookup(&tree, repo, &tree_id));
505-
cl_git_pass(git_commit_create(&commit_id, repo, NULL, sig, sig, NULL, "message", tree, 0, NULL));
505+
cl_git_pass(git_commit_create(&commit_id, repo, sig, sig, NULL, "message", tree, 0, NULL));
506506
cl_git_pass(git_reference_create(&ref, repo, "refs/heads/master", &commit_id, 1, "commit: foo"));
507507
git_reference_free(ref);
508508
git_signature_free(sig);

0 commit comments

Comments
 (0)