Commit 7562422ab9310b81142986f69964194f491948cc

Patrick Steinhardt 2019-01-24T10:30:58

examples: merge common network code Right now, we have two sets of "common" code, one containing general common code and one containing network common code. As we intend to get rid of the network subdirectory and instead merge all examples into a single standalone executable, this distinction doesn't make a lot of sense now. Furthermore, the common network code is not that big. Let's get rid of the common network code by merging it into the general common code.

diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index e631b96..c02af06 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -1,7 +1,7 @@
 INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
 INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
 
-FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c network/*.h common.?)
+FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c common.?)
 ADD_EXECUTABLE(cgit2 ${SRC_EXAMPLE_GIT2})
 SET_TARGET_PROPERTIES(cgit2 PROPERTIES C_STANDARD 90)
 
diff --git a/examples/common.c b/examples/common.c
index e20767a..f1ee27e 100644
--- a/examples/common.c
+++ b/examples/common.c
@@ -13,6 +13,9 @@
  */
 
 #include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
 
 #include "common.h"
 
@@ -289,3 +292,76 @@ int resolve_refish(git_annotated_commit **commit, git_repository *repo, const ch
 
 	return err;
 }
+
+static int readline(char **out)
+{
+	int c, error = 0, length = 0, allocated = 0;
+	char *line = NULL;
+
+	errno = 0;
+
+	while ((c = getchar()) != EOF) {
+		if (length == allocated) {
+			allocated += 16;
+
+			if ((line = realloc(line, allocated)) == NULL) {
+				error = -1;
+				goto error;
+			}
+		}
+
+		if (c == '\n')
+			break;
+
+		line[length++] = c;
+	}
+
+	if (errno != 0) {
+		error = -1;
+		goto error;
+	}
+
+	line[length] = '\0';
+	*out = line;
+	line = NULL;
+	error = length;
+error:
+	free(line);
+	return error;
+}
+
+int cred_acquire_cb(git_cred **out,
+		const char *url,
+		const char *username_from_url,
+		unsigned int allowed_types,
+		void *payload)
+{
+	char *username = NULL, *password = NULL;
+	int error;
+
+	UNUSED(url);
+	UNUSED(username_from_url);
+	UNUSED(allowed_types);
+	UNUSED(payload);
+
+	printf("Username: ");
+	if (readline(&username) < 0) {
+		fprintf(stderr, "Unable to read username: %s", strerror(errno));
+		return -1;
+	}
+
+	/* Yup. Right there on your terminal. Careful where you copy/paste output. */
+	printf("Password: ");
+	if (readline(&password) < 0) {
+		fprintf(stderr, "Unable to read password: %s", strerror(errno));
+		free(username);
+		return -1;
+	}
+
+	error = git_cred_userpass_plaintext_new(out, username, password);
+
+	free(username);
+	free(password);
+
+	return error;
+}
diff --git a/examples/common.h b/examples/common.h
index 1c7b203..63f25c7 100644
--- a/examples/common.h
+++ b/examples/common.h
@@ -17,6 +17,17 @@
 #include <stdlib.h>
 #include <git2.h>
 
+#ifndef PRIuZ
+/* Define the printf format specifer to use for size_t output */
+#if defined(_MSC_VER) || defined(__MINGW32__)
+#	define PRIuZ "Iu"
+#else
+#	define PRIuZ "zu"
+#endif
+#endif
+
+#define UNUSED(x) (void)(x)
+
 /**
  * Check libgit2 error code, printing error to stderr on failure and
  * exiting the program.
@@ -122,3 +133,17 @@ extern void *xrealloc(void *oldp, size_t newsz);
  * Convert a refish to an annotated commit.
  */
 extern int resolve_refish(git_annotated_commit **commit, git_repository *repo, const char *refish);
