Skip to content

Commit 9fc0a64

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ab292bc commit 9fc0a64

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

refs.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -367,18 +367,17 @@ static int ref_entry_cmp_sslice(const void *key_, const void *ent_)
367367
}
368368

369369
/*
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.
373373
*/
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)
376375
{
377376
struct ref_entry **r;
378377
struct string_slice key;
379378

380379
if (refname == NULL || !dir->nr)
381-
return NULL;
380+
return -1;
382381

383382
sort_ref_dir(dir);
384383
key.len = len;
@@ -387,9 +386,9 @@ static struct ref_entry *search_ref_dir(struct ref_dir *dir,
387386
ref_entry_cmp_sslice);
388387

389388
if (r == NULL)
390-
return NULL;
389+
return -1;
391390

392-
return *r;
391+
return r - dir->entries;
393392
}
394393

395394
/*
@@ -403,8 +402,9 @@ static struct ref_dir *search_for_subdir(struct ref_dir *dir,
403402
const char *subdirname, size_t len,
404403
int mkdir)
405404
{
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) {
408408
if (!mkdir)
409409
return NULL;
410410
/*
@@ -415,6 +415,8 @@ static struct ref_dir *search_for_subdir(struct ref_dir *dir,
415415
*/
416416
entry = create_dir_entry(dir->ref_cache, subdirname, len, 0);
417417
add_entry_to_dir(dir, entry);
418+
} else {
419+
entry = dir->entries[entry_index];
418420
}
419421
return get_ref_dir(entry);
420422
}
@@ -453,12 +455,16 @@ static struct ref_dir *find_containing_dir(struct ref_dir *dir,
453455
*/
454456
static struct ref_entry *find_ref(struct ref_dir *dir, const char *refname)
455457
{
458+
int entry_index;
456459
struct ref_entry *entry;
457460
dir = find_containing_dir(dir, refname, 0);
458461
if (!dir)
459462
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;
462468
}
463469

464470
/*

0 commit comments

Comments
 (0)