blob: 78a88f74769645f0be86aa77d3dee3f5e99c916f [file] [log] [blame]
Daniel Barkalow18f7c512007-10-29 21:05:43 -04001#include "builtin.h"
2#include "cache.h"
3#include "transport.h"
4#include "remote.h"
5
Daniel Barkalow8951d7c2007-11-04 15:51:17 -05006static const char ls_remote_usage[] =
Stefan Naewe0a2bb552008-11-11 16:52:31 +01007"git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>] <repository> <refs>...";
Daniel Barkalow18f7c512007-10-29 21:05:43 -04008
Junio C Hamano2ea7fe02007-12-08 22:52:59 -08009/*
Junio C Hamano3d3c4f52007-12-09 12:16:55 -080010 * Is there one among the list of patterns that match the tail part
11 * of the path?
Junio C Hamano2ea7fe02007-12-08 22:52:59 -080012 */
Junio C Hamano2ea7fe02007-12-08 22:52:59 -080013static int tail_match(const char **pattern, const char *path)
14{
Junio C Hamano2ea7fe02007-12-08 22:52:59 -080015 const char *p;
Junio C Hamano3d3c4f52007-12-09 12:16:55 -080016 char pathbuf[PATH_MAX];
Junio C Hamano2ea7fe02007-12-08 22:52:59 -080017
Junio C Hamano3d3c4f52007-12-09 12:16:55 -080018 if (!pattern)
Junio C Hamano2ea7fe02007-12-08 22:52:59 -080019 return 1; /* no restriction */
20
Junio C Hamano3d3c4f52007-12-09 12:16:55 -080021 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;
Junio C Hamano2ea7fe02007-12-08 22:52:59 -080026 }
27 return 0;
28}
29
Daniel Barkalow8951d7c2007-11-04 15:51:17 -050030int cmd_ls_remote(int argc, const char **argv, const char *prefix)
Daniel Barkalow18f7c512007-10-29 21:05:43 -040031{
32 int i;
33 const char *dest = NULL;
SZEDER Gáboraf05d672008-03-25 22:06:26 +010034 int nongit;
Daniel Barkalow18f7c512007-10-29 21:05:43 -040035 unsigned flags = 0;
36 const char *uploadpack = NULL;
Junio C Hamano2ea7fe02007-12-08 22:52:59 -080037 const char **pattern = NULL;
Daniel Barkalow18f7c512007-10-29 21:05:43 -040038
Shawn O. Pearce7c2c6ee2007-11-06 20:29:20 -050039 struct remote *remote;
Daniel Barkalow18f7c512007-10-29 21:05:43 -040040 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 }
Miklos Vajna3e345262008-01-15 21:34:54 +010057 if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
Daniel Barkalow18f7c512007-10-29 21:05:43 -040058 flags |= REF_TAGS;
59 continue;
60 }
Miklos Vajna3e345262008-01-15 21:34:54 +010061 if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
Daniel Barkalow18f7c512007-10-29 21:05:43 -040062 flags |= REF_HEADS;
63 continue;
64 }
65 if (!strcmp("--refs", arg)) {
66 flags |= REF_NORMAL;
67 continue;
68 }
Daniel Barkalow8951d7c2007-11-04 15:51:17 -050069 usage(ls_remote_usage);
Daniel Barkalow18f7c512007-10-29 21:05:43 -040070 }
71 dest = arg;
Junio C Hamano3d3c4f52007-12-09 12:16:55 -080072 i++;
Daniel Barkalow18f7c512007-10-29 21:05:43 -040073 break;
74 }
75
Junio C Hamano2ea7fe02007-12-08 22:52:59 -080076 if (!dest)
Daniel Barkalow8951d7c2007-11-04 15:51:17 -050077 usage(ls_remote_usage);
Junio C Hamano3d3c4f52007-12-09 12:16:55 -080078
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 }
Shawn O. Pearce7c2c6ee2007-11-06 20:29:20 -050089 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);
Daniel Barkalow18f7c512007-10-29 21:05:43 -040093 if (uploadpack != NULL)
94 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
95
96 ref = transport_get_remote_refs(transport);
Junio C Hamano27b40702008-03-04 00:34:39 -080097 if (transport_disconnect(transport))
Daniel Barkalow18f7c512007-10-29 21:05:43 -040098 return 1;
Junio C Hamano2ea7fe02007-12-08 22:52:59 -080099 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);
Daniel Barkalow18f7c512007-10-29 21:05:43 -0400105 }
106 return 0;
107}