mirror of
https://github.com/git/git
synced 2024-08-24 18:26:02 +00:00
![Tay Ray Chuan](/assets/img/avatar_default.png)
Instead of breaking execution when no remote (as specified in the variable dest) is specified when git-ls-remote is invoked, continue on and let remote_get() handle it. This way, we are able to use the default remotes (eg. "origin", branch.<name>.remote), as git-fetch, git-push, and other users of remote_get(), do. If no suitable remote is found, exit with a message describing the issue, instead of just the usage text, as we do previously. Add several tests to check that git-ls-remote handles the no-remote-specified situation. Also add a test that "git ls-remote <pattern>" does not work; we are unable to guess the remote in that situation, as are git-fetch and git-push. In that test, we are testing for messages coming from two separate processes, but we should be OK, because the second message is triggered by closing the fd which must happen after the first message is printed. (analysis by Jeff King.) Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
111 lines
2.5 KiB
C
111 lines
2.5 KiB
C
#include "builtin.h"
|
|
#include "cache.h"
|
|
#include "transport.h"
|
|
#include "remote.h"
|
|
|
|
static const char ls_remote_usage[] =
|
|
"git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>]\n"
|
|
" [<repository> [<refs>...]]";
|
|
|
|
/*
|
|
* Is there one among the list of patterns that match the tail part
|
|
* of the path?
|
|
*/
|
|
static int tail_match(const char **pattern, const char *path)
|
|
{
|
|
const char *p;
|
|
char pathbuf[PATH_MAX];
|
|
|
|
if (!pattern)
|
|
return 1; /* no restriction */
|
|
|
|
if (snprintf(pathbuf, sizeof(pathbuf), "/%s", path) > sizeof(pathbuf))
|
|
return error("insanely long ref %.*s...", 20, path);
|
|
while ((p = *(pattern++)) != NULL) {
|
|
if (!fnmatch(p, pathbuf, 0))
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int cmd_ls_remote(int argc, const char **argv, const char *prefix)
|
|
{
|
|
int i;
|
|
const char *dest = NULL;
|
|
int nongit;
|
|
unsigned flags = 0;
|
|
const char *uploadpack = NULL;
|
|
const char **pattern = NULL;
|
|
|
|
struct remote *remote;
|
|
struct transport *transport;
|
|
const struct ref *ref;
|
|
|
|
setup_git_directory_gently(&nongit);
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
const char *arg = argv[i];
|
|
|
|
if (*arg == '-') {
|
|
if (!prefixcmp(arg, "--upload-pack=")) {
|
|
uploadpack = arg + 14;
|
|
continue;
|
|
}
|
|
if (!prefixcmp(arg, "--exec=")) {
|
|
uploadpack = arg + 7;
|
|
continue;
|
|
}
|
|
if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
|
|
flags |= REF_TAGS;
|
|
continue;
|
|
}
|
|
if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
|
|
flags |= REF_HEADS;
|
|
continue;
|
|
}
|
|
if (!strcmp("--refs", arg)) {
|
|
flags |= REF_NORMAL;
|
|
continue;
|
|
}
|
|
usage(ls_remote_usage);
|
|
}
|
|
dest = arg;
|
|
i++;
|
|
break;
|
|
}
|
|
|
|
if (argv[i]) {
|
|
int j;
|
|
pattern = xcalloc(sizeof(const char *), argc - i + 1);
|
|
for (j = i; j < argc; j++) {
|
|
int len = strlen(argv[j]);
|
|
char *p = xmalloc(len + 3);
|
|
sprintf(p, "*/%s", argv[j]);
|
|
pattern[j - i] = p;
|
|
}
|
|
}
|
|
remote = remote_get(dest);
|
|
if (!remote) {
|
|
if (dest)
|
|
die("bad repository '%s'", dest);
|
|
die("No remote configured to list refs from.");
|
|
}
|
|
if (!remote->url_nr)
|
|
die("remote %s has no configured URL", dest);
|
|
transport = transport_get(remote, NULL);
|
|
if (uploadpack != NULL)
|
|
transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
|
|
|
|
ref = transport_get_remote_refs(transport);
|
|
if (transport_disconnect(transport))
|
|
return 1;
|
|
for ( ; ref; ref = ref->next) {
|
|
if (!check_ref_type(ref, flags))
|
|
continue;
|
|
if (!tail_match(pattern, ref->name))
|
|
continue;
|
|
printf("%s %s\n", sha1_to_hex(ref->old_sha1), ref->name);
|
|
}
|
|
return 0;
|
|
}
|