cmd_update_ref(): make logic more straightforward
authorMichael Haggerty <mhagger@alum.mit.edu>
Mon, 22 Jun 2015 14:03:09 +0000 (16:03 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 22 Jun 2015 20:17:14 +0000 (13:17 -0700)
Restructure the code to avoid clearing oldsha1 when oldval is unset.
It's value is not needed in that case, so this change makes it more
obvious that its initialization is consistent with its later use.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/update-ref.c
index 3d79a46b036fa7bea29bbc0941442b5dd92f648c..160c7ac1d3c63d6acfea5794958e27eac9ab159e 100644 (file)
@@ -408,9 +408,16 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
                        die("%s: not a valid SHA1", value);
        }
 
-       hashclr(oldsha1); /* all-zero hash in case oldval is the empty string */
-       if (oldval && *oldval && get_sha1(oldval, oldsha1))
-               die("%s: not a valid old SHA1", oldval);
+       if (oldval) {
+               if (!*oldval)
+                       /*
+                        * The empty string implies that the reference
+                        * must not already exist:
+                        */
+                       hashclr(oldsha1);
+               else if (get_sha1(oldval, oldsha1))
+                       die("%s: not a valid old SHA1", oldval);
+       }
 
        if (no_deref)
                flags = REF_NODEREF;