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

[PATCH] ls-remote: create option to sort by versions

From
Harald Nordgren <haraldnordgren@gmail.com>
Date
Apr 2, 2018, 00:52 UTC
Message-ID
<20180402005248.52418-1-haraldnordgren@gmail.com>

Create the options '-V ' and '--version-sort' to sort 'git ls-remote' output by version semantics. This is useful e.g. for the Go repository after the release of version 1.10, where otherwise v1.10 is sorted before v1.2. See:

	$ git ls-remote -t https://go.googlesource.com/go
	...
	205f850ceacfc39d1e9d76a9569416284594ce8c	refs/tags/go1.1
	d260448f6b6ac10efe4ae7f6dfe944e72bc2a676	refs/tags/go1.1.1
	1d6d8fca241bb611af51e265c1b5a2e9ae904702	refs/tags/go1.1.2
	bf86aec25972f3a100c3aa58a6abcbcc35bdea49	refs/tags/go1.10
	ac7c0ee26dda18076d5f6c151d8f920b43340ae3	refs/tags/go1.10.1
	9ce6b5c2ed5d3d5251b9a6a0c548d5fb2c8567e8	refs/tags/go1.10beta1
	594668a5a96267a46282ce3007a584ec07adf705	refs/tags/go1.10beta2
	5348aed83e39bd1d450d92d7f627e994c2db6ebf	refs/tags/go1.10rc1
	20e228f2fdb44350c858de941dff4aea9f3127b8	refs/tags/go1.10rc2
	1c5438aae896edcd1e9f9618f4776517f08053b3	refs/tags/go1.1rc2
	46a6097aa7943a490e9bd2e04274845d0e5e200f	refs/tags/go1.1rc3
	402d3590b54e4a0df9fb51ed14b2999e85ce0b76	refs/tags/go1.2
	9c9802fad57c1bcb72ea98c5c55ea2652efc5772	refs/tags/go1.2.1
	...
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 builtin/ls-remote.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)
diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
index 540d56429..740c6f117 100644
--- a/builtin/ls-remote.c
+++ b/builtin/ls-remote.c
@@ -1,6 +1,7 @@
 #include "builtin.h"
 #include "cache.h"
 #include "transport.h"
+#include "ref-filter.h"
 #include "remote.h"
 
 static const char * const ls_remote_usage[] = {
@@ -33,11 +34,20 @@ static int tail_match(const char **pattern, const char *path)
 	return 0;
 }
 
