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 <mhagger@alum.mit.edu> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Jun 19, 2013 at 08:36 UTC 47f534bf92300c2e48c39999bc89e941ebc5d0c8
1 file changed +35 -21
refs.c
+35 -21
@@ -1197,6 +1197,37 @@ static struct ref_entry *get_packed_ref(const char *refname)
1197 return find_ref(get_packed_refs(&ref_cache), refname);
1198 }
1199
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 +
1231 const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reading, int *flag)
1232 {
1233 int depth = MAXDEPTH;
@@ -1222,28 +1253,11 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
1253 git_snpath(path, sizeof(path), "%s", refname);
1254
1255 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
1260 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 - }
1261 }
1262
1263 /* Follow "normalized" - ie "refs/.." symlinks by hand */