threads / patch / 40499

patchpretend_sha1_file(): Change return type from int to void

Subject: [PATCH] pretend_sha1_file(): Change return type from int to void

## tl;dr

12 messages between Oct 6, 2015 and Oct 8, 2015. Diffs are folded; open one to read it.

replies: 11people: 4as markdown or json

Tobias Klauser· Oct 6, 2015, 12:15 UTC · lore

prented_sha1_file() always returns 0 and its only callsite in builtin/blame.c doesn't use the return value, so change the return type to void.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
---
 cache.h     | 2 +-
 sha1_file.c | 5 ++---
 2 files changed, 3 insertions(+), 4 deletions(-)
Show changes to 2 files +3 −4

cache.h, sha1_file.c

diff --git a/cache.h b/cache.h
index 752031e..445853b 100644
--- a/cache.h
+++ b/cache.h
@@ -970,7 +970,7 @@ extern int sha1_object_info(const unsigned char *, unsigned long *);
 extern int hash_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1);
 extern int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
 extern int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type, unsigned char *sha1, unsigned flags);
-extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);
+extern void pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);
 extern int force_object_loose(const unsigned char *sha1, time_t mtime);
 extern int git_open_noatime(const char *name);
 extern void *map_sha1_file(const unsigned char *sha1, unsigned long *size);
diff --git a/sha1_file.c b/sha1_file.c
index d295a32..d76b723 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -2789,14 +2789,14 @@ static void *read_packed_sha1(const unsigned char *sha1,
 	return data;
 }
 
