(WIP) rough ideas for git-abort / git-continue
[git/wpalmer.git] / builtin / ls-remote.c
blob34480cfad6a8842d65ae61f6257c597a2a987b33
1 #include "builtin.h"
2 #include "cache.h"
3 #include "transport.h"
4 #include "remote.h"
6 static const char ls_remote_usage[] =
7 "git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>]\n"
8 " [-q|--quiet] [<repository> [<refs>...]]";
11 * Is there one among the list of patterns that match the tail part
12 * of the path?
14 static int tail_match(const char **pattern, const char *path)
16 const char *p;
17 char pathbuf[PATH_MAX];
19 if (!pattern)
20 return 1; /* no restriction */
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;
28 return 0;
31 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
33 int i;
34 const char *dest = NULL;
35 int nongit;
36 unsigned flags = 0;
37 int quiet = 0;
38 const char *uploadpack = NULL;
39 const char **pattern = NULL;
41 struct remote *remote;
42 struct transport *transport;
43 const struct ref *ref;
45 setup_git_directory_gently(&nongit);
47 for (i = 1; i < argc; i++) {
48 const char *arg = argv[i];
50 if (*arg == '-') {
51 if (!prefixcmp(arg, "--upload-pack=")) {
52 uploadpack = arg + 14;
53 continue;
55 if (!prefixcmp(arg, "--exec=")) {
56 uploadpack = arg + 7;
57 continue;
59 if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
60 flags |= REF_TAGS;
61 continue;
63 if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
64 flags |= REF_HEADS;
65 continue;
67 if (!strcmp("--refs", arg)) {
68 flags |= REF_NORMAL;
69 continue;
71 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
72 quiet = 1;
73 continue;
75 usage(ls_remote_usage);
77 dest = arg;
78 i++;
79 break;
82 if (argv[i]) {
83 int j;
84 pattern = xcalloc(sizeof(const char *), argc - i + 1);
85 for (j = i; j < argc; j++) {
86 int len = strlen(argv[j]);
87 char *p = xmalloc(len + 3);
88 sprintf(p, "*/%s", argv[j]);
89 pattern[j - i] = p;
92 remote = remote_get(dest);
93 if (!remote) {
94 if (dest)
95 die("bad repository '%s'", dest);
96 die("No remote configured to list refs from.");
98 if (!remote->url_nr)
99 die("remote %s has no configured URL", dest);
100 transport = transport_get(remote, NULL);
101 if (uploadpack != NULL)
102 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
104 ref = transport_get_remote_refs(transport);
105 if (transport_disconnect(transport))
106 return 1;
108 if (!dest && !quiet)
109 fprintf(stderr, "From %s\n", *remote->url);
110 for ( ; ref; ref = ref->next) {
111 if (!check_ref_type(ref, flags))
112 continue;
113 if (!tail_match(pattern, ref->name))
114 continue;
115 printf("%s %s\n", sha1_to_hex(ref->old_sha1), ref->name);
117 return 0;