revert: refactor code into a do_pick_commit() function
[git/dscho.git] / builtin / ls-remote.c
blob8ee91eb547c2bafedd573045c977cc7d0469c95f
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 " [<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 const char *uploadpack = NULL;
38 const char **pattern = NULL;
40 struct remote *remote;
41 struct transport *transport;
42 const struct ref *ref;
44 setup_git_directory_gently(&nongit);
46 for (i = 1; i < argc; i++) {
47 const char *arg = argv[i];
49 if (*arg == '-') {
50 if (!prefixcmp(arg, "--upload-pack=")) {
51 uploadpack = arg + 14;
52 continue;
54 if (!prefixcmp(arg, "--exec=")) {
55 uploadpack = arg + 7;
56 continue;
58 if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
59 flags |= REF_TAGS;
60 continue;
62 if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
63 flags |= REF_HEADS;
64 continue;
66 if (!strcmp("--refs", arg)) {
67 flags |= REF_NORMAL;
68 continue;
70 usage(ls_remote_usage);
72 dest = arg;
73 i++;
74 break;
77 if (argv[i]) {
78 int j;
79 pattern = xcalloc(sizeof(const char *), argc - i + 1);
80 for (j = i; j < argc; j++) {
81 int len = strlen(argv[j]);
82 char *p = xmalloc(len + 3);
83 sprintf(p, "*/%s", argv[j]);
84 pattern[j - i] = p;
87 remote = remote_get(dest);
88 if (!remote) {
89 if (dest)
90 die("bad repository '%s'", dest);
91 die("No remote configured to list refs from.");
93 if (!remote->url_nr)
94 die("remote %s has no configured URL", dest);
95 transport = transport_get(remote, NULL);
96 if (uploadpack != NULL)
97 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
99 ref = transport_get_remote_refs(transport);
100 if (transport_disconnect(transport))
101 return 1;
102 for ( ; ref; ref = ref->next) {
103 if (!check_ref_type(ref, flags))
104 continue;
105 if (!tail_match(pattern, ref->name))
106 continue;
107 printf("%s %s\n", sha1_to_hex(ref->old_sha1), ref->name);
109 return 0;