]> git.proxmox.com Git - libgit2.git/blob - examples/ls-remote.c
Enable reproducible builds by default
[libgit2.git] / examples / ls-remote.c
1 #include "common.h"
2
3 static int use_remote(git_repository *repo, char *name)
4 {
5 git_remote *remote = NULL;
6 int error;
7 const git_remote_head **refs;
8 size_t refs_len, i;
9 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
10
11 /* Find the remote by name */
12 error = git_remote_lookup(&remote, repo, name);
13 if (error < 0) {
14 error = git_remote_create_anonymous(&remote, repo, name);
15 if (error < 0)
16 goto cleanup;
17 }
18
19 /**
20 * Connect to the remote and call the printing function for
21 * each of the remote references.
22 */
23 callbacks.credentials = cred_acquire_cb;
24
25 error = git_remote_connect(remote, GIT_DIRECTION_FETCH, &callbacks, NULL, NULL);
26 if (error < 0)
27 goto cleanup;
28
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 }
41
42 cleanup:
43 git_remote_free(remote);
44 return error;
45 }
46
47 /** Entry point for this command */
48 int lg2_ls_remote(git_repository *repo, int argc, char **argv)
49 {
50 int error;
51
52 if (argc < 2) {
53 fprintf(stderr, "usage: %s ls-remote <remote>\n", argv[-1]);
54 return EXIT_FAILURE;
55 }
56
57 error = use_remote(repo, argv[1]);
58
59 return error;
60 }