git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] setup.c: move statement under condition
@ 2017-12-23 20:02 Vadim Petrov
  2017-12-23 22:48 ` Johannes Schindelin
  0 siblings, 1 reply; 7+ messages in thread
From: Vadim Petrov @ 2017-12-23 20:02 UTC (permalink / raw)
  To: git@vger.kernel.org

I suppose that if the condition is fulfilled then the previously
obtained value will not be necessary.

Signed-off-by: Vadim Petrov <tridronet@protonmail.com>
---
 setup.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

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);
 	}
 	path0 = path;
 	path += off;
-- 
2.15.1.433.g936d1b989


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] setup.c: move statement under condition
  2017-12-23 20:02 [PATCH] setup.c: move statement under condition Vadim Petrov
@ 2017-12-23 22:48 ` Johannes Schindelin
  2017-12-24  8:15   ` Vadim Petrov
  2017-12-27 18:57   ` Junio C Hamano
  0 siblings, 2 replies; 7+ messages in thread
From: Johannes Schindelin @ 2017-12-23 22:48 UTC (permalink / raw)
  To: Vadim Petrov; +Cc: git@vger.kernel.org

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.

> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] setup.c: move statement under condition
  2017-12-23 22:48 ` Johannes Schindelin
@ 2017-12-24  8:15   ` Vadim Petrov
  2017-12-24 17:11     ` Kevin Daudt
  2017-12-27 18:57   ` Junio C Hamano
  1 sibling, 1 reply; 7+ messages in thread
From: Vadim Petrov @ 2017-12-24  8:15 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: git@vger.kernel.org

Thank you for your replay.

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

I still only learn English and correctly express my thoughts while somewhat difficult.

> 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.

I have no such reason. I just saw it and wanted to change it.

> 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.

I was guided by the rule from the Documentation/CodingGuidelines:
	When there are multiple arms to a conditional and some of them
	require braces, enclose even a single line block in braces for
	consistency.
And other code from setup.c:
	from function get_common_dir:
		if (!has_common) {
			/* several commands */
		} else {
			free(candidate->work_tree);
		}
	from function get_common_dir_noenv:
		if (file_exists(path.buf)) {
			/* several commands */
		} else {
			strbuf_addstr(sb, gitdir);
		}

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

I fixed the patch according to your suggestions.


Signed-off-by: Vadim Petrov <tridronet@yandex.ru>
---
 setup.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/setup.c b/setup.c
index 8cc34186c..1a414c256 100644
--- a/setup.c
+++ b/setup.c
@@ -27,26 +27,26 @@ static int abspath_part_inside_repo(char *path)
 {
 	size_t len;
 	size_t wtlen;
 	char *path0;
 	int off;
 	const char *work_tree = get_git_work_tree();
 
 	if (!work_tree)
 		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 { /* check if work tree is already the prefix */
 		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;
 	}
-- 
2.15.1.433.g936d1b989

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] setup.c: move statement under condition
  2017-12-24  8:15   ` Vadim Petrov
@ 2017-12-24 17:11     ` Kevin Daudt
  2017-12-24 19:35       ` Ævar Arnfjörð Bjarmason
  0 siblings, 1 reply; 7+ messages in thread
From: Kevin Daudt @ 2017-12-24 17:11 UTC (permalink / raw)
  To: Vadim Petrov; +Cc: Johannes Schindelin, git@vger.kernel.org

On Sun, Dec 24, 2017 at 12:15:35PM +0400, Vadim Petrov wrote:
> Thank you for your replay.
> 
> > I have to be honest: this commit message (including the subject) left me
> > quite puzzled as to the intent of this patch.
> 
> I still only learn English and correctly express my thoughts while somewhat difficult.
> 
> > 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.
> 
> I have no such reason. I just saw it and wanted to change it.

A commit message contains the reason why this is a good change to make.
It lets others know what problems it's trying to solve or what usecase
it tries to satisfy.

The commit message basically needs to convince others why the change is
necessary / desired, now, and in the future. 

This will help others to follow your thought process and it gives you
the possiblity to communicate trade-offs you made, all which cannot
inferred from the patch.

For simple changes, the motivation can be simple too.

To make it concrete: You are talking about a condition. What condition?
And you say that the previously obtained value will not be necessary.
What do you do with that value then? Why does this change improve the
situation? 

These are things you can state in your commit message.

Hope this helps, Kevin

> > 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.
> 
> I was guided by the rule from the Documentation/CodingGuidelines:
> 	When there are multiple arms to a conditional and some of them
> 	require braces, enclose even a single line block in braces for
> 	consistency.
> And other code from setup.c:
> 	from function get_common_dir:
> 		if (!has_common) {
> 			/* several commands */
> 		} else {
> 			free(candidate->work_tree);
> 		}
> 	from function get_common_dir_noenv:
> 		if (file_exists(path.buf)) {
> 			/* several commands */
> 		} else {
> 			strbuf_addstr(sb, gitdir);
> 		}
> 
> > In short: I think your patch does the right thing, and I hope that you
> > find my suggestions to improve the patch useful.
> 
> I fixed the patch according to your suggestions.
> 
> 
> Signed-off-by: Vadim Petrov <tridronet@yandex.ru>
> ---
>  setup.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/setup.c b/setup.c
> index 8cc34186c..1a414c256 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -27,26 +27,26 @@ static int abspath_part_inside_repo(char *path)
>  {
>  	size_t len;
>  	size_t wtlen;
>  	char *path0;
>  	int off;
>  	const char *work_tree = get_git_work_tree();
>  
>  	if (!work_tree)
>  		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 { /* check if work tree is already the prefix */
>  		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;
>  	}
> -- 
> 2.15.1.433.g936d1b989

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] setup.c: move statement under condition
  2017-12-24 17:11     ` Kevin Daudt
