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

[RFC/PATCH 4/9] ref-filter: add support to sort by version

From
Karthik Nayak <karthik.188@gmail.com>
Date
Jun 25, 2015, 11:43 UTC
Message-ID
<1435232596-27466-4-git-send-email-karthik.188@gmail.com>
In-Reply-To
<1435232596-27466-1-git-send-email-karthik.188@gmail.com>

Add support to sort by version using the "v:refname" and "version:refname" option. This is achieved by using the 'version_cmp()' function as the comparing function for qsort.

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 ref-filter.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index 57a5cfb..e307fab 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -10,8 +10,9 @@
 #include "quote.h"
 #include "ref-filter.h"
 #include "revision.h"
+#include "version.h"
 
-typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
+typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME, FIELD_VER } cmp_type;
 
 static struct {
 	const char *name;
@@ -53,6 +54,8 @@ static struct {
 	{ "flag" },
 	{ "HEAD" },
 	{ "color" },
+	{ "version", FIELD_VER },
+	{ "v", FIELD_VER },
 };
 
 /*
@@ -629,7 +632,9 @@ static void populate_value(struct ref_array_item *ref)
 			name++;
 		}
 
-		if (starts_with(name, "refname"))
+		if (starts_with(name, "refname") ||
+		    starts_with(name, "version:") ||
+		    starts_with(name, "v:"))
 			refname = ref->refname;
 		else if (starts_with(name, "symref"))
 			refname = ref->symref ? ref->symref : "";
@@ -695,7 +700,13 @@ static void populate_value(struct ref_array_item *ref)
 			int num_ours, num_theirs;
 
 			formatp++;
-			if (starts_with(formatp, "lalign")) {
+			if (starts_with(name, "version") || starts_with(name, "v")) {
+				if (strcmp(formatp, "refname"))
+					die("unknown %.*s format %s",
+					    (int)(formatp - name), name, formatp);
+				v->s = refname;
+				continue;
+			} else if (starts_with(formatp, "lalign")) {
 				const char *valp;
 				int val;
 
@@ -1165,6 +1176,9 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
 	case FIELD_STR:
 		cmp = strcmp(va->s, vb->s);
 		break;
+	case FIELD_VER:
+		cmp = versioncmp(va->s, vb->s);
+		break;
 	default:
 		if (va->ul < vb->ul)
 			cmp = -1;
-- 
2.4.4
Previous: Karthik NayakNext: Duy Nguyen
Message 5 of 23 in “port tag.c to use ref-filter library”
  1. 0/9 port tag.c to use ref-filter libraryKarthik Nayak, Jun 25, 2015
  2. 1/9 ref-filter: add %(refname:lalignX) optionKarthik Nayak, Jun 25, 2015
  3. 2/9 ref-filter: add option to filter only tagsKarthik Nayak, Jun 25, 2015
  4. 3/9 ref-filter: support printing N lines from tag annotationKarthik Nayak, Jun 25, 2015
  5. 4/9 ref-filter: add support to sort by versionKarthik Nayak, Jun 25, 2015
  6. Duy NguyenJul 12, 2015
  7. Karthik NayakJul 12, 2015
  8. 5/9 ref-filter: add option to match literal patternKarthik Nayak, Jun 25, 2015
  9. Karthik NayakJun 26, 2015
  10. Junio C HamanoJun 29, 2015
  11. Karthik NayakJun 30, 2015
  12. 6/9 tag.c: use 'ref-filter' data structuresKarthik Nayak, Jun 25, 2015
  13. 7/9 tag.c: use 'ref-filter' APIsKarthik Nayak, Jun 25, 2015
  14. 8/9 tag.c: implement '--format' optionKarthik Nayak, Jun 25, 2015
  15. 8/9 tag.c: implement '--format' optionKarthik Nayak, Jun 25, 2015
  16. 9/9 tag.c: implement '--merged' and '--no-merged' optionsKarthik Nayak, Jun 25, 2015
  17. Christian CouderJun 27, 2015
  18. Christian CouderJun 28, 2015
  19. Karthik NayakJun 28, 2015
  20. Duy NguyenJun 27, 2015
  21. Junio C HamanoJun 28, 2015
  22. Karthik NayakJun 28, 2015
  23. Junio C HamanoJun 29, 2015

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.