builtin-ls-remote.con commit git-svn: Make create-ignore use git add -f (c4c66b2)
   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 [--upload-pack=<git-upload-pack>] [<host>:]<directory>";
   8
   9/*
  10 * Is there one among the list of patterns that match the tail part
  11 * of the path?
  12 */
  13static int tail_match(const char **pattern, const char *path)
  14{
  15        const char *p;
  16        char pathbuf[PATH_MAX];
  17
  18        if (!pattern)
  19                return 1; /* no restriction */
  20
  21        if (snprintf(pathbuf, sizeof(pathbuf), "/%s", path) > sizeof(pathbuf))
  22                return error("insanely long ref %.*s...", 20, path);
  23        while ((p = *(pattern++)) != NULL) {
  24                if (!fnmatch(p, pathbuf, 0))
  25                        return 1;
  26        }
  27        return 0;
  28}
  29
  30int cmd_ls_remote(int argc, const char **argv, const char *prefix)
  31{
  32        int i;
  33        const char *dest = NULL;
  34        int nongit;
  35        unsigned flags = 0;
  36        const char *uploadpack = NULL;
  37        const char **pattern = NULL;
  38
  39        struct remote *remote;
  40        struct transport *transport;
  41        const struct ref *ref;
  42
  43        setup_git_directory_gently(&nongit);
  44
  45        for (i = 1; i < argc; i++) {
  46                const char *arg = argv[i];
  47
  48                if (*arg == '-') {
  49                        if (!prefixcmp(arg, "--upload-pack=")) {
  50                                uploadpack = arg + 14;
  51                                continue;
  52                        }
  53                        if (!prefixcmp(arg, "--exec=")) {
  54                                uploadpack = arg + 7;
  55                                continue;
  56                        }
  57                        if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
  58                                flags |= REF_TAGS;
  59                                continue;
  60                        }
  61                        if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
  62                                flags |= REF_HEADS;
  63                                continue;
  64                        }
  65                        if (!strcmp("--refs", arg)) {
  66                                flags |= REF_NORMAL;
  67                                continue;
  68                        }
  69                        usage(ls_remote_usage);
  70                }
  71                dest = arg;
  72                i++;
  73                break;
  74        }
  75
  76        if (!dest)
  77                usage(ls_remote_usage);
  78
  79        if (argv[i]) {
  80                int j;
  81                pattern = xcalloc(sizeof(const char *), argc - i + 1);
  82                for (j = i; j < argc; j++) {
  83                        int len = strlen(argv[j]);
  84                        char *p = xmalloc(len + 3);
  85                        sprintf(p, "*/%s", argv[j]);
  86                        pattern[j - i] = p;
  87                }
  88        }
  89        remote = nongit ? NULL : remote_get(dest);
  90        if (remote && !remote->url_nr)
  91                die("remote %s has no configured URL", dest);
  92        transport = transport_get(remote, remote ? remote->url[0] : dest);
  93        if (uploadpack != NULL)
  94                transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
  95
  96        ref = transport_get_remote_refs(transport);
  97        if (transport_disconnect(transport))
  98                return 1;
  99        for ( ; ref; ref = ref->next) {
 100                if (!check_ref_type(ref, flags))
 101                        continue;
 102                if (!tail_match(pattern, ref->name))
 103                        continue;
 104                printf("%s      %s\n", sha1_to_hex(ref->old_sha1), ref->name);
 105        }
 106        return 0;
 107}