Skip to content

Commit 284b786

Browse files
pks-tgitster
authored andcommitted
object-name: move logic to iterate through loose prefixed objects
The logic to iterate through loose objects that have a certain prefix is currently hosted in "object-name.c". This logic reaches into specifics of the loose object source, so it breaks once a different backend is used for the object storage. Move the logic to iterate through loose objects with a prefix into "object-file.c". This is done by extending the for-each-object options to support an optional prefix that is then honored by the loose source. Naturally, we'll also have this support in the packfile store. This is done in the next commit. Furthermore, there are no users of the loose cache outside of "object-file.c" anymore. As such, convert `odb_source_loose_cache()` to have file scope. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent cfd575f commit 284b786

4 files changed

Lines changed: 40 additions & 13 deletions

File tree

object-file.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
/* The maximum size for an object header. */
3434
#define MAX_HEADER_LEN 32
3535

36+
static struct oidtree *odb_source_loose_cache(struct odb_source *source,
37+
const struct object_id *oid);
38+
3639
static int get_conv_flags(unsigned flags)
3740
{
3841
if (flags & INDEX_RENORMALIZE)
@@ -1845,6 +1848,23 @@ static int for_each_object_wrapper_cb(const struct object_id *oid,
18451848
}
18461849
}
18471850

1851+
static int for_each_prefixed_object_wrapper_cb(const struct object_id *oid,
1852+
void *cb_data)
1853+
{
1854+
struct for_each_object_wrapper_data *data = cb_data;
1855+
if (data->request) {
1856+
struct object_info oi = *data->request;
1857+
1858+
if (odb_source_loose_read_object_info(data->source,
1859+
oid, &oi, 0) < 0)
1860+
return -1;
1861+
1862+
return data->cb(oid, &oi, data->cb_data);
1863+
} else {
1864+
return data->cb(oid, NULL, data->cb_data);
1865+
}
1866+
}
1867+
18481868
int odb_source_loose_for_each_object(struct odb_source *source,
18491869
const struct object_info *request,
18501870
odb_for_each_object_cb cb,
@@ -1864,6 +1884,11 @@ int odb_source_loose_for_each_object(struct odb_source *source,
18641884
if ((opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY) && !source->local)
18651885
return 0;
18661886

1887+
if (opts->prefix)
1888+
return oidtree_each(odb_source_loose_cache(source, opts->prefix),
1889+
opts->prefix, opts->prefix_hex_len,
1890+
for_each_prefixed_object_wrapper_cb, &data);
1891+
18671892
return for_each_loose_file_in_source(source, for_each_object_wrapper_cb,
18681893
NULL, NULL, &data);
18691894
}
@@ -1935,8 +1960,8 @@ static int append_loose_object(const struct object_id *oid,
19351960
return 0;
19361961
}
19371962

1938-
struct oidtree *odb_source_loose_cache(struct odb_source *source,
1939-
const struct object_id *oid)
1963+
static struct oidtree *odb_source_loose_cache(struct odb_source *source,
1964+
const struct object_id *oid)
19401965
{
19411966
struct odb_source_files *files = odb_source_files_downcast(source);
19421967
int subdir_nr = oid->hash[0];

object-file.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,6 @@ int odb_source_loose_write_stream(struct odb_source *source,
7474
struct odb_write_stream *stream, size_t len,
7575
struct object_id *oid);
7676

77-
/*
78-
* Populate and return the loose object cache array corresponding to the
79-
* given object ID.
80-
*/
81-
struct oidtree *odb_source_loose_cache(struct odb_source *source,
82-
const struct object_id *oid);
83-
8477
/*
8578
* Put in `buf` the name of the file in the local object database that
8679
* would be used to store a loose object with the specified oid.

object-name.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "remote.h"
1717
#include "dir.h"
1818
#include "oid-array.h"
19-
#include "oidtree.h"
2019
#include "packfile.h"
2120
#include "pretty.h"
2221
#include "object-file.h"
@@ -103,7 +102,7 @@ static void update_candidates(struct disambiguate_state *ds, const struct object
103102

104103
static int match_hash(unsigned, const unsigned char *, const unsigned char *);
105104

106-
static int match_prefix(const struct object_id *oid, void *arg)
105+
static int match_prefix(const struct object_id *oid, struct object_info *oi UNUSED, void *arg)
107106
{
108107
struct disambiguate_state *ds = arg;
109108
/* no need to call match_hash, oidtree_each did prefix match */
@@ -113,11 +112,14 @@ static int match_prefix(const struct object_id *oid, void *arg)
113112

114113
static void find_short_object_filename(struct disambiguate_state *ds)
115114
{
115+
struct odb_for_each_object_options opts = {
116+
.prefix = &ds->bin_pfx,
117+
.prefix_hex_len = ds->len,
118+
};
116119
struct odb_source *source;
117120

118121
for (source = ds->repo->objects->sources; source && !ds->ambiguous; source = source->next)
119-
oidtree_each(odb_source_loose_cache(source, &ds->bin_pfx),
120-
&ds->bin_pfx, ds->len, match_prefix, ds);
122+
odb_source_loose_for_each_object(source, NULL, match_prefix, ds, &opts);
121123
}
122124

123125
static int match_hash(unsigned len, const unsigned char *a, const unsigned char *b)

odb.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,13 @@ typedef int (*odb_for_each_object_cb)(const struct object_id *oid,
488488
struct odb_for_each_object_options {
489489
/* A bitfield of `odb_for_each_object_flags`. */
490490
enum odb_for_each_object_flags flags;
491+
492+
/*
493+
* If set, only iterate through objects whose first `prefix_hex_len`
494+
* hex characters matches the given prefix.
495+
*/
496+
const struct object_id *prefix;
497+
size_t prefix_hex_len;
491498
};
492499

493500
/*

0 commit comments

Comments
 (0)