]> git.proxmox.com Git - libgit2.git/blame - examples/ls-remote.c
Add myself to uploaders
[libgit2.git] / examples / ls-remote.c
CommitLineData
24012627
CMN
1#include "common.h"
2
ae789622 3static int use_remote(git_repository *repo, char *name)
24012627
CMN
4{
5 git_remote *remote = NULL;
24012627 6 int error;
359dce72
CMN
7 const git_remote_head **refs;
8 size_t refs_len, i;
e3c131c5 9 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
24012627 10
ac3d33df 11 /* Find the remote by name */
209425ce 12 error = git_remote_lookup(&remote, repo, name);
d6d52348 13 if (error < 0) {
ae5b9362 14 error = git_remote_create_anonymous(&remote, repo, name);
d6d52348
KA
15 if (error < 0)
16 goto cleanup;
17 }
18
c44820c6
CMN
19 /**
20 * Connect to the remote and call the printing function for
21 * each of the remote references.
22 */
e3c131c5 23 callbacks.credentials = cred_acquire_cb;
24012627 24
07bd3e57 25 error = git_remote_connect(remote, GIT_DIRECTION_FETCH, &callbacks, NULL, NULL);
e172cf08 26 if (error < 0)
24012627
CMN
27 goto cleanup;
28
359dce72
CMN
29 /**
30 * Get the list of references on the remote and print out
31 * their name next to what they point to.
32 */
33 if (git_remote_ls(&refs, &refs_len, remote) < 0)
34 goto cleanup;
35
36 for (i = 0; i < refs_len; i++) {
37 char oid[GIT_OID_HEXSZ + 1] = {0};
38 git_oid_fmt(oid, &refs[i]->oid);
39 printf("%s\t%s\n", oid, refs[i]->name);
40 }
24012627
CMN
41
42cleanup:
43 git_remote_free(remote);
24012627
CMN
44 return error;
45}
46
c44820c6 47/** Entry point for this command */
0c9c969a 48int lg2_ls_remote(git_repository *repo, int argc, char **argv)
24012627 49{
ae789622 50 int error;
24012627 51
9da187e8
RB
52 if (argc < 2) {
53 fprintf(stderr, "usage: %s ls-remote <remote>\n", argv[-1]);
54 return EXIT_FAILURE;
55 }
56
d6d52348 57 error = use_remote(repo, argv[1]);
24012627
CMN
58
59 return error;
60}