# [PATCH] Functions for updating refs.

10 messages from 2007-09-04 to 2007-09-05. Participants: Carlos Rica, Johannes Schindelin, Johannes Sixt, Karl Hasselström, Junio C Hamano.
Thread: https://gitlist.dev/t/9773

## Carlos Rica, 2007-09-04 13:39

Subject: [PATCH] Functions for updating refs.
Message-ID: <46DD6020.4050401@gmail.com>
URL: https://gitlist.dev/e/46DD6020.4050401%40gmail.com

```
Signed-off-by: Carlos Rica <jasampler@gmail.com>
---

   They are designed to be reused also from other builtins,
   like the recently changed builtin-tag.c and the upcoming
   builtin-reset.c, and perhaps also from builtin-fetch--tool.c.

 builtin-update-ref.c |    8 ++------
 refs.c               |   32 ++++++++++++++++++++++++++++++++
 refs.h               |    8 ++++++++
 send-pack.c          |   12 ++++--------
 4 files changed, 46 insertions(+), 14 deletions(-)

diff --git a/builtin-update-ref.c b/builtin-update-ref.c
index 8339cf1..bd7fe4d 100644
--- a/builtin-update-ref.c
+++ b/builtin-update-ref.c
@@ -62,10 +62,6 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 	if (oldval && *oldval && get_sha1(oldval, oldsha1))
 		die("%s: not a valid old SHA1", oldval);

-	lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL, ref_flags);
-	if (!lock)
-		die("%s: cannot lock the ref", refname);
-	if (write_ref_sha1(lock, sha1, msg) < 0)
-		die("%s: cannot update the ref", refname);
-	return 0;
+	return update_ref_or_die(msg, refname, sha1,
+				oldval ? oldsha1 : NULL, ref_flags);
 }
diff --git a/refs.c b/refs.c
index 09a2c87..4fd5065 100644
--- a/refs.c
+++ b/refs.c
@@ -1455,3 +1455,35 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)
 {
 	return do_for_each_reflog("", fn, cb_data);
 }
+
+int update_ref_or_die(const char *action, const char *refname,
+				const unsigned char *sha1,
+				const unsigned char *oldval, int flags)
+{
+	static struct ref_lock *lock;
+	lock = lock_any_ref_for_update(refname, oldval, flags);
+	if (!lock)
+		die("Cannot lock the ref '%s'.", refname);
+	if (write_ref_sha1(lock, sha1, action) < 0)
+		die("Cannot update the ref '%s'.", refname);
+	return 0;
+}
+
+int update_ref_or_error(const char *action, const char *refname,
+				const unsigned char *sha1,
+				const unsigned char *oldval, int quiet)
+{
+	static struct ref_lock *lock;
+	lock = lock_any_ref_for_update(refname, oldval, 0);
+	if (!lock) {
+		if (!quiet)
+			error("Cannot lock the ref '%s'.", refname);
+		return 1;
+	}
+	if (write_ref_sha1(lock, sha1, action) < 0) {
+		if (!quiet)
+			error("Cannot update the ref '%s'.", refname);
+		return 1;
+	}
+	return 0;
+}
diff --git a/refs.h b/refs.h
index f234eb7..3d0100e 100644
--- a/refs.h
+++ b/refs.h
@@ -64,4 +64,12 @@ extern int rename_ref(const char *oldref, const char *newref, const char *logmsg
 /** resolve ref in nested "gitlink" repository */
 extern int resolve_gitlink_ref(const char *name, const char *refname, unsigned char *result);

+/** lock a ref and then write its file */
+int update_ref_or_die(const char *action, const char *refname,
+				const unsigned char *sha1,
+				const unsigned char *oldval, int flags);
+int update_ref_or_error(const char *action, const char *refname,
+				const unsigned char *sha1,
+				const unsigned char *oldval, int quiet);
+
 #endif /* REFS_H */
diff --git a/send-pack.c b/send-pack.c
index 9fc8a81..1907684 100644
--- a/send-pack.c
+++ b/send-pack.c
@@ -313,14 +313,10 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
 					if (delete_ref(rs.dst, NULL)) {
 						error("Failed to delete");
 					}
-				} else {
-					lock = lock_any_ref_for_update(rs.dst, NULL, 0);
-					if (!lock)
-						error("Failed to lock");
-					else
-						write_ref_sha1(lock, ref->new_sha1,
-							       "update by push");
-				}
+				} else
+					update_ref_or_error("update by push",
+							rs.dst, ref->new_sha1,
+							NULL, 0);
 				free(rs.dst);
 			}
 		}
-- 
1.5.0

```