@ 2017-12-24 19:35       ` Ævar Arnfjörð Bjarmason
  2017-12-25 17:52         ` Martin Werner
  0 siblings, 1 reply; 7+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2017-12-24 19:35 UTC (permalink / raw)
  To: Kevin Daudt
  Cc: Vadim Petrov, Johannes Schindelin, git@vger.kernel.org,
	Martin Erik Werner


On Sun, Dec 24 2017, Kevin Daudt jotted:

> On Sun, Dec 24, 2017 at 12:15:35PM +0400, Vadim Petrov wrote:
>> Thank you for your replay.
>>
>> > I have to be honest: this commit message (including the subject) left me
>> > quite puzzled as to the intent of this patch.
>>
>> I still only learn English and correctly express my thoughts while somewhat difficult.
>>
>> > 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.
>>
>> I have no such reason. I just saw it and wanted to change it.
>
> A commit message contains the reason why this is a good change to make.
> It lets others know what problems it's trying to solve or what usecase
> it tries to satisfy.
>
> The commit message basically needs to convince others why the change is
> necessary / desired, now, and in the future.
>
> This will help others to follow your thought process and it gives you
> the possiblity to communicate trade-offs you made, all which cannot
> inferred from the patch.
>
> For simple changes, the motivation can be simple too.

...and a good example would be 6127ff63cf which introduced this logic
Vadim is trying to change, i.e. does this still retain the fix for
whatever issue that was trying to address?

It's also good to CC the people who've directly worked on the code
you're changing in the past, I've CC'd Martin.

> To make it concrete: You are talking about a condition. What condition?
> And you say that the previously obtained value will not be necessary.
> What do you do with that value then? Why does this change improve the
> situation?
>
> These are things you can state in your commit message.
>
> Hope this helps, Kevin
>
>> > 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.
>>
>> I was guided by the rule from the Documentation/CodingGuidelines:
>> 	When there are multiple arms to a conditional and some of them
>> 	require braces, enclose even a single line block in braces for
>> 	consistency.
>> And other code from setup.c:
>> 	from function get_common_dir:
>> 		if (!has_common) {
>> 			/* several commands */
>> 		} else {
>> 			free(candidate->work_tree);
>> 		}
>> 	from function get_common_dir_noenv:
>> 		if (file_exists(path.buf)) {
>> 			/* several commands */
>> 		} else {
>> 			strbuf_addstr(sb, gitdir);
>> 		}
>>
>> > In short: I think your patch does the right thing, and I hope that you
>> > find my suggestions to improve the patch useful.
>>
>> I fixed the patch according to your suggestions.
>>
>>
>> Signed-off-by: Vadim Petrov <tridronet@yandex.ru>
>> ---
>>  setup.c | 6 +++---
>>  1 file changed, 3 insertions(+), 3 deletions(-)
>>
>> diff --git a/setup.c b/setup.c
>> index 8cc34186c..1a414c256 100644
>> --- a/setup.c
>> +++ b/setup.c
>> @@ -27,26 +27,26 @@ static int abspath_part_inside_repo(char *path)
>>  {
>>  	size_t len;
>>  	size_t wtlen;
>>  	char *path0;
>>  	int off;
>>  	const char *work_tree = get_git_work_tree();
>>
>>  	if (!work_tree)
>>  		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 { /* check if work tree is already the prefix */
>>  		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;
>>  	}
>> --
>> 2.15.1.433.g936d1b989

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] setup.c: move statement under condition
  2017-12-24 19:35       ` Ævar Arnfjörð Bjarmason
@ 2017-12-25 17:52         ` Martin Werner
  0 siblings, 0 replies; 7+ messages in thread
From: Martin Werner @ 2017-12-25 17:52 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: Kevin Daudt, Vadim Petrov, Johannes Schindelin,
	git@vger.kernel.org

