Hi Rafael,

On 4 May 2018 at 18:28, Rafael Ascensão <rafa.al...@gmail.com> wrote:
> While trying to create a pseudo reference named REF pointing to the
> empty tree iff it doesn't exist, I stumbled on the following:
>
> I assume both are valid ways to create such reference:
>         a) $ echo -e option no-deref\\nupdate REF $(git hash-object -t
>         tree /dev/null) 0000000000000000000000000000000000000000 | git
>         update-ref --stdin
>         b) $ git update-ref --no-deref REF $(git hash-object -t tree
>         /dev/null) 0000000000000000000000000000000000000000
>
> While a) works, b) will throw:
>         fatal: could not read ref 'REF'


I can reproduce this and I agree with your understanding of what should
happen here. The patch below makes this work according to my and your
expectations, at least in my command-line testing.

The die("... already exists") could instead be a no-op, trusting that
the backend discovers the problem. "die" could also be strbuf_addf(...),
I'm just following 2c3aed138 here.

Anyway, that's not where I'm stuck... Regardless of how I try to write
tests (in t1400), they just pass beautifully even before this patch. I
might be able to look into that more on the weekend. If anyone has
ideas, I am all ears. Or if someone feels like picking this up and
running with it, feel free.

Martin

diff --git a/refs.c b/refs.c
index 8b7a77fe5e..cdb0a5ab29 100644
--- a/refs.c
+++ b/refs.c
@@ -666,9 +666,12 @@ static int write_pseudoref(const char *pseudoref, const 
struct object_id *oid,
        if (old_oid) {
                struct object_id actual_old_oid;
 
-               if (read_ref(pseudoref, &actual_old_oid))
-                       die("could not read ref '%s'", pseudoref);
-               if (oidcmp(&actual_old_oid, old_oid)) {
+               if (read_ref(pseudoref, &actual_old_oid)) {
+                       if (!is_null_oid(old_oid))
+                               die("could not read ref '%s'", pseudoref);
+               } else if (is_null_oid(old_oid)) {
+                       die("reference '%s' already exists", pseudoref);
+               } else if (oidcmp(&actual_old_oid, old_oid)) {
                        strbuf_addf(err, "unexpected sha1 when writing '%s'", 
pseudoref);
                        rollback_lock_file(&lock);
                        goto done;
-- 
2.17.0.392.g7fa371e468

Reply via email to