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 <pedrorijo91@gmail.com> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Jan 19, 2016 at 00:20 UTC 99c08d4eb28f0525d71125e7903fd4462bfd6787
3 files changed +61 -3
Documentation/git-ls-remote.txt
+8 -1
@@ -10,7 +10,8 @@ SYNOPSIS
10 --------
11 [verse]
12 '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>...]]
15
16 DESCRIPTION
17 -----------
@@ -53,6 +54,12 @@ OPTIONS
54 "url.<base>.insteadOf" config setting (See linkgit:git-config[1]) and
55 exit without talking to the remote.
56
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 +
63 <repository>::
64 The "remote" repository to query. This parameter can be
65 either a URL or the name of a remote (see the GIT URLS and
builtin/ls-remote.c
+8 -2
@@ -5,7 +5,8 @@
5
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] [<repository> [<refs>...]]"),
8 + " [-q | --quiet] [--exit-code] [--get-url]\n"
9 + " [--symref] [<repository> [<refs>...]]"),
10 NULL
11 };
12
@@ -37,6 +38,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
38 int get_url = 0;
39 int quiet = 0;
40 int status = 0;
41 + int show_symref_target = 0;
42 const char *uploadpack = NULL;
43 const char **pattern = NULL;
44
@@ -58,6 +60,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
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 };
67
@@ -101,7 +105,9 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
105 continue;
106 if (!tail_match(pattern, ref->name))
107 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);
111 status = 0; /* we found something */
112 }
113 return status;
t/t5512-ls-remote.sh
+45
@@ -163,4 +163,49 @@ test_expect_success 'overrides work between mixed transfer/upload-pack hideRefs'
163 grep refs/tags/magic actual
164 '
165
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 +
211 test_done