search_ref_dir(): return an index rather than a pointer

Change search_ref_dir() to return the index of the sought entry (or -1 on error) rather than a pointer to the entry. This will make it more natural to use the function for removing an entry from the list. 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 9fc0a64806aa72ca7ef1c20468ffcef5c86978b8
1 file changed +18 -12
refs.c
+18 -12
@@ -367,18 +367,17 @@ static int ref_entry_cmp_sslice(const void *key_, const void *ent_)
367 }
368
369 /*
370 - * Return the entry with the given refname from the ref_dir
371 - * (non-recursively), sorting dir if necessary. Return NULL if no
372 - * such entry is found. dir must already be complete.
370 + * Return the index of the entry with the given refname from the
371 + * ref_dir (non-recursively), sorting dir if necessary. Return -1 if
372 + * no such entry is found. dir must already be complete.
373 */
374 -static struct ref_entry *search_ref_dir(struct ref_dir *dir,
375 - const char *refname, size_t len)
374 +static int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len)
375 {
376 struct ref_entry **r;
377 struct string_slice key;
378
379 if (refname == NULL || !dir->nr)
381 - return NULL;
380 + return -1;
381
382 sort_ref_dir(dir);
383 key.len = len;
@@ -387,9 +386,9 @@ static struct ref_entry *search_ref_dir(struct ref_dir *dir,
386 ref_entry_cmp_sslice);
387
388 if (r == NULL)
390 - return NULL;
389 + return -1;
390
392 - return *r;
391 + return r - dir->entries;
392 }
393
394 /*
@@ -403,8 +402,9 @@ static struct ref_dir *search_for_subdir(struct ref_dir *dir,
402 const char *subdirname, size_t len,
403 int mkdir)
404 {
406 - struct ref_entry *entry = search_ref_dir(dir, subdirname, len);
407 - if (!entry) {
405 + int entry_index = search_ref_dir(dir, subdirname, len);
406 + struct ref_entry *entry;
407 + if (entry_index == -1) {
408 if (!mkdir)
409 return NULL;
410 /*
@@ -415,6 +415,8 @@ static struct ref_dir *search_for_subdir(struct ref_dir *dir,
415 */
416 entry = create_dir_entry(dir->ref_cache, subdirname, len, 0);
417 add_entry_to_dir(dir, entry);
418 + } else {
419 + entry = dir->entries[entry_index];
420 }
421 return get_ref_dir(entry);
422 }
@@ -453,12 +455,16 @@ static struct ref_dir *find_containing_dir(struct ref_dir *dir,
455 */
456 static struct ref_entry *find_ref(struct ref_dir *dir, const char *refname)
457 {
458 + int entry_index;
459 struct ref_entry *entry;
460 dir = find_containing_dir(dir, refname, 0);
461 if (!dir)
462 return NULL;
460 - entry = search_ref_dir(dir, refname, strlen(refname));
461 - return (entry && !(entry->flag & REF_DIR)) ? entry : NULL;
463 + entry_index = search_ref_dir(dir, refname, strlen(refname));
464 + if (entry_index == -1)
465 + return NULL;
466 + entry = dir->entries[entry_index];
467 + return (entry->flag & REF_DIR) ? NULL : entry;
468 }
469
470 /*