## Johannes Schindelin, 2007-09-04 13:45

Subject: Re: [PATCH] Functions for updating refs.
Message-ID: <Pine.LNX.4.64.0709041444070.28586@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0709041444070.28586%40racer.site
In-Reply-To: <46DD6020.4050401@gmail.com>

```
Hi,

On Tue, 4 Sep 2007, Carlos Rica wrote:

> Signed-off-by: Carlos Rica <jasampler@gmail.com>
> ---
> 
>    They are designed to be reused also from other builtins,
>    like the recently changed builtin-tag.c and the upcoming
>    builtin-reset.c, and perhaps also from builtin-fetch--tool.c.

This should go into the commit message.

> +int update_ref_or_die(const char *action, const char *refname,
> +				const unsigned char *sha1,
> +				const unsigned char *oldval, int flags)

Should this not be "void"?  And should it not use update_ref_or_error()?

Otherwise I like it.

Ciao,
Dscho

```

## Johannes Sixt, 2007-09-04 14:28

Subject: Re: [PATCH] Functions for updating refs.
Message-ID: <46DD6B93.10005@eudaptics.com>
URL: https://gitlist.dev/e/46DD6B93.10005%40eudaptics.com
In-Reply-To: <Pine.LNX.4.64.0709041444070.28586@racer.site>

```
Johannes Schindelin schrieb:
> On Tue, 4 Sep 2007, Carlos Rica wrote:
>> +int update_ref_or_die(const char *action, const char *refname,
>> +				const unsigned char *sha1,
>> +				const unsigned char *oldval, int flags)
> 
> Should this not be "void"?  And should it not use update_ref_or_error()?

It should not use *_error() directly because then it would print two error 
messages in a row.

-- Hannes

```

## Johannes Schindelin, 2007-09-04 14:58

Subject: Re: [PATCH] Functions for updating refs.
Message-ID: <Pine.LNX.4.64.0709041557590.28586@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0709041557590.28586%40racer.site
In-Reply-To: <46DD6B93.10005@eudaptics.com>

```
Hi,

On Tue, 4 Sep 2007, Johannes Sixt wrote:

> Johannes Schindelin schrieb:
> > On Tue, 4 Sep 2007, Carlos Rica wrote:
> > > +int update_ref_or_die(const char *action, const char *refname,
> > > +				const unsigned char *sha1,
> > > +				const unsigned char *oldval, int flags)
> > 
> > Should this not be "void"?  And should it not use update_ref_or_error()?
> 
> It should not use *_error() directly because then it would print two error
> messages in a row.

Well, my idea was to let _error() print the message, and just die().

Ciao,
Dscho

```

## Johannes Sixt, 2007-09-04 15:08

Subject: Re: [PATCH] Functions for updating refs.
Message-ID: <46DD7506.3090503@eudaptics.com>
URL: https://gitlist.dev/e/46DD7506.3090503%40eudaptics.com
In-Reply-To: <Pine.LNX.4.64.0709041557590.28586@racer.site>

```
Johannes Schindelin schrieb:
> Hi,
> 
> On Tue, 4 Sep 2007, Johannes Sixt wrote:
> 
>> Johannes Schindelin schrieb:
>>> On Tue, 4 Sep 2007, Carlos Rica wrote:
>>>> +int update_ref_or_die(const char *action, const char *refname,
>>>> +				const unsigned char *sha1,
>>>> +				const unsigned char *oldval, int flags)
>>> Should this not be "void"?  And should it not use update_ref_or_error()?
>> It should not use *_error() directly because then it would print two error
>> messages in a row.
> 
> Well, my idea was to let _error() print the message, and just die().

How do you avoid that die() prints an error, too?

-- Hannes

```

