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

[PATCH v12 00/13] port tag.c to use ref-filter.c APIs

From
Karthik Nayak <karthik.188@gmail.com>
Date
Aug 18, 2015, 18:37 UTC
Message-ID
<1439923052-7373-1-git-send-email-Karthik.188@gmail.com>

This is part of my GSoC project to "unify the code used by branch -l, tag -l, and for-each-ref". This series ports over tag.c to use ref-filter APIs.

Version 11 was posted here: http://article.gmane.org/gmane.comp.version-control.git/275997

Changes in this version:
* Small style and formatting changes.
* Remove unnecessary variable from push_new_state().
* pop_state doesn't return a value now and attaches the buffer 
  into the previous state.
* use strcmp() rather than starts_with() while checking for 
  alignment position.
* Change attend to at_end
Karthik Nayak (13):
  ref-filter: move `struct atom_value` to ref-filter.c
  ref-filter: introduce ref_formatting_state
  ref-filter: introduce the ref_formatting_state stack machinery
  utf8: add function to align a string into given strbuf
  ref-filter: implement an `align` atom
  ref-filter: add option to filter out tags, branches and remotes
  ref-filter: support printing N lines from tag annotation
  ref-filter: add support to sort by version
  ref-filter: add option to match literal pattern
  tag.c: use 'ref-filter' data structures
  tag.c: use 'ref-filter' APIs
  tag.c: implement '--format' option
  tag.c: implement '--merged' and '--no-merged' options
 Documentation/git-for-each-ref.txt |  11 ++
 Documentation/git-tag.txt          |  34 +++-
 builtin/for-each-ref.c             |   3 +-
 builtin/tag.c                      | 367 +++++++------------------------------
 ref-filter.c                       | 312 +++++++++++++++++++++++++++----
 ref-filter.h                       |  32 +++-
 refs.c                             |   9 +
 refs.h                             |   1 +
 t/t6302-for-each-ref-filter.sh     |  84 +++++++++
 t/t7004-tag.sh                     |  51 +++++-
 utf8.c                             |  21 +++
 utf8.h                             |  15 ++
 12 files changed, 577 insertions(+), 363 deletions(-)
Interdiff:
diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
index 3099631..760d719 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -128,8 +128,8 @@ color::
 	are described in `color.branch.*`.
 
 align::
-	left-, middle-, or right-align the content between %(align:..)
-	and %(end). Followed by `:<position>,<width>`, where the
+	Left-, middle-, or right-align the content between %(align:..)
+	and %(end). Followed by `:<width>>,<position>`, where the
 	`<position>` is either left, right or middle and `<width>` is
 	the total length of the content with alignment. If the
 	contents length is more than the width then no alignment is
diff --git a/ref-filter.c b/ref-filter.c
index a6086b2..665221b 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -66,9 +66,9 @@ struct align {
 };
 
 struct ref_formatting_state {
-	struct strbuf output;
 	struct ref_formatting_state *prev;
-	void (*attend)(struct ref_formatting_state *state);
+	struct strbuf output;
+	void (*at_end)(struct ref_formatting_state *state);
 	void *cb_data;
 	int quote_style;
 };
@@ -149,24 +149,25 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
 	return at;
 }
 
-static struct ref_formatting_state *push_new_state(struct ref_formatting_state **state)
+static void push_new_state(struct ref_formatting_state **stack)
 {
-	struct ref_formatting_state *new = xcalloc(1, sizeof(struct ref_formatting_state));
-	struct ref_formatting_state *old = *state;
+	struct ref_formatting_state *s = xcalloc(1, sizeof(struct ref_formatting_state));
 
-	*state = new;
-	new->prev = old;
-	return new;
+	strbuf_init(&s->output, 0);
+	s->prev = *stack;
+	*stack = s;
 }
 
