Skip to content

Commit 99c08d4

Browse files
tgummerergitster
authored andcommitted
ls-remote: add support for showing symrefs
Sometimes it's useful to know the main branch of a git repository without actually downloading the repository. This can be done by looking at the symrefs stored in the remote repository. Currently git doesn't provide a simple way to show the symrefs stored on the remote repository, even though the information is available. Add a --symref command line argument to the ls-remote command, which shows the symrefs in the remote repository. While there, replace a literal tab in the format string with \t to make it more obvious to the reader. Suggested-by: pedro rijo <[email protected]> Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ba5f28b commit 99c08d4

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

Documentation/git-ls-remote.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ SYNOPSIS
1010
--------
1111
[verse]
1212
'git ls-remote' [--heads] [--tags] [--refs] [--upload-pack=<exec>]
13-
[-q | --quiet] [--exit-code] [--get-url] [<repository> [<refs>...]]
13+
[-q | --quiet] [--exit-code] [--get-url]
14+
[--symref] [<repository> [<refs>...]]
1415

1516
DESCRIPTION
1617
-----------
@@ -53,6 +54,12 @@ OPTIONS
5354
"url.<base>.insteadOf" config setting (See linkgit:git-config[1]) and
5455
exit without talking to the remote.
5556

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+
5663
<repository>::
5764
The "remote" repository to query. This parameter can be
5865
either a URL or the name of a remote (see the GIT URLS and

builtin/ls-remote.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

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

@@ -37,6 +38,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
3738
int get_url = 0;
3839
int quiet = 0;
3940
int status = 0;
41+
int show_symref_target = 0;
4042
const char *uploadpack = NULL;
4143
const char **pattern = NULL;
4244

@@ -58,6 +60,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
5860
N_("take url.<base>.insteadOf into account")),
5961
OPT_SET_INT(0, "exit-code", &status,
6062
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")),
6165
OPT_END()
6266
};
6367

@@ -101,7 +105,9 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
101105
continue;
102106
if (!tail_match(pattern, ref->name))
103107
continue;
104-
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);
105111
status = 0; /* we found something */
106112
}
107113
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)