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

Re: [PATCH] pretty-print: de-tabify indented logs to make things line up properly

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 16, 2016, 18:01 UTC
Message-ID
<xmqq7fh25mkc.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<alpine.LFD.2.20.1603160926060.13030@i7>
Linus Torvalds <torvalds@linux-foundation.org> writes:
Show 33 quoted lines
> From: Linus Torvalds <torvalds@linux-foundation.org>
> Date: Wed, 16 Mar 2016 09:15:53 -0700
> Subject: [PATCH] pretty-print: de-tabify indented logs to make things line up properly
>
> This should all line up:
>
>   Column 1	Column 2
>   --------	--------
>   A		B
>   ABCD		EFGH
>   SPACES        Instead of Tabs
>
> Even with multi-byte UTF8 characters:
>
>   Column 1	Column 2
>   --------	--------
>   Ä		B
>   åäö		100
>   A Møøse	once bit my sister..
>
> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
> ---
>
> This seems to work for me, and while there is some cost, it's minimal. 
> Doing a "git log > /dev/null" of the current git tree is about 1% slower 
> because of the tab-finding. A tree with a lot of tabs in the commit 
> messages would be more noticeable, because then you actually end up 
> hitting the whole "how wide is this" issue.
>
> (But if the tabs are all at the beginning of a line, you'd still be ok 
> and avoid the utf8 width calculations).
>
> Comments?
I stared at it for a while, and didn't spot anything wrong with it.
I did wonder about two things, though:
 (1) if turning your "preparation; do { ... } while()" into
     "while () { }" would make the result a bit easier to read;
 (2) if we can somehow eliminate duplication of "tab + 1" (spelled
     differently on the previous line as "1+tab"), the end result
     may get easier to follow.
