summaryrefslogtreecommitdiff
path: root/examples/network/ls-remote.c
diff options
context:
space:
mode:
authorCarlos Martín Nieto <carlos@cmartin.tk>2011-10-03 00:33:13 +0200
committerCarlos Martín Nieto <carlos@cmartin.tk>2011-10-03 02:32:32 +0200
commit2401262778fa50cea30d1988cec45dcb82b50712 (patch)
tree80adb600f2bb02c941314dc725706827890e07c9 /examples/network/ls-remote.c
parentcd19ca9584bd01925e05e94e7f3bddae6880acda (diff)
downloadlibgit2-2401262778fa50cea30d1988cec45dcb82b50712.tar.gz
examples: add ls-remote, fetch and index-pack examples
Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
Diffstat (limited to 'examples/network/ls-remote.c')
-rw-r--r--examples/network/ls-remote.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/examples/network/ls-remote.c b/examples/network/ls-remote.c
new file mode 100644
index 000000000..77a9f215d
--- /dev/null
+++ b/examples/network/ls-remote.c
@@ -0,0 +1,104 @@
+#include <git2.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "common.h"
+
+static void show_refs(git_headarray *refs)
+{
+ int i;
+ git_remote_head *head;
+
+// Take each head that the remote has advertised, store the string
+// representation of the OID in a buffer and print it
+
+ for(i = 0; i < refs->len; ++i){
+ char oid[GIT_OID_HEXSZ + 1] = {0};
+ head = refs->heads[i];
+ git_oid_fmt(oid, &head->oid);
+ printf("%s\t%s\n", oid, head->name);
+ }
+}
+
+int use_unnamed(git_repository *repo, const char *url)
+{
+ git_remote *remote = NULL;
+ git_headarray refs;
+ int error;
+
+ // Create an instance of a remote from the URL. The transport to use
+ // is detected from the URL
+ error = git_remote_new(&remote, repo, url);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ // When connecting, the underlying code needs to know wether we
+ // want to push or fetch
+ error = git_remote_connect(remote, GIT_DIR_FETCH);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ // With git_remote_ls we can retrieve the advertised heads
+ error = git_remote_ls(remote, &refs);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ show_refs(&refs);
+
+cleanup:
+ git_remote_free(remote);
+
+ return error;
+}
+
+int use_remote(git_repository *repo, char *name)
+{
+ git_remote *remote = NULL;
+ git_config *cfg = NULL;
+ git_headarray refs;
+ int error;
+
+ // Load the local configuration for the repository
+ error = git_repository_config(&cfg, repo, NULL, NULL);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ // Find the remote by name
+ error = git_remote_get(&remote, cfg, name);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ error = git_remote_connect(remote, GIT_DIR_FETCH);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ error = git_remote_ls(remote, &refs);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ show_refs(&refs);
+
+cleanup:
+ git_remote_free(remote);
+
+ return error;
+}
+
+// This gets called to do the work. The remote can be given either as
+// the name of a configured remote or an URL.
+
+int ls_remote(git_repository *repo, int argc, char **argv)
+{
+ git_headarray heads;
+ git_remote_head *head;
+ int error, i;
+
+ /* 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]);
+ }
+
+ return error;
+}