From 9ba49bb5c861651c5136ad6678fc1570801ddec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 23 Jun 2011 03:04:23 +0200 Subject: [PATCH] Add git_remote_connect and git_remote_ls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These allow you to implement git-ls-remote when given a reference name and a repository. Signed-off-by: Carlos Martín Nieto --- include/git2/remote.h | 21 +++++++++++++++++++++ src/remote.c | 34 ++++++++++++++++++++++++++++++++++ src/remote.h | 2 ++ 3 files changed, 57 insertions(+) diff --git a/include/git2/remote.h b/include/git2/remote.h index ccc130578..9a988f36c 100644 --- a/include/git2/remote.h +++ b/include/git2/remote.h @@ -56,6 +56,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 * diff --git a/src/remote.c b/src/remote.c index aa22debce..59ea6a797 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 afd2d1bb9..fdd6cd569 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