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

12 messages from 2015-10-06 to 2015-10-08. Participants: Tobias Klauser, Johannes Schindelin, Junio C Hamano, Stefan Beller.
Thread: https://gitlist.dev/t/40499

## Tobias Klauser, 2015-10-06 12:15

Subject: [PATCH] pretend_sha1_file(): Change return type from int to void
Message-ID: <1444133704-29571-1-git-send-email-tklauser@distanz.ch>
URL: https://gitlist.dev/e/1444133704-29571-1-git-send-email-tklauser%40distanz.ch

```
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(-)

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, 2015-10-06 13:16

Subject: Re: [PATCH] pretend_sha1_file(): Change return type from int to void
Message-ID: <632cbcf1dc9fa45ce71693a2cfae73e4@dscho.org>
URL: https://gitlist.dev/e/632cbcf1dc9fa45ce71693a2cfae73e4%40dscho.org
In-Reply-To: <1444133704-29571-1-git-send-email-tklauser@distanz.ch>

```
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, 2015-10-06 13:51

Subject: Re: [PATCH] pretend_sha1_file(): Change return type from int to void
Message-ID: <20151006135101.GA11304@distanz.ch>
URL: https://gitlist.dev/e/20151006135101.GA11304%40distanz.ch
In-Reply-To: <632cbcf1dc9fa45ce71693a2cfae73e4@dscho.org>

```
Hi Johannes

Thanks for your feedback.

On 2015-10-06 at 15:16:12 +0200, Johannes Schindelin <johannes.schindelin@gmx.de> wrote:
> 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, 2015-10-06 14:30

Subject: Re: [PATCH] pretend_sha1_file(): Change return type from int to void
Message-ID: <ef5b20ed42ea20b2891fc3998a81f339@dscho.org>
URL: https://gitlist.dev/e/ef5b20ed42ea20b2891fc3998a81f339%40dscho.org
In-Reply-To: <20151006135101.GA11304@distanz.ch>

```
Hi Tobias,

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.

Ciao,
Johannes

```

## Tobias Klauser, 2015-10-07 08:13

Subject: Re: [PATCH] pretend_sha1_file(): Change return type from int to void
Message-ID: <20151007081344.GC11304@distanz.ch>
URL: https://gitlist.dev/e/20151007081344.GC11304%40distanz.ch
In-Reply-To: <ef5b20ed42ea20b2891fc3998a81f339@dscho.org>

```
Hi Johannes

On 2015-10-06 at 16:30:36 +0200, Johannes Schindelin <johannes.schindelin@gmx.de> wrote:
> 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, 2015-10-07 17:36

Subject: Re: [PATCH] pretend_sha1_file(): Change return type from int to void
Message-ID: <xmqqfv1mvawu.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqfv1mvawu.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <ef5b20ed42ea20b2891fc3998a81f339@dscho.org>

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

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, 2015-10-07 20:42

Subject: Re: [PATCH] pretend_sha1_file(): Change return type from int to void
Message-ID: <CAGZ79kZmon6xwDE2reSOjM87HfG_dqc6-Rk2KzxnePLAN=BiQw@mail.gmail.com>
URL: https://gitlist.dev/e/CAGZ79kZmon6xwDE2reSOjM87HfG_dqc6-Rk2KzxnePLAN%3DBiQw%40mail.gmail.com
In-Reply-To: <xmqqfv1mvawu.fsf@gitster.mtv.corp.google.com>

```
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, 2015-10-07 21:14

Subject: Re: [PATCH] pretend_sha1_file(): Change return type from int to void
Message-ID: <xmqqh9m2tm8v.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqh9m2tm8v.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <CAGZ79kZmon6xwDE2reSOjM87HfG_dqc6-Rk2KzxnePLAN=BiQw@mail.gmail.com>

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

```

## Junio C Hamano, 2015-10-07 21:22

Subject: Re: [PATCH] pretend_sha1_file(): Change return type from int to void
Message-ID: <xmqqa8rutlu4.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqa8rutlu4.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <ef5b20ed42ea20b2891fc3998a81f339@dscho.org>

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

 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

```

## Stefan Beller, 2015-10-07 21:24

Subject: Re: [PATCH] pretend_sha1_file(): Change return type from int to void
Message-ID: <CAGZ79kYfcA4Atsx9zy+DNA_uhW-f91c5dLMGqhwSpEV7tPE5dA@mail.gmail.com>
URL: https://gitlist.dev/e/CAGZ79kYfcA4Atsx9zy%2BDNA_uhW-f91c5dLMGqhwSpEV7tPE5dA%40mail.gmail.com
In-Reply-To: <xmqqh9m2tm8v.fsf@gitster.mtv.corp.google.com>

```
On Wed, Oct 7, 2015 at 2:14 PM, Junio C Hamano <gitster@pobox.com> wrote:
> 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.

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

>
> 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, 2015-10-07 21:29

Subject: Re: [PATCH] pretend_sha1_file(): Change return type from int to void
Message-ID: <xmqq612itlj8.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqq612itlj8.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <CAGZ79kYfcA4Atsx9zy+DNA_uhW-f91c5dLMGqhwSpEV7tPE5dA@mail.gmail.com>

```
Stefan Beller <sbeller@google.com> writes:

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

```

## Tobias Klauser, 2015-10-08 07:45

Subject: Re: [PATCH] pretend_sha1_file(): Change return type from int to void
Message-ID: <20151008074526.GD11304@distanz.ch>
URL: https://gitlist.dev/e/20151008074526.GD11304%40distanz.ch
In-Reply-To: <xmqqa8rutlu4.fsf@gitster.mtv.corp.google.com>

```
On 2015-10-07 at 23:22:59 +0200, Junio C Hamano <gitster@pobox.com> wrote:
> 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.

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

```