-static void pop_state(struct ref_formatting_state **state)
+static void pop_state(struct ref_formatting_state **stack)
 {
-	struct ref_formatting_state *current = *state;
+	struct ref_formatting_state *current = *stack;
 	struct ref_formatting_state *prev = current->prev;
 
+	if (prev)
+		strbuf_addbuf(&prev->output, &current->output);
 	strbuf_release(&current->output);
 	free(current);
-	*state = prev;
+	*stack = prev;
 }
 
 /*
@@ -641,29 +642,31 @@ static inline char *copy_advance(char *dst, const char *src)
 
 static void align_handler(struct ref_formatting_state *state)
 {
-	struct strbuf aligned = STRBUF_INIT;
-	struct ref_formatting_state *return_to = state->prev;
 	struct align *align = (struct align *)state->cb_data;
+	struct strbuf s = STRBUF_INIT;
 
-	strbuf_utf8_align(&aligned, align->position, align->width, state->output.buf);
-	strbuf_addbuf(&return_to->output, &aligned);
-	strbuf_release(&aligned);
+	strbuf_utf8_align(&s, align->position, align->width, state->output.buf);
+	strbuf_reset(&state->output);
+	strbuf_addbuf(&state->output, &s);
+	free(align);
 }
 
 static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_state **state)
 {
-	struct ref_formatting_state *new = push_new_state(state);
-	strbuf_init(&new->output, 0);
-	new->attend = align_handler;
+	struct ref_formatting_state *new;
+
+	push_new_state(state);
+	new = *state;
+	new->at_end = align_handler;
 	new->cb_data = atomv->align;
 }
 
 static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state **state)
 {
 	struct ref_formatting_state *current = *state;
-	if (!current->attend)
+	if (!current->at_end)
 		die(_("format: `end` atom used without a supporting atom"));
-	current->attend(current);
+	current->at_end(current);
 	pop_state(state);
 }
 
@@ -771,11 +774,11 @@ static void populate_value(struct ref_array_item *ref)
 			if (strtoul_ui(valp, 10, &align->width))
 				die(_("positive width expected align:%s"), valp);
 
-			if (!ep || starts_with(ep + 1, "left"))
+			if (!ep || !strcmp(ep + 1, "left"))
 				align->position = ALIGN_LEFT;
-			else if (starts_with(ep + 1, "right"))
+			else if (!strcmp(ep + 1, "right"))
 				align->position = ALIGN_RIGHT;
-			else if (starts_with(ep + 1, "middle"))
+			else if (!strcmp(ep + 1, "middle"))
 				align->position = ALIGN_MIDDLE;
 			else
 				die(_("improper format entered align:%s"), ep + 1);
@@ -1305,7 +1308,7 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
 
 	/*  Simple per-ref filtering */
 	if (type & FILTER_REFS_INCLUDE_BROKEN) {
-		type -= FILTER_REFS_INCLUDE_BROKEN;
+		type &= ~FILTER_REFS_INCLUDE_BROKEN;
 		broken = 1;
 	}
 
@@ -1484,7 +1487,7 @@ void show_ref_array_item(struct ref_array_item *info, const char *format,
 	struct strbuf *final_buf;
 	struct ref_formatting_state *state = NULL;
 
-	state = push_new_state(&state);
+	push_new_state(&state);
 	state->quote_style = quote_style;
 
 	for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
diff --git a/ref-filter.h b/ref-filter.h
index 72fdf9e..8241066 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -5,7 +5,6 @@
 #include "refs.h"
 #include "commit.h"
 #include "parse-options.h"
-#include "utf8.h"
 
 /* Quoting styles */
 #define QUOTE_NONE 0
diff --git a/refs.c b/refs.c
index 0f18c34..a92bba1 100644
--- a/refs.c
+++ b/refs.c
@@ -2147,9 +2147,11 @@ int for_each_replace_ref(each_ref_fn fn, void *cb_data)
 
 int for_each_reftype_fullpath(each_ref_fn fn, char *type, unsigned int broken, void *cb_data)
 {
+	unsigned int flag = 0;
+
 	if (broken)
-		broken = DO_FOR_EACH_INCLUDE_BROKEN;
-	return do_for_each_ref(&ref_cache, type, fn, 0, broken, cb_data);
+		flag = DO_FOR_EACH_INCLUDE_BROKEN;
+	return do_for_each_ref(&ref_cache, type, fn, 0, flag, cb_data);
 }
 
 int head_ref_namespaced(each_ref_fn fn, void *cb_data)
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index 5b73539..335396e 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -1462,7 +1462,7 @@ test_expect_success 'invalid sort parameter on command line' '
 
 test_expect_success 'invalid sort parameter in configuratoin' '
 	git config tag.sort "v:notvalid" &&
-	test_must_fail git tag -l "foo*" >actual
+	test_must_fail git tag -l "foo*"
 '
 
 test_expect_success 'version sort with prerelease reordering' '
diff --git a/utf8.c b/utf8.c
index 0fb8e9d..00e10c8 100644
--- a/utf8.c
+++ b/utf8.c
@@ -660,7 +660,7 @@ void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int wid
 	if (position == ALIGN_LEFT)
 		strbuf_addf(buf, "%-*s", width + utf8_compensation, s);
 	else if (position == ALIGN_MIDDLE) {
-		int left = (width - display_len)/2;
+		int left = (width - display_len) / 2;
 		strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s);
 	} else if (position == ALIGN_RIGHT)
 		strbuf_addf(buf, "%*s", width + utf8_compensation, s);
