refs: extract function peel_object()
It is a nice, logical unit of work, and putting it in a function removes the need to use a goto in peel_ref(). Soon it will also have other uses. The algorithm is unchanged. 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
cb2ae1c418eae2cf2c632ca78a14861ff5f3da68
1 file changed
+30
-20
refs.c
+30
-20
@@ -1273,11 +1273,38 @@ static int filter_refs(const char *refname, const unsigned char *sha1, int flags
1273
return filter->fn(refname, sha1, flags, filter->cb_data);
1274
}
1275
1276
+/*
1277
+ * Peel the named object; i.e., if the object is a tag, resolve the
1278
+ * tag recursively until a non-tag is found. Store the result to sha1
1279
+ * and return 0 iff successful. If the object is not a tag or is not
1280
+ * valid, return -1 and leave sha1 unchanged.
1281
+ */
1282
+static int peel_object(const unsigned char *name, unsigned char *sha1)
1283
+{
1284
+ struct object *o = lookup_unknown_object(name);
1285
+
1286
+ if (o->type == OBJ_NONE) {
1287
+ int type = sha1_object_info(name, NULL);
1288
+ if (type < 0)
1289
+ return -1;
1290
+ o->type = type;
1291
+ }
1292
+
1293
+ if (o->type != OBJ_TAG)
1294
+ return -1;
1295
+
1296
+ o = deref_tag_noverify(o);
1297
+ if (!o)
1298
+ return -1;
1299
+
1300
+ hashcpy(sha1, o->sha1);
1301
+ return 0;
1302
+}
1303
+
1304
int peel_ref(const char *refname, unsigned char *sha1)
1305
{
1306
int flag;
1307
unsigned char base[20];
1280
- struct object *o;
1308
1309
if (current_ref && (current_ref->name == refname
1310
|| !strcmp(current_ref->name, refname))) {
@@ -1287,8 +1314,7 @@ int peel_ref(const char *refname, unsigned char *sha1)
1314
hashcpy(sha1, current_ref->u.value.peeled);
1315
return 0;
1316
}
1290
- hashcpy(base, current_ref->u.value.sha1);
1291
- goto fallback;
1317
+ return peel_object(current_ref->u.value.sha1, sha1);
1318
}
1319
1320
if (read_ref_full(refname, base, 1, &flag))
@@ -1303,23 +1329,7 @@ int peel_ref(const char *refname, unsigned char *sha1)
1329
}
1330
}
1331
1306
-fallback:
1307
- o = lookup_unknown_object(base);
1308
- if (o->type == OBJ_NONE) {
1309
- int type = sha1_object_info(base, NULL);
1310
- if (type < 0)
1311
- return -1;
1312
- o->type = type;
1313
- }
1314
-
1315
- if (o->type == OBJ_TAG) {
1316
- o = deref_tag_noverify(o);
1317
- if (o) {
1318
- hashcpy(sha1, o->sha1);
1319
- return 0;
1320
- }
1321
- }
1322
- return -1;
1332
+ return peel_object(base, sha1);
1333
}
1334
1335
struct warn_if_dangling_data {