|
| 1 | +/* |
| 2 | + * libgit2 "commit" example - shows how to create a git commit |
| 3 | + * |
| 4 | + * Written by the libgit2 contributors |
| 5 | + * |
| 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. |
| 9 | + * |
| 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/>. |
| 13 | + */ |
| 14 | + |
| 15 | +#include "common.h" |
| 16 | + |
| 17 | +/** |
| 18 | + * This example demonstrates the libgit2 commit APIs to roughly |
| 19 | + * simulate `git commit` with the commit message argument. |
| 20 | + * |
| 21 | + * This does not have: |
| 22 | + * |
| 23 | + * - Robust error handling |
| 24 | + * - Most of the `git commit` options |
| 25 | + * |
| 26 | + * This does have: |
| 27 | + * |
| 28 | + * - Example of performing a git commit with a comment |
| 29 | + * |
| 30 | + */ |
| 31 | +int lg2_commit(git_repository *repo, int argc, char **argv) |
| 32 | +{ |
| 33 | + const char *opt = argv[1]; |
| 34 | + const char *comment = argv[2]; |
| 35 | + int error; |
| 36 | + |
| 37 | + git_oid commit_oid,tree_oid; |
| 38 | + git_tree *tree; |
| 39 | + git_index *index; |
| 40 | + git_object *parent = NULL; |
| 41 | + git_reference *ref = NULL; |
| 42 | + git_signature *signature; |
| 43 | + |
| 44 | + /* Validate args */ |
| 45 | + if (argc < 3 || strcmp(opt, "-m") != 0) { |
| 46 | + printf ("USAGE: %s -m <comment>\n", argv[0]); |
| 47 | + return -1; |
| 48 | + } |
| 49 | + |
| 50 | + error = git_revparse_ext(&parent, &ref, repo, "HEAD"); |
| 51 | + if (error == GIT_ENOTFOUND) { |
| 52 | + printf("HEAD not found. Creating first commit\n"); |
| 53 | + error = 0; |
| 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); |
| 58 | + } |
| 59 | + |
| 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);; |
| 63 | + |
| 64 | + check_lg2(git_tree_lookup(&tree, repo, &tree_oid), "Error looking up tree", NULL); |
| 65 | + |
| 66 | + check_lg2(git_signature_default(&signature, repo), "Error creating signature", NULL); |
| 67 | + |
| 68 | + check_lg2(git_commit_create_v( |
| 69 | + &commit_oid, |
| 70 | + repo, |
| 71 | + "HEAD", |
| 72 | + signature, |
| 73 | + signature, |
| 74 | + NULL, |
| 75 | + comment, |
| 76 | + tree, |
| 77 | + parent ? 1 : 0, parent), "Error creating commit", NULL); |
| 78 | + |
| 79 | + git_index_free(index); |
| 80 | + git_signature_free(signature); |
| 81 | + git_tree_free(tree); |
| 82 | + |
| 83 | + return error; |
| 84 | +} |
0 commit comments