git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/4] builtin/ls-remote: add support for showing symrefs

From
Thomas Gummerer <t.gummerer@gmail.com>
Date
Jan 17, 2016, 11:04 UTC
Message-ID
<1453028643-13978-5-git-send-email-t.gummerer@gmail.com>
In-Reply-To
<1453028643-13978-1-git-send-email-t.gummerer@gmail.com>

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 --symrefs command line argument to the ls-remote command, which shows the symrefs on the remote repository.

Suggested-by: pedro rijo <pedrorijo91@gmail.com>
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 Documentation/git-ls-remote.txt |  8 +++++++-
 builtin/ls-remote.c             |  9 ++++++++-
 t/t5512-ls-remote.sh            | 20 ++++++++++++++++++++
 3 files changed, 35 insertions(+), 2 deletions(-)
diff --git a/Documentation/git-ls-remote.txt b/Documentation/git-ls-remote.txt
index 31c1427..5efef9e 100644
--- a/Documentation/git-ls-remote.txt
+++ b/Documentation/git-ls-remote.txt
@@ -10,7 +10,8 @@ SYNOPSIS
 --------
 [verse]
 'git ls-remote' [--heads] [--tags]  [--upload-pack=<exec>]
-	      [-q | --quiet] [--exit-code] [--get-url] [<repository> [<refs>...]]
+	      [-q | --quiet] [--exit-code]
+	      [--symrefs] [--get-url] [<repository> [<refs>...]]
 
 DESCRIPTION
 -----------
@@ -50,6 +51,11 @@ OPTIONS
 	"url.<base>.insteadOf" config setting (See linkgit:git-config[1]) and
 	exit without talking to the remote.
 
+--symrefs::
+	Show the symrefs on the server.  Shows only the symrefs by
+	default, and can be combined with --tags and --heads to show
+	refs/heads and refs/tags as well.
+
 <repository>::
 	The "remote" repository to query.  This parameter can be
 	either a URL or the name of a remote (see the GIT URLS and
diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
index 6ee7b0e..f33ada9 100644
--- a/builtin/ls-remote.c
+++ b/builtin/ls-remote.c
@@ -5,7 +5,8 @@
 
 static const char * const ls_remote_usage[] = {
 	N_("git ls-remote [--heads] [--tags]  [--upload-pack=<exec>]\n"
-	   "                     [-q | --quiet] [--exit-code] [--get-url] [<repository> [<refs>...]]"),
+	   "                     [-q | --quiet] [--exit-code] [--get-url]\n"
+	   "                     [--symrefs] [<repository> [<refs>...]]"),
 	NULL
 };
 
@@ -38,6 +39,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	int quiet = 0;
 	int status = 0;
 	int tags = 0, heads = 0, refs = 0;
+	int symrefs = 0;
 	const char *uploadpack = NULL;
 	const char **pattern = NULL;
 
@@ -58,6 +60,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 			    N_("take url.<base>.insteadOf into account"), 1),
 		OPT_SET_INT(0, "exit-code", &status,
 			    N_("exit with exit code 2 if no matching refs are found"), 2),
+		OPT_BOOL(0, "symrefs", &symrefs, N_("show symrefs")),
 		OPT_END()
 	};
 
