Commit e3c131c544bc79573ebefab4931b5ca89836ace1

Carlos Martín Nieto 2013-09-16T05:02:25

remote: move the credentials callback to the struct Move this one as well, letting us have a single way of setting the callbacks for the remote, and removing fields from the clone options.

diff --git a/examples/network/clone.c b/examples/network/clone.c
index f100265..f553c40 100644
--- a/examples/network/clone.c
+++ b/examples/network/clone.c
@@ -76,9 +76,9 @@ int do_clone(git_repository *repo, int argc, char **argv)
 	checkout_opts.progress_payload = &pd;
 	clone_opts.checkout_opts = checkout_opts;
 	callbacks.transfer_progress = &fetch_progress;
+	callbacks.credentials = cred_acquire_cb;
 	callbacks.payload = &pd;
 	clone_opts.remote_callbacks = &callbacks;
-	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/fetch.c b/examples/network/fetch.c
index 1de2233..0c545ad 100644
--- a/examples/network/fetch.c
+++ b/examples/network/fetch.c
@@ -91,8 +91,8 @@ int fetch(git_repository *repo, int argc, char **argv)
 	// Set up the callbacks (only update_tips for now)
 	callbacks.update_tips = &update_cb;
 	callbacks.progress = &progress_cb;
+	callbacks.credentials = cred_acquire_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 b22ac47..b65759e 100644
--- a/examples/network/ls-remote.c
+++ b/examples/network/ls-remote.c
@@ -18,6 +18,7 @@ static int use_remote(git_repository *repo, char *name)
 {
 	git_remote *remote = NULL;
 	int error;
+	git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
 
 	// Find the remote by name
 	error = git_remote_load(&remote, repo, name);
@@ -27,7 +28,8 @@ static int use_remote(git_repository *repo, char *name)
 			goto cleanup;
 	}
 
-	git_remote_set_cred_acquire_cb(remote, &cred_acquire_cb, NULL);
+	callbacks.credentials = cred_acquire_cb;
+	git_remote_set_callbacks(remote, &callbacks);
 
 	error = git_remote_connect(remote, GIT_DIRECTION_FETCH);
 	if (error < 0)
diff --git a/include/git2/clone.h b/include/git2/clone.h
index 122806a..38c759f 100644
--- a/include/git2/clone.h
+++ b/include/git2/clone.h
@@ -48,9 +48,6 @@ GIT_BEGIN_DECL
  *   results in the same behavior as GIT_REMOTE_DEFAULT_FETCH.
  * - `push_spec` is the fetch specification to be used for pushing.  NULL means
  *   use the same spec as for fetching.
- * - `cred_acquire_cb` is a callback to be used if credentials are required
- *   during the initial fetch.
- * - `cred_acquire_payload` is the payload for the above callback.
  * - `transport_flags` is flags used to create transport if no transport is
  *   provided.
  * - `transport` is a custom transport to be used for the initial fetch.  NULL
