Skip to contentSkip to conversation

usage: make error functions a stack

PATCH In review Jacob Keller 3 participants 11 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH v9 1/4 usage: make error functions a stack

Rename set_error_routine to be push_error_routine, and add a new pop_error_routine. This allows temporary modifications of the error routine over a small block of code.

---
Renamed set_error_routine to push_error_routine in order to match the pop_error_routine.

 git-compat-util.h |  3 ++-
 run-command.c     |  2 +-
 usage.c           | 32 ++++++++++++++++++++++++++++----
 3 files changed, 31 insertions(+), 6 deletions(-)
git-compat-util.h +2 -1
index 9de318071083..b650e3cb6cdc 100644
@@ -343,7 +343,8 @@ static inline int const_error(void)
 #endif
 
 extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
-extern void set_error_routine(void (*routine)(const char *err, va_list params));
+extern void push_error_routine(void (*routine)(const char *err, va_list params));
+extern void pop_error_routine(void);
 extern void set_die_is_recursing_routine(int (*routine)(void));
 
 extern int starts_with(const char *str, const char *prefix);
run-command.c +1 -1
index be07d4ad335b..a611b819c25d 100644
@@ -360,7 +360,7 @@ fail_pipe:
 			set_cloexec(child_err);
 		}
 		set_die_routine(die_child);
-		set_error_routine(error_child);
+		push_error_routine(error_child);
 
 		close(notify_pipe[0]);
 		set_cloexec(notify_pipe[1]);
usage.c +28 -4
index ed146453cabe..3fe26771f7e6 100644
@@ -57,18 +57,42 @@ static int die_is_recursing_builtin(void)
  * (ugh), so keep things static. */
 static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
 static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
-static void (*error_routine)(const char *err, va_list params) = error_builtin;
 static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
 static int (*die_is_recursing)(void) = die_is_recursing_builtin;
 
+struct error_func_list {
+	void (*func)(const char *, va_list);
+	struct error_func_list *next;
+};
+static struct error_func_list default_error_func = { error_builtin };
+static struct error_func_list *error_funcs = &default_error_func;
+
 void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
 {
 	die_routine = routine;
 }
 
-void set_error_routine(void (*routine)(const char *err, va_list params))
+/* push error routine onto the error function stack */
+void push_error_routine(void (*routine)(const char *err, va_list params))
 {
-	error_routine = routine;
+	struct error_func_list *efl = xmalloc(sizeof(*efl));
+	efl->func = routine;
+	efl->next = error_funcs;
+	error_funcs = efl;
+}
+
+/* pop a single error routine off of the error function stack, thus reverting
+ * to previous error. Should always be paired with a push_error_routine */
+void pop_error_routine(void)
+{
+	struct error_func_list *efl = error_funcs;
+
+	assert(error_funcs != &default_error_func);
+
+	if (efl->next) {
+		error_funcs = efl->next;
+		free(efl);
+	}
 }
 
 void set_die_is_recursing_routine(int (*routine)(void))
@@ -144,7 +168,7 @@ int error(const char *err, ...)
 	va_list params;
 
 	va_start(params, err);
-	error_routine(err, params);
+	error_funcs->func(err, params);
 	va_end(params);
 	return -1;
 }
Signature · 1 line
2.0.1.475.g9b8d714
#1 lore raw

PATCH v9 2/4 tag: fix --sort tests to use cat<<-\EOF format

The --sort tests should use the better format for >expect to maintain indenting and ensure that no substitution is occurring. This makes parsing and understanding the tests a bit easier.

---
 t/t7004-tag.sh | 40 ++++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 20 deletions(-)
t/t7004-tag.sh +20 -20
index e4ab0f5b6419..66010b0e7066 100755
@@ -1385,41 +1385,41 @@ test_expect_success 'lexical sort' '
 	git tag foo1.6 &&
 	git tag foo1.10 &&
 	git tag -l --sort=refname "foo*" >actual &&
-	cat >expect <<EOF &&
-foo1.10
-foo1.3
-foo1.6
-EOF
+	cat >expect <<-\EOF &&
+	foo1.10
+	foo1.3
+	foo1.6
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'version sort' '
 	git tag -l --sort=version:refname "foo*" >actual &&
-	cat >expect <<EOF &&
-foo1.3
-foo1.6
-foo1.10
-EOF
+	cat >expect <<-\EOF &&
+	foo1.3
+	foo1.6
+	foo1.10
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'reverse version sort' '
 	git tag -l --sort=-version:refname "foo*" >actual &&
