Skip to content

Commit bd6934a

Browse files
committed
Merge branch 'tg/ls-remote-symref'
"ls-remote" learned an option to show which branch the remote repository advertises as its primary by pointing its HEAD at. * tg/ls-remote-symref: ls-remote: add support for showing symrefs ls-remote: use parse-options api ls-remote: fix synopsis ls-remote: document --refs option ls-remote: document --quiet option
2 parents 05f1539 + 99c08d4 commit bd6934a

File tree

3 files changed

+97
-56
lines changed

3 files changed

+97
-56
lines changed

Documentation/git-ls-remote.txt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ git-ls-remote - List references in a remote repository
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git ls-remote' [--heads] [--tags] [--upload-pack=<exec>]
13-
[--exit-code] <repository> [<refs>...]
12+
'git ls-remote' [--heads] [--tags] [--refs] [--upload-pack=<exec>]
13+
[-q | --quiet] [--exit-code] [--get-url]
14+
[--symref] [<repository> [<refs>...]]
1415

1516
DESCRIPTION
1617
-----------
@@ -29,6 +30,13 @@ OPTIONS
2930
both, references stored in refs/heads and refs/tags are
3031
displayed.
3132

33+
--refs::
34+
Do not show peeled tags or pseudorefs like HEAD in the output.
35+
36+
-q::
37+
--quiet::
38+
Do not print remote URL to stderr.
39+
3240
--upload-pack=<exec>::
3341
Specify the full path of 'git-upload-pack' on the remote
3442
host. This allows listing references from repositories accessed via
@@ -46,6 +54,12 @@ OPTIONS
4654
"url.<base>.insteadOf" config setting (See linkgit:git-config[1]) and
4755
exit without talking to the remote.
4856

57+
--symref::
58+
In addition to the object pointed by it, show the underlying
59+
ref pointed by it when showing a symbolic ref. Currently,
60+
upload-pack only shows the symref HEAD, so it will be the only
61+
one shown by ls-remote.
62+
4963
<repository>::
5064
The "remote" repository to query. This parameter can be
5165
either a URL or the name of a remote (see the GIT URLS and

builtin/ls-remote.c

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
#include "transport.h"
44
#include "remote.h"
55

6-
static const char ls_remote_usage[] =
7-
"git ls-remote [--heads] [--tags] [--upload-pack=<exec>]\n"
8-
" [-q | --quiet] [--exit-code] [--get-url] [<repository> [<refs>...]]";
6+
static const char * const ls_remote_usage[] = {
7+
N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
8+
" [-q | --quiet] [--exit-code] [--get-url]\n"
9+
" [--symref] [<repository> [<refs>...]]"),
10+
NULL
11+
};
912

1013
/*
1114
* Is there one among the list of patterns that match the tail part
@@ -30,72 +33,49 @@ static int tail_match(const char **pattern, const char *path)
3033

3134
int cmd_ls_remote(int argc, const char **argv, const char *prefix)
3235
{
33-
int i;
3436
const char *dest = NULL;
3537
unsigned flags = 0;
3638
int get_url = 0;
3739
int quiet = 0;
3840
int status = 0;
41+
int show_symref_target = 0;
3942
const char *uploadpack = NULL;
4043
const char **pattern = NULL;
4144

4245
struct remote *remote;
4346
struct transport *transport;
4447
const struct ref *ref;
4548

46-
if (argc == 2 && !strcmp("-h", argv[1]))
47-
usage(ls_remote_usage);
49+
struct option options[] = {
50+
OPT__QUIET(&quiet, N_("do not print remote URL")),
51+
OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
52+
N_("path of git-upload-pack on the remote host")),
53+
{ OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
54+
N_("path of git-upload-pack on the remote host"),
55+
PARSE_OPT_HIDDEN },
56+
OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
57+
OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
58+
OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
59+
OPT_BOOL(0, "get-url", &get_url,
60+
N_("take url.<base>.insteadOf into account")),
61+
OPT_SET_INT(0, "exit-code", &status,
62+
N_("exit with exit code 2 if no matching refs are found"), 2),
63+
OPT_BOOL(0, "symref", &show_symref_target,
64+
N_("show underlying ref in addition to the object pointed by it")),
65+
OPT_END()
66+
};
4867

49-
for (i = 1; i < argc; i++) {
50-
const char *arg = argv[i];
68+
argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
69+
PARSE_OPT_STOP_AT_NON_OPTION);
70+
dest = argv[0];
5171

52-
if (*arg == '-') {
53-
if (starts_with(arg, "--upload-pack=")) {
54-
uploadpack = arg + 14;
55-
continue;
56-
}
57-
if (starts_with(arg, "--exec=")) {
58-
uploadpack = arg + 7;
59-
continue;
60-
}
61-
if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
62-
flags |= REF_TAGS;
63-
continue;
64-
}
65-
if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
66-
flags |= REF_HEADS;
67-
continue;
68-
}
69-
if (!strcmp("--refs", arg)) {
70-
flags |= REF_NORMAL;
71-
continue;
72-
}
73-
if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
74-
quiet = 1;
75-
continue;
76-
}
77-
if (!strcmp("--get-url", arg)) {
78-
get_url = 1;
79-
continue;
80-
}
81-
if (!strcmp("--exit-code", arg)) {
82-
/* return this code if no refs are reported */
83-
status = 2;
84-
continue;
85-
}
86-
usage(ls_remote_usage);
87-
}
88-
dest = arg;
89-
i++;
90-
break;
72+
if (argc > 1) {
73+
int i;
74+
pattern = xcalloc(argc, sizeof(const char *));
75+
for (i = 1; i < argc; i++)
76+
pattern[i - 1] = xstrfmt("*/%s", argv[i]);
9177
}
9278