-int pretend_sha1_file(void *buf, unsigned long len, enum object_type type,
+void pretend_sha1_file(void *buf, unsigned long len, enum object_type type,
 		      unsigned char *sha1)
 {
 	struct cached_object *co;
 
 	hash_sha1_file(buf, len, typename(type), sha1);
 	if (has_sha1_file(sha1) || find_cached_object(sha1))
-		return 0;
+		return;
 	ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
 	co = &cached_objects[cached_object_nr++];
 	co->size = len;
@@ -2804,7 +2804,6 @@ int pretend_sha1_file(void *buf, unsigned long len, enum object_type type,
 	co->buf = xmalloc(len);
 	memcpy(co->buf, buf, len);
 	hashcpy(co->sha1, sha1);
-	return 0;
 }
 
 static void *read_object(const unsigned char *sha1, enum object_type *type,
-- 
2.6.0
Johannes Schindelin· Oct 6, 2015, 13:16 UTC · re: Tobias Klauser · lore

Re: [PATCH] pretend_sha1_file(): Change return type from int to void

Hi Tobias,
On 2015-10-06 14:15, Tobias Klauser wrote:
> prented_sha1_file() always returns 0 and its only callsite in
> builtin/blame.c doesn't use the return value, so change the return type
> to void.
While this commit message is technically correct, it would appear that there are some things left unsaid.
Is there a problem with the current code that is solved by not returning 0? If so, could you add it to the commit message? And in particular, change the oneline appropriately?

Ciao, Johannes

Tobias Klauser· Oct 6, 2015, 13:51 UTC · re: Johannes Schindelin · lore

Re: [PATCH] pretend_sha1_file(): Change return type from int to void

Hi Johannes
Thanks for your feedback.
On 2015-10-06 at 15:16:12 +0200, Johannes Schindelin <johannes.schindelin@gmx.de> wrote:
Show 10 quoted lines
> Hi Tobias,
> 
> On 2015-10-06 14:15, Tobias Klauser wrote:
> > prented_sha1_file() always returns 0 and its only callsite in
> > builtin/blame.c doesn't use the return value, so change the return type
> > to void.
> 
> While this commit message is technically correct, it would appear that there are some things left unsaid.
> 
> Is there a problem with the current code that is solved by not returning 0? If so, could you add it to the commit message? And in particular, change the oneline appropriately?

There's no problem with the current code other than that the return value is unused and thus unnecessary for correct funcionality. So it's certainly not a functional problem but rather a cosmetic change.

Does such a change even make sense (it's one of my first patch to git, so I'm not really sure what your criteria in this respect are)?

If yes, would something like the following bring across the intention more clearly?

  pretend_sha1_file() always returns 0 and its only user in
  builtin/blame.c doesn't use the returned value. Thus, the return value
  is unnecessary and the return type of pretend_sha1_file() can be
  changed to void.

Cheers Tobias

Johannes Schindelin· Oct 6, 2015, 14:30 UTC · re: Tobias Klauser · lore

Re: [PATCH] pretend_sha1_file(): Change return type from int to void

Hi Tobias,
On 2015-10-06 15:51, Tobias Klauser wrote:
Show 15 quoted lines
> On 2015-10-06 at 15:16:12 +0200, Johannes Schindelin
> <johannes.schindelin@gmx.de> wrote:
>>
>> On 2015-10-06 14:15, Tobias Klauser wrote:
>> > prented_sha1_file() always returns 0 and its only callsite in
>> > builtin/blame.c doesn't use the return value, so change the return type
>> > to void.
>>
>> While this commit message is technically correct, it would appear that there are some things left unsaid.
>>
>> Is there a problem with the current code that is solved by not returning 0? If so, could you add it to the commit message? And in particular, change the oneline appropriately?
> 
> There's no problem with the current code other than that the return
> value is unused and thus unnecessary for correct funcionality. So it's
> certainly not a functional problem but rather a cosmetic change.
Okay.
> Does such a change even make sense (it's one of my first patch to git,
> so I'm not really sure what your criteria in this respect are)?
Welcome!
As to the patch, I cannot speak for Junio, of course, but my preference would be to keep the return type. Traditionally, functions that can fail either die() or return an int; non-zero indicates an error. In this case, it seems that we do not have any condition (yet...) under which an error could occur. It does not seem very unlikely that we may eventually have such conditions, though, hence my preference.

Ciao, Johannes

Tobias Klauser· Oct 7, 2015, 08:13 UTC · re: Johannes Schindelin · lore

Re: [PATCH] pretend_sha1_file(): Change return type from int to void

Hi Johannes
On 2015-10-06 at 16:30:36 +0200, Johannes Schindelin <johannes.schindelin@gmx.de> wrote:
Show 26 quoted lines
> On 2015-10-06 15:51, Tobias Klauser wrote:
> 
> > On 2015-10-06 at 15:16:12 +0200, Johannes Schindelin
> > <johannes.schindelin@gmx.de> wrote:
> >>
> >> On 2015-10-06 14:15, Tobias Klauser wrote:
> >> > prented_sha1_file() always returns 0 and its only callsite in
> >> > builtin/blame.c doesn't use the return value, so change the return type
> >> > to void.
> >>
> >> While this commit message is technically correct, it would appear that there are some things left unsaid.
> >>
> >> Is there a problem with the current code that is solved by not returning 0? If so, could you add it to the commit message? And in particular, change the oneline appropriately?
> > 
> > There's no problem with the current code other than that the return
> > value is unused and thus unnecessary for correct funcionality. So it's
> > certainly not a functional problem but rather a cosmetic change.
> 
> Okay.
> 
> > Does such a change even make sense (it's one of my first patch to git,
> > so I'm not really sure what your criteria in this respect are)?
> 
> Welcome!
> 
> As to the patch, I cannot speak for Junio, of course, but my preference would be to keep the return type. Traditionally, functions that can fail either die() or return an int; non-zero indicates an error. In this case, it seems that we do not have any condition (yet...) under which an error could occur. It does not seem very unlikely that we may eventually have such conditions, though, hence my preference.

Ok, I see. Thank you for your explanation. I'll wait for Junio's decision then :)

Cheers Tobias

Junio C Hamano· Oct 7, 2015, 17:36 UTC · re: Johannes Schindelin · lore

Re: [PATCH] pretend_sha1_file(): Change return type from int to void

Johannes Schindelin <johannes.schindelin@gmx.de> writes:
Show 7 quoted lines
> As to the patch, I cannot speak for Junio, of course, but my
> preference would be to keep the return type. Traditionally, functions
> that can fail either die() or return an int; non-zero indicates an
> error. In this case, it seems that we do not have any condition
> (yet...) under which an error could occur. It does not seem very
> unlikely that we may eventually have such conditions, though, hence my
> preference.

Accepting Tobias's patch may have a documentation value to let the callers know that the function does not give the caller any error diagnosis, and it may matter a lot if this were a very frequently used function, but that is not exactly the case here.

I do not care too deeply.
Stefan Beller· Oct 7, 2015, 20:42 UTC · re: Junio C Hamano · lore

Re: [PATCH] pretend_sha1_file(): Change return type from int to void

Compare to a patch[1] I sent a while back and the discussion on it.
[1] https://www.mail-archive.com/git@vger.kernel.org/msg70474.html
Junio C Hamano· Oct 7, 2015, 21:14 UTC · re: Stefan Beller · lore

Re: [PATCH] pretend_sha1_file(): Change return type from int to void

Stefan Beller <sbeller@google.com> writes:
> Compare to a patch[1] I sent a while back and the discussion on it.
>
> [1] https://www.mail-archive.com/git@vger.kernel.org/msg70474.html

It is not clear what conclusion you want others to draw from the comparison, I am afraid.

I am guessing that you are in favor of dropping this patch, because 'int' that signals success or error is the most natural return type and meanint for this function if its callers ever start using the value as the indication of an error, just like in the old thread, the return value from get_remote_heads() had the most useful type and the meaning for its callers if they wanted to use it.

And if that is what you wanted to say, I fully agree with the conclusion.

By the way, it is not a very good comparison, though. The patch in the old thread deliberately attempted to discard a useful piece of information. The information the patch in this thread attempts to discard is not so useful, as there currently is nobody that returns an error in the codepath. So in that sense, the patch in this thread to change the return value to void is a bit more justifiable than the one in the old thread, I think.

Thanks.
Stefan Beller· Oct 7, 2015, 21:24 UTC · re: Junio C Hamano · lore

Re: [PATCH] pretend_sha1_file(): Change return type from int to void

On Wed, Oct 7, 2015 at 2:14 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 8 quoted lines
> Stefan Beller <sbeller@google.com> writes:
>
>> Compare to a patch[1] I sent a while back and the discussion on it.
>>
>> [1] https://www.mail-archive.com/git@vger.kernel.org/msg70474.html
>
> It is not clear what conclusion you want others to draw from the
> comparison, I am afraid.

I did not draw a conclusion. All I wanted is to point out, we've had similar patches before. Maybe I wanted to point out, you had a different opinion about the patch I linked to than you seem to have now about this patch.

Show 10 quoted lines
>
> I am guessing that you are in favor of dropping this patch, because
> 'int' that signals success or error is the most natural return type
> and meanint for this function if its callers ever start using the
> value as the indication of an error, just like in the old thread,
> the return value from get_remote_heads() had the most useful type
> and the meaning for its callers if they wanted to use it.
>
> And if that is what you wanted to say, I fully agree with the
> conclusion.

I really did not want to say anything except for pointing out how similar cases were dealt with in the past. So I guess for a good comparision we'd need to asses how similar the patches are. If they are similar it's easier to link to the old discussion instead of retyping the same reasons.

Show 6 quoted lines
>
> By the way, it is not a very good comparison, though.  The patch in
> the old thread deliberately attempted to discard a useful piece of
> information.  The information the patch in this thread attempts to
> discard is not so useful, as there currently is nobody that returns
> an error in the codepath.

Isn't that a bit picky? (old thread: the information is useful, but nobody uses it, this thread: information is useless, and nobody uses it)

So the similarity is nobody is using the result, the difference is the usefulness of the information provided.

>  So in that sense, the patch in this
> thread to change the return value to void is a bit more justifiable
> than the one in the old thread, I think.
That makes sense to me.
>
> Thanks.
Junio C Hamano· Oct 7, 2015, 21:29 UTC · re: Stefan Beller · lore

Re: [PATCH] pretend_sha1_file(): Change return type from int to void

Stefan Beller <sbeller@google.com> writes:
Show 13 quoted lines
>> By the way, it is not a very good comparison, though.  The patch in
>> the old thread deliberately attempted to discard a useful piece of
>> information.  The information the patch in this thread attempts to
>> discard is not so useful, as there currently is nobody that returns
>> an error in the codepath.
>
> Isn't that a bit picky? (old thread: the information is useful, but
> nobody uses it,
> this thread: information is useless, and nobody uses it)
>
> So the similarity is nobody is using the result, the difference is the
> usefulness of
> the information provided.
Exactly.  Why is it picky?

The amount of work in the existing code that is discarded is the amount of work it will take when somebody wants to resurrect the compuation of that useful information. When you judge pros and cons for a patch that discards existing code, you would need to take both into account---the cost of carrying it and the future cost of having to resurrect it.

Junio C Hamano· Oct 7, 2015, 21:22 UTC · re: Johannes Schindelin · lore

Re: [PATCH] pretend_sha1_file(): Change return type from int to void

Johannes Schindelin <johannes.schindelin@gmx.de> writes:
Show 7 quoted lines
> As to the patch, I cannot speak for Junio, of course, but my
> preference would be to keep the return type. Traditionally, functions
> that can fail either die() or return an int; non-zero indicates an
> error. In this case, it seems that we do not have any condition
> (yet...) under which an error could occur. It does not seem very
> unlikely that we may eventually have such conditions, though, hence my
> preference.
Perhaps the attached is a better approach.

Even though the current implementation of "pretend" implementation does not, future generations are allowed to make pretend_sha1_file() return failure when appropriate.

 builtin/blame.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Show changes to builtin/blame.c +2 −1
diff --git a/builtin/blame.c b/builtin/blame.c
index 203a981..fa24f8f 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -2362,7 +2362,8 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
 	convert_to_git(path, buf.buf, buf.len, &buf, 0);
 	origin->file.ptr = buf.buf;
 	origin->file.size = buf.len;
-	pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);
+	if (pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1))
+		die("failed to create a fake commit for the working tree version.");
 
 	/*
 	 * Read the current index, replace the path entry with
Tobias Klauser· Oct 8, 2015, 07:45 UTC · re: Junio C Hamano · lore

Re: [PATCH] pretend_sha1_file(): Change return type from int to void

On 2015-10-07 at 23:22:59 +0200, Junio C Hamano <gitster@pobox.com> wrote:
Show 15 quoted lines
> Johannes Schindelin <johannes.schindelin@gmx.de> writes:
> 
> > As to the patch, I cannot speak for Junio, of course, but my
> > preference would be to keep the return type. Traditionally, functions
> > that can fail either die() or return an int; non-zero indicates an
> > error. In this case, it seems that we do not have any condition
> > (yet...) under which an error could occur. It does not seem very
> > unlikely that we may eventually have such conditions, though, hence my
> > preference.
> 
> Perhaps the attached is a better approach.
> 
> Even though the current implementation of "pretend" implementation
> does not, future generations are allowed to make pretend_sha1_file()
> return failure when appropriate.

For my original patch I didn't consider that pretend_sha1_file() might return failure in the future. I was just confused by the fact that the return value was seemingly useless (but now I realize that unused != useless ;-), sorry for the noise.

Please disregard my patch and apply yours instead, if you see fit.
Show 19 quoted lines
> 
>  builtin/blame.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/builtin/blame.c b/builtin/blame.c
> index 203a981..fa24f8f 100644
> --- a/builtin/blame.c
> +++ b/builtin/blame.c
> @@ -2362,7 +2362,8 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
>  	convert_to_git(path, buf.buf, buf.len, &buf, 0);
>  	origin->file.ptr = buf.buf;
>  	origin->file.size = buf.len;
> -	pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);
> +	if (pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1))
> +		die("failed to create a fake commit for the working tree version.");
>  
>  	/*
>  	 * Read the current index, replace the path entry with
> 

← back to recent threads