-	cat >expect <<EOF &&
-foo1.10
-foo1.6
-foo1.3
-EOF
+	cat >expect <<-\EOF &&
+	foo1.10
+	foo1.6
+	foo1.3
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'reverse lexical sort' '
 	git tag -l --sort=-refname "foo*" >actual &&
-	cat >expect <<EOF &&
-foo1.6
-foo1.3
-foo1.10
-EOF
+	cat >expect <<-\EOF &&
+	foo1.6
+	foo1.3
+	foo1.10
+	EOF
 	test_cmp expect actual
 '
 
Signature · 1 line
2.0.1.475.g9b8d714
#2 lore raw

PATCH v9 3/4 tag: update parsing to be more precise regarding errors

In reply to Jacob Keller

Update the parsing of sort string specifications so that it is able to properly detect errors in the function type and allowed atoms.

---
 builtin/tag.c | 55 +++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 41 insertions(+), 14 deletions(-)
builtin/tag.c +41 -14
index ef765563388c..7d82526e76be 100644
@@ -522,24 +522,51 @@ static int parse_opt_points_at(const struct option *opt __attribute__((unused)),
 static int parse_opt_sort(const struct option *opt, const char *arg, int unset)
 {
 	int *sort = opt->value;
-	int flags = 0;
+	char *value, *separator, *type, *atom;
+	int flags = 0, function = 0, err = 0;
 
-	if (*arg == '-') {
+	/* skip the '-' prefix for reverse sort order first */
+	if (skip_prefix(arg, "-", &arg))
 		flags |= REVERSE_SORT;
-		arg++;
+
+	/* duplicate string so we can modify it in place */
+	value = xstrdup(arg);
+
+	/* determine the sort function and the sorting atom */
+	separator = strchr(value, ':');
+	if (separator) {
+		/* split the string at the separator with a NULL byte */
+		*separator = '\0';
+		type = value;
+		atom = separator + 1;
+	} else {
+		/* we have no separator, so assume the whole string is the * atom */
+		type = NULL;
+		atom = value;
 	}
-	if (starts_with(arg, "version:")) {
-		*sort = VERCMP_SORT;
-		arg += 8;
-	} else if (starts_with(arg, "v:")) {
-		*sort = VERCMP_SORT;
-		arg += 2;
+
+	if (type) {
+		if (!strcmp(type, "version") || !strcmp(type, "v"))
+			function = VERCMP_SORT;
+		else {
+			err = error(_("unsupported sort function '%s'"), type);
+			goto abort;
+		}
+
 	} else
-		*sort = STRCMP_SORT;
-	if (strcmp(arg, "refname"))
-		die(_("unsupported sort specification %s"), arg);
-	*sort |= flags;
-	return 0;
+		function = STRCMP_SORT;
+
+	/* for now, only the refname is a valid atom */
+	if (atom && strcmp(atom, "refname")) {
+		err = error(_("unsupported sort specification '%s'"), atom);
+		goto abort;
+	}
+
+	*sort = (flags | function);
+
+abort:
+	free(value);
+	return err;
 }
 
 int cmd_tag(int argc, const char **argv, const char *prefix)
Signature · 1 line
2.0.1.475.g9b8d714
#3 lore raw

PATCH v9 4/4 tag: support configuring --sort via .gitconfig

In reply to Jacob Keller

Add support for configuring default sort specification for git tags. Command line option will override the configured value. Both methods use the same syntax. Make use of (set/pop)_error_routine to temporarily modify error reporting when parsing as a configuration option.

---
 Documentation/config.txt  |   5 ++
 Documentation/git-tag.txt |   5 +-
 builtin/tag.c             | 124 ++++++++++++++++++++++++++++------------------
 t/t7004-tag.sh            |  36 ++++++++++++++
 4 files changed, 120 insertions(+), 50 deletions(-)
Documentation/config.txt +5 -0
index 1d718bdb9662..c55c22ab7be9 100644
@@ -2354,6 +2354,11 @@ submodule.<name>.ignore::
 	"--ignore-submodules" option. The 'git submodule' commands are not
 	affected by this setting.
 
+tag.sort::
+	This variable controls the sort ordering of tags when displayed by
+	linkgit:git-tag[1]. Without the "--sort=<value>" option provided, the
+	value of this variable will be used as the default.
+
 tar.umask::
 	This variable can be used to restrict the permission bits of
 	tar archive entries.  The default is 0002, which turns off the
Documentation/git-tag.txt +4 -1
index b424a1bc48bb..320908369f06 100644
@@ -99,7 +99,9 @@ OPTIONS
 	Sort in a specific order. Supported type is "refname"
 	(lexicographic order), "version:refname" or "v:refname" (tag
 	names are treated as versions). Prepend "-" to reverse sort
-	order.
+	order. When this option is not given, the sort order defaults to the
+	value configured for the 'tag.sort' variable if it exists, or
+	lexicographic order otherwise. See linkgit:git-config[1].
 
 --column[=<options>]::
 --no-column::
@@ -317,6 +319,7 @@ include::date-formats.txt[]
 SEE ALSO
 --------
 linkgit:git-check-ref-format[1].
+linkgit:git-config[1].
 
 GIT
 ---
builtin/tag.c +75 -49

164 lines not on this page · raw message

t/t7004-tag.sh +36 -0
index 66010b0e7066..036665308841 100755
@@ -1423,6 +1423,42 @@ test_expect_success 'reverse lexical sort' '
 	test_cmp expect actual
 '
 
+test_expect_success 'configured lexical sort' '
+	git config tag.sort "v:refname" &&
+	git tag -l "foo*" >actual &&
+	cat >expect <<-\EOF &&
+	foo1.3
+	foo1.6
+	foo1.10
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'option override configured sort' '
+	git tag -l --sort=-refname "foo*" >actual &&
+	cat >expect <<-\EOF &&
+	foo1.6
+	foo1.3
+	foo1.10
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'invalid sort parameter on command line' '
+	test_must_fail git tag -l --sort=notvalid "foo*" >actual
+'
+
+test_expect_success 'invalid sort parameter in configuratoin' '
+	git config tag.sort "v:notvalid" &&
+	git tag -l "foo*" >actual &&
+	cat >expect <<-\EOF &&
+	foo1.10
+	foo1.3
+	foo1.6
+	EOF
+	test_cmp expect actual
+'
+
 run_with_limited_stack () {
 	(ulimit -s 64 && "$@")
 }
Signature · 1 line
2.0.1.475.g9b8d714
#4 lore raw

Re: [PATCH v9 4/4] tag: support configuring --sort via .gitconfig

builtin/tag.c static const char tag_template_nocleanup[] = · line 403

Quoted 4 lines · Jacob Keller
builtin/tag.c +4 -0
+static void error_bad_sort_config(const char *err, va_list params)
+{
+	vreportf("warning: tag.sort has ", err, params);
+}

This feels a little like an abuse of the "prefix" field of vreportf, but as you probably saw in my "for fun" patch, doing it right means formatting into a buffer and then reformatting that (which we're already doing again in vreportf, but less flexibly). I dunno.

At any rate, this should be marked for translation, no?

-Peff

#5 lore raw

Re: [PATCH v9 4/4] tag: support configuring --sort via .gitconfig

Quoted 12 lines · Jeff King

builtin/tag.c static const char tag_template_nocleanup[] = · line 403

Quoted 4 lines · Jacob Keller
builtin/tag.c +4 -0
+static void error_bad_sort_config(const char *err, va_list params)
+{
+	vreportf("warning: tag.sort has ", err, params);
+}

This feels a little like an abuse of the "prefix" field of vreportf, but as you probably saw in my "for fun" patch, doing it right means formatting into a buffer and then reformatting that (which we're already doing again in vreportf, but less flexibly). I dunno.

Quoted 3 lines

At any rate, this should be marked for translation, no?

-Peff

Oh, yes it probably should. It's definitely a little bit abusive of the prefix field, but that seemed easier.

Thanks,
Jake

#6 lore raw

Re: [PATCH v9 4/4] tag: support configuring --sort via .gitconfig

Quoted 13 lines · Jacob E
Quoted 12 lines · Jeff King

builtin/tag.c static const char tag_template_nocleanup[] = · line 403

Quoted 4 lines · Jacob Keller
builtin/tag.c +4 -0
+static void error_bad_sort_config(const char *err, va_list params)
+{
+	vreportf("warning: tag.sort has ", err, params);
+}

This feels a little like an abuse of the "prefix" field of vreportf, but as you probably saw in my "for fun" patch, doing it right means formatting into a buffer and then reformatting that (which we're already doing again in vreportf, but less flexibly). I dunno.

Quoted 6 lines
Quoted 3 lines

At any rate, this should be marked for translation, no?

-Peff

Oh, yes it probably should. It's definitely a little bit abusive of the prefix field, but that seemed easier.

And i18n would automatically mean this will not work, no? There is no guarantee that the translation of "warning: " will be followed directly by the translation of "tag.sort has" without any words from variable part in all languages.

After all, it seems to me that the one in

    http://thread.gmane.org/gmane.comp.version-control.git/253346

struck the right balance among various abuses; let's use the error reporter from that version, instead of going down this rabbit hole.

Thanks.

#7 lore raw

Re: [PATCH v9 4/4] tag: support configuring --sort via .gitconfig

Quoted 27 lines · Junio C Hamano
Quoted 13 lines · Jacob E
Quoted 12 lines · Jeff King

builtin/tag.c static const char tag_template_nocleanup[] = · line 403

Quoted 4 lines · Jacob Keller
builtin/tag.c +4 -0
+static void error_bad_sort_config(const char *err, va_list params)
+{
+	vreportf("warning: tag.sort has ", err, params);
+}

This feels a little like an abuse of the "prefix" field of vreportf, but as you probably saw in my "for fun" patch, doing it right means formatting into a buffer and then reformatting that (which we're already doing again in vreportf, but less flexibly). I dunno.

Quoted 6 lines
Quoted 3 lines

At any rate, this should be marked for translation, no?

-Peff

Oh, yes it probably should. It's definitely a little bit abusive of the prefix field, but that seemed easier.

And i18n would automatically mean this will not work, no? There is no guarantee that the translation of "warning: " will be followed directly by the translation of "tag.sort has" without any words from variable part in all languages.

Quoted 11 lines

After all, it seems to me that the one in

    http://thread.gmane.org/gmane.comp.version-control.git/253346

struck the right balance among various abuses; let's use the error reporter from that version, instead of going down this rabbit hole.

Thanks.

This is fine with me :)

Thanks,
Jake

#8 lore raw

Re: [PATCH v9 4/4] tag: support configuring --sort via .gitconfig

In reply to Junio C Hamano
Quoted 27 lines · Junio C Hamano
Quoted 13 lines · Jacob E
Quoted 12 lines · Jeff King

builtin/tag.c static const char tag_template_nocleanup[] = · line 403

Quoted 4 lines · Jacob Keller
builtin/tag.c +4 -0
+static void error_bad_sort_config(const char *err, va_list params)
+{
+	vreportf("warning: tag.sort has ", err, params);
+}

This feels a little like an abuse of the "prefix" field of vreportf, but as you probably saw in my "for fun" patch, doing it right means formatting into a buffer and then reformatting that (which we're already doing again in vreportf, but less flexibly). I dunno.

Quoted 6 lines
Quoted 3 lines

At any rate, this should be marked for translation, no?

-Peff

Oh, yes it probably should. It's definitely a little bit abusive of the prefix field, but that seemed easier.

And i18n would automatically mean this will not work, no? There is no guarantee that the translation of "warning: " will be followed directly by the translation of "tag.sort has" without any words from variable part in all languages.

Quoted 11 lines

After all, it seems to me that the one in

    http://thread.gmane.org/gmane.comp.version-control.git/253346

struck the right balance among various abuses; let's use the error reporter from that version, instead of going down this rabbit hole.

Thanks.

So is that patch series version acceptable then? Or should I spin one again that is in that vein?

Thanks,
Jake

#9 lore raw

Re: [PATCH v9 4/4] tag: support configuring --sort via .gitconfig

Quoted 11 lines · Jacob E
Quoted 8 lines

After all, it seems to me that the one in

    http://thread.gmane.org/gmane.comp.version-control.git/253346

struck the right balance among various abuses; let's use the error reporter from that version, instead of going down this rabbit hole.

Thanks.

So is that patch series version acceptable then? Or should I spin one again that is in that vein?

I do not offhand know what other changes you had in v9 since $gmane/253346, so I'll leave it up to you. If the only difference is the error reporting codepath, and you are happy with what I have in my tree

    $ git log -p --reverse -3 a93d886b9

then we can proceed with that version. Have there been any issues raised on that older version other than error reporting?

#10 lore raw

Re: [PATCH v9 4/4] tag: support configuring --sort via .gitconfig

Quoted 19 lines · Junio C Hamano
Quoted 11 lines · Jacob E
Quoted 8 lines

After all, it seems to me that the one in

    http://thread.gmane.org/gmane.comp.version-control.git/253346

struck the right balance among various abuses; let's use the error reporter from that version, instead of going down this rabbit hole.

Thanks.

So is that patch series version acceptable then? Or should I spin one again that is in that vein?

I do not offhand know what other changes you had in v9 since $gmane/253346, so I'll leave it up to you. If the only difference is the error reporting codepath, and you are happy with what I have in my tree

Quoted 8 lines
    $ git log -p --reverse -3 a93d886b9

then we can proceed with that version. Have there been any issues raised on that older version other than error reporting?

I'll double check.

Thanks,
Jake