Commit 7562422a by Patrick Steinhardt

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.
parent c4271314
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)
......
......@@ -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;
}
......@@ -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);
#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;
......
#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;
}
#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__ */
#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)
{
......
#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;
......
#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
......
#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)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment