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>
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
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