but both are minor.
Show 97 quoted lines
>  pretty.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 74 insertions(+), 2 deletions(-)
>
> diff --git a/pretty.c b/pretty.c
> index 92b2870a7eab..0b40457f99f0 100644
> --- a/pretty.c
> +++ b/pretty.c
> @@ -1629,6 +1629,76 @@ void pp_title_line(struct pretty_print_context *pp,
>  	strbuf_release(&title);
>  }
>  
> +static int pp_utf8_width(const char *start, const char *end)
> +{
> +	int width = 0;
> +	size_t remain = end - start;
> +
> +	while (remain) {
> +		int n = utf8_width(&start, &remain);
> +		if (n < 0 || !start)
> +			return -1;
> +		width += n;
> +	}
> +	return width;
> +}
> +
> +/*
> + * pp_handle_indent() prints out the intendation, and
> + * perhaps the whole line (without the final newline)
> + *
> + * Why "perhaps"? If there are tabs in the indented line
> + * it will print it out in order to de-tabify the line.
> + *
> + * But if there are no tabs, we just fall back on the
> + * normal "print the whole line".
> + */
> +static int pp_handle_indent(struct strbuf *sb, int indent,
> +			     const char *line, int linelen)
> +{
> +	const char *tab;
> +
> +	strbuf_addchars(sb, ' ', indent);
> +
> +	tab = memchr(line, '\t', linelen);
> +	if (!tab)
> +		return 0;
> +
> +	do {
> +		int width = pp_utf8_width(line, tab);
> +
> +		/*
> +		 * If it wasn't well-formed utf8, or it
> +		 * had characters with badly defined
> +		 * width (control characters etc), just
> +		 * give up on trying to align things.
> +		 */
> +		if (width < 0)
> +			break;
> +
> +		/* Output the data .. */
> +		strbuf_add(sb, line, tab - line);
> +
> +		/* .. and the de-tabified tab */
> +		strbuf_addchars(sb, ' ', 8-(width & 7));
> +
> +		/* Skip over the printed part .. */
> +		linelen -= 1+tab-line;
> +		line = tab + 1;
> +
> +		/* .. and look for the next tab */
> +		tab = memchr(line, '\t', linelen);
> +	} while (tab);
> +
> +	/*
> +	 * Print out everything after the last tab without
> +	 * worrying about width - there's nothing more to
> +	 * align.
> +	 */
> +	strbuf_add(sb, line, linelen);
> +	return 1;
> +}
> +
>  void pp_remainder(struct pretty_print_context *pp,
>  		  const char **msg_p,
>  		  struct strbuf *sb,
> @@ -1652,8 +1722,10 @@ void pp_remainder(struct pretty_print_context *pp,
>  		first = 0;
>  
>  		strbuf_grow(sb, linelen + indent + 20);
> -		if (indent)
> -			strbuf_addchars(sb, ' ', indent);
> +		if (indent) {
> +			if (pp_handle_indent(sb, indent, line, linelen))
> +				linelen = 0;
> +		}
>  		strbuf_add(sb, line, linelen);
>  		strbuf_addch(sb, '\n');
>  	}
Previous: Linus TorvaldsNext: Linus Torvalds
Message 3 of 57 in “pretty-print: de-tabify indented logs to make things line up properly”
  1. pretty-print: de-tabify indented logs to make things line up properlyLinus Torvalds, Mar 16, 2016
  2. Linus TorvaldsMar 16, 2016
  3. Junio C HamanoMar 16, 2016
  4. Linus TorvaldsMar 16, 2016
  5. Junio C HamanoMar 16, 2016
  6. Junio C HamanoMar 16, 2016
  7. Linus TorvaldsMar 16, 2016
  8. Junio C HamanoMar 16, 2016
  9. Linus TorvaldsMar 16, 2016
  10. 1/4 pretty-print: de-tabify indented logs to make things line up properlyJunio C Hamano, Mar 17, 2016
  11. 2/4 pretty-print: simplify the interaction between pp_handle_indent() and its callerJunio C Hamano, Mar 17, 2016
  12. 3/4 pretty-print: further abstract out pp_handle_indent()Junio C Hamano, Mar 17, 2016
  13. 4/4 pretty-print: add --pretty=noexpandJunio C Hamano, Mar 17, 2016
  14. Linus TorvaldsMar 17, 2016
  15. Junio C HamanoMar 17, 2016
  16. Jeff KingMar 18, 2016
  17. Linus TorvaldsMar 18, 2016
  18. Jeff KingMar 18, 2016
  19. Junio C HamanoMar 18, 2016
  20. 0/5 Expanding tabs in "git log" outputJunio C Hamano, Mar 23, 2016
  21. 1/5 pretty-print: de-tabify indented logs to make things line up properlyJunio C Hamano, Mar 23, 2016
  22. 2/5 pretty-print: simplify the interaction between pp_handle_indent() and its callerJunio C Hamano, Mar 23, 2016
  23. 3/5 pretty-print: further abstract out pp_handle_indent()Junio C Hamano, Mar 23, 2016
  24. 4/5 pretty-print: limit expand-tabs to selected --pretty formatsJunio C Hamano, Mar 23, 2016
  25. 5/5 pretty-print: teach "--no-expand-tabs" option to "git log"Junio C Hamano, Mar 23, 2016
  26. Linus TorvaldsMar 23, 2016
  27. Jeff KingMar 24, 2016
  28. Junio C HamanoMar 24, 2016
  29. Torsten BögershausenMar 24, 2016
  30. Junio C HamanoMar 24, 2016
  31. Junio C HamanoMar 24, 2016
  32. Torsten BögershausenMar 25, 2016
  33. Torsten BögershausenMar 25, 2016
  34. Junio C HamanoMar 25, 2016
  35. Junio C HamanoMar 25, 2016
  36. 0/3 Expanding tabs in "git log" outputJunio C Hamano, Mar 29, 2016
  37. 1/3 pretty: expand tabs in indented logs to make things line up properlyJunio C Hamano, Mar 29, 2016
  38. Eric SunshineMar 30, 2016
  39. Junio C HamanoMar 30, 2016
  40. 2/3 pretty: enable --expand-tabs by default for selected pretty formatsJunio C Hamano, Mar 29, 2016
  41. Jeff KingMar 30, 2016
  42. Junio C HamanoMar 30, 2016
  43. 3/3 pretty: allow tweaking tabwidth in --expand-tabsJunio C Hamano, Mar 29, 2016
  44. 0/4 Expanding tabs in "git log" outputJunio C Hamano, Apr 5, 2016
  45. 1/4 pretty: expand tabs in indented logs to make things line up properlyJunio C Hamano, Apr 5, 2016
  46. 2/4 pretty: enable --expand-tabs by default for selected pretty formatsJunio C Hamano, Apr 5, 2016
  47. 3/4 pretty: allow tweaking tabwidth in --expand-tabsJunio C Hamano, Apr 5, 2016
  48. 4/4 pretty: test --expand-tabsJunio C Hamano, Apr 5, 2016
  49. Eric SunshineApr 5, 2016
  50. Jeff KingApr 5, 2016
  51. Junio C HamanoApr 5, 2016
  52. Jeff KingApr 5, 2016
  53. Junio C HamanoApr 5, 2016
  54. Perry HutchisonApr 5, 2016
  55. Jeff KingApr 5, 2016
  56. Linus TorvaldsMar 16, 2016
  57. Junio C HamanoMar 16, 2016

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.