examples: buff up rev-list by adding OID support

This allows the example to be used as a quick revwalk test harness.
parent 313908f9
...@@ -15,16 +15,24 @@ ...@@ -15,16 +15,24 @@
#include "common.h" #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) int lg2_rev_list(git_repository *repo, int argc, char **argv)
{ {
struct args_info args = ARGS_INFO_INIT;
git_revwalk *walk; git_revwalk *walk;
git_oid oid; git_oid oid;
git_sort_t sort;
char buf[GIT_OID_HEXSZ+1]; 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(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)) { while (!git_revwalk_next(&oid, walk)) {
git_oid_fmt(buf, &oid); git_oid_fmt(buf, &oid);
...@@ -32,6 +40,7 @@ int lg2_rev_list(git_repository *repo, int argc, char **argv) ...@@ -32,6 +40,7 @@ int lg2_rev_list(git_repository *repo, int argc, char **argv)
printf("%s\n", buf); printf("%s\n", buf);
} }
git_revwalk_free(walk);
return 0; return 0;
} }
...@@ -80,33 +89,60 @@ out: ...@@ -80,33 +89,60 @@ out:
return error; 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; assert(sort && args);
unsigned int sorting = GIT_SORT_NONE; *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; hide = 0;
for (i = 0; i < nopts; i++) { for (; args->pos < args->argc; ++args->pos) {
if (!strcmp(opts[i], "--topo-order")) { const char *curr = args->argv[args->pos];
sorting = GIT_SORT_TOPOLOGICAL | (sorting & GIT_SORT_REVERSE);
git_revwalk_sorting(walk, sorting); if (!strcmp(curr, "--not")) {
} 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")) {
hide = !hide; hide = !hide;
} else if (opts[i][0] == '^') { } else if (curr[0] == '^') {
if ((error = push_spec(repo, walk, opts[i] + 1, !hide))) if ((error = push_spec(repo, walk, curr + 1, !hide)))
return error; return error;
} else if (strstr(opts[i], "..")) { } else if (strstr(curr, "..")) {
if ((error = push_range(repo, walk, opts[i], hide))) if ((error = push_range(repo, walk, curr, hide)))
return error; return error;
} else { } 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; return error;
} }
} }
......
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