summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-01-24 10:30:58 +0100
committerPatrick Steinhardt <ps@pks.im>2019-02-15 12:04:14 +0100
commit7562422ab9310b81142986f69964194f491948cc (patch)
treec6db8953aba8939257b83b9fbc0b86ef9db0e632
parentc4271314e10b13acbe57c4a1dac0209d1c14a28a (diff)
downloadlibgit2-7562422ab9310b81142986f69964194f491948cc.tar.gz
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.
-rw-r--r--examples/CMakeLists.txt2
-rw-r--r--examples/common.c76
-rw-r--r--examples/common.h25
-rw-r--r--examples/network/clone.c11
-rw-r--r--examples/network/common.c85
-rw-r--r--examples/network/common.h30
-rw-r--r--examples/network/fetch.c10
-rw-r--r--examples/network/git2.c7
-rw-r--r--examples/network/index-pack.c7
-rw-r--r--examples/network/ls-remote.c6
10 files changed, 109 insertions, 150 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index e631b96f2..c02af0684 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 e20767a9e..f1ee27e6a 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 1c7b2035e..63f25c7c5 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 bbcd2e848..970e84ff9 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 b0afb0238..000000000
--- 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 1b09caad4..000000000
--- 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 76b301193..bb5f1b189 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 d0d0eb610..4fa28390c 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 128c7ebf5..87445e439 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 fb258acbe..8181629b9 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)
{