On Sun, Dec 24, 2017 at 8:35 PM, Ævar Arnfjörð Bjarmason
<avarab@gmail.com> wrote:
>
> On Sun, Dec 24 2017, Kevin Daudt jotted:
>
>> On Sun, Dec 24, 2017 at 12:15:35PM +0400, Vadim Petrov wrote:
>>> Thank you for your replay.
>>>
>>> > I have to be honest: this commit message (including the subject) left me
>>> > quite puzzled as to the intent of this patch.
>>>
>>> I still only learn English and correctly express my thoughts while somewhat difficult.
>>>
>>> > 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.
>>>
>>> I have no such reason. I just saw it and wanted to change it.
>>
>> A commit message contains the reason why this is a good change to make.
>> It lets others know what problems it's trying to solve or what usecase
>> it tries to satisfy.
>>
>> The commit message basically needs to convince others why the change is
>> necessary / desired, now, and in the future.
>>
>> This will help others to follow your thought process and it gives you
>> the possiblity to communicate trade-offs you made, all which cannot
>> inferred from the patch.
>>
>> For simple changes, the motivation can be simple too.
>
> ...and a good example would be 6127ff63cf which introduced this logic
> Vadim is trying to change, i.e. does this still retain the fix for
> whatever issue that was trying to address?
>
> It's also good to CC the people who've directly worked on the code
> you're changing in the past, I've CC'd Martin.
>
>> To make it concrete: You are talking about a condition. What condition?
>> And you say that the previously obtained value will not be necessary.
>> What do you do with that value then? Why does this change improve the
>> situation?
>>
>> These are things you can state in your commit message.
>>
>> Hope this helps, Kevin
>>
>>> > 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.
>>>
>>> I was guided by the rule from the Documentation/CodingGuidelines:
>>>      When there are multiple arms to a conditional and some of them
>>>      require braces, enclose even a single line block in braces for
>>>      consistency.
>>> And other code from setup.c:
>>>      from function get_common_dir:
>>>              if (!has_common) {
>>>                      /* several commands */
>>>              } else {
>>>                      free(candidate->work_tree);
>>>              }
>>>      from function get_common_dir_noenv:
>>>              if (file_exists(path.buf)) {
>>>                      /* several commands */
>>>              } else {
>>>                      strbuf_addstr(sb, gitdir);
>>>              }
>>>
>>> > In short: I think your patch does the right thing, and I hope that you
>>> > find my suggestions to improve the patch useful.
>>>
>>> I fixed the patch according to your suggestions.
>>>
>>>
>>> Signed-off-by: Vadim Petrov <tridronet@yandex.ru>
>>> ---
>>>  setup.c | 6 +++---
>>>  1 file changed, 3 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/setup.c b/setup.c
>>> index 8cc34186c..1a414c256 100644
>>> --- a/setup.c
>>> +++ b/setup.c
>>> @@ -27,26 +27,26 @@ static int abspath_part_inside_repo(char *path)
>>>  {
>>>      size_t len;
>>>      size_t wtlen;
>>>      char *path0;
>>>      int off;
>>>      const char *work_tree = get_git_work_tree();
>>>
>>>      if (!work_tree)
>>>              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 { /* check if work tree is already the prefix */
>>>              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;
>>>      }

As far as I can tell this retains existing functionality.

Is this intended as just a style change or a speculative performance
change?

So the general concept is:

x = fa();

if (...) {
  if (...) {
    return 0;
  }
  x = fb();
}

being rewritten as

if (...) {
  if (...) {
    return 0;
  }
  x = fb();
} else {
  x = fa();
}

or, in the last iteration

if (!...) {
  x = fa();
} else {
  if (...) {
    return 0;
  }
  x = fb();
}

which (at least conceptually) avoids setting x unnecessarily when we do
the early return.

I think the last iteration might suffer a bit from the condition
inversion, since the comment feels a bit odd placed there at the
"} else {" line, and if it were to be placed at the top, it would have
to be negated "check if work tree is NOT already the prefix". Therefore
I think the original or the first iteration might be a tad better from a
readability perspective.


(Going down this path, We could potentially also remove the 'off'
variable completely, increment the 'path' pointer directly, and set
the 'path0' pointer before, not sure if that's a good idea though...)

--
Martin Erik Werner <martinerikwerner@gmail.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] setup.c: move statement under condition
  2017-12-23 22:48 ` Johannes Schindelin
  2017-12-24  8:15   ` Vadim Petrov
@ 2017-12-27 18:57   ` Junio C Hamano
  1 sibling, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2017-12-27 18:57 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Vadim Petrov, git@vger.kernel.org

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

>> 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.

Sensible.

>> 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. 

That is not quite correct.  We do encourage 

	if (...)
		single statement;

with or without

	else
		another single statement;

IOW, when both sides do not need curlies, we save vertical space.
On the other hand, when one side needs curlies, we tend to add to
both to make it easier to spot the correspondence, i.e.

	if (...) {
		compond statement;
		compond statement;
	} else {
		single statement;
	}

or the other way around.


	if (...) {
		single statement;
	} else {
		compond statement;
		compond statement;
	}

> 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;
> 	}

This also may allow you to further dedent the if/else chain and make
the result easier to follow.  I dunno.

Thanks for a review.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-12-27 18:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-23 20:02 [PATCH] setup.c: move statement under condition Vadim Petrov
2017-12-23 22:48 ` Johannes Schindelin
2017-12-24  8:15   ` Vadim Petrov
2017-12-24 17:11     ` Kevin Daudt
2017-12-24 19:35       ` Ævar Arnfjörð Bjarmason
2017-12-25 17:52         ` Martin Werner
2017-12-27 18:57   ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).