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

Re: [PATCH 5/7] Makefile: add 'check-sort' target

From
Jeff King <peff@peff.net>
Date
Mar 17, 2021, 17:32 UTC
Message-ID
<YFI9QzKMKLMXYoyz@coredump.intra.peff.net>
In-Reply-To
<87mtv2dk18.fsf@evledraar.gmail.com>
On Wed, Mar 17, 2021 at 01:47:15PM +0100, Ævar Arnfjörð Bjarmason wrote:
Show 13 quoted lines
> Why does this part need to be a Perl script at all? We can check this in
> the makefile itself. Make has a sort function and string comparisons,
> e.g.:
> 
> LIB_OBJS_SORTED =
> LIB_OBJS_SORTED += $(sort $(LIB_OBJS))
> ifneq ("$(LIB_OBJS)", "$(LIB_OBJS_SORTED)")
> $(error "please sort and de-duplicate LIB_OBJS!")
> endif
> 
> This will fail/pass before/after your patches. Note that make's sort
> isn't just a sort, it also de-deplicates (not that we're likely to have
> that issue).

I like that much better, if only because it runs quickly and automatically (as opposed to much later as part of some CI process). Reducing the length of the feedback loop makes automated checks much less annoying.

It doesn't indicate which lines are out of order or duplicated. We could probably add some $(shell) magic to dump both sides to diff inside the ifneq. That's getting unwieldy to use in each site, but I wonder if something like this solves that:

diff --git a/Makefile b/Makefile
index dfb0f1000f..3fa7893c8b 100644
--- a/Makefile
+++ b/Makefile
@@ -596,6 +596,16 @@ THIRD_PARTY_SOURCES =
 # interactive shell sessions without exporting it.
 unexport CDPATH
 
+# usage: $(call check-sort,VAR)
+# If this complains, then make sure the contents of VAR are ASCII-sorted.
+define check-sort-template
+SORTED_$1 = $$(sort $$($1))
+ifneq ($$($1),$$(SORTED_$1))
+$$(error "please sort and de-duplicate $1!")
+endif
+endef
+check-sort = $(eval $(call check-sort-template,$1))
+
 SCRIPT_SH += git-bisect.sh
 SCRIPT_SH += git-difftool--helper.sh
 SCRIPT_SH += git-filter-branch.sh
@@ -1037,6 +1047,7 @@ LIB_OBJS += ws.o
 LIB_OBJS += wt-status.o
 LIB_OBJS += xdiff-interface.o
 LIB_OBJS += zlib.o
+$(call check-sort,LIB_OBJS)
 
 BUILTIN_OBJS += builtin/add.o
 BUILTIN_OBJS += builtin/am.o

And then it's just a single-liner for each block that should be checked.
We haven't used $(call) or $(eval) yet in our Makefile, but past
discussions have reached the conclusion that they should be safe
(they're both in GNU make 3.80, which is the oldest version worth caring
about).

TBH, I'm a little on the fence on whether automatically checking this is
even worth the hassle. Doing the make function above was a fun
diversion, but already I think this discussion has taken more time than
people actually spend resolving conflicts on unsorted Makefile lists.

> > [...]
> > +	./check-sort.perl '\t\{ "[^"]*",' <git.c
> 
> This last one you can IMO be done better as (or if we want to be more
> anal, we could make git die on startup if it's not true):
>     
>     diff --git a/t/t0012-help.sh b/t/t0012-help.sh
>     index 5679e29c62..5bd2ebceca 100755
>     --- a/t/t0012-help.sh
>     +++ b/t/t0012-help.sh
>     @@ -77,6 +77,11 @@ test_expect_success 'generate builtin list' '
>             git --list-cmds=builtins >builtins
>      '
>      
>     +test_expect_success 'list of builtins in git.c should be sorted' '
>     +       sort builtins >sorted &&
>     +       test_cmp sorted builtins
>     +'

Again, much nicer, because it just happens as part of the test suite
(which I hope everyone is running after making changes). If we care
about the order in the source code, then this check implies that we are
not sorting the list internally before outputting it, but that is
probably reasonable.

> > +	./check-sort.perl 'int cmd_[^(]*\(' <builtin.h
> > +	./check-sort.perl 'int cmd__[^(]*\(' <t/helper/test-tool.h
> 
> As something that can't be done in the Makefile itself or that we're
> already extracting from the tests.
> 
> Both of those IMO would be better handled down the line by making the
> relevant part of these files generated from the data in the Makefile, at
> which point we'd have no need for the external perl script.

Agreed. Automating away a tedious task is always better than
automatically checking that somebody did it right. :)

-Peff
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 12 of 24 in “Sort lists and add static-analysis”
  1. 0/7 Sort lists and add static-analysisDenton Liu, Mar 16, 2021
  2. 3/7 builtin.h: ASCII-sort list of functionsDenton Liu, Mar 16, 2021
  3. Junio C HamanoMar 17, 2021
  4. 2/7 Makefile: ASCII-sort LIB_OBJSDenton Liu, Mar 16, 2021
  5. 1/7 Makefile: mark 'check-builtins' as a .PHONY targetDenton Liu, Mar 16, 2021
  6. Eric SunshineMar 16, 2021
  7. Junio C HamanoMar 17, 2021
  8. 5/7 Makefile: add 'check-sort' targetDenton Liu, Mar 16, 2021
  9. Eric SunshineMar 16, 2021
  10. Denton LiuMar 17, 2021
  11. Ævar Arnfjörð BjarmasonMar 17, 2021
  12. Jeff KingMar 17, 2021
  13. Ævar Arnfjörð BjarmasonMar 17, 2021
  14. Eric SunshineMar 17, 2021
  15. Jeff KingMar 17, 2021
  16. Junio C HamanoMar 17, 2021
  17. Ævar Arnfjörð BjarmasonMar 17, 2021
  18. Junio C HamanoMar 17, 2021
  19. 6/7 ci/run-static-analysis.sh: make check-builtinsDenton Liu, Mar 16, 2021
  20. 4/7 test-tool.h: ASCII-sort list of functionsDenton Liu, Mar 16, 2021
  21. Junio C HamanoMar 17, 2021
  22. 7/7 ci/run-static-analysis.sh: make check-sortDenton Liu, Mar 16, 2021
  23. Bagas SanjayaMar 17, 2021
  24. Junio C HamanoMar 17, 2021

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.