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 +#include +#include +#include #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 #include +#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 -#include -#include -#include -#include -#ifndef _WIN32 -# include -# include -#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 -#include -#include - -/* 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 - -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 -#include -#include -#include -#ifndef _WIN32 -# include -# include -#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 -#include -#include - #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 -#include -#include -#include +#include "../common.h" + #include #include #include @@ -17,7 +15,6 @@ #else # include #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 -#include -#include -#include -#include "common.h" +#include "../common.h" static int use_remote(git_repository *repo, char *name) {