+
+/**
+ * Acquire credentials via command line
+ */
+extern int cred_acquire_cb(git_cred **out,
+		const char *url,
+		const char *username_from_url,
+		unsigned int allowed_types,
+		void *payload);
+
+extern int ls_remote(git_repository *repo, int argc, char **argv);
+extern int fetch(git_repository *repo, int argc, char **argv);
+extern int index_pack(git_repository *repo, int argc, char **argv);
+extern int do_clone(git_repository *repo, int argc, char **argv);
diff --git a/examples/network/clone.c b/examples/network/clone.c
index bbcd2e8..970e84f 100644
--- a/examples/network/clone.c
+++ b/examples/network/clone.c
@@ -1,13 +1,4 @@
-#include "common.h"
-#include <git2.h>
-#include <git2/clone.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#ifndef _WIN32
-# include <pthread.h>
-# include <unistd.h>
-#endif
+#include "../common.h"
 
 typedef struct progress_data {
 	git_transfer_progress fetch_progress;
diff --git a/examples/network/common.c b/examples/network/common.c
deleted file mode 100644
index b0afb02..0000000
--- a/examples/network/common.c
+++ /dev/null
@@ -1,85 +0,0 @@
-#include "common.h"
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-
-/* Shamelessly borrowed from http://stackoverflow.com/questions/3417837/
- * with permission of the original author, Martin Pool.
- * http://sourcefrog.net/weblog/software/languages/C/unused.html
- */
-#ifdef UNUSED
-#elif defined(__GNUC__)
-# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
-#elif defined(__LCLINT__)
-# define UNUSED(x) /*@unused@*/ x
-#else
-# define UNUSED(x) x
-#endif
-
-static int readline(char **out)
-{
-	int c, error = 0, length = 0, allocated = 0;
-	char *line = NULL;
-
-	errno = 0;
-
-	while ((c = getchar()) != EOF) {
-		if (length == allocated) {
-			allocated += 16;
-
-			if ((line = realloc(line, allocated)) == NULL) {
-				error = -1;
-				goto error;
-			}
-		}
-
-		if (c == '\n')
-			break;
-
-		line[length++] = c;
-	}
-
-	if (errno != 0) {
-		error = -1;
-		goto error;
-	}
-
-	line[length] = '\0';
-	*out = line;
-	line = NULL;
-	error = length;
-error:
-	free(line);
-	return error;
-}
-
-int cred_acquire_cb(git_cred **out,
-		const char * UNUSED(url),
-		const char * UNUSED(username_from_url),
-		unsigned int UNUSED(allowed_types),
-		void * UNUSED(payload))
-{
-	char *username = NULL, *password = NULL;
-	int error;
-
-	printf("Username: ");
-	if (readline(&username) < 0) {
-		fprintf(stderr, "Unable to read username: %s", strerror(errno));
-		return -1;
-	}
-
-	/* Yup. Right there on your terminal. Careful where you copy/paste output. */
-	printf("Password: ");
-	if (readline(&password) < 0) {
-		fprintf(stderr, "Unable to read password: %s", strerror(errno));
-		free(username);
-		return -1;
-	}
-
-	error = git_cred_userpass_plaintext_new(out, username, password);
-
-	free(username);
-	free(password);
-
-	return error;
-}
diff --git a/examples/network/common.h b/examples/network/common.h
deleted file mode 100644
index 1b09caa..0000000
--- a/examples/network/common.h
+++ /dev/null
@@ -1,30 +0,0 @@
-#ifndef __COMMON_H__
-#define __COMMON_H__
-
-#include <git2.h>
-
-typedef int (*git_cb)(git_repository *, int , char **);
-
-int ls_remote(git_repository *repo, int argc, char **argv);
-int parse_pkt_line(git_repository *repo, int argc, char **argv);
-int show_remote(git_repository *repo, int argc, char **argv);
-int fetch(git_repository *repo, int argc, char **argv);
-int index_pack(git_repository *repo, int argc, char **argv);
-int do_clone(git_repository *repo, int argc, char **argv);
-
-int cred_acquire_cb(git_cred **out,
-		const char * url,
-		const char * username_from_url,
-		unsigned int allowed_types,
-		void *payload);
-
-#ifndef PRIuZ
-/* Define the printf format specifer to use for size_t output */
-#if defined(_MSC_VER) || defined(__MINGW32__)
-#	define PRIuZ "Iu"
-#else
-#	define PRIuZ "zu"
-#endif
-#endif
-
-#endif /* __COMMON_H__ */
diff --git a/examples/network/fetch.c b/examples/network/fetch.c
index 76b3011..bb5f1b1 100644
--- a/examples/network/fetch.c
+++ b/examples/network/fetch.c
@@ -1,12 +1,4 @@
-#include "common.h"
-#include <git2.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#ifndef _WIN32
-# include <pthread.h>
-# include <unistd.h>
-#endif
+#include "../common.h"
 
 static int progress_cb(const char *str, int len, void *data)
 {
diff --git a/examples/network/git2.c b/examples/network/git2.c
index d0d0eb6..4fa2839 100644
--- a/examples/network/git2.c
+++ b/examples/network/git2.c
@@ -1,13 +1,10 @@
-#include <stdlib.h>
-#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 */
 
+typedef int (*git_cb)(git_repository *, int , char **);
+
 struct {
 	char *name;
 	git_cb fn;
diff --git a/examples/network/index-pack.c b/examples/network/index-pack.c
index 128c7eb..87445e4 100644
--- a/examples/network/index-pack.c
+++ b/examples/network/index-pack.c
@@ -1,7 +1,5 @@
-#include <git2.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
+#include "../common.h"
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -17,7 +15,6 @@
 #else
 # include <unistd.h>
 #endif
-#include "common.h"
 
 /*
  * This could be run in the main loop whilst the application waits for
diff --git a/examples/network/ls-remote.c b/examples/network/ls-remote.c
index fb258ac..8181629 100644
--- a/examples/network/ls-remote.c
+++ b/examples/network/ls-remote.c
@@ -1,8 +1,4 @@
-#include <git2.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include "common.h"
+#include "../common.h"
 
 static int use_remote(git_repository *repo, char *name)
 {