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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
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)
{