]> git.proxmox.com Git - libgit2.git/blobdiff - examples/network/ls-remote.c
Adding credentials callback to ls-remote and fetch too.
[libgit2.git] / examples / network / ls-remote.c
index 822d6f66818f8e768c5afc6c1299432f08541954..b22ac47a0f52394c361210621a1cd7fa3c0251ed 100644 (file)
@@ -8,37 +8,12 @@ static int show_ref__cb(git_remote_head *head, void *payload)
 {
        char oid[GIT_OID_HEXSZ + 1] = {0};
 
-       payload = payload;
+       (void)payload;
        git_oid_fmt(oid, &head->oid);
        printf("%s\t%s\n", oid, head->name);
        return 0;
 }
 
-static int use_unnamed(git_repository *repo, const char *url)
-{
-       git_remote *remote = NULL;
-       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, NULL, url, NULL);
-       if (error < 0)
-               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 < 0)
-               goto cleanup;
-
-       // With git_remote_ls we can retrieve the advertised heads
-       error = git_remote_ls(remote, &show_ref__cb, NULL);
-
-cleanup:
-       git_remote_free(remote);
-       return error;
-}
-
 static int use_remote(git_repository *repo, char *name)
 {
        git_remote *remote = NULL;
@@ -46,10 +21,15 @@ static int use_remote(git_repository *repo, char *name)
 
        // Find the remote by name
        error = git_remote_load(&remote, repo, name);
-       if (error < 0)
-               goto cleanup;
+       if (error < 0) {
+               error = git_remote_create_inmemory(&remote, repo, NULL, name);
+               if (error < 0)
+                       goto cleanup;
+       }
 
-       error = git_remote_connect(remote, GIT_DIR_FETCH);
+       git_remote_set_cred_acquire_cb(remote, &cred_acquire_cb, NULL);
+
+       error = git_remote_connect(remote, GIT_DIRECTION_FETCH);
        if (error < 0)
                goto cleanup;
 
@@ -67,13 +47,12 @@ int ls_remote(git_repository *repo, int argc, char **argv)
 {
        int error;
 
-       argc = argc;
-       /* 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]);
+       if (argc < 2) {
+               fprintf(stderr, "usage: %s ls-remote <remote>\n", argv[-1]);
+               return EXIT_FAILURE;
        }
 
+       error = use_remote(repo, argv[1]);
+
        return error;
 }