builtin / ls-remote.con commit Merge branch 'jk/index-pack-wo-repo-from-stdin' (49d45de)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "transport.h"
   4#include "remote.h"
   5
   6static const char * const ls_remote_usage[] = {
   7        N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
   8           "                     [-q | --quiet] [--exit-code] [--get-url]\n"
   9           "                     [--symref] [<repository> [<refs>...]]"),
  10        NULL
  11};
  12
  13/*
  14 * Is there one among the list of patterns that match the tail part
  15 * of the path?
  16 */
  17static int tail_match(const char **pattern, const char *path)
  18{
  19        const char *p;
  20        char pathbuf[PATH_MAX];
  21
  22        if (!pattern)
  23                return 1; /* no restriction */
  24
  25        if (snprintf(pathbuf, sizeof(pathbuf), "/%s", path) > sizeof(pathbuf))
  26                return error("insanely long ref %.*s...", 20, path);
  27        while ((p = *(pattern++)) != NULL) {
  28                if (!wildmatch(p, pathbuf, 0, NULL))
  29                        return 1;
  30        }
  31        return 0;
  32}
  33
  34int cmd_ls_remote(int argc, const char **argv, const char *prefix)
  35{
  36        const char *dest = NULL;
  37        unsigned flags = 0;
  38        int get_url = 0;
  39        int quiet = 0;
  40        int status = 0;
  41        int show_symref_target = 0;
  42        const char *uploadpack = NULL;
  43        const char **pattern = NULL;
  44
  45        struct remote *remote;
  46        struct transport *transport;
  47        const struct ref *ref;
  48
  49        struct option options[] = {
  50                OPT__QUIET(&quiet, N_("do not print remote URL")),
  51                OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
  52                           N_("path of git-upload-pack on the remote host")),
  53                { OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
  54                           N_("path of git-upload-pack on the remote host"),
  55                           PARSE_OPT_HIDDEN },
  56                OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
  57                OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
  58                OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
  59                OPT_BOOL(0, "get-url", &get_url,
  60                         N_("take url.<base>.insteadOf into account")),
  61                OPT_SET_INT(0, "exit-code", &status,
  62                            N_("exit with exit code 2 if no matching refs are found"), 2),
  63                OPT_BOOL(0, "symref", &show_symref_target,
  64                         N_("show underlying ref in addition to the object pointed by it")),
  65                OPT_END()
  66        };
  67
  68        argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
  69                             PARSE_OPT_STOP_AT_NON_OPTION);
  70        dest = argv[0];
  71
  72        if (argc > 1) {
  73                int i;
  74                pattern = xcalloc(argc, sizeof(const char *));
  75                for (i = 1; i < argc; i++)
  76                        pattern[i - 1] = xstrfmt("*/%s", argv[i]);
  77        }
  78
  79        remote = remote_get(dest);
  80        if (!remote) {
  81                if (dest)
  82                        die("bad repository '%s'", dest);
  83                die("No remote configured to list refs from.");
  84        }
  85        if (!remote->url_nr)
  86                die("remote %s has no configured URL", dest);
  87
  88        if (get_url) {
  89                printf("%s\n", *remote->url);
  90                return 0;
  91        }
  92
  93        transport = transport_get(remote, NULL);
  94        if (uploadpack != NULL)
  95                transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
  96
  97        ref = transport_get_remote_refs(transport);
  98        if (transport_disconnect(transport))
  99                return 1;
 100
 101        if (!dest && !quiet)
 102                fprintf(stderr, "From %s\n", *remote->url);
 103        for ( ; ref; ref = ref->next) {
 104                if (!check_ref_type(ref, flags))
 105                        continue;
 106                if (!tail_match(pattern, ref->name))
 107                        continue;
 108                if (show_symref_target && ref->symref)
 109                        printf("ref: %s\t%s\n", ref->symref, ref->name);
 110                printf("%s\t%s\n", oid_to_hex(&ref->old_oid), ref->name);
 111                status = 0; /* we found something */
 112        }
 113        return status;
 114}