## Karl Hasselström, 2007-09-04 15:29

Subject: Re: [PATCH] Functions for updating refs.
Message-ID: <20070904152901.GA31299@diana.vm.bytemark.co.uk>
URL: https://gitlist.dev/e/20070904152901.GA31299%40diana.vm.bytemark.co.uk
In-Reply-To: <46DD7506.3090503@eudaptics.com>

```
On 2007-09-04 17:08:54 +0200, Johannes Sixt wrote:

> Johannes Schindelin schrieb:
>
> > Well, my idea was to let _error() print the message, and just die().
>
> How do you avoid that die() prints an error, too?

Make a generic function that takes a function pointer argument, then
call it with die or error as appropriate.

-- 
Karl Hasselström, kha@treskal.com
      www.treskal.com/kalle

```

## Johannes Schindelin, 2007-09-04 15:33

Subject: Re: [PATCH] Functions for updating refs.
Message-ID: <Pine.LNX.4.64.0709041632410.28586@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0709041632410.28586%40racer.site
In-Reply-To: <46DD7506.3090503@eudaptics.com>

```
Hi,

On Tue, 4 Sep 2007, Johannes Sixt wrote:

> Johannes Schindelin schrieb:
> 
> > Well, my idea was to let _error() print the message, and just die().
> 
> How do you avoid that die() prints an error, too?

Good point.  Colour me convinced.

Ciao,
Dscho

```

## Junio C Hamano, 2007-09-04 17:52

Subject: Re: [PATCH] Functions for updating refs.
Message-ID: <7v642qnwr7.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v642qnwr7.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <46DD6020.4050401@gmail.com>

```
Carlos Rica <jasampler@gmail.com> writes:

> diff --git a/refs.c b/refs.c
> index 09a2c87..4fd5065 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -1455,3 +1455,35 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)
>  {
>  	return do_for_each_reflog("", fn, cb_data);
>  }
> +
> +int update_ref_or_die(const char *action, const char *refname,
> +				const unsigned char *sha1,
> +				const unsigned char *oldval, int flags)
> +{
> +	static struct ref_lock *lock;
> +	lock = lock_any_ref_for_update(refname, oldval, flags);
> +	if (!lock)
> +		die("Cannot lock the ref '%s'.", refname);
> +	if (write_ref_sha1(lock, sha1, action) < 0)
> +		die("Cannot update the ref '%s'.", refname);
> +	return 0;
> +}
> +
> +int update_ref_or_error(const char *action, const char *refname,
> +				const unsigned char *sha1,
> +				const unsigned char *oldval, int quiet)
> +{
> +	static struct ref_lock *lock;
> +	lock = lock_any_ref_for_update(refname, oldval, 0);
> +	if (!lock) {
> +		if (!quiet)
> +			error("Cannot lock the ref '%s'.", refname);
> +		return 1;
> +	}
> +	if (write_ref_sha1(lock, sha1, action) < 0) {
> +		if (!quiet)
> +			error("Cannot update the ref '%s'.", refname);
> +		return 1;
> +	}
> +	return 0;
> +}

This makes me wonder three things:

 - Why doesn't "or_error" side allow "flags" as "or_die" one?
   Could the 'quiet' option become part of "flags" perhaps?

 - They look quite similar.  Is it a good idea to refactor them
   further, or they are so small it does not matter?

 - Why isn't lock released with unlock_ref()?

```

## Carlos Rica, 2007-09-04 23:32

Subject: Re: [PATCH] Functions for updating refs.
Message-ID: <1b46aba20709041632x60ee4d3eweae9f5217d2f3b86@mail.gmail.com>
URL: https://gitlist.dev/e/1b46aba20709041632x60ee4d3eweae9f5217d2f3b86%40mail.gmail.com
In-Reply-To: <7v642qnwr7.fsf@gitster.siamese.dyndns.org>

