Commit fe42557a6f17db9345f1b5f7cb17a9784e59ea5f

Etienne Samson 2019-11-06T11:08:52

examples: buff up rev-list by adding OID support This allows the example to be used as a quick revwalk test harness.

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;
 		}
 	}