mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 10:17:16 +08:00
GitTest: Allow a commit to not have any parents
This is the case when it is the first commit
This commit is contained in:
@ -150,11 +150,22 @@ int gj_git_commit(char *git_base_path, char *author_name, char *author_email, ch
|
|||||||
git_oid parent_id;
|
git_oid parent_id;
|
||||||
git_commit *parent_commit = NULL;
|
git_commit *parent_commit = NULL;
|
||||||
|
|
||||||
git_reference_name_to_id(&parent_id, repo, "HEAD");
|
err = git_reference_name_to_id(&parent_id, repo, "HEAD");
|
||||||
git_commit_lookup(&parent_commit, repo, &parent_id);
|
if (err)
|
||||||
|
{
|
||||||
const git_commit *parents = {parent_commit};
|
// Probably first commit
|
||||||
err = git_commit_create(&commit_id, repo, "HEAD", sig, sig, NULL, message, tree, 1, &parents);
|
err = git_commit_create(&commit_id, repo, "HEAD", sig, sig, NULL, message, tree, 0, NULL);
|
||||||
|
if (err < 0)
|
||||||
|
{
|
||||||
|
git_signature_free(sig);
|
||||||
|
git_tree_free(tree);
|
||||||
|
git_repository_free(repo);
|
||||||
|
return handle_error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
err = git_commit_lookup(&parent_commit, repo, &parent_id);
|
||||||
if (err < 0)
|
if (err < 0)
|
||||||
{
|
{
|
||||||
git_signature_free(sig);
|
git_signature_free(sig);
|
||||||
@ -163,6 +174,19 @@ int gj_git_commit(char *git_base_path, char *author_name, char *author_email, ch
|
|||||||
return handle_error(err);
|
return handle_error(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const git_commit *parents = {parent_commit};
|
||||||
|
err = git_commit_create(&commit_id, repo, "HEAD", sig, sig, NULL, message, tree, 1, &parents);
|
||||||
|
if (err < 0)
|
||||||
|
{
|
||||||
|
git_commit_free(parent_commit);
|
||||||
|
git_signature_free(sig);
|
||||||
|
git_tree_free(tree);
|
||||||
|
git_repository_free(repo);
|
||||||
|
return handle_error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
git_commit_free(parent_commit);
|
||||||
git_tree_free(tree);
|
git_tree_free(tree);
|
||||||
git_repository_free(repo);
|
git_repository_free(repo);
|
||||||
git_signature_free(sig);
|
git_signature_free(sig);
|
||||||
@ -186,7 +210,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
git_libgit2_init();
|
git_libgit2_init();
|
||||||
|
|
||||||
//gj_git_init("/tmp/foo");
|
gj_git_init(git_base_path);
|
||||||
gj_git_commit(git_base_path, author_name, author_email, message);
|
gj_git_commit(git_base_path, author_name, author_email, message);
|
||||||
|
|
||||||
git_libgit2_shutdown();
|
git_libgit2_shutdown();
|
||||||
|
Reference in New Issue
Block a user