diff options
| author | Russell Belfer <rb@github.com> | 2012-11-01 14:08:30 -0700 |
|---|---|---|
| committer | Russell Belfer <rb@github.com> | 2012-11-01 14:08:30 -0700 |
| commit | b90500f03d3ae60f1f79d7adb36d95632a29d7e5 (patch) | |
| tree | 6ca43785060429e23e891c3234ca171ad4cb2a95 /examples | |
| parent | dbd6850d06111eb0761499d7c876ff7cd4ad57fa (diff) | |
| download | libgit2-b90500f03d3ae60f1f79d7adb36d95632a29d7e5.tar.gz | |
Improve docs, examples, warnings
This improves docs in some of the public header files, cleans
up and improves some of the example code, and fixes a couple
of pedantic warnings in places.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/Makefile | 2 | ||||
| -rw-r--r-- | examples/diff.c | 24 | ||||
| -rw-r--r-- | examples/showindex.c | 85 |
3 files changed, 63 insertions, 48 deletions
diff --git a/examples/Makefile b/examples/Makefile index fe99c75cb..da4df5240 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -1,7 +1,7 @@ .PHONY: all CC = gcc -CFLAGS = -g -I../include -I../src +CFLAGS = -g -I../include -I../src -Wall -Wextra -Wmissing-prototypes LFLAGS = -L../build -lgit2 -lz APPS = general showindex diff diff --git a/examples/diff.c b/examples/diff.c index b72a75e1c..31ebf6bfb 100644 --- a/examples/diff.c +++ b/examples/diff.c @@ -3,7 +3,7 @@ #include <stdlib.h> #include <string.h> -void check(int error, const char *message) +static void check(int error, const char *message) { if (error) { fprintf(stderr, "%s (%d)\n", message, error); @@ -11,7 +11,8 @@ void check(int error, const char *message) } } -int resolve_to_tree(git_repository *repo, const char *identifier, git_tree **tree) +static int resolve_to_tree( + git_repository *repo, const char *identifier, git_tree **tree) { int err = 0; size_t len = strlen(identifier); @@ -61,16 +62,18 @@ char *colors[] = { "\033[36m" /* cyan */ }; -int printer( +static int printer( void *data, - git_diff_delta *delta, - git_diff_range *range, + const git_diff_delta *delta, + const git_diff_range *range, char usage, const char *line, size_t line_len) { int *last_color = data, color = 0; + (void)delta; (void)range; (void)line_len; + if (*last_color >= 0) { switch (usage) { case GIT_DIFF_LINE_ADDITION: color = 3; break; @@ -93,7 +96,7 @@ int printer( return 0; } -int check_uint16_param(const char *arg, const char *pattern, uint16_t *val) +static int check_uint16_param(const char *arg, const char *pattern, uint16_t *val) { size_t len = strlen(pattern); uint16_t strval; @@ -107,7 +110,7 @@ int check_uint16_param(const char *arg, const char *pattern, uint16_t *val) return 1; } -int check_str_param(const char *arg, const char *pattern, char **val) +static int check_str_param(const char *arg, const char *pattern, char **val) { size_t len = strlen(pattern); if (strncmp(arg, pattern, len)) @@ -116,7 +119,7 @@ int check_str_param(const char *arg, const char *pattern, char **val) return 1; } -void usage(const char *message, const char *arg) +static void usage(const char *message, const char *arg) { if (message && arg) fprintf(stderr, "%s: %s\n", message, arg); @@ -128,14 +131,15 @@ void usage(const char *message, const char *arg) int main(int argc, char *argv[]) { - char path[GIT_PATH_MAX]; git_repository *repo = NULL; git_tree *t1 = NULL, *t2 = NULL; - git_diff_options opts = {0}; + git_diff_options opts; git_diff_list *diff; int i, color = -1, compact = 0, cached = 0; char *a, *dir = ".", *treeish1 = NULL, *treeish2 = NULL; + memset(&opts, 0, sizeof(opts)); + /* parse arguments as copied from git-diff */ for (i = 1; i < argc; ++i) { diff --git a/examples/showindex.c b/examples/showindex.c index d26fbaebd..4b50ffd0f 100644 --- a/examples/showindex.c +++ b/examples/showindex.c @@ -3,42 +3,53 @@ int main (int argc, char** argv) { - git_repository *repo; - git_index *index; - unsigned int i, e, ecount; - git_index_entry **entries; - git_oid oid; - - char out[41]; - out[40] = '\0'; - - git_repository_open(&repo, "/opt/libgit2-test/.git"); - - git_repository_index(&index, repo); - git_index_read(index); - - ecount = git_index_entrycount(index); - for (i = 0; i < ecount; ++i) { - git_index_entry *e = git_index_get_byindex(index, i); - - oid = e->oid; - git_oid_fmt(out, &oid); - - printf("File Path: %s\n", e->path); - printf(" Stage: %d\n", git_index_entry_stage(e)); - printf(" Blob SHA: %s\n", out); - printf("File Size: %d\n", (int)e->file_size); - printf(" Device: %d\n", (int)e->dev); - printf(" Inode: %d\n", (int)e->ino); - printf(" UID: %d\n", (int)e->uid); - printf(" GID: %d\n", (int)e->gid); - printf(" ctime: %d\n", (int)e->ctime.seconds); - printf(" mtime: %d\n", (int)e->mtime.seconds); - printf("\n"); - } - - git_index_free(index); - - git_repository_free(repo); + git_repository *repo; + git_index *index; + unsigned int i, ecount; + char *dir = "."; + char out[41]; + out[40] = '\0'; + + if (argc > 1) + dir = argv[1]; + if (argc > 2) { + fprintf(stderr, "usage: showindex [<repo-dir>]\n"); + return 1; + } + + if (git_repository_open_ext(&repo, dir, 0, NULL) < 0) { + fprintf(stderr, "could not open repository: %s\n", dir); + return 1; + } + + git_repository_index(&index, repo); + git_index_read(index); + + ecount = git_index_entrycount(index); + if (!ecount) + printf("Empty index\n"); + + for (i = 0; i < ecount; ++i) { + const git_index_entry *e = git_index_get_byindex(index, i); + + git_oid_fmt(out, &e->oid); + + printf("File Path: %s\n", e->path); + printf(" Stage: %d\n", git_index_entry_stage(e)); + printf(" Blob SHA: %s\n", out); + printf("File Size: %d\n", (int)e->file_size); + printf(" Device: %d\n", (int)e->dev); + printf(" Inode: %d\n", (int)e->ino); + printf(" UID: %d\n", (int)e->uid); + printf(" GID: %d\n", (int)e->gid); + printf(" ctime: %d\n", (int)e->ctime.seconds); + printf(" mtime: %d\n", (int)e->mtime.seconds); + printf("\n"); + } + + git_index_free(index); + git_repository_free(repo); + + return 0; } |