@@ -74,8 +71,6 @@ typedef struct git_clone_options {
 	const char *pushurl;
 	const char *fetch_spec;
 	const char *push_spec;
-	git_cred_acquire_cb cred_acquire_cb;
-	void *cred_acquire_payload;
     git_transport_flags_t transport_flags;
 	git_transport *transport;
 	git_remote_callbacks *remote_callbacks;
diff --git a/include/git2/remote.h b/include/git2/remote.h
index 2bde5e3..83ad195 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -395,6 +395,7 @@ struct git_remote_callbacks {
 	unsigned int version;
 	void (*progress)(const char *str, int len, void *data);
 	int (*completion)(git_remote_completion_type type, void *data);
+	int (*credentials)(git_cred **cred, const char *url, const char *username_from_url, unsigned int allowed_types,	void *data);
 	int (*transfer_progress)(const git_transfer_progress *stats, void *data);
 	int (*update_tips)(const char *refname, const git_oid *a, const git_oid *b, void *data);
 	void *payload;
diff --git a/src/clone.c b/src/clone.c
index 90d677b..8385fb2 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -310,8 +310,6 @@ static int create_and_configure_origin(
 	if ((error = git_remote_create(&origin, repo, options->remote_name, url)) < 0)
 		goto on_error;
 
-	git_remote_set_cred_acquire_cb(origin, options->cred_acquire_cb,
-			options->cred_acquire_payload);
 	git_remote_set_autotag(origin, options->remote_autotag);
 	/*
 	 * Don't write FETCH_HEAD, we'll check out the remote tracking
diff --git a/src/remote.c b/src/remote.c
index e4696c4..2d0321e 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -591,7 +591,7 @@ int git_remote_connect(git_remote *remote, git_direction direction)
 	if (!remote->check_cert)
 		flags |= GIT_TRANSPORTFLAGS_NO_CHECK_CERT;
 
-	if (t->connect(t, url, remote->cred_acquire_cb, remote->cred_acquire_payload, direction, flags) < 0)
+	if (t->connect(t, url, remote->callbacks.credentials, remote->callbacks.payload, direction, flags) < 0)
 		goto on_error;
 
 	remote->transport = t;
@@ -1152,17 +1152,6 @@ int git_remote_set_callbacks(git_remote *remote, git_remote_callbacks *callbacks
 	return 0;
 }
 
-void git_remote_set_cred_acquire_cb(
-	git_remote *remote,
-	git_cred_acquire_cb cred_acquire_cb,
-	void *payload)
-{
-	assert(remote);
-
-	remote->cred_acquire_cb = cred_acquire_cb;
-	remote->cred_acquire_payload = payload;
-}
-
 int git_remote_set_transport(git_remote *remote, git_transport *transport)
 {
 	assert(remote && transport);
diff --git a/src/remote.h b/src/remote.h
index dce4803..269584d 100644
--- a/src/remote.h
+++ b/src/remote.h
@@ -21,8 +21,6 @@ struct git_remote {
 	char *pushurl;
 	git_vector refs;
 	git_vector refspecs;
-	git_cred_acquire_cb cred_acquire_cb;
-	void *cred_acquire_payload;
 	git_transport *transport;
 	git_repository *repo;
 	git_remote_callbacks callbacks;
diff --git a/src/transports/local.c b/src/transports/local.c
index 9ebea97..3c1f988 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -434,7 +434,7 @@ static int local_push(
 
 		if (!url || t->parent.close(&t->parent) < 0 ||
 			t->parent.connect(&t->parent, url,
-			push->remote->cred_acquire_cb, NULL, GIT_DIRECTION_PUSH, flags))
+			push->remote->callbacks.credentials, NULL, GIT_DIRECTION_PUSH, flags))
 			goto on_error;
 	}
 
diff --git a/tests-clar/online/clone.c b/tests-clar/online/clone.c
index bda2608..b82cbcd 100644
--- a/tests-clar/online/clone.c
+++ b/tests-clar/online/clone.c
@@ -155,11 +155,13 @@ void test_online_clone__credentials(void)
 		cl_getenv("GITTEST_REMOTE_USER"),
 		cl_getenv("GITTEST_REMOTE_PASS")
 	};
+	git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
 
 	if (!remote_url) return;
 
-	g_options.cred_acquire_cb = git_cred_userpass;
-	g_options.cred_acquire_payload = &user_pass;
+	callbacks.credentials = git_cred_userpass;
+	callbacks.payload = &user_pass;
+	g_options.remote_callbacks = &callbacks;
 
 	cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options));
 	git_repository_free(g_repo); g_repo = NULL;
@@ -172,8 +174,11 @@ void test_online_clone__bitbucket_style(void)
 		"libgit2", "libgit2"
 	};
 
-	g_options.cred_acquire_cb = git_cred_userpass;
-	g_options.cred_acquire_payload = &user_pass;
+	git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
+
+	callbacks.credentials = git_cred_userpass;
+	callbacks.payload = &user_pass;
+	g_options.remote_callbacks = &callbacks;
 
 	cl_git_pass(git_clone(&g_repo, BB_REPO_URL, "./foo", &g_options));
 	git_repository_free(g_repo); g_repo = NULL;
diff --git a/tests-clar/online/push.c b/tests-clar/online/push.c
index d0d4ed0..05cef56 100644
--- a/tests-clar/online/push.c
+++ b/tests-clar/online/push.c
@@ -17,6 +17,8 @@ static char *_remote_url;
 static char *_remote_user;
 static char *_remote_pass;
 
+static int cred_acquire_cb(git_cred **,	const char *, const char *, unsigned int, void *);
+
 static git_remote *_remote;
 static bool _cred_acquire_called;
 static record_callbacks_data _record_cbs_data = {{ 0 }};
@@ -294,7 +296,6 @@ void test_online_push__initialize(void)
 	if (_remote_url) {
 		cl_git_pass(git_remote_create(&_remote, _repo, "test", _remote_url));
 
-		git_remote_set_cred_acquire_cb(_remote, cred_acquire_cb, &_cred_acquire_called);
 		record_callbacks_data_clear(&_record_cbs_data);
 		git_remote_set_callbacks(_remote, &_record_cbs);
 
diff --git a/tests-clar/online/push_util.h b/tests-clar/online/push_util.h
index 659c6dd..64f02cf 100644
--- a/tests-clar/online/push_util.h
+++ b/tests-clar/online/push_util.h
@@ -12,7 +12,7 @@ extern const git_oid OID_ZERO;
  * @param data pointer to a record_callbacks_data instance
  */
 #define RECORD_CALLBACKS_INIT(data) \
-	{ GIT_REMOTE_CALLBACKS_VERSION, NULL, NULL, NULL, record_update_tips_cb, data }
+	{ GIT_REMOTE_CALLBACKS_VERSION, NULL, NULL, cred_acquire_cb, NULL, record_update_tips_cb, data }
 
 typedef struct {
 	char *name;