examples: general: extract function demonstrating OID parsing
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
diff --git a/examples/general.c b/examples/general.c
index 685dce0..d58e93f 100644
--- a/examples/general.c
+++ b/examples/general.c
@@ -43,6 +43,7 @@
#include <stdio.h>
#include <string.h>
+static void oid_parsing(git_oid *out);
static void object_database(git_repository *repo, git_oid *oid);
static void commit_writing(git_repository *repo);
static void commit_parsing(git_repository *repo);
@@ -71,6 +72,8 @@ static void check_error(int error_code, const char *action)
int main (int argc, char** argv)
{
+ git_oid oid;
+
// Initialize the library, this will set up any global state which libgit2 needs
// including threading and crypto
git_libgit2_init();
@@ -91,32 +94,7 @@ int main (int argc, char** argv)
error = git_repository_open(&repo, repo_path);
check_error(error, "opening repository");
- // ### SHA-1 Value Conversions
-
- // For our first example, we will convert a 40 character hex value to the
- // 20 byte raw SHA1 value.
- printf("*Hex to Raw*\n");
- char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045";
-
- // The `git_oid` is the structure that keeps the SHA value. We will use
- // this throughout the example for storing the value of the current SHA
- // key we're working with.
- git_oid oid;
- git_oid_fromstr(&oid, hex);
-
- // Once we've converted the string into the oid value, we can get the raw
- // value of the SHA by accessing `oid.id`
-
- // Next we will convert the 20 byte raw SHA1 value to a human readable 40
- // char hex value.
- printf("\n*Raw to Hex*\n");
- char out[GIT_OID_HEXSZ+1];
- out[GIT_OID_HEXSZ] = '\0';
-
- // If you have a oid, you can easily get the hex value of the SHA as well.
- git_oid_fmt(out, &oid);
- printf("SHA hex string: %s\n", out);
-
+ oid_parsing(&oid);
object_database(repo, &oid);
commit_writing(repo);
commit_parsing(repo);
@@ -135,6 +113,46 @@ int main (int argc, char** argv)
}
/**
+ * ### SHA-1 Value Conversions
+ */
+static void oid_parsing(git_oid *oid)
+{
+ char out[GIT_OID_HEXSZ+1];
+ char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045";
+
+ printf("*Hex to Raw*\n");
+
+ /**
+ * For our first example, we will convert a 40 character hex value to the
+ * 20 byte raw SHA1 value.
+ *
+ * The `git_oid` is the structure that keeps the SHA value. We will use
+ * this throughout the example for storing the value of the current SHA
+ * key we're working with.
+ */
+ git_oid_fromstr(oid, hex);
+
+ // Once we've converted the string into the oid value, we can get the raw
+ // value of the SHA by accessing `oid.id`
+
+ // Next we will convert the 20 byte raw SHA1 value to a human readable 40
+ // char hex value.
+ printf("\n*Raw to Hex*\n");
+ out[GIT_OID_HEXSZ] = '\0';
+
+ /**
+ * If you have a oid, you can easily get the hex value of the SHA as well.
+ */
+ git_oid_fmt(out, oid);
+
+ /**
+ * If you have a oid, you can easily get the hex value of the SHA as well.
+ */
+ git_oid_fmt(out, oid);
+ printf("SHA hex string: %s\n", out);
+}
+
+/**
* ### Working with the Object Database
*
* **libgit2** provides [direct access][odb] to the object database. The