Merge pull request #1822 from kadamski/examples-cleanup Small cleanup in examples.
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
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1c70ec2..317ed1b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -426,4 +426,19 @@ IF (BUILD_EXAMPLES)
ADD_EXECUTABLE(git-rev-list examples/rev-list.c)
TARGET_LINK_LIBRARIES(git-rev-list git2)
+
+ ADD_EXECUTABLE(git-rev-parse examples/rev-parse.c)
+ TARGET_LINK_LIBRARIES(git-rev-parse git2)
+
+ ADD_EXECUTABLE(git-log examples/log.c)
+ TARGET_LINK_LIBRARIES(git-log git2)
+
+ ADD_EXECUTABLE(git-status examples/status.c)
+ TARGET_LINK_LIBRARIES(git-status git2)
+
+ ADD_EXECUTABLE(git-init examples/init.c)
+ TARGET_LINK_LIBRARIES(git-init git2)
+
+ ADD_EXECUTABLE(git-cat-file examples/cat-file.c)
+ TARGET_LINK_LIBRARIES(git-cat-file git2)
ENDIF ()
diff --git a/examples/network/Makefile b/examples/network/Makefile
index 810eb70..f65c6cb 100644
--- a/examples/network/Makefile
+++ b/examples/network/Makefile
@@ -11,7 +11,8 @@ OBJECTS = \
ls-remote.o \
fetch.o \
clone.o \
- index-pack.o
+ index-pack.o \
+ common.o
all: $(OBJECTS)
$(CC) $(CFLAGS) $(LDFLAGS) -o git2 $(OBJECTS) $(LIBRARIES)
diff --git a/examples/network/clone.c b/examples/network/clone.c
index 00c25c1..a09a947 100644
--- a/examples/network/clone.c
+++ b/examples/network/clone.c
@@ -9,19 +9,6 @@
# include <unistd.h>
#endif
-/* 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
-
typedef struct progress_data {
git_transfer_progress fetch_progress;
size_t completed_steps;
@@ -63,24 +50,6 @@ static void checkout_progress(const char *path, size_t cur, size_t tot, void *pa
print_progress(pd);
}
-static int cred_acquire(git_cred **out,
- const char * UNUSED(url),
- const char * UNUSED(username_from_url),
- unsigned int UNUSED(allowed_types),
- void * UNUSED(payload))
-{
- char username[128] = {0};
- char password[128] = {0};
-
- printf("Username: ");
- scanf("%s", username);
-
- /* Yup. Right there on your terminal. Careful where you copy/paste output. */
- printf("Password: ");
- scanf("%s", password);
-
- return git_cred_userpass_plaintext_new(out, username, password);
-}
int do_clone(git_repository *repo, int argc, char **argv)
{
@@ -107,7 +76,7 @@ int do_clone(git_repository *repo, int argc, char **argv)
clone_opts.checkout_opts = checkout_opts;
clone_opts.fetch_progress_cb = &fetch_progress;
clone_opts.fetch_progress_payload = &pd;
- clone_opts.cred_acquire_cb = cred_acquire;
+ clone_opts.cred_acquire_cb = cred_acquire_cb;
// Do the clone
error = git_clone(&cloned_repo, url, path, &clone_opts);
diff --git a/examples/network/common.c b/examples/network/common.c
new file mode 100644
index 0000000..d123eed
--- /dev/null
+++ b/examples/network/common.c
@@ -0,0 +1,34 @@
+#include "common.h"
+#include <stdio.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
+
+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[128] = {0};
+ char password[128] = {0};
+
+ printf("Username: ");
+ scanf("%s", username);
+
+ /* Yup. Right there on your terminal. Careful where you copy/paste output. */
+ printf("Password: ");
+ scanf("%s", password);
+
+ return git_cred_userpass_plaintext_new(out, username, password);
+}
diff --git a/examples/network/common.h b/examples/network/common.h
index a4cfa1a..1b09caa 100644
--- a/examples/network/common.h
+++ b/examples/network/common.h
@@ -12,6 +12,12 @@ 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__)
diff --git a/examples/network/fetch.c b/examples/network/fetch.c
index 6020ec6..ce016ce 100644
--- a/examples/network/fetch.c
+++ b/examples/network/fetch.c
@@ -92,6 +92,7 @@ int fetch(git_repository *repo, int argc, char **argv)
callbacks.update_tips = &update_cb;
callbacks.progress = &progress_cb;
git_remote_set_callbacks(remote, &callbacks);
+ git_remote_set_cred_acquire_cb(remote, &cred_acquire_cb, NULL);
// Set up the information for the background worker thread
data.remote = remote;
diff --git a/examples/network/ls-remote.c b/examples/network/ls-remote.c
index 2520118..b22ac47 100644
--- a/examples/network/ls-remote.c
+++ b/examples/network/ls-remote.c
@@ -14,31 +14,6 @@ static int show_ref__cb(git_remote_head *head, void *payload)
return 0;
}
-static int use_unnamed(git_repository *repo, const char *url)
-{
- git_remote *remote = NULL;
- int error;
-
- // Create an instance of a remote from the URL. The transport to use
- // is detected from the URL
- error = git_remote_create_inmemory(&remote, repo, NULL, url);
- if (error < 0)
- goto cleanup;
-
- // When connecting, the underlying code needs to know wether we
- // want to push or fetch
- error = git_remote_connect(remote, GIT_DIRECTION_FETCH);
- if (error < 0)
- goto cleanup;
-
- // With git_remote_ls we can retrieve the advertised heads
- error = git_remote_ls(remote, &show_ref__cb, NULL);
-
-cleanup:
- git_remote_free(remote);
- return error;
-}
-
static int use_remote(git_repository *repo, char *name)
{
git_remote *remote = NULL;
@@ -46,8 +21,13 @@ static int use_remote(git_repository *repo, char *name)
// Find the remote by name
error = git_remote_load(&remote, repo, name);
- if (error < 0)
- goto cleanup;
+ if (error < 0) {
+ error = git_remote_create_inmemory(&remote, repo, NULL, name);
+ if (error < 0)
+ goto cleanup;
+ }
+
+ git_remote_set_cred_acquire_cb(remote, &cred_acquire_cb, NULL);
error = git_remote_connect(remote, GIT_DIRECTION_FETCH);
if (error < 0)
@@ -72,12 +52,7 @@ int ls_remote(git_repository *repo, int argc, char **argv)
return EXIT_FAILURE;
}
- /* If there's a ':' in the name, assume it's an URL */
- if (strchr(argv[1], ':') != NULL) {
- error = use_unnamed(repo, argv[1]);
- } else {
- error = use_remote(repo, argv[1]);
- }
+ error = use_remote(repo, argv[1]);
return error;
}