+static int cmp_ref_versions(const void *_a, const void *_b)
+{
+	const struct ref *a = *(const struct ref **)_a;
+	const struct ref *b = *(const struct ref **)_b;
+
+	return versioncmp(a->name, b->name);
+}
+
 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 {
 	const char *dest = NULL;
 	unsigned flags = 0;
 	int get_url = 0;
+	int version_sort = 0;
 	int quiet = 0;
 	int status = 0;
 	int show_symref_target = 0;
@@ -47,6 +57,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	struct remote *remote;
 	struct transport *transport;
 	const struct ref *ref;
+	const struct ref **refs = NULL;
+	int nr = 0;
 
 	struct option options[] = {
 		OPT__QUIET(&quiet, N_("do not print remote URL")),
@@ -60,6 +72,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 		OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
 		OPT_BOOL(0, "get-url", &get_url,
 			 N_("take url.<base>.insteadOf into account")),
+		OPT_BOOL('V', "version-sort", &version_sort,
+			 N_("sort tags by version numbers")),
 		OPT_SET_INT_F(0, "exit-code", &status,
 			      N_("exit with exit code 2 if no matching refs are found"),
 			      2, PARSE_OPT_NOCOMPLETE),
@@ -101,13 +115,22 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	if (transport_disconnect(transport))
 		return 1;
 
-	if (!dest && !quiet)
-		fprintf(stderr, "From %s\n", *remote->url);
 	for ( ; ref; ref = ref->next) {
 		if (!check_ref_type(ref, flags))
 			continue;
 		if (!tail_match(pattern, ref->name))
 			continue;
+		REALLOC_ARRAY(refs, nr + 1);
+		refs[nr++] = ref;
+	}
+
+	if (version_sort)
+		QSORT(refs, nr, cmp_ref_versions);
+
+	if (!dest && !quiet)
+		fprintf(stderr, "From %s\n", *remote->url);
+	for (int i = 0; i < nr; i++) {
+		const struct ref *ref = refs[i];
 		if (show_symref_target && ref->symref)
 			printf("ref: %s\t%s\n", ref->symref, ref->name);
 		printf("%s\t%s\n", oid_to_hex(&ref->old_oid), ref->name);
-- 
2.14.3 (Apple Git-98)
Next: Ævar Arnfjörð Bjarmason
Message 1 of 62 in “ls-remote: create option to sort by versions”
  1. ls-remote: create option to sort by versionsHarald Nordgren, Apr 2, 2018
  2. Ævar Arnfjörð BjarmasonApr 2, 2018
  3. Harald NordgrenApr 2, 2018
  4. Ævar Arnfjörð BjarmasonApr 2, 2018
  5. Harald NordgrenApr 2, 2018
  6. Jeff KingApr 2, 2018
  7. Junio C HamanoApr 2, 2018
  8. Harald NordgrenApr 2, 2018
  9. ls-remote: create '--sort' optionHarald Nordgren, Apr 2, 2018
  10. Eric SunshineApr 2, 2018
  11. Eric SunshineApr 2, 2018
  12. ls-remote: create '--sort' optionHarald Nordgren, Apr 3, 2018
  13. ls-remote: create '--sort' optionHarald Nordgren, Apr 2, 2018
  14. ls-remote: create '--sort' optionHarald Nordgren, Apr 4, 2018
  15. Harald NordgrenApr 4, 2018
  16. Harald NordgrenApr 4, 2018
  17. Jeff KingApr 4, 2018
  18. Jeff KingApr 4, 2018
  19. ls-remote: create '--sort' optionHarald Nordgren, Apr 4, 2018
  20. Harald NordgrenApr 4, 2018
  21. Jeff KingApr 6, 2018
  22. 1/3 ref-filter: use "struct object_id" consistentlyJeff King, Apr 6, 2018
  23. 2/3 ref-filter: make ref_array_item allocation more consistentJeff King, Apr 6, 2018
  24. 3/3 ref-filter: factor ref_array pushing into its own functionJeff King, Apr 6, 2018
  25. Derrick StoleeApr 6, 2018
  26. Harald NordgrenApr 7, 2018
  27. Junio C HamanoApr 8, 2018
  28. Jeff KingApr 9, 2018
  29. ls-remote: create '--sort' optionHarald Nordgren, Apr 4, 2018
  30. ls-remote: create '--sort' optionHarald Nordgren, Apr 5, 2018
  31. 1/4 ref-filter: use "struct object_id" consistentlyHarald Nordgren, Apr 7, 2018
  32. 2/4 ref-filter: make ref_array_item allocation more consistentHarald Nordgren, Apr 7, 2018
  33. 3/4 ref-filter: factor ref_array pushing into its own functionHarald Nordgren, Apr 7, 2018
  34. 4/4 ls-remote: create '--sort' optionHarald Nordgren, Apr 7, 2018
  35. Eric SunshineApr 8, 2018
  36. Eric SunshineApr 8, 2018
  37. Harald NordgrenApr 8, 2018
  38. 1/4 ref-filter: use "struct object_id" consistentlyHarald Nordgren, Apr 8, 2018
  39. 3/4 ref-filter: factor ref_array pushing into its own functionHarald Nordgren, Apr 8, 2018
  40. 4/4 ls-remote: create '--sort' optionHarald Nordgren, Apr 8, 2018
  41. Junio C HamanoApr 8, 2018
  42. Harald NordgrenApr 9, 2018
  43. Junio C HamanoApr 9, 2018
  44. Eric SunshineApr 9, 2018
  45. 2/4 ref-filter: make ref_array_item allocation more consistentHarald Nordgren, Apr 8, 2018
  46. 1/4 ref-filter: use "struct object_id" consistentlyHarald Nordgren, Apr 8, 2018
  47. 2/4 ref-filter: make ref_array_item allocation more consistentHarald Nordgren, Apr 8, 2018
  48. 4/4 ls-remote: create '--sort' optionHarald Nordgren, Apr 8, 2018
  49. Junio C HamanoApr 9, 2018
  50. Harald NordgrenApr 9, 2018
  51. 3/4 ref-filter: factor ref_array pushing into its own functionHarald Nordgren, Apr 8, 2018
  52. 1/4 ref-filter: use "struct object_id" consistentlyHarald Nordgren, Apr 9, 2018
  53. 2/4 ref-filter: make ref_array_item allocation more consistentHarald Nordgren, Apr 9, 2018
  54. Harald NordgrenApr 11, 2018
  55. Stefan BellerApr 11, 2018
  56. Todd ZullingerApr 11, 2018
  57. Eric SunshineApr 11, 2018
  58. Junio C HamanoApr 11, 2018
  59. 3/4 ref-filter: factor ref_array pushing into its own functionHarald Nordgren, Apr 9, 2018
  60. 4/4 ls-remote: create '--sort' optionHarald Nordgren, Apr 9, 2018
  61. René ScharfeMay 12, 2018
  62. Jeff KingMay 12, 2018

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.