@@ -98,6 +101,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	if (!dest && !quiet)
 		fprintf(stderr, "From %s\n", *remote->url);
 	for ( ; ref; ref = ref->next) {
+		if (symrefs && ref->symref)
+			printf("symref: %s	%s\n", ref->symref, ref->name);
+		if (symrefs && !flags)
+			continue;
 		if (!check_ref_type(ref, flags))
 			continue;
 		if (!tail_match(pattern, ref->name))
diff --git a/t/t5512-ls-remote.sh b/t/t5512-ls-remote.sh
index aadaac5..68a1429 100755
--- a/t/t5512-ls-remote.sh
+++ b/t/t5512-ls-remote.sh
@@ -163,4 +163,24 @@ test_expect_success 'overrides work between mixed transfer/upload-pack hideRefs'
 	grep refs/tags/magic actual
 '
 
+test_expect_success 'ls-remote --symrefs' '
+	cat >expect <<-EOF &&
+	symref: refs/heads/master	HEAD
+	EOF
+	git ls-remote --symrefs >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'ls-remote with symrefs and refs combined' '
+	cat >expect <<-EOF &&
+	symref: refs/heads/master	HEAD
+	1bd44cb9d13204b0fe1958db0082f5028a16eb3a	refs/heads/master
+	1bd44cb9d13204b0fe1958db0082f5028a16eb3a	refs/remotes/origin/HEAD
+	1bd44cb9d13204b0fe1958db0082f5028a16eb3a	refs/remotes/origin/master
+	1bd44cb9d13204b0fe1958db0082f5028a16eb3a	refs/tags/mark
+	EOF
+	git ls-remote --symrefs --refs >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.7.0.14.g2b6d3d6
Previous: Thomas GummererNext: Thomas Gummerer
Message 9 of 44 in “ls-remote: introduce symref argument”
  1. 0/4 ls-remote: introduce symref argumentThomas Gummerer, Jan 17, 2016
  2. 1/4 ls-remote: document --quiet optionThomas Gummerer, Jan 17, 2016
  3. Jeff KingJan 17, 2016
  4. Thomas GummererJan 17, 2016
  5. 2/4 ls-remote: fix synopsisThomas Gummerer, Jan 17, 2016
  6. 3/4 ls-remote: use parse-options apiThomas Gummerer, Jan 17, 2016
  7. Jeff KingJan 17, 2016
  8. Thomas GummererJan 17, 2016
  9. 4/4 builtin/ls-remote: add support for showing symrefsThomas Gummerer, Jan 17, 2016
  10. Thomas GummererJan 17, 2016
  11. 4/4 ls-remote: add support for showing symrefsThomas Gummerer, Jan 17, 2016
  12. Jeff KingJan 17, 2016
  13. Thomas GummererJan 17, 2016
  14. Junio C HamanoJan 17, 2016
  15. Thomas GummererJan 17, 2016
  16. Jeff KingJan 17, 2016
  17. Thomas GummererJan 17, 2016
  18. Junio C HamanoJan 17, 2016
  19. 0/5 ls-remote: introduce symrefs argumentThomas Gummerer, Jan 18, 2016
  20. 1/5 ls-remote: document --quiet optionThomas Gummerer, Jan 18, 2016
  21. 2/5 ls-remote: document --refs optionThomas Gummerer, Jan 18, 2016
  22. Jeff KingJan 18, 2016
  23. Junio C HamanoJan 18, 2016
  24. Thomas GummererJan 18, 2016
  25. 3/5 ls-remote: fix synopsisThomas Gummerer, Jan 18, 2016
  26. 4/5 ls-remote: use parse-options apiThomas Gummerer, Jan 18, 2016
  27. Jeff KingJan 18, 2016
  28. 5/5 ls-remote: add support for showing symrefsThomas Gummerer, Jan 18, 2016
  29. Jeff KingJan 18, 2016
  30. Jeff KingJan 18, 2016
  31. Thomas GummererJan 18, 2016
  32. Thomas GummererJan 18, 2016
  33. Jeff KingJan 18, 2016
  34. Thomas GummererJan 18, 2016
  35. Junio C HamanoJan 18, 2016
  36. Thomas GummererJan 18, 2016
  37. Jeff KingJan 18, 2016
  38. 0/5 ls-remote: introduce symref argumentThomas Gummerer, Jan 18, 2016
  39. 1/5 ls-remote: document --quiet optionThomas Gummerer, Jan 18, 2016
  40. 2/5 ls-remote: document --refs optionThomas Gummerer, Jan 18, 2016
  41. 3/5 ls-remote: fix synopsisThomas Gummerer, Jan 18, 2016
  42. 4/5 ls-remote: use parse-options apiThomas Gummerer, Jan 18, 2016
  43. 5/5 ls-remote: add support for showing symrefsThomas Gummerer, Jan 18, 2016
  44. Junio C HamanoJan 19, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.