diff options
author | Patrick Steinhardt <ps@pks.im> | 2016-08-16 09:59:28 +0200 |
---|---|---|
committer | Patrick Steinhardt <ps@pks.im> | 2016-10-10 09:04:41 +0200 |
commit | b009adad3506ba634611bb17ab332b1226bcb116 (patch) | |
tree | 12c177ac1c66a5f96d3b79fc22bf29c7820420a0 /examples | |
parent | 15960454c58ebe538632efac551f71c9efee8eb5 (diff) | |
download | libgit2-b009adad3506ba634611bb17ab332b1226bcb116.tar.gz |
examples: general: extract function demonstrating commit writing
Diffstat (limited to 'examples')
-rw-r--r-- | examples/general.c | 87 |
1 files changed, 51 insertions, 36 deletions
diff --git a/examples/general.c b/examples/general.c index 2b6dd47d6..56fa11bb2 100644 --- a/examples/general.c +++ b/examples/general.c @@ -43,6 +43,7 @@ #include <stdio.h> #include <string.h> +static void commit_writing(git_repository *repo); static void commit_parsing(git_repository *repo); static void tag_parsing(git_repository *repo); static void tree_parsing(git_repository *repo); @@ -175,43 +176,69 @@ int main (int argc, char** argv) git_oid_fmt(out, &oid); printf("Written Object: %s\n", out); + commit_writing(repo); + commit_parsing(repo); + tag_parsing(repo); + tree_parsing(repo); + blob_parsing(repo); + revwalking(repo); + index_walking(repo); + reference_listing(repo); + config_files(repo_path); - // #### Writing Commits + // Finally, when you're done with the repository, you can free it as well. + git_repository_free(repo); - // libgit2 provides a couple of methods to create commit objects easily as - // well. There are four different create signatures, we'll just show one - // of them here. You can read about the other ones in the [commit API - // docs][cd]. - // - // [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit + return 0; +} - printf("\n*Commit Writing*\n"); +/** + * #### Writing Commits + * + * libgit2 provides a couple of methods to create commit objects easily as + * well. There are four different create signatures, we'll just show one + * of them here. You can read about the other ones in the [commit API + * docs][cd]. + * + * [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit + */ +static void commit_writing(git_repository *repo) +{ git_oid tree_id, parent_id, commit_id; git_tree *tree; git_commit *parent; const git_signature *author, *cmtter; + char oid_hex[GIT_OID_HEXSZ+1] = { 0 }; - // Creating signatures for an authoring identity and time is simple. You - // will need to do this to specify who created a commit and when. Default - // values for the name and email should be found in the `user.name` and - // `user.email` configuration options. See the `config` section of this - // example file to see how to access config values. + printf("\n*Commit Writing*\n"); + + /** + * Creating signatures for an authoring identity and time is simple. You + * will need to do this to specify who created a commit and when. Default + * values for the name and email should be found in the `user.name` and + * `user.email` configuration options. See the `config` section of this + * example file to see how to access config values. + */ git_signature_new((git_signature **)&author, "Scott Chacon", "schacon@gmail.com", 123456789, 60); git_signature_new((git_signature **)&cmtter, "Scott A Chacon", "scott@github.com", 987654321, 90); - // Commit objects need a tree to point to and optionally one or more - // parents. Here we're creating oid objects to create the commit with, - // but you can also use + /** + * Commit objects need a tree to point to and optionally one or more + * parents. Here we're creating oid objects to create the commit with, + * but you can also use + */ git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1"); git_tree_lookup(&tree, repo, &tree_id); git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); git_commit_lookup(&parent, repo, &parent_id); - // Here we actually create the commit object with a single call with all - // the values we need to create the commit. The SHA key is written to the - // `commit_id` variable here. + /** + * Here we actually create the commit object with a single call with all + * the values we need to create the commit. The SHA key is written to the + * `commit_id` variable here. + */ git_commit_create_v( &commit_id, /* out id */ repo, @@ -223,23 +250,11 @@ int main (int argc, char** argv) tree, 1, parent); - // Now we can take a look at the commit SHA we've generated. - git_oid_fmt(out, &commit_id); - printf("New Commit: %s\n", out); - - commit_parsing(repo); - tag_parsing(repo); - tree_parsing(repo); - blob_parsing(repo); - revwalking(repo); - index_walking(repo); - reference_listing(repo); - config_files(repo_path); - - // Finally, when you're done with the repository, you can free it as well. - git_repository_free(repo); - - return 0; + /** + * Now we can take a look at the commit SHA we've generated. + */ + git_oid_fmt(oid_hex, &commit_id); + printf("New Commit: %s\n", oid_hex); } /** |