-- 
2.5.0
Next: Karthik Nayak
Message 1 of 38 in “port tag.c to use ref-filter.c APIs”
  1. 00/13 port tag.c to use ref-filter.c APIsKarthik Nayak, Aug 18, 2015
  2. 01/13 ref-filter: move `struct atom_value` to ref-filter.cKarthik Nayak, Aug 18, 2015
  3. Matthieu MoyAug 19, 2015
  4. Karthik NayakAug 19, 2015
  5. 02/13 ref-filter: introduce ref_formatting_stateKarthik Nayak, Aug 18, 2015
  6. 03/13 ref-filter: introduce the ref_formatting_state stack machineryKarthik Nayak, Aug 18, 2015
  7. Matthieu MoyAug 19, 2015
  8. Karthik NayakAug 19, 2015
  9. Matthieu MoyAug 19, 2015
  10. Karthik NayakAug 19, 2015
  11. Karthik NayakAug 19, 2015
  12. Matthieu MoyAug 20, 2015
  13. Karthik NayakAug 20, 2015
  14. Junio C HamanoAug 20, 2015
  15. Matthieu MoyAug 20, 2015
  16. Junio C HamanoAug 20, 2015
  17. Junio C HamanoAug 19, 2015
  18. Karthik NayakAug 20, 2015
  19. Junio C HamanoAug 20, 2015
  20. 04/13 utf8: add function to align a string into given strbufKarthik Nayak, Aug 18, 2015
  21. 05/13 ref-filter: implement an `align` atomKarthik Nayak, Aug 18, 2015
  22. Karthik NayakAug 18, 2015
  23. Eric SunshineAug 20, 2015
  24. Karthik NayakAug 21, 2015
  25. 06/13 ref-filter: add option to filter out tags, branches and remotesKarthik Nayak, Aug 18, 2015
  26. 07/13 ref-filter: support printing N lines from tag annotationKarthik Nayak, Aug 18, 2015
  27. 08/13 ref-filter: add support to sort by versionKarthik Nayak, Aug 18, 2015
  28. 09/13 ref-filter: add option to match literal patternKarthik Nayak, Aug 18, 2015
  29. 10/13 tag.c: use 'ref-filter' data structuresKarthik Nayak, Aug 18, 2015
  30. Matthieu MoyAug 19, 2015
  31. Karthik NayakAug 19, 2015
  32. 11/13 tag.c: use 'ref-filter' APIsKarthik Nayak, Aug 18, 2015
  33. 12/13 tag.c: implement '--format' optionKarthik Nayak, Aug 18, 2015
  34. Matthieu MoyAug 19, 2015
  35. Karthik NayakAug 20, 2015
  36. 13/13 tag.c: implement '--merged' and '--no-merged' optionsKarthik Nayak, Aug 18, 2015
  37. Eric SunshineAug 18, 2015
  38. Karthik NayakAug 18, 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.