pack: move nth_packed_object_{sha1,oid}

Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Aug 18, 2017 at 15:20 UTC d5a16761820f2539bf8610c8f0c64f610e29314e
4 files changed +46 -46
cache.h
-14
@@ -1630,20 +1630,6 @@ extern int odb_pack_keep(const char *name);
1630 */
1631 extern void check_pack_index_ptr(const struct packed_git *p, const void *ptr);
1632
1633 -/*
1634 - * Return the SHA-1 of the nth object within the specified packfile.
1635 - * Open the index if it is not already open. The return value points
1636 - * at the SHA-1 within the mmapped index. Return NULL if there is an
1637 - * error.
1638 - */
1639 -extern const unsigned char *nth_packed_object_sha1(struct packed_git *, uint32_t n);
1640 -/*
1641 - * Like nth_packed_object_sha1, but write the data into the object specified by
1642 - * the the first argument. Returns the first argument on success, and NULL on
1643 - * error.
1644 - */
1645 -extern const struct object_id *nth_packed_object_oid(struct object_id *, struct packed_git *, uint32_t n);
1646 -
1633 /*
1634 * Return the offset of the nth object within the specified packfile.
1635 * The index must already be opened.
packfile.c
+31
@@ -1636,3 +1636,34 @@ out:
1636
1637 return data;
1638 }
1639 +
1640 +const unsigned char *nth_packed_object_sha1(struct packed_git *p,
1641 + uint32_t n)
1642 +{
1643 + const unsigned char *index = p->index_data;
1644 + if (!index) {
1645 + if (open_pack_index(p))
1646 + return NULL;
1647 + index = p->index_data;
1648 + }
1649 + if (n >= p->num_objects)
1650 + return NULL;
1651 + index += 4 * 256;
1652 + if (p->index_version == 1) {
1653 + return index + 24 * n + 4;
1654 + } else {
1655 + index += 8;
1656 + return index + 20 * n;
1657 + }
1658 +}
1659 +
1660 +const struct object_id *nth_packed_object_oid(struct object_id *oid,
1661 + struct packed_git *p,
1662 + uint32_t n)
1663 +{
1664 + const unsigned char *hash = nth_packed_object_sha1(p, n);
1665 + if (!hash)
1666 + return NULL;
1667 + hashcpy(oid->hash, hash);
1668 + return oid;
1669 +}
packfile.h
+15 -1
@@ -63,6 +63,21 @@ extern void unuse_pack(struct pack_window **);
63 extern void clear_delta_base_cache(void);
64 extern struct packed_git *add_packed_git(const char *path, size_t path_len, int local);
65
66 +/*
67 + * Return the SHA-1 of the nth object within the specified packfile.
68 + * Open the index if it is not already open. The return value points
69 + * at the SHA-1 within the mmapped index. Return NULL if there is an
70 + * error.
71 + */
72 +extern const unsigned char *nth_packed_object_sha1(struct packed_git *, uint32_t n);
73 +/*
74 + * Like nth_packed_object_sha1, but write the data into the object specified by
75 + * the the first argument. Returns the first argument on success, and NULL on
76 + * error.
77 + */
78 +extern const struct object_id *nth_packed_object_oid(struct object_id *, struct packed_git *, uint32_t n);
79 +
80 +
81 extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);
82 extern unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
83 extern unsigned long get_size_from_delta(struct packed_git *, struct pack_window **, off_t);
@@ -79,5 +94,4 @@ extern int packed_object_info(struct packed_git *pack, off_t offset, struct obje
94
95 extern void mark_bad_packed_object(struct packed_git *p, const unsigned char *sha1);
96 extern const struct packed_git *has_packed_and_bad(const unsigned char *sha1);
82 -
97 #endif
sha1_file.c
-31
@@ -1075,37 +1075,6 @@ int parse_sha1_header(const char *hdr, unsigned long *sizep)
1075 return parse_sha1_header_extended(hdr, &oi, 0);
1076 }
1077
1078 -const unsigned char *nth_packed_object_sha1(struct packed_git *p,
1079 - uint32_t n)
1080 -{
1081 - const unsigned char *index = p->index_data;
1082 - if (!index) {
1083 - if (open_pack_index(p))
1084 - return NULL;
1085 - index = p->index_data;
1086 - }
1087 - if (n >= p->num_objects)
1088 - return NULL;
1089 - index += 4 * 256;
1090 - if (p->index_version == 1) {
1091 - return index + 24 * n + 4;
1092 - } else {
1093 - index += 8;
1094 - return index + 20 * n;
1095 - }
1096 -}
1097 -
1098 -const struct object_id *nth_packed_object_oid(struct object_id *oid,
1099 - struct packed_git *p,
1100 - uint32_t n)
1101 -{
1102 - const unsigned char *hash = nth_packed_object_sha1(p, n);
1103 - if (!hash)
1104 - return NULL;
1105 - hashcpy(oid->hash, hash);
1106 - return oid;
1107 -}
1108 -
1078 void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
1079 {
1080 const unsigned char *ptr = vptr;