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

Re: [PATCH] setup.c: move statement under condition

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Dec 23, 2017, 22:48 UTC
Message-ID
<alpine.DEB.2.21.1.1712232332000.406@MININT-6BKU6QN.europe.corp.microsoft.com>
In-Reply-To
<3853941514059379@web42g.yandex.ru>
Hi Vadim,
thank you for your contribution!
On Sun, 24 Dec 2017, Vadim Petrov wrote:
> I suppose that if the condition is fulfilled then the previously
> obtained value will not be necessary.

I have to be honest: this commit message (including the subject) left me quite puzzled as to the intent of this patch.

Maybe something like this would have spared me that puzzlement:
	Avoid unnecessary offset_1st_component() when prefixing filenames
	In the abspath_part_inside_repo() function that is called
	somewhere deep in the call-chain when prefixing paths, we
	calculate the offset of the first component, but under certain
	circumstances, the result is not even used.
	This patch changes the code to avoid that.

If you also have a background story that motivated you to work on this patch (for example, if you hit a huge performance bottleneck with some tool that fed thousands of absolute paths to Git that needed to be turned into paths relative to the worktree's top-level directory), I would definitely put that into the commit message, too, if I were you.

Show 19 quoted lines
> diff --git a/setup.c b/setup.c
> index 8cc34186c..1ce0189fa 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -35,7 +35,6 @@ static int abspath_part_inside_repo(char *path)
>  		return -1;
>  	wtlen = strlen(work_tree);
>  	len = strlen(path);
> -	off = offset_1st_component(path);
>  
>  	/* check if work tree is already the prefix */
>  	if (wtlen <= len && !strncmp(path, work_tree, wtlen)) {
> @@ -49,6 +48,8 @@ static int abspath_part_inside_repo(char *path)
>  		}
>  		/* work tree might match beginning of a symlink to work tree */
>  		off = wtlen;
> +	} else {
> +		off = offset_1st_component(path);
>  	}

Up until recently, we encouraged dropping the curly brackets from single-line statements, but apparently that changed. It is now no longer clear, and often left to the taste of the contributor. But not always. Sometimes we start a beautiful thread discussion the pros and cons of curly brackets in the middle of patch review, and drop altogether reviewing the actual patch.

However, we still encourage to put shorter alternative code paths (i.e. the blocks after `if` and `else`) first, in your case:

@@ -35,18 +35,19 @@ static int abspath_part_inside_repo(char *path)
 		return -1;
 	wtlen = strlen(work_tree);
 	len = strlen(path);
-	off = offset_1st_component(path);
 
 	/* check if work tree is already the prefix */
- 	if (wtlen <= len && !strncmp(path, work_tree, wtlen)) {
+	if (wtlen > len || strncmp(path, work_tree, wtlen))
+		off = offset_1st_component(path);
+	else {
 		if (path[wtlen] == '/') {
 			memmove(path, path + wtlen + 1, len - wtlen);
 			return 0;
		} else if (path[wtlen - 1] == '/' || path[wtlen] == '\0') {
			/* work tree is the root, or the whole path */
			memmove(path, path + wtlen, len - wtlen + 1);
			return 0;
		}
		/* work tree might match beginning of a symlink to work * tree */
		off = wtlen;
	}

I would also encourage to generate the patch so that it includes the `off
= wtlen` line (by passing -U11 or some such to `git format-patch`), to
make the review super easy.

In short: I think your patch does the right thing, and I hope that you
find my suggestions to improve the patch useful.

Ciao,
Johannes
Previous: Vadim PetrovNext: Vadim Petrov
Message 2 of 7 in “setup.c: move statement under condition”
  1. setup.c: move statement under conditionVadim Petrov, Dec 23, 2017
  2. Johannes SchindelinDec 23, 2017
  3. Vadim PetrovDec 24, 2017
  4. Kevin DaudtDec 24, 2017
  5. Ævar Arnfjörð BjarmasonDec 24, 2017
  6. Martin WernerDec 25, 2017
  7. Junio C HamanoDec 27, 2017

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.