Skip to content

Commit 47f534b

Browse files
mhaggergitster
authored andcommitted
resolve_ref_unsafe(): extract function handle_missing_loose_ref()
The nesting was getting a bit out of hand, and it's about to get worse. Signed-off-by: Michael Haggerty <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4d1c565 commit 47f534b

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

refs.c

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,37 @@ static struct ref_entry *get_packed_ref(const char *refname)
11971197
return find_ref(get_packed_refs(&ref_cache), refname);
11981198
}
11991199

1200+
/*
1201+
* A loose ref file doesn't exist; check for a packed ref. The
1202+
* options are forwarded from resolve_safe_unsafe().
1203+
*/
1204+
static const char *handle_missing_loose_ref(const char *refname,
1205+
unsigned char *sha1,
1206+
int reading,
1207+
int *flag)
1208+
{
1209+
struct ref_entry *entry;
1210+
1211+
/*
1212+
* The loose reference file does not exist; check for a packed
1213+
* reference.
1214+
*/
1215+
entry = get_packed_ref(refname);
1216+
if (entry) {
1217+
hashcpy(sha1, entry->u.value.sha1);
1218+
if (flag)
1219+
*flag |= REF_ISPACKED;
1220+
return refname;
1221+
}
1222+
/* The reference is not a packed reference, either. */
1223+
if (reading) {
1224+
return NULL;
1225+
} else {
1226+
hashclr(sha1);
1227+
return refname;
1228+
}
1229+
}
1230+
12001231
const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reading, int *flag)
12011232
{
12021233
int depth = MAXDEPTH;
@@ -1222,28 +1253,11 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
12221253
git_snpath(path, sizeof(path), "%s", refname);
12231254

12241255
if (lstat(path, &st) < 0) {
1225-
struct ref_entry *entry;
1226-
1227-
if (errno != ENOENT)
1256+
if (errno == ENOENT)
1257+
return handle_missing_loose_ref(refname, sha1,
1258+
reading, flag);
1259+
else
12281260
return NULL;
1229-
/*
1230-
* The loose reference file does not exist;
1231-
* check for a packed reference.
1232-
*/
1233-
entry = get_packed_ref(refname);
1234-
if (entry) {
1235-
hashcpy(sha1, entry->u.value.sha1);
1236-
if (flag)
1237-
*flag |= REF_ISPACKED;
1238-
return refname;
1239-
}
1240-
/* The reference is not a packed reference, either. */
1241-
if (reading) {
1242-
return NULL;
1243-
} else {
1244-
hashclr(sha1);
1245-
return refname;
1246-
}
12471261
}
12481262

12491263
/* Follow "normalized" - ie "refs/.." symlinks by hand */

0 commit comments

Comments
 (0)