diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:47:08 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:47:08 +0000 |
commit | 29b5ab554790bb57337a3b6ab9dcd963cf69d22e (patch) | |
tree | be1456d2bc6c1fb078695fad7bc8f6b212062d3c /examples/ls-remote.c | |
parent | Initial commit. (diff) | |
download | libgit2-c1e1eb71b50aea7924f9091d188fa736bc813c6f.tar.xz libgit2-c1e1eb71b50aea7924f9091d188fa736bc813c6f.zip |
Adding upstream version 1.7.2+ds.upstream/1.7.2+ds
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'examples/ls-remote.c')
-rw-r--r-- | examples/ls-remote.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/examples/ls-remote.c b/examples/ls-remote.c new file mode 100644 index 0000000..24caae7 --- /dev/null +++ b/examples/ls-remote.c @@ -0,0 +1,60 @@ +#include "common.h" + +static int use_remote(git_repository *repo, char *name) +{ + git_remote *remote = NULL; + int error; + const git_remote_head **refs; + size_t refs_len, i; + git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT; + + /* Find the remote by name */ + error = git_remote_lookup(&remote, repo, name); + if (error < 0) { + error = git_remote_create_anonymous(&remote, repo, name); + if (error < 0) + goto cleanup; + } + + /** + * Connect to the remote and call the printing function for + * each of the remote references. + */ + callbacks.credentials = cred_acquire_cb; + + error = git_remote_connect(remote, GIT_DIRECTION_FETCH, &callbacks, NULL, NULL); + if (error < 0) + goto cleanup; + + /** + * Get the list of references on the remote and print out + * their name next to what they point to. + */ + if (git_remote_ls(&refs, &refs_len, remote) < 0) + goto cleanup; + + for (i = 0; i < refs_len; i++) { + char oid[GIT_OID_SHA1_HEXSIZE + 1] = {0}; + git_oid_fmt(oid, &refs[i]->oid); + printf("%s\t%s\n", oid, refs[i]->name); + } + +cleanup: + git_remote_free(remote); + return error; +} + +/** Entry point for this command */ +int lg2_ls_remote(git_repository *repo, int argc, char **argv) +{ + int error; + + if (argc < 2) { + fprintf(stderr, "usage: %s ls-remote <remote>\n", argv[-1]); + return EXIT_FAILURE; + } + + error = use_remote(repo, argv[1]); + + return error; +} |