Skip to content

Commit 7562422

Browse files
committed
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.
1 parent c427131 commit 7562422

File tree

10 files changed

+109
-150
lines changed

10 files changed

+109
-150
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
22
INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
33

4-
FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c network/*.h common.?)
4+
FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c common.?)
55
ADD_EXECUTABLE(cgit2 ${SRC_EXAMPLE_GIT2})
66
SET_TARGET_PROPERTIES(cgit2 PROPERTIES C_STANDARD 90)
77

examples/common.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
*/
1414

1515
#include <assert.h>
16+
#include <stdio.h>
17+
#include <string.h>
18+
#include <errno.h>
1619

1720
#include "common.h"
1821

@@ -289,3 +292,76 @@ int resolve_refish(git_annotated_commit **commit, git_repository *repo, const ch
289292

290293
return err;
291294
}
295+
296+
static int readline(char **out)
297+
{
298+
int c, error = 0, length = 0, allocated = 0;
299+
char *line = NULL;
300+
301+
errno = 0;
302+
303+
while ((c = getchar()) != EOF) {
304+
if (length == allocated) {
305+
allocated += 16;
306+
307+
if ((line = realloc(line, allocated)) == NULL) {
308+
error = -1;
309+
goto error;
310+
}
311+
}
312+
313+
if (c == '\n')
314+
break;
315+
316+
line[length++] = c;
317+
}
318+
319+
if (errno != 0) {
320+
error = -1;
321+
goto error;
322+
}
323+
324+
line[length] = '\0';
325+
*out = line;
326+
line = NULL;
327+
error = length;
328+
error:
329+
free(line);
330+
return error;
331+
}
332+
333+
int cred_acquire_cb(git_cred **out,
334+
const char *url,
335+
const char *username_from_url,
336+
unsigned int allowed_types,
337+
void *payload)
338+
{
339+
char *username = NULL, *password = NULL;
340+
int error;
341+
342+
UNUSED(url);
343+
UNUSED(username_from_url);
344+
UNUSED(allowed_types);
345+
UNUSED(payload);
346+
347+
printf("Username: ");
348+
if (readline(&username) < 0) {
349+
fprintf(stderr, "Unable to read username: %s", strerror(errno));
350+
return -1;
351+
}
352+
353+
/* Yup. Right there on your terminal. Careful where you copy/paste output. */
354+
printf("Password: ");
355+
if (readline(&password) < 0) {
356+
fprintf(stderr, "Unable to read password: %s", strerror(errno));
357+
free(username);
358+
return -1;
359+
}
360+
361+
error = git_cred_userpass_plaintext_new(out, username, password);
362+
363+
free(username);
364+
free(password);
365+
366+
return error;
367+
}

examples/common.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
#include <stdlib.h>
1818
#include <git2.h>
1919

20+
#ifndef PRIuZ
21+
/* Define the printf format specifer to use for size_t output */
22+
#if defined(_MSC_VER) || defined(__MINGW32__)
23+
# define PRIuZ "Iu"
24+
#else
25+
# define PRIuZ "zu"
26+
#endif
27+
#endif
28+
29+
#define UNUSED(x) (void)(x)
30+
2031
/**
2132
* Check libgit2 error code, printing error to stderr on failure and
2233
* exiting the program.
@@ -122,3 +133,17 @@ extern void *xrealloc(void *oldp, size_t newsz);
122133
* Convert a refish to an annotated commit.
123134
*/
124135
extern int resolve_refish(git_annotated_commit **commit, git_repository *repo, const char *refish);
136+
137+
/**
138+
* Acquire credentials via command line
139+
*/
140+
extern int cred_acquire_cb(git_cred **out,
141+
const char *url,
142+
const char *username_from_url,
143+
unsigned int allowed_types,
144+
void *payload);
145+
146+
extern int ls_remote(git_repository *repo, int argc, char **argv);
147+
extern int fetch(git_repository *repo, int argc, char **argv);
148+
extern int index_pack(git_repository *repo, int argc, char **argv);
149+
extern int do_clone(git_repository *repo, int argc, char **argv);

examples/network/clone.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
#include "common.h"
2-
#include <git2.h>
3-
#include <git2/clone.h>
4-
#include <stdio.h>
5-
#include <stdlib.h>
6-
#include <string.h>
7-
#ifndef _WIN32
8-
# include <pthread.h>
9-
# include <unistd.h>
10-
#endif
1+
#include "../common.h"
112

123
typedef struct progress_data {
134
git_transfer_progress fetch_progress;

examples/network/common.c

Lines changed: 0 additions & 85 deletions
This file was deleted.

examples/network/common.h

Lines changed: 0 additions & 30 deletions
This file was deleted.

examples/network/fetch.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
#include "common.h"
2-
#include <git2.h>
3-
#include <stdio.h>
4-
#include <stdlib.h>
5-
#include <string.h>
6-
#ifndef _WIN32
7-
# include <pthread.h>
8-
# include <unistd.h>
9-
#endif
1+
#include "../common.h"
102

113
static int progress_cb(const char *str, int len, void *data)
124
{

examples/network/git2.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
#include <stdlib.h>
2-
#include <stdio.h>
3-
#include <string.h>
4-
51
#include "../common.h"
6-
#include "common.h"
72

83
/* This part is not strictly libgit2-dependent, but you can use this
94
* as a starting point for a git-like tool */
105

6+
typedef int (*git_cb)(git_repository *, int , char **);
7+
118
struct {
129
char *name;
1310
git_cb fn;

examples/network/index-pack.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
#include <git2.h>
2-
#include <stdlib.h>
3-
#include <stdio.h>
4-
#include <string.h>
1+
#include "../common.h"
2+
53
#include <sys/types.h>
64
#include <sys/stat.h>
75
#include <fcntl.h>
@@ -17,7 +15,6 @@
1715
#else
1816
# include <unistd.h>
1917
#endif
20-
#include "common.h"
2118

2219
/*
2320
* This could be run in the main loop whilst the application waits for

examples/network/ls-remote.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
#include <git2.h>
2-
#include <stdlib.h>
3-
#include <stdio.h>
4-
#include <string.h>
5-
#include "common.h"
1+
#include "../common.h"
62

73
static int use_remote(git_repository *repo, char *name)
84
{

0 commit comments

Comments
 (0)