Commit 784b3b49 by David Boyce

Fixed typo in example Makefile code and slimmed it down more.

Reverted signature of git_signature_new.
Removed error check wrappers (voted down). Made Makefile
work out of the box on Linux and Solaris when standard
cmake build instructions for the library are followed.
parent 0251733e
.PHONY: all .PHONY: all
CC = gcc
CFLAGS = -g -I../include CFLAGS = -g -I../include
LFLAGS = -Ldirectory-containing-libgit LFLAGS = -L../build -lgit2 -lz
all: general showindex all: general showindex
general : general.c % : %.c
gcc -o general $(CFLAGS) general.c $(LFLAGS) -lgit2 -lz $(CC) -o $@ $(CFLAGS) $< $(LFLAGS)
showindex : showindex.c
gcc -o showindex $(CFLAGS) showindex.c $(LFLAGS) -lgit2 -lz
clean: clean:
rm general showindex $(RM) general showindex
...@@ -28,19 +28,6 @@ ...@@ -28,19 +28,6 @@
#include <git2.h> #include <git2.h>
#include <stdio.h> #include <stdio.h>
// It's not necessarily recommended that you use macros like this in a real application
// but they're handy for a simple demo.
#define CHK_INT(fcn) \
if ((fcn) != GIT_SUCCESS) { \
fprintf(stderr, "%s: Error: %s at %s:%d\n", argv[0], git_lasterror(), __FILE__, __LINE__); \
exit(2); \
}
#define CHK_PTR(fcn) \
if ((fcn) == NULL) { \
fprintf(stderr, "%s: Error: %s at %s:%d\n", argv[0], git_lasterror(), __FILE__, __LINE__); \
exit(2); \
}
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
// ### Opening the Repository // ### Opening the Repository
...@@ -52,9 +39,9 @@ int main (int argc, char** argv) ...@@ -52,9 +39,9 @@ int main (int argc, char** argv)
// [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository // [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository
git_repository *repo; git_repository *repo;
if (argc > 1) { if (argc > 1) {
CHK_INT(git_repository_open(&repo, argv[1])); git_repository_open(&repo, argv[1]);
} else { } else {
CHK_INT(git_repository_open(&repo, "/opt/libgit2-test/.git")); git_repository_open(&repo, "/opt/libgit2-test/.git");
} }
// ### SHA-1 Value Conversions // ### SHA-1 Value Conversions
...@@ -66,7 +53,7 @@ int main (int argc, char** argv) ...@@ -66,7 +53,7 @@ int main (int argc, char** argv)
// The `git_oid` is the structure that keeps the SHA value. We will use this throughout the example // 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. // for storing the value of the current SHA key we're working with.
git_oid oid; git_oid oid;
CHK_INT(git_oid_fromstr(&oid, hex)); git_oid_fromstr(&oid, hex);
// Once we've converted the string into the oid value, we can get the raw value of the SHA. // Once we've converted the string into the oid value, we can get the raw value of the SHA.
printf("Raw 20 bytes: [%.20s]\n", (&oid)->id); printf("Raw 20 bytes: [%.20s]\n", (&oid)->id);
...@@ -87,7 +74,7 @@ int main (int argc, char** argv) ...@@ -87,7 +74,7 @@ int main (int argc, char** argv)
// repository. // repository.
// [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb // [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb
git_odb *odb; git_odb *odb;
CHK_PTR(odb = git_repository_database(repo)); odb = git_repository_database(repo);
// #### Raw Object Reading // #### Raw Object Reading
...@@ -96,11 +83,12 @@ int main (int argc, char** argv) ...@@ -96,11 +83,12 @@ int main (int argc, char** argv)
git_otype otype; git_otype otype;
const unsigned char *data; const unsigned char *data;
const char *str_type; const char *str_type;
int error;
// We can read raw objects directly from the object database if we have the oid (SHA) // We can read raw objects directly from the object database if we have the oid (SHA)
// of the object. This allows us to access objects without knowing thier type and inspect // of the object. This allows us to access objects without knowing thier type and inspect
// the raw bytes unparsed. // the raw bytes unparsed.
CHK_INT(git_odb_read(&obj, odb, &oid)); error = git_odb_read(&obj, odb, &oid);
// A raw object only has three properties - the type (commit, blob, tree or tag), the size // A raw object only has three properties - the type (commit, blob, tree or tag), the size
// of the raw data and the raw, unparsed data itself. For a commit or tag, that raw data // of the raw data and the raw, unparsed data itself. For a commit or tag, that raw data
...@@ -129,7 +117,7 @@ int main (int argc, char** argv) ...@@ -129,7 +117,7 @@ int main (int argc, char** argv)
// direct access to the key/value properties of Git. Here we'll write a new blob object // direct access to the key/value properties of Git. Here we'll write a new blob object
// that just contains a simple string. Notice that we have to specify the object type as // that just contains a simple string. Notice that we have to specify the object type as
// the `git_otype` enum. // the `git_otype` enum.
CHK_INT(git_odb_write(&oid, odb, "test data", sizeof("test data") - 1, GIT_OBJ_BLOB)); git_odb_write(&oid, odb, "test data", sizeof("test data") - 1, GIT_OBJ_BLOB);
// Now that we've written the object, we can check out what SHA1 was generated when the // Now that we've written the object, we can check out what SHA1 was generated when the
// object was written to our database. // object was written to our database.
...@@ -149,9 +137,9 @@ int main (int argc, char** argv) ...@@ -149,9 +137,9 @@ int main (int argc, char** argv)
printf("\n*Commit Parsing*\n"); printf("\n*Commit Parsing*\n");
git_commit *commit; git_commit *commit;
CHK_INT(git_oid_fromstr(&oid, "f0877d0b841d75172ec404fc9370173dfffc20d1")); git_oid_fromstr(&oid, "f0877d0b841d75172ec404fc9370173dfffc20d1");
CHK_INT(git_commit_lookup(&commit, repo, &oid)); error = git_commit_lookup(&commit, repo, &oid);
const git_signature *author, *cmtter; const git_signature *author, *cmtter;
const char *message; const char *message;
...@@ -176,7 +164,7 @@ int main (int argc, char** argv) ...@@ -176,7 +164,7 @@ int main (int argc, char** argv)
parents = git_commit_parentcount(commit); parents = git_commit_parentcount(commit);
for (p = 0;p < parents;p++) { for (p = 0;p < parents;p++) {
git_commit *parent; git_commit *parent;
CHK_INT(git_commit_parent(&parent, commit, p)); git_commit_parent(&parent, commit, p);
git_oid_fmt(out, git_commit_id(parent)); git_oid_fmt(out, git_commit_id(parent));
printf("Parent: %s\n", out); printf("Parent: %s\n", out);
git_commit_close(parent); git_commit_close(parent);
...@@ -202,17 +190,17 @@ int main (int argc, char** argv) ...@@ -202,17 +190,17 @@ int main (int argc, char** argv)
// this to create a commit in order to specify who created it and when. Default values for the name // this to create a commit in order to specify who created it and when. Default values for the name
// and email should be found in the `user.name` and `user.email` configuration options. See the `config` // 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. // section of this example file to see how to access config values.
CHK_INT(git_signature_new(&author, "Scott Chacon", "schacon@gmail.com", git_signature_new((git_signature **)&author, "Scott Chacon", "schacon@gmail.com",
123456789, 60)); 123456789, 60);
CHK_INT(git_signature_new(&cmtter, "Scott A Chacon", "scott@github.com", git_signature_new((git_signature **)&cmtter, "Scott A Chacon", "scott@github.com",
987654321, 90)); 987654321, 90);
// Commit objects need a tree to point to and optionally one or more parents. Here we're creating oid // 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 // objects to create the commit with, but you can also use
CHK_INT(git_oid_fromstr(&tree_id, "28873d96b4e8f4e33ea30f4c682fd325f7ba56ac")); git_oid_fromstr(&tree_id, "28873d96b4e8f4e33ea30f4c682fd325f7ba56ac");
CHK_INT(git_tree_lookup(&tree, repo, &tree_id)); git_tree_lookup(&tree, repo, &tree_id);
CHK_INT(git_oid_fromstr(&parent_id, "f0877d0b841d75172ec404fc9370173dfffc20d1")); git_oid_fromstr(&parent_id, "f0877d0b841d75172ec404fc9370173dfffc20d1");
CHK_INT(git_commit_lookup(&parent, repo, &parent_id)); 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 // 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. // the commit. The SHA key is written to the `commit_id` variable here.
...@@ -242,14 +230,14 @@ int main (int argc, char** argv) ...@@ -242,14 +230,14 @@ int main (int argc, char** argv)
// We create an oid for the tag object if we know the SHA and look it up in the repository the same // We create an oid for the tag object if we know the SHA and look it up in the repository the same
// way that we would a commit (or any other) object. // way that we would a commit (or any other) object.
CHK_INT(git_oid_fromstr(&oid, "bc422d45275aca289c51d79830b45cecebff7c3a")); git_oid_fromstr(&oid, "bc422d45275aca289c51d79830b45cecebff7c3a");
CHK_INT(git_tag_lookup(&tag, repo, &oid)); error = git_tag_lookup(&tag, repo, &oid);
// Now that we have the tag object, we can extract the information it generally contains: the target // Now that we have the tag object, we can extract the information it generally contains: the target
// (usually a commit object), the type of the target object (usually 'commit'), the name ('v1.0'), // (usually a commit object), the type of the target object (usually 'commit'), the name ('v1.0'),
// the tagger (a git_signature - name, email, timestamp), and the tag message. // the tagger (a git_signature - name, email, timestamp), and the tag message.
CHK_INT(git_tag_target((git_object **)&commit, tag)); git_tag_target((git_object **)&commit, tag);
tname = git_tag_name(tag); // "test" tname = git_tag_name(tag); // "test"
ttype = git_tag_type(tag); // GIT_OBJ_COMMIT (otype enum) ttype = git_tag_type(tag); // GIT_OBJ_COMMIT (otype enum)
tmessage = git_tag_message(tag); // "tag message\n" tmessage = git_tag_message(tag); // "tag message\n"
...@@ -269,18 +257,18 @@ int main (int argc, char** argv) ...@@ -269,18 +257,18 @@ int main (int argc, char** argv)
git_object *objt; git_object *objt;
// Create the oid and lookup the tree object just like the other objects. // Create the oid and lookup the tree object just like the other objects.
CHK_INT(git_oid_fromstr(&oid, "2a741c18ac5ff082a7caaec6e74db3075a1906b5")); git_oid_fromstr(&oid, "2a741c18ac5ff082a7caaec6e74db3075a1906b5");
CHK_INT(git_tree_lookup(&tree, repo, &oid)); git_tree_lookup(&tree, repo, &oid);
// Getting the count of entries in the tree so you can iterate over them if you want to. // Getting the count of entries in the tree so you can iterate over them if you want to.
int cnt = git_tree_entrycount(tree); // 3 int cnt = git_tree_entrycount(tree); // 3
printf("tree entries: %d\n", cnt); printf("tree entries: %d\n", cnt);
CHK_PTR(entry = git_tree_entry_byindex(tree, 0)); entry = git_tree_entry_byindex(tree, 0);
printf("Entry name: %s\n", git_tree_entry_name(entry)); // "hello.c" printf("Entry name: %s\n", git_tree_entry_name(entry)); // "hello.c"
// You can also access tree entries by name if you know the name of the entry you're looking for. // You can also access tree entries by name if you know the name of the entry you're looking for.
CHK_PTR(entry = git_tree_entry_byname(tree, "hello.c")); entry = git_tree_entry_byname(tree, "hello.c");
git_tree_entry_name(entry); // "hello.c" git_tree_entry_name(entry); // "hello.c"
// Once you have the entry object, you can access the content or subtree (or commit, in the case // Once you have the entry object, you can access the content or subtree (or commit, in the case
...@@ -303,15 +291,15 @@ int main (int argc, char** argv) ...@@ -303,15 +291,15 @@ int main (int argc, char** argv)
printf("\n*Blob Parsing*\n"); printf("\n*Blob Parsing*\n");
git_blob *blob; git_blob *blob;
CHK_INT(git_oid_fromstr(&oid, "af7574ea73f7b166f869ef1a39be126d9a186ae0")); git_oid_fromstr(&oid, "af7574ea73f7b166f869ef1a39be126d9a186ae0");
CHK_INT(git_blob_lookup(&blob, repo, &oid)); git_blob_lookup(&blob, repo, &oid);
// You can access a buffer with the raw contents of the blob directly. // You can access a buffer with the raw contents of the blob directly.
// Note that this buffer may not be contain ASCII data for certain blobs (e.g. binary files): // Note that this buffer may not be contain ASCII data for certain blobs (e.g. binary files):
// do not consider the buffer a NULL-terminated string, and use the `git_blob_rawsize` attribute to // do not consider the buffer a NULL-terminated string, and use the `git_blob_rawsize` attribute to
// find out its exact size in bytes // find out its exact size in bytes
printf("Blob Size: %d\n", git_blob_rawsize(blob)); // 8 printf("Blob Size: %ld\n", git_blob_rawsize(blob)); // 8
CHK_PTR(git_blob_rawcontent(blob)); // "content" git_blob_rawcontent(blob); // "content"
// ### Revwalking // ### Revwalking
// //
...@@ -327,7 +315,7 @@ int main (int argc, char** argv) ...@@ -327,7 +315,7 @@ int main (int argc, char** argv)
git_revwalk *walk; git_revwalk *walk;
git_commit *wcommit; git_commit *wcommit;
CHK_INT(git_oid_fromstr(&oid, "f0877d0b841d75172ec404fc9370173dfffc20d1")); git_oid_fromstr(&oid, "f0877d0b841d75172ec404fc9370173dfffc20d1");
// To use the revwalker, create a new walker, tell it how you want to sort the output and then push // To use the revwalker, create a new walker, tell it how you want to sort the output and then push
// one or more starting points onto the walker. If you want to emulate the output of `git log` you // one or more starting points onto the walker. If you want to emulate the output of `git log` you
...@@ -335,9 +323,9 @@ int main (int argc, char** argv) ...@@ -335,9 +323,9 @@ int main (int argc, char** argv)
// You can also 'hide' commits that you want to stop at or not see any of their ancestors. So if you // You can also 'hide' commits that you want to stop at or not see any of their ancestors. So if you
// want to emulate `git log branch1..branch2`, you would push the oid of `branch2` and hide the oid // want to emulate `git log branch1..branch2`, you would push the oid of `branch2` and hide the oid
// of `branch1`. // of `branch1`.
CHK_INT(git_revwalk_new(&walk, repo)); git_revwalk_new(&walk, repo);
git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE); git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE);
CHK_INT(git_revwalk_push(walk, &oid)); git_revwalk_push(walk, &oid);
const git_signature *cauth; const git_signature *cauth;
const char *cmsg; const char *cmsg;
...@@ -348,7 +336,7 @@ int main (int argc, char** argv) ...@@ -348,7 +336,7 @@ int main (int argc, char** argv)
// note that this operation is specially fast since the raw contents of the commit object will // note that this operation is specially fast since the raw contents of the commit object will
// be cached in memory // be cached in memory
while ((git_revwalk_next(&oid, walk)) == GIT_SUCCESS) { while ((git_revwalk_next(&oid, walk)) == GIT_SUCCESS) {
CHK_INT(git_commit_lookup(&wcommit, repo, &oid)); error = git_commit_lookup(&wcommit, repo, &oid);
cmsg = git_commit_message(wcommit); cmsg = git_commit_message(wcommit);
cauth = git_commit_author(wcommit); cauth = git_commit_author(wcommit);
printf("%s (%s)\n", cmsg, cauth->email); printf("%s (%s)\n", cmsg, cauth->email);
...@@ -375,7 +363,7 @@ int main (int argc, char** argv) ...@@ -375,7 +363,7 @@ int main (int argc, char** argv)
// You can either open the index from the standard location in an open repository, as we're doing // You can either open the index from the standard location in an open repository, as we're doing
// here, or you can open and manipulate any index file with `git_index_open_bare()`. The index // here, or you can open and manipulate any index file with `git_index_open_bare()`. The index
// for the repository will be located and loaded from disk. // for the repository will be located and loaded from disk.
CHK_INT(git_repository_index(&index, repo)); git_repository_index(&index, repo);
// For each entry in the index, you can get a bunch of information including the SHA (oid), path // For each entry in the index, you can get a bunch of information including the SHA (oid), path
// and mode which map to the tree objects that are written out. It also has filesystem properties // and mode which map to the tree objects that are written out. It also has filesystem properties
...@@ -404,7 +392,7 @@ int main (int argc, char** argv) ...@@ -404,7 +392,7 @@ int main (int argc, char** argv)
// Here we will implement something like `git for-each-ref` simply listing out all available // Here we will implement something like `git for-each-ref` simply listing out all available
// references and the object SHA they resolve to. // references and the object SHA they resolve to.
git_strarray ref_list; git_strarray ref_list;
CHK_INT(git_reference_listall(&ref_list, repo, GIT_REF_LISTALL)); git_reference_listall(&ref_list, repo, GIT_REF_LISTALL);
const char *refname; const char *refname;
git_reference *ref; git_reference *ref;
...@@ -413,7 +401,7 @@ int main (int argc, char** argv) ...@@ -413,7 +401,7 @@ int main (int argc, char** argv)
// resolve them to the SHA, then print both values out. // resolve them to the SHA, then print both values out.
for (i = 0; i < ref_list.count; ++i) { for (i = 0; i < ref_list.count; ++i) {
refname = ref_list.strings[i]; refname = ref_list.strings[i];
CHK_INT(git_reference_lookup(&ref, repo, refname)); git_reference_lookup(&ref, repo, refname);
switch (git_reference_type(ref)) { switch (git_reference_type(ref)) {
case GIT_REF_OID: case GIT_REF_OID:
...@@ -447,7 +435,7 @@ int main (int argc, char** argv) ...@@ -447,7 +435,7 @@ int main (int argc, char** argv)
git_config *cfg; git_config *cfg;
// Open a config object so we can read global values from it. // Open a config object so we can read global values from it.
CHK_INT(git_config_open_ondisk(&cfg, "~/.gitconfig")); git_config_open_ondisk(&cfg, "~/.gitconfig");
git_config_get_int(cfg, "help.autocorrect", &j); git_config_get_int(cfg, "help.autocorrect", &j);
printf("Autocorrect: %d\n", j); printf("Autocorrect: %d\n", j);
......
...@@ -48,7 +48,7 @@ GIT_BEGIN_DECL ...@@ -48,7 +48,7 @@ GIT_BEGIN_DECL
* @param offset timezone offset in minutes for the time * @param offset timezone offset in minutes for the time
* @return 0 on success; error code otherwise * @return 0 on success; error code otherwise
*/ */
GIT_EXTERN(int) git_signature_new(const git_signature **sig_out, const char *name, const char *email, git_time_t time, int offset); GIT_EXTERN(int) git_signature_new(git_signature **sig_out, const char *name, const char *email, git_time_t time, int offset);
/** /**
* Create a new action signature with a timestamp of 'now'. The * Create a new action signature with a timestamp of 'now'. The
......
...@@ -81,7 +81,7 @@ static int process_trimming(const char *input, char **storage, const char *input ...@@ -81,7 +81,7 @@ static int process_trimming(const char *input, char **storage, const char *input
return GIT_SUCCESS; return GIT_SUCCESS;
} }
int git_signature_new(const git_signature **sig_out, const char *name, const char *email, git_time_t time, int offset) int git_signature_new(git_signature **sig_out, const char *name, const char *email, git_time_t time, int offset)
{ {
int error; int error;
git_signature *p = NULL; git_signature *p = NULL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment