Commit 9ba49bb5c861651c5136ad6678fc1570801ddec1

Carlos Martín Nieto 2011-06-23T03:04:23

Add git_remote_connect and git_remote_ls These allow you to implement git-ls-remote when given a reference name and a repository. Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>

diff --git a/include/git2/remote.h b/include/git2/remote.h
index ccc1305..9a988f3 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -57,6 +57,27 @@ GIT_EXTERN(const git_refspec *) git_remote_fetchspec(struct git_remote *remote);
 GIT_EXTERN(const git_refspec *) git_remote_fetchspec(struct git_remote *remote);
 
 /**
+ * Open a connection to a remote
+ *
+ * The transport is selected based on the URL
+ *
+ * @param remote the remote to connect to
+ * @return GIT_SUCCESS or an error code
+ */
+GIT_EXTERN(int) git_remote_connect(struct git_remote *remote, git_net_direction dir);
+
+/**
+ * Get a list of refs at the remote
+ *
+ * The remote (or more exactly its transport) must be connected.
+ *
+ * @param refs where to store the refs
+ * @param remote the remote
+ * @return GIT_SUCCESS or an error code
+ */
+GIT_EXTERN(int) git_remote_ls(git_remote *remote, git_headarray *refs);
+
+/**
  * Free the memory associated with a remote
  *
  * @param remote the remote to free
diff --git a/src/remote.c b/src/remote.c
index aa22deb..59ea6a7 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -172,6 +172,35 @@ const git_refspec *git_remote_pushspec(struct git_remote *remote)
 	return &remote->push;
 }
 
+int git_remote_connect(git_remote *remote, git_net_direction dir)
+{
+	int error;
+	git_transport *t;
+
+	error = git_transport_new(&t, remote->url);
+	if (error < GIT_SUCCESS)
+		return git__rethrow(error, "Failed to create transport");
+
+	error = git_transport_connect(t, dir);
+	if (error < GIT_SUCCESS) {
+		error = git__rethrow(error, "Failed to connect the transport");
+		goto cleanup;
+	}
+
+	remote->transport = t;
+
+cleanup:
+	if (error < GIT_SUCCESS)
+		git_transport_free(t);
+
+	return error;
+}
+
+int git_remote_ls(git_remote *remote, git_headarray *refs)
+{
+	return git_transport_ls(remote->transport, refs);
+}
+
 void git_remote_free(git_remote *remote)
 {
 	free(remote->fetch.src);
@@ -180,5 +209,10 @@ void git_remote_free(git_remote *remote)
 	free(remote->push.dst);
 	free(remote->url);
 	free(remote->name);
+	if (remote->transport != NULL) {
+		if (remote->transport->connected)
+			git_transport_close(remote->transport);
+		git_transport_free(remote->transport);
+	}
 	free(remote);
 }
diff --git a/src/remote.h b/src/remote.h
index afd2d1b..fdd6cd5 100644
--- a/src/remote.h
+++ b/src/remote.h
@@ -3,12 +3,14 @@
 
 #include "remote.h"
 #include "refspec.h"
+#include "transport.h"
 
 struct git_remote {
 	char *name;
 	char *url;
 	struct git_refspec fetch;
 	struct git_refspec push;
+	git_transport *transport;
 };
 
 #endif