refs: reorder definitions

Move resolve_gitlink_ref() and related functions lower in the file to avoid the need for forward declarations in the next step. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 4, 2016 at 18:08 UTC 6356c658e424dbd45b278f164b0c36a2062a1bcb
1 file changed +83 -83
refs/files-backend.c
+83 -83
@@ -1316,89 +1316,6 @@ static struct ref_dir *get_loose_refs(struct files_ref_store *refs)
1316 return get_ref_dir(refs->loose);
1317 }
1318
1319 -#define MAXREFLEN (1024)
1320 -
1321 -/*
1322 - * Called by resolve_gitlink_ref_recursive() after it failed to read
1323 - * from the loose refs in refs. Find <refname> in the packed-refs file
1324 - * for the submodule.
1325 - */
1326 -static int resolve_gitlink_packed_ref(struct files_ref_store *refs,
1327 - const char *refname, unsigned char *sha1)
1328 -{
1329 - struct ref_entry *ref;
1330 - struct ref_dir *dir = get_packed_refs(refs);
1331 -
1332 - ref = find_ref(dir, refname);
1333 - if (ref == NULL)
1334 - return -1;
1335 -
1336 - hashcpy(sha1, ref->u.value.oid.hash);
1337 - return 0;
1338 -}
1339 -
1340 -static int resolve_gitlink_ref_recursive(struct files_ref_store *refs,
1341 - const char *refname, unsigned char *sha1,
1342 - int recursion)
1343 -{
1344 - int fd, len;
1345 - char buffer[128], *p;
1346 - char *path;
1347 -
1348 - if (recursion > SYMREF_MAXDEPTH || strlen(refname) > MAXREFLEN)
1349 - return -1;
1350 - path = *refs->base.submodule
1351 - ? git_pathdup_submodule(refs->base.submodule, "%s", refname)
1352 - : git_pathdup("%s", refname);
1353 - fd = open(path, O_RDONLY);
1354 - free(path);
1355 - if (fd < 0)
1356 - return resolve_gitlink_packed_ref(refs, refname, sha1);
1357 -
1358 - len = read(fd, buffer, sizeof(buffer)-1);
1359 - close(fd);
1360 - if (len < 0)
1361 - return -1;
1362 - while (len && isspace(buffer[len-1]))
1363 - len--;
1364 - buffer[len] = 0;
1365 -
1366 - /* Was it a detached head or an old-fashioned symlink? */
1367 - if (!get_sha1_hex(buffer, sha1))
1368 - return 0;
1369 -
1370 - /* Symref? */
1371 - if (strncmp(buffer, "ref:", 4))
1372 - return -1;
1373 - p = buffer + 4;
1374 - while (isspace(*p))
1375 - p++;
1376 -
1377 - return resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1);
1378 -}
1379 -
1380 -int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)
1381 -{
1382 - int len = strlen(path);
1383 - struct strbuf submodule = STRBUF_INIT;
1384 - struct files_ref_store *refs;
1385 -
1386 - while (len && path[len-1] == '/')
1387 - len--;
1388 - if (!len)
1389 - return -1;
1390 -
1391 - strbuf_add(&submodule, path, len);
1392 - refs = get_files_ref_store(submodule.buf, "resolve_gitlink_ref");
1393 - if (!refs) {
1394 - strbuf_release(&submodule);
1395 - return -1;
1396 - }
1397 - strbuf_release(&submodule);
1398 -
1399 - return resolve_gitlink_ref_recursive(refs, refname, sha1, 0);
1400 -}
1401 -
1319 /*
1320 * Return the ref_entry for the given refname from the packed
1321 * references. If it does not exist, return NULL.
@@ -1572,6 +1489,89 @@ static void unlock_ref(struct ref_lock *lock)
1489 free(lock);
1490 }
1491
1492 +#define MAXREFLEN (1024)
1493 +
1494 +/*
1495 + * Called by resolve_gitlink_ref_recursive() after it failed to read
1496 + * from the loose refs in refs. Find <refname> in the packed-refs file
1497 + * for the submodule.
1498 + */
1499 +static int resolve_gitlink_packed_ref(struct files_ref_store *refs,
1500 + const char *refname, unsigned char *sha1)
1501 +{
1502 + struct ref_entry *ref;
1503 + struct ref_dir *dir = get_packed_refs(refs);
1504 +
1505 + ref = find_ref(dir, refname);
1506 + if (ref == NULL)
1507 + return -1;
1508 +
1509 + hashcpy(sha1, ref->u.value.oid.hash);
1510 + return 0;
1511 +}
1512 +
1513 +static int resolve_gitlink_ref_recursive(struct files_ref_store *refs,
1514 + const char *refname, unsigned char *sha1,
1515 + int recursion)
1516 +{
1517 + int fd, len;
1518 + char buffer[128], *p;
1519 + char *path;
1520 +
1521 + if (recursion > SYMREF_MAXDEPTH || strlen(refname) > MAXREFLEN)
1522 + return -1;
1523 + path = *refs->base.submodule
1524 + ? git_pathdup_submodule(refs->base.submodule, "%s", refname)
1525 + : git_pathdup("%s", refname);
1526 + fd = open(path, O_RDONLY);
1527 + free(path);
1528 + if (fd < 0)
1529 + return resolve_gitlink_packed_ref(refs, refname, sha1);
1530 +
1531 + len = read(fd, buffer, sizeof(buffer)-1);
1532 + close(fd);
1533 + if (len < 0)
1534 + return -1;
1535 + while (len && isspace(buffer[len-1]))
1536 + len--;
1537 + buffer[len] = 0;
1538 +
1539 + /* Was it a detached head or an old-fashioned symlink? */
1540 + if (!get_sha1_hex(buffer, sha1))
1541 + return 0;
1542 +
1543 + /* Symref? */
1544 + if (strncmp(buffer, "ref:", 4))
1545 + return -1;
1546 + p = buffer + 4;
1547 + while (isspace(*p))
1548 + p++;
1549 +
1550 + return resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1);
1551 +}
1552 +
1553 +int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)
1554 +{
1555 + int len = strlen(path);
1556 + struct strbuf submodule = STRBUF_INIT;
1557 + struct files_ref_store *refs;
1558 +
1559 + while (len && path[len-1] == '/')
1560 + len--;
1561 + if (!len)
1562 + return -1;
1563 +
1564 + strbuf_add(&submodule, path, len);
1565 + refs = get_files_ref_store(submodule.buf, "resolve_gitlink_ref");
1566 + if (!refs) {
1567 + strbuf_release(&submodule);
1568 + return -1;
1569 + }
1570 + strbuf_release(&submodule);
1571 +
1572 + return resolve_gitlink_ref_recursive(refs, refname, sha1, 0);
1573 +}
1574 +
1575 /*
1576 * Lock refname, without following symrefs, and set *lock_p to point
1577 * at a newly-allocated lock object. Fill in lock->old_oid, referent,