```
2007/9/4, Junio C Hamano <gitster@pobox.com>:
> Carlos Rica <jasampler@gmail.com> writes:
>
> > diff --git a/refs.c b/refs.c
> > index 09a2c87..4fd5065 100644
> > --- a/refs.c
> > +++ b/refs.c
> > @@ -1455,3 +1455,35 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)
> >  {
> >       return do_for_each_reflog("", fn, cb_data);
> >  }
> > +
> > +int update_ref_or_die(const char *action, const char *refname,
> > +                             const unsigned char *sha1,
> > +                             const unsigned char *oldval, int flags)
> > +{
> > +     static struct ref_lock *lock;
> > +     lock = lock_any_ref_for_update(refname, oldval, flags);
> > +     if (!lock)
> > +             die("Cannot lock the ref '%s'.", refname);
> > +     if (write_ref_sha1(lock, sha1, action) < 0)
> > +             die("Cannot update the ref '%s'.", refname);
> > +     return 0;
> > +}
> > +
> > +int update_ref_or_error(const char *action, const char *refname,
> > +                             const unsigned char *sha1,
> > +                             const unsigned char *oldval, int quiet)
> > +{
> > +     static struct ref_lock *lock;
> > +     lock = lock_any_ref_for_update(refname, oldval, 0);
> > +     if (!lock) {
> > +             if (!quiet)
> > +                     error("Cannot lock the ref '%s'.", refname);
> > +             return 1;
> > +     }
> > +     if (write_ref_sha1(lock, sha1, action) < 0) {
> > +             if (!quiet)
> > +                     error("Cannot update the ref '%s'.", refname);
> > +             return 1;
> > +     }
> > +     return 0;
> > +}
>
> This makes me wonder three things:
>
>  - Why doesn't "or_error" side allow "flags" as "or_die" one?
>    Could the 'quiet' option become part of "flags" perhaps?

I saw that the only code that needed the flags was the
builtin-update-ref.c, and it also needed to die(). The
others I saw only want that parameter set to 0.
builtin-tag.c was doing die() also, not using flags, though.

>  - They look quite similar.  Is it a good idea to refactor them
>    further, or they are so small it does not matter?

I would like to know how to refactor it, however the code I saw
sometimes needs to call die(), others to error() and others
need to get only a value of success or not.

The function die() returns 128 and terminates the program,
prepending "fatal: " in the message, while error() doesn't exit
and prepend "error: ", so they were very different and I
resolved to separate them.

Another option is returning different error codes, so
the caller could decide what to output in each case, but
I thought that these functions were only useful to unify those
instructions with those error messages for this common
operation that many builtins use, specially when they come
from scripts who call to "git update-ref".

>
>  - Why isn't lock released with unlock_ref()?

I inspected this some weeks ago, and I finally came to think
that it is released in the write_ref_sha1 call after the lock.
But the code was complex, can someone confirm this?

```

## Junio C Hamano, 2007-09-05 00:16

Subject: Re: [PATCH] Functions for updating refs.
Message-ID: <7vd4wym0ec.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vd4wym0ec.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <1b46aba20709041632x60ee4d3eweae9f5217d2f3b86@mail.gmail.com>

```
"Carlos Rica" <jasampler@gmail.com> writes:
>>  - Why doesn't "or_error" side allow "flags" as "or_die" one?
>>    Could the 'quiet' option become part of "flags" perhaps?
>
> I saw that the only code that needed the flags was the
> builtin-update-ref.c, and it also needed to die(). The
> others I saw only want that parameter set to 0.
> builtin-tag.c was doing die() also, not using flags, though.

Ok, when other built-ins start using these functions, they might
want to pass different flags, but it is easy enough for us to
extend the interface later.

>>  - They look quite similar.  Is it a good idea to refactor them
>>    further, or they are so small it does not matter?
>
> The function die() returns 128 and terminates the program,
> prepending "fatal: " in the message, while error() doesn't exit
> and prepend "error: ", so they were very different and I
> resolved to separate them.

Fair enough.

>>  - Why isn't lock released with unlock_ref()?
>
> I inspected this some weeks ago, and I finally came to think
> that it is released in the write_ref_sha1 call after the lock.

Ah, that's right!

```
