peel_ref(): fix return value for non-peelable, not-current reference

The old version was inconsistent: when a reference was REF_KNOWS_PEELED but with a null peeled value, it returned non-zero for the current reference but zero for other references. Change the behavior for non-current references to match that of current_ref, which is what callers expect. Document the behavior. Current callers only call peel_ref() from within a for_each_ref-style iteration and only for the current ref; therefore, the buggy code path was never reached. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Apr 22, 2013 at 21:52 UTC 2312a7932080f17c2847ec3ce5dddbc65c2e0b41
2 files changed +12 -1
refs.c
+4 -1
@@ -119,7 +119,8 @@ struct ref_value {
119 /*
120 * If REF_KNOWS_PEELED, then this field holds the peeled value
121 * of this reference, or null if the reference is known not to
122 - * be peelable.
122 + * be peelable. See the documentation for peel_ref() for an
123 + * exact definition of "peelable".
124 */
125 unsigned char peeled[20];
126 };
@@ -1340,6 +1341,8 @@ int peel_ref(const char *refname, unsigned char *sha1)
1341 struct ref_entry *r = get_packed_ref(refname);
1342
1343 if (r && (r->flag & REF_KNOWS_PEELED)) {
1344 + if (is_null_sha1(r->u.value.peeled))
1345 + return -1;
1346 hashcpy(sha1, r->u.value.peeled);
1347 return 0;
1348 }
refs.h
+8
@@ -74,6 +74,14 @@ extern void add_packed_ref(const char *refname, const unsigned char *sha1);
74
75 extern int ref_exists(const char *);
76
77 +/*
78 + * If refname is a non-symbolic reference that refers to a tag object,
79 + * and the tag can be (recursively) dereferenced to a non-tag object,
80 + * store the SHA1 of the referred-to object to sha1 and return 0. If
81 + * any of these conditions are not met, return a non-zero value.
82 + * Symbolic references are considered unpeelable, even if they
83 + * ultimately resolve to a peelable tag.
84 + */
85 extern int peel_ref(const char *refname, unsigned char *sha1);
86
87 /** Locks a "refs/" ref returning the lock on success and NULL on failure. **/