Commit b67200186fe65cfeb2d87a3c408186ae1d17b091

Etienne Samson 2018-01-17T02:25:36

examples: Switch to the nifty argv helpers from common

diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index a6f26c7..99e2ba9 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -1,7 +1,7 @@
 LINK_DIRECTORIES(${LIBGIT2_LIBDIRS})
 INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
 
-FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c network/*.h)
+FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c network/*.h common.?)
 ADD_EXECUTABLE(cgit2 ${SRC_EXAMPLE_GIT2})
 IF(WIN32 OR ANDROID)
 	TARGET_LINK_LIBRARIES(cgit2 git2)
diff --git a/examples/network/git2.c b/examples/network/git2.c
index 448103c..1d3d27b 100644
--- a/examples/network/git2.c
+++ b/examples/network/git2.c
@@ -2,10 +2,11 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "../common.h"
 #include "common.h"
 
-// This part is not strictly libgit2-dependent, but you can use this
-// as a starting point for a git-like tool
+/* This part is not strictly libgit2-dependent, but you can use this
+ * as a starting point for a git-like tool */
 
 struct {
 	char *name;
@@ -18,20 +19,12 @@ struct {
 	{ NULL, NULL}
 };
 
-static int run_command(git_cb fn, int argc, char **argv)
+static int run_command(git_cb fn, git_repository *repo, struct args_info args)
 {
 	int error;
-	git_repository *repo;
-
-	// Before running the actual command, create an instance of the local
-	// repository and pass it to the function.
-
-	error = git_repository_open(&repo, ".git");
-	if (error < 0)
-		repo = NULL;
 
-	// Run the command. If something goes wrong, print the error message to stderr
-	error = fn(repo, argc, argv);
+	/* Run the command. If something goes wrong, print the error message to stderr */
+	error = fn(repo, args.argc - args.pos, &args.argv[args.pos]);
 	if (error < 0) {
 		if (giterr_last() == NULL)
 			fprintf(stderr, "Error without message");
@@ -39,9 +32,6 @@ static int run_command(git_cb fn, int argc, char **argv)
 			fprintf(stderr, "Bad news:\n %s\n", giterr_last()->message);
 	}
 
-	if(repo)
-		git_repository_free(repo);
-
 	return !!error;
 }
 
@@ -49,6 +39,10 @@ int main(int argc, char **argv)
 {
 	int i;
 	int return_code = 1;
+	int error;
+	git_repository *repo;
+	struct args_info args = ARGS_INFO_INIT;
+	const char *git_dir = NULL;
 
 	if (argc < 2) {
 		fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
@@ -57,9 +51,30 @@ int main(int argc, char **argv)
 
 	git_libgit2_init();
 
+	for (args.pos = 1; args.pos < args.argc; ++args.pos) {
+		char *a = args.argv[args.pos];
+
+		if (a[0] != '-') {
+			/* non-arg */
+			break;
+		} else if (optional_str_arg(&git_dir, &args, "--git-dir", ".git")) {
+			continue;
+		} else if (!strcmp(a, "--")) {
+			/* arg separator */
+			break;
+		}
+	}
+
+	/* Before running the actual command, create an instance of the local
+	 * repository and pass it to the function. */
+
+	error = git_repository_open(&repo, git_dir);
+	if (error < 0)
+		repo = NULL;
+
 	for (i = 0; commands[i].name != NULL; ++i) {
-		if (!strcmp(argv[1], commands[i].name)) {
-			return_code = run_command(commands[i].fn, --argc, ++argv);
+		if (!strcmp(args.argv[args.pos], commands[i].name)) {
+			return_code = run_command(commands[i].fn, repo, args);
 			goto shutdown;
 		}
 	}
@@ -67,6 +82,8 @@ int main(int argc, char **argv)
 	fprintf(stderr, "Command not found: %s\n", argv[1]);
 
 shutdown:
+	git_repository_free(repo);
+
 	git_libgit2_shutdown();
 
 	return return_code;