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
CC = gcc
CFLAGS = -g -I../include
LFLAGS = -Ldirectory-containing-libgit
LFLAGS = -L../build -lgit2 -lz
all: general showindex
general : general.c
gcc -o general $(CFLAGS) general.c $(LFLAGS) -lgit2 -lz
showindex : showindex.c
gcc -o showindex $(CFLAGS) showindex.c $(LFLAGS) -lgit2 -lz
% : %.c
$(CC) -o $@ $(CFLAGS) $< $(LFLAGS)
clean:
rm general showindex
$(RM) general showindex
......@@ -28,19 +28,6 @@
#include <git2.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)
{
// ### Opening the Repository
......@@ -52,9 +39,9 @@ int main (int argc, char** argv)
// [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository
git_repository *repo;
if (argc > 1) {
CHK_INT(git_repository_open(&repo, argv[1]));
git_repository_open(&repo, argv[1]);
} else {
CHK_INT(git_repository_open(&repo, "/opt/libgit2-test/.git"));
git_repository_open(&repo, "/opt/libgit2-test/.git");
}
// ### SHA-1 Value Conversions
......@@ -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
// for storing the value of the current SHA key we're working with.
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.
printf("Raw 20 bytes: [%.20s]\n", (&oid)->id);
......@@ -87,7 +74,7 @@ int main (int argc, char** argv)
// repository.
// [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb
git_odb *odb;
CHK_PTR(odb = git_repository_database(repo));
odb = git_repository_database(repo);
// #### Raw Object Reading
......@@ -96,11 +83,12 @@ int main (int argc, char** argv)
git_otype otype;
const unsigned char *data;
const char *str_type;
int error;
// 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
// 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
// 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)
// 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
// 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
// object was written to our database.
......@@ -149,9 +137,9 @@ int main (int argc, char** argv)
printf("\n*Commit Parsing*\n");
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 char *message;
......@@ -176,7 +164,7 @@ int main (int argc, char** argv)
parents = git_commit_parentcount(commit);
for (p = 0;p < parents;p++) {
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));
printf("Parent: %s\n", out);
git_commit_close(parent);
......@@ -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
// 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.
CHK_INT(git_signature_new(&author, "Scott Chacon", "schacon@gmail.com",
123456789, 60));
CHK_INT(git_signature_new(&cmtter, "Scott A Chacon", "scott@github.com",
987654321, 90));
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
CHK_INT(git_oid_fromstr(&tree_id, "28873d96b4e8f4e33ea30f4c682fd325f7ba56ac"));
CHK_INT(git_tree_lookup(&tree, repo, &tree_id));
CHK_INT(git_oid_fromstr(&parent_id, "f0877d0b841d75172ec404fc9370173dfffc20d1"));
CHK_INT(git_commit_lookup(&parent, repo, &parent_id));
git_oid_fromstr(&tree_id, "28873d96b4e8f4e33ea30f4c682fd325f7ba56ac");
git_tree_lookup(&tree, repo, &tree_id);
git_oid_fromstr(&parent_id, "f0877d0b841d75172ec404fc9370173dfffc20d1");
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.
......@@ -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
// 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
// (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.
CHK_INT(git_tag_target((git_object **)&commit, tag));
git_tag_target((git_object **)&commit, tag);
tname = git_tag_name(tag); // "test"
ttype = git_tag_type(tag); // GIT_OBJ_COMMIT (otype enum)
tmessage = git_tag_message(tag); // "tag message\n"
......@@ -269,18 +257,18 @@ int main (int argc, char** argv)
git_object *objt;
// Create the oid and lookup the tree object just like the other objects.
CHK_INT(git_oid_fromstr(&oid, "2a741c18ac5ff082a7caaec6e74db3075a1906b5"));
CHK_INT(git_tree_lookup(&tree, repo, &oid));
git_oid_fromstr(&oid, "2a741c18ac5ff082a7caaec6e74db3075a1906b5");
git_tree_lookup(&tree, repo, &oid);
// 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
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"
// 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"
// 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)
printf("\n*Blob Parsing*\n");
git_blob *blob;
CHK_INT(git_oid_fromstr(&oid, "af7574ea73f7b166f869ef1a39be126d9a186ae0"));
CHK_INT(git_blob_lookup(&blob, repo, &oid));
git_oid_fromstr(&oid, "af7574ea73f7b166f869ef1a39be126d9a186ae0");
git_blob_lookup(&blob, repo, &oid);
// 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):
// do not consider the buffer a NULL-terminated string, and use the `git_blob_rawsize` attribute to
// find out its exact size in bytes
printf("Blob Size: %d\n", git_blob_rawsize(blob)); // 8
CHK_PTR(git_blob_rawcontent(blob)); // "content"
printf("Blob Size: %ld\n", git_blob_rawsize(blob)); // 8
git_blob_rawcontent(blob); // "content"
// ### Revwalking
//
......@@ -327,7 +315,7 @@ int main (int argc, char** argv)
git_revwalk *walk;
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
// 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)
// 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
// of `branch1`.
CHK_INT(git_revwalk_new(&walk, repo));
git_revwalk_new(&walk, repo);
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 char *cmsg;
......@@ -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
// be cached in memory
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);
cauth = git_commit_author(wcommit);
printf("%s (%s)\n", cmsg, cauth->email);
......@@ -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
// 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.
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
// 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)
// Here we will implement something like `git for-each-ref` simply listing out all available
// references and the object SHA they resolve to.
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;
git_reference *ref;
......@@ -413,7 +401,7 @@ int main (int argc, char** argv)
// resolve them to the SHA, then print both values out.
for (i = 0; i < ref_list.count; ++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)) {
case GIT_REF_OID:
......@@ -447,7 +435,7 @@ int main (int argc, char** argv)
git_config *cfg;
// 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);
printf("Autocorrect: %d\n", j);
......
......@@ -48,7 +48,7 @@ GIT_BEGIN_DECL
* @param offset timezone offset in minutes for the time
* @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
......
......@@ -81,7 +81,7 @@ static int process_trimming(const char *input, char **storage, const char *input
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;
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