return ret;
}
+struct find_abbrev_len_data {
+ const struct object_id *oid;
+ unsigned len;
+};
+
+static int find_abbrev_len_cb(const struct object_id *oid,
+ struct object_info *oi UNUSED,
+ void *cb_data)
+{
+ struct find_abbrev_len_data *data = cb_data;
+ unsigned len = oid_common_prefix_hexlen(oid, data->oid);
+ if (len != hash_algos[oid->algo].hexsz && len >= data->len)
+ data->len = len + 1;
+ return 0;
+}
+
+int odb_source_loose_find_abbrev_len(struct odb_source *source,
+ const struct object_id *oid,
+ unsigned min_len,
+ unsigned *out)
+{
+ struct odb_for_each_object_options opts = {
+ .prefix = oid,
+ .prefix_hex_len = min_len,
+ };
+ struct find_abbrev_len_data data = {
+ .oid = oid,
+ .len = min_len,
+ };
+ int ret;
+
+ ret = odb_source_loose_for_each_object(source, NULL, find_abbrev_len_cb,
+ &data, &opts);
+ *out = data.len;
+
+ return ret;
+}
+
static int append_loose_object(const struct object_id *oid,
const char *path UNUSED,
void *data)
enum odb_count_objects_flags flags,
unsigned long *out);
+/*
+ * Find the shortest unique prefix for the given object ID, where `min_len` is
+ * the minimum length that the prefix should have.
+ *
+ * Returns 0 on success, in which case the computed length will be written to
+ * `out`. Otherwise, a negative error code is returned.
+ */
+int odb_source_loose_find_abbrev_len(struct odb_source *source,
+ const struct object_id *oid,
+ unsigned min_len,
+ unsigned *out);
+
/**
* format_object_header() is a thin wrapper around s xsnprintf() that
* writes the initial "<type> <obj-len>" part of the loose object
return 0;
}
-static int extend_abbrev_len_loose(const struct object_id *oid,
- struct object_info *oi UNUSED,
- void *cb_data)
-{
- struct min_abbrev_data *data = cb_data;
- extend_abbrev_len(oid, data);
- return 0;
-}
-
-static void find_abbrev_len_loose(struct min_abbrev_data *mad)
-{
- struct odb_for_each_object_options opts = {
- .prefix = mad->oid,
- .prefix_hex_len = mad->cur_len,
- };
- struct odb_source *source;
-
- for (source = mad->repo->objects->sources; source; source = source->next)
- odb_source_loose_for_each_object(source, NULL, extend_abbrev_len_loose,
- mad, &opts);
-}
-
static void find_abbrev_len_for_midx(struct multi_pack_index *m,
struct min_abbrev_data *mad)
{
mad.oid = oid;
find_abbrev_len_packed(&mad);
- find_abbrev_len_loose(&mad);
+
+ odb_prepare_alternates(r->objects);
+ for (struct odb_source *s = r->objects->sources; s; s = s->next)
+ odb_source_loose_find_abbrev_len(s, mad.oid, mad.cur_len, &mad.cur_len);
hex[mad.cur_len] = 0;
return mad.cur_len;