examples: buff up rev-list by adding OID support This allows the example to be used as a quick revwalk test harness.
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
diff --git a/examples/rev-list.c b/examples/rev-list.c
index c366ece..75fb19e 100644
--- a/examples/rev-list.c
+++ b/examples/rev-list.c
@@ -15,16 +15,24 @@
#include "common.h"
-static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts, char **opts);
+#include <assert.h>
+
+static int revwalk_parse_options(git_sort_t *sort, struct args_info *args);
+static int revwalk_parse_revs(git_repository *repo, git_revwalk *walk, struct args_info *args);
int lg2_rev_list(git_repository *repo, int argc, char **argv)
{
+ struct args_info args = ARGS_INFO_INIT;
git_revwalk *walk;
git_oid oid;
+ git_sort_t sort;
char buf[GIT_OID_HEXSZ+1];
+ check_lg2(revwalk_parse_options(&sort, &args), "parsing options", NULL);
+
check_lg2(git_revwalk_new(&walk, repo), "allocating revwalk", NULL);
- check_lg2(revwalk_parseopts(repo, walk, argc-1, argv+1), "parsing options", NULL);
+ git_revwalk_sorting(walk, sort);
+ check_lg2(revwalk_parse_revs(repo, walk, &args), "parsing revs", NULL);
while (!git_revwalk_next(&oid, walk)) {
git_oid_fmt(buf, &oid);
@@ -32,6 +40,7 @@ int lg2_rev_list(git_repository *repo, int argc, char **argv)
printf("%s\n", buf);
}
+ git_revwalk_free(walk);
return 0;
}
@@ -80,33 +89,60 @@ out:
return error;
}
-static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts, char **opts)
+static void print_usage(void)
+{
+ fprintf(stderr, "rev-list [--git-dir=dir] [--topo-order|--date-order] [--reverse] <revspec>\n");
+ exit(-1);
+}
+
+static int revwalk_parse_options(git_sort_t *sort, struct args_info *args)
{
- int hide, i, error;
- unsigned int sorting = GIT_SORT_NONE;
+ assert(sort && args);
+ *sort = GIT_SORT_NONE;
+
+ if (args->argc < 1)
+ print_usage();
+
+ for (args->pos = 1; args->pos < args->argc; ++args->pos) {
+ const char *curr = args->argv[args->pos];
+
+ if (!strcmp(curr, "--topo-order")) {
+ *sort |= GIT_SORT_TOPOLOGICAL;
+ } else if (!strcmp(curr, "--date-order")) {
+ *sort |= GIT_SORT_TIME;
+ } else if (!strcmp(curr, "--reverse")) {
+ *sort |= (*sort & ~GIT_SORT_REVERSE) ^ GIT_SORT_REVERSE;
+ } else {
+ break;
+ }
+ }
+ return 0;
+}
+
+static int revwalk_parse_revs(git_repository *repo, git_revwalk *walk, struct args_info *args)
+{
+ int hide, error;
+ git_oid oid;
hide = 0;
- for (i = 0; i < nopts; i++) {
- if (!strcmp(opts[i], "--topo-order")) {
- sorting = GIT_SORT_TOPOLOGICAL | (sorting & GIT_SORT_REVERSE);
- git_revwalk_sorting(walk, sorting);
- } else if (!strcmp(opts[i], "--date-order")) {
- sorting = GIT_SORT_TIME | (sorting & GIT_SORT_REVERSE);
- git_revwalk_sorting(walk, sorting);
- } else if (!strcmp(opts[i], "--reverse")) {
- sorting = (sorting & ~GIT_SORT_REVERSE)
- | ((sorting & GIT_SORT_REVERSE) ? 0 : GIT_SORT_REVERSE);
- git_revwalk_sorting(walk, sorting);
- } else if (!strcmp(opts[i], "--not")) {
+ for (; args->pos < args->argc; ++args->pos) {
+ const char *curr = args->argv[args->pos];
+
+ if (!strcmp(curr, "--not")) {
hide = !hide;
- } else if (opts[i][0] == '^') {
- if ((error = push_spec(repo, walk, opts[i] + 1, !hide)))
+ } else if (curr[0] == '^') {
+ if ((error = push_spec(repo, walk, curr + 1, !hide)))
return error;
- } else if (strstr(opts[i], "..")) {
- if ((error = push_range(repo, walk, opts[i], hide)))
+ } else if (strstr(curr, "..")) {
+ if ((error = push_range(repo, walk, curr, hide)))
return error;
} else {
- if ((error = push_spec(repo, walk, opts[i], hide)))
+ if (push_spec(repo, walk, curr, hide) == 0)
+ continue;
+
+ if ((error = git_oid_fromstr(&oid, curr)))
+ return error;
+ if ((error = push_commit(walk, &oid, hide)))
return error;
}
}