builtin / ls-remote.con commit Merge branch 'ps/gitweb-js-with-lineno' into maint-1.7.6 (58f75bc)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "transport.h"
   4#include "remote.h"
   5
   6static const char ls_remote_usage[] =
   7"git ls-remote [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\n"
   8"                     [-q|--quiet] [--exit-code] [<repository> [<refs>...]]";
   9
  10/*
  11 * Is there one among the list of patterns that match the tail part
  12 * of the path?
  13 */
  14static int tail_match(const char **pattern, const char *path)
  15{
  16        const char *p;
  17        char pathbuf[PATH_MAX];
  18
  19        if (!pattern)
  20                return 1; /* no restriction */
  21
  22        if (snprintf(pathbuf, sizeof(pathbuf), "/%s", path) > sizeof(pathbuf))
  23                return error("insanely long ref %.*s...", 20, path);
  24        while ((p = *(pattern++)) != NULL) {
  25                if (!fnmatch(p, pathbuf, 0))
  26                        return 1;
  27        }
  28        return 0;
  29}
  30
  31int cmd_ls_remote(int argc, const char **argv, const char *prefix)
  32{
  33        int i;
  34        const char *dest = NULL;
  35        unsigned flags = 0;
  36        int get_url = 0;
  37        int quiet = 0;
  38        int status = 0;
  39        const char *uploadpack = NULL;
  40        const char **pattern = NULL;
  41
  42        struct remote *remote;
  43        struct transport *transport;
  44        const struct ref *ref;
  45
  46        for (i = 1; i < argc; i++) {
  47                const char *arg = argv[i];
  48
  49                if (*arg == '-') {
  50                        if (!prefixcmp(arg, "--upload-pack=")) {
  51                                uploadpack = arg + 14;
  52                                continue;
  53                        }
  54                        if (!prefixcmp(arg, "--exec=")) {
  55                                uploadpack = arg + 7;
  56                                continue;
  57                        }
  58                        if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
  59                                flags |= REF_TAGS;
  60                                continue;
  61                        }
  62                        if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
  63                                flags |= REF_HEADS;
  64                                continue;
  65                        }
  66                        if (!strcmp("--refs", arg)) {
  67                                flags |= REF_NORMAL;
  68                                continue;
  69                        }
  70                        if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
  71                                quiet = 1;
  72                                continue;
  73                        }
  74                        if (!strcmp("--get-url", arg)) {
  75                                get_url = 1;
  76                                continue;
  77                        }
  78                        if (!strcmp("--exit-code", arg)) {
  79                                /* return this code if no refs are reported */
  80                                status = 2;
  81                                continue;
  82                        }
  83                        usage(ls_remote_usage);
  84                }
  85                dest = arg;
  86                i++;
  87                break;
  88        }
  89
  90        if (argv[i]) {
  91                int j;
  92                pattern = xcalloc(sizeof(const char *), argc - i + 1);
  93                for (j = i; j < argc; j++) {
  94                        int len = strlen(argv[j]);
  95                        char *p = xmalloc(len + 3);
  96                        sprintf(p, "*/%s", argv[j]);
  97                        pattern[j - i] = p;
  98                }
  99        }
 100        remote = remote_get(dest);
 101        if (!remote) {
 102                if (dest)
 103                        die("bad repository '%s'", dest);
 104                die("No remote configured to list refs from.");
 105        }
 106        if (!remote->url_nr)
 107                die("remote %s has no configured URL", dest);
 108
 109        if (get_url) {
 110                printf("%s\n", *remote->url);
 111                return 0;
 112        }
 113
 114        transport = transport_get(remote, NULL);
 115        if (uploadpack != NULL)
 116                transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
 117
 118        ref = transport_get_remote_refs(transport);
 119        if (transport_disconnect(transport))
 120                return 1;
 121
 122        if (!dest && !quiet)
 123                fprintf(stderr, "From %s\n", *remote->url);
 124        for ( ; ref; ref = ref->next) {
 125                if (!check_ref_type(ref, flags))
 126                        continue;
 127                if (!tail_match(pattern, ref->name))
 128                        continue;
 129                printf("%s      %s\n", sha1_to_hex(ref->old_sha1), ref->name);
 130                status = 0; /* we found something */
 131        }
 132        return status;
 133}