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

Re: [PATCH 2/4] Extract and improve whitespace check from "git apply"

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 12, 2007, 19:39 UTC
Message-ID
<7vsl277ldc.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<1197476582-18956-3-git-send-email-win@wincent.com>
Wincent Colaiuta <win@wincent.com> writes:
Show 13 quoted lines
> diff --git a/ws.c b/ws.c
> index 52c10ca..884d373 100644
> --- a/ws.c
> +++ b/ws.c
> +unsigned check_whitespace(const char *line, int len, unsigned ws_rule)
> +{
> + ...
> +	if (ws_rule & WS_TRAILING_SPACE) {
> +		/* Lines start with "+" or "-" so length is at least 1 */
> +		if (line[len - 1] == '\n') {
> +			if (isspace(line[len - 2]))
> +				result |= WS_TRAILING_SPACE;
> +		}

I like the direction, but I think it would make much more sense if you make check_whitespace() not about "a line in a patch that adds a line", but about "here is a line, check if that is acceptable". IOW, make line variable zero-based (and len = strlen(line)). The change would mean that existing callers need to be modified to do something like:

	if (line[0] == '+')
        	check_whitespace(line+1, len-1, ...);

but at the same time we could conceivably teach "git show" to show whitespace errors in a blob, i.e. "git show --show-ws-error HEAD:ws.c" by using such a check_whitespace().

The highlighting code may need similar changes. I was actually hoping you would consolidate the logic there that decides which segment of the string to highlight, and the logic in check_whitespace() to decide if there is an error to begin with. Conceptually, if emit_line_with_ws() decides there is nothing to highlight with DIFF_WHITESPACE color, that means there is no whitespace error on the line and vice-versa, no?

Previous: Wincent ColaiutaNext: Wincent Colaiuta
Message 6 of 7 in “Refactoring for apply and diff”
  1. 0/4 Refactoring for apply and diffWincent Colaiuta, Dec 12, 2007
  2. 1/4 Fix "diff --check" whitespace detectionWincent Colaiuta, Dec 12, 2007
  3. 2/4 Extract and improve whitespace check from "git apply"Wincent Colaiuta, Dec 12, 2007
  4. 3/3 Make "diff --check" use shared whitespace functionsWincent Colaiuta, Dec 12, 2007
  5. 4/4 Add tests for "git diff --check" with core.whitespace optionsWincent Colaiuta, Dec 12, 2007
  6. Junio C HamanoDec 12, 2007
  7. Wincent ColaiutaDec 12, 2007

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.