examples: general: extract function demonstrating commit writing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
diff --git a/examples/general.c b/examples/general.c
index 2b6dd47..56fa11b 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);
}
/**