Merge branch 'mg/notes-reflog'
[git/git.git] / builtin / ls-remote.c
CommitLineData
18f7c51c 1#include "builtin.h"
18705953 2#include "cache.h"
18f7c51c
DB
3#include "transport.h"
4#include "remote.h"
18705953 5
8951d7c1 6static const char ls_remote_usage[] =
0a2bb558 7"git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>] <repository> <refs>...";
18705953 8
2ea7fe0d 9/*
3d3c4f5d
JH
10 * Is there one among the list of patterns that match the tail part
11 * of the path?
2ea7fe0d 12 */
2ea7fe0d
JH
13static int tail_match(const char **pattern, const char *path)
14{
2ea7fe0d 15 const char *p;
3d3c4f5d 16 char pathbuf[PATH_MAX];
2ea7fe0d 17
3d3c4f5d 18 if (!pattern)
2ea7fe0d
JH
19 return 1; /* no restriction */
20
3d3c4f5d
JH
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;
2ea7fe0d
JH
26 }
27 return 0;
28}
29
8951d7c1 30int cmd_ls_remote(int argc, const char **argv, const char *prefix)
18705953 31{
18f7c51c
DB
32 int i;
33 const char *dest = NULL;
af05d679 34 int nongit;
2718ff09 35 unsigned flags = 0;
18f7c51c 36 const char *uploadpack = NULL;
2ea7fe0d 37 const char **pattern = NULL;
18f7c51c 38
7c2c6ee7 39 struct remote *remote;
18f7c51c
DB
40 struct transport *transport;
41 const struct ref *ref;
e44eb3e4
JH
42
43 setup_git_directory_gently(&nongit);
18705953
JH
44
45 for (i = 1; i < argc; i++) {
18f7c51c 46 const char *arg = argv[i];
18705953
JH
47
48 if (*arg == '-') {
599065a3 49 if (!prefixcmp(arg, "--upload-pack=")) {
27dca07f
UKK
50 uploadpack = arg + 14;
51 continue;
52 }
599065a3 53 if (!prefixcmp(arg, "--exec=")) {
27dca07f 54 uploadpack = arg + 7;
2718ff09
LT
55 continue;
56 }
3e345269 57 if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
2718ff09
LT
58 flags |= REF_TAGS;
59 continue;
60 }
3e345269 61 if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
2718ff09
LT
62 flags |= REF_HEADS;
63 continue;
64 }
65 if (!strcmp("--refs", arg)) {
66 flags |= REF_NORMAL;
67 continue;
68 }
8951d7c1 69 usage(ls_remote_usage);
18705953
JH
70 }
71 dest = arg;
3d3c4f5d 72 i++;
18705953
JH
73 break;
74 }
2718ff09 75
2ea7fe0d 76 if (!dest)
8951d7c1 77 usage(ls_remote_usage);
3d3c4f5d
JH
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 }
c1d45cf7
DB
89 remote = remote_get(dest);
90 if (!remote->url_nr)
7c2c6ee7 91 die("remote %s has no configured URL", dest);
fb0cc87e 92 transport = transport_get(remote, NULL);
18f7c51c
DB
93 if (uploadpack != NULL)
94 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
95
96 ref = transport_get_remote_refs(transport);
27b4070e 97 if (transport_disconnect(transport))
18f7c51c 98 return 1;
2ea7fe0d
JH
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);
18f7c51c
DB
105 }
106 return 0;
18705953 107}