]>
git.proxmox.com Git - libgit2.git/blob - examples/commit.c
2 * libgit2 "commit" example - shows how to create a git commit
4 * Written by the libgit2 contributors
6 * To the extent possible under law, the author(s) have dedicated all copyright
7 * and related and neighboring rights to this software to the public domain
8 * worldwide. This software is distributed without any warranty.
10 * You should have received a copy of the CC0 Public Domain Dedication along
11 * with this software. If not, see
12 * <http://creativecommons.org/publicdomain/zero/1.0/>.
18 * This example demonstrates the libgit2 commit APIs to roughly
19 * simulate `git commit` with the commit message argument.
23 * - Robust error handling
24 * - Most of the `git commit` options
28 * - Example of performing a git commit with a comment
31 int lg2_commit(git_repository
*repo
, int argc
, char **argv
)
33 const char *opt
= argv
[1];
34 const char *comment
= argv
[2];
37 git_oid commit_oid
,tree_oid
;
40 git_object
*parent
= NULL
;
41 git_reference
*ref
= NULL
;
42 git_signature
*signature
;
45 if (argc
< 3 || strcmp(opt
, "-m") != 0) {
46 printf ("USAGE: %s -m <comment>\n", argv
[0]);
50 error
= git_revparse_ext(&parent
, &ref
, repo
, "HEAD");
51 if (error
== GIT_ENOTFOUND
) {
52 printf("HEAD not found. Creating first commit\n");
54 } else if (error
!= 0) {
55 const git_error
*err
= git_error_last();
56 if (err
) printf("ERROR %d: %s\n", err
->klass
, err
->message
);
57 else printf("ERROR %d: no detailed info\n", error
);
60 check_lg2(git_repository_index(&index
, repo
), "Could not open repository index", NULL
);
61 check_lg2(git_index_write_tree(&tree_oid
, index
), "Could not write tree", NULL
);;
62 check_lg2(git_index_write(index
), "Could not write index", NULL
);;
64 check_lg2(git_tree_lookup(&tree
, repo
, &tree_oid
), "Error looking up tree", NULL
);
66 check_lg2(git_signature_default(&signature
, repo
), "Error creating signature", NULL
);
68 check_lg2(git_commit_create_v(
77 parent
? 1 : 0, parent
), "Error creating commit", NULL
);
79 git_index_free(index
);
80 git_signature_free(signature
);
82 git_object_free(parent
);
83 git_reference_free(ref
);