Skip to content

Commit 7709f46

Browse files
jonathantanmygitster
authored andcommitted
pack: move for_each_packed_object()
Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f9a8672 commit 7709f46

File tree

6 files changed

+54
-46
lines changed

6 files changed

+54
-46
lines changed

builtin/cat-file.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "streaming.h"
1313
#include "tree-walk.h"
1414
#include "sha1-array.h"
15+
#include "packfile.h"
1516

1617
struct batch_options {
1718
int enabled;

cache.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,17 +1662,12 @@ int for_each_loose_file_in_objdir_buf(struct strbuf *path,
16621662
void *data);
16631663

16641664
/*
1665-
* Iterate over loose and packed objects in both the local
1665+
* Iterate over loose objects in both the local
16661666
* repository and any alternates repositories (unless the
16671667
* LOCAL_ONLY flag is set).
16681668
*/
16691669
#define FOR_EACH_OBJECT_LOCAL_ONLY 0x1
1670-
typedef int each_packed_object_fn(const struct object_id *oid,
1671-
struct packed_git *pack,
1672-
uint32_t pos,
1673-
void *data);
16741670
extern int for_each_loose_object(each_loose_object_fn, void *, unsigned flags);
1675-
extern int for_each_packed_object(each_packed_object_fn, void *, unsigned flags);
16761671

16771672
struct object_info {
16781673
/* Request */

packfile.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,3 +1854,43 @@ int has_pack_index(const unsigned char *sha1)
18541854
return 0;
18551855
return 1;
18561856
}
1857+
1858+
static int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn cb, void *data)
1859+
{
1860+
uint32_t i;
1861+
int r = 0;
1862+
1863+
for (i = 0; i < p->num_objects; i++) {
1864+
struct object_id oid;
1865+
1866+
if (!nth_packed_object_oid(&oid, p, i))
1867+
return error("unable to get sha1 of object %u in %s",
1868+
i, p->pack_name);
1869+
1870+
r = cb(&oid, p, i, data);
1871+
if (r)
1872+
break;
1873+
}
1874+
return r;
1875+
}
1876+
1877+
int for_each_packed_object(each_packed_object_fn cb, void *data, unsigned flags)
1878+
{
1879+
struct packed_git *p;
1880+
int r = 0;
1881+
int pack_errors = 0;
1882+
1883+
prepare_packed_git();
1884+
for (p = packed_git; p; p = p->next) {
1885+
if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
1886+
continue;
1887+
if (open_pack_index(p)) {
1888+
pack_errors = 1;
1889+
continue;
1890+
}
1891+
r = for_each_object_in_pack(p, cb, data);
1892+
if (r)
1893+
break;
1894+
}
1895+
return r ? r : pack_errors;
1896+
}

packfile.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,15 @@ extern int has_sha1_pack(const unsigned char *sha1);
124124

125125
extern int has_pack_index(const unsigned char *sha1);
126126

127+
/*
128+
* Iterate over packed objects in both the local
129+
* repository and any alternates repositories (unless the
130+
* FOR_EACH_OBJECT_LOCAL_ONLY flag, defined in cache.h, is set).
131+
*/
132+
typedef int each_packed_object_fn(const struct object_id *oid,
133+
struct packed_git *pack,
134+
uint32_t pos,
135+
void *data);
136+
extern int for_each_packed_object(each_packed_object_fn, void *, unsigned flags);
137+
127138
#endif

reachable.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "cache-tree.h"
1010
#include "progress.h"
1111
#include "list-objects.h"
12+
#include "packfile.h"
1213

1314
struct connectivity_progress {
1415
struct progress *progress;

sha1_file.c

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,46 +2015,6 @@ int for_each_loose_object(each_loose_object_fn cb, void *data, unsigned flags)
20152015
return foreach_alt_odb(loose_from_alt_odb, &alt);
20162016
}
20172017

2018-
static int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn cb, void *data)
2019-
{
2020-
uint32_t i;
2021-
int r = 0;
2022-
2023-
for (i = 0; i < p->num_objects; i++) {
2024-
struct object_id oid;
2025-
2026-
if (!nth_packed_object_oid(&oid, p, i))
2027-
return error("unable to get sha1 of object %u in %s",
2028-
i, p->pack_name);
2029-
2030-
r = cb(&oid, p, i, data);
2031-
if (r)
2032-
break;
2033-
}
2034-
return r;
2035-
}
2036-
2037-
int for_each_packed_object(each_packed_object_fn cb, void *data, unsigned flags)
2038-
{
2039-
struct packed_git *p;
2040-
int r = 0;
2041-
int pack_errors = 0;
2042-
2043-
prepare_packed_git();
2044-
for (p = packed_git; p; p = p->next) {
2045-
if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
2046-
continue;
2047-
if (open_pack_index(p)) {
2048-
pack_errors = 1;
2049-
continue;
2050-
}
2051-
r = for_each_object_in_pack(p, cb, data);
2052-
if (r)
2053-
break;
2054-
}
2055-
return r ? r : pack_errors;
2056-
}
2057-
20582018
static int check_stream_sha1(git_zstream *stream,
20592019
const char *hdr,
20602020
unsigned long size,

0 commit comments

Comments
 (0)