@matheusbsbs / matheus-bagliano / commits / b66fde9a28
Fork 0

find_unique_abbrev(): redefine semantics

The function returned NULL when no object that matches the name was found, but that made the callers more complicated, as nobody used that NULL return as an indication that no object with such a name exists. They (at least the careful ones) instead took the full 40-hexdigit and used in such a case, and the careless ones segfaulted. With this "git rev-parse --short 5555555555555555555555555555555555555555" would stop segfaulting. This is based on Jeff King's rewrite to my RFC patch, but "missing" logic swapped to "exists". The final logic reads: For existing objects, make sure the abbreviated string uniquely identifies it. Otherwise, make sure the abbreviated string is long enough so that it would not name any existing object. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Mar 1, 2008 at 23:35 UTC b66fde9a287f64ed81d4d4a1639997ad5eedb432
1 file changed +6 -7
sha1_name.c
+6 -7
@@ -192,26 +192,25 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
192
193 const char *find_unique_abbrev(const unsigned char *sha1, int len)
194 {
195 - int status, is_null;
195 + int status, exists;
196 static char hex[41];
197
198 - is_null = is_null_sha1(sha1);
198 + exists = has_sha1_file(sha1);
199 memcpy(hex, sha1_to_hex(sha1), 40);
200 if (len == 40 || !len)
201 return hex;
202 while (len < 40) {
203 unsigned char sha1_ret[20];
204 status = get_short_sha1(hex, len, sha1_ret, 1);
205 - if (!status ||
206 - (is_null && status != SHORT_NAME_AMBIGUOUS)) {
205 + if (exists
206 + ? !status
207 + : status == SHORT_NAME_NOT_FOUND) {
208 hex[len] = 0;
209 return hex;
210 }
210 - if (status != SHORT_NAME_AMBIGUOUS)
211 - return NULL;
211 len++;
212 }
214 - return NULL;
213 + return hex;
214 }
215
216 static int ambiguous_path(const char *path, int len)