93-
if (argv[i]) {
94-
int j;
95-
pattern = xcalloc(argc - i + 1, sizeof(const char *));
96-
for (j = i; j < argc; j++)
97-
pattern[j - i] = xstrfmt("*/%s", argv[j]);
98-
}
9979
remote = remote_get(dest);
10080
if (!remote) {
10181
if (dest)
@@ -125,7 +105,9 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
125105
continue;
126106
if (!tail_match(pattern, ref->name))
127107
continue;
128-
printf("%s %s\n", oid_to_hex(&ref->old_oid), ref->name);
108+
if (show_symref_target && ref->symref)
109+
printf("ref: %s\t%s\n", ref->symref, ref->name);
110+
printf("%s\t%s\n", oid_to_hex(&ref->old_oid), ref->name);
129111
status = 0; /* we found something */
130112
}
131113
return status;

t/t5512-ls-remote.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,49 @@ test_expect_success 'overrides work between mixed transfer/upload-pack hideRefs'
163163
grep refs/tags/magic actual
164164
'
165165

166+
test_expect_success 'ls-remote --symref' '
167+
cat >expect <<-\EOF &&
168+
ref: refs/heads/master HEAD
169+
1bd44cb9d13204b0fe1958db0082f5028a16eb3a HEAD
170+
1bd44cb9d13204b0fe1958db0082f5028a16eb3a refs/heads/master
171+
1bd44cb9d13204b0fe1958db0082f5028a16eb3a refs/remotes/origin/HEAD
172+
1bd44cb9d13204b0fe1958db0082f5028a16eb3a refs/remotes/origin/master
173+
1bd44cb9d13204b0fe1958db0082f5028a16eb3a refs/tags/mark
174+
EOF
175+
git ls-remote --symref >actual &&
176+
test_cmp expect actual
177+
'
178+
179+
test_expect_success 'ls-remote with filtered symref (refname)' '
180+
cat >expect <<-\EOF &&
181+
ref: refs/heads/master HEAD
182+
1bd44cb9d13204b0fe1958db0082f5028a16eb3a HEAD
183+
EOF
184+
git ls-remote --symref . HEAD >actual &&
185+
test_cmp expect actual
186+
'
187+
188+
test_expect_failure 'ls-remote with filtered symref (--heads)' '
189+
git symbolic-ref refs/heads/foo refs/tags/mark &&
190+
cat >expect <<-\EOF &&
191+
ref: refs/tags/mark refs/heads/foo
192+
1bd44cb9d13204b0fe1958db0082f5028a16eb3a refs/heads/foo
193+
1bd44cb9d13204b0fe1958db0082f5028a16eb3a refs/heads/master
194+
EOF
195+
git ls-remote --symref --heads . >actual &&
196+
test_cmp expect actual
197+
'
198+
199+
test_expect_success 'ls-remote --symref omits filtered-out matches' '
200+
cat >expect <<-\EOF &&
201+
1bd44cb9d13204b0fe1958db0082f5028a16eb3a refs/heads/foo
202+
1bd44cb9d13204b0fe1958db0082f5028a16eb3a refs/heads/master
203+
EOF
204+
git ls-remote --symref --heads . >actual &&
205+
test_cmp expect actual &&
206+
git ls-remote --symref . "refs/heads/*" >actual &&
207+
test_cmp expect actual
208+
'
209+
210+
166211
test_done

0 commit comments

Comments
 (0)