blob: baf0090fc8dac79201a682f1d2c56257d8587283 [file] [log] [blame]
Matthias Kestenholz5d4a6002006-08-03 17:24:36 +02001#include "builtin.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00002#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00003#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00004#include "hex.h"
Stefan Bellera80d72d2018-03-23 18:20:59 +01005#include "repository.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07006#include "config.h"
Junio C Hamanoa74db822007-05-19 00:39:31 -07007#include "attr.h"
Linus Torvaldsc323ac72005-06-25 14:42:43 -07008#include "object.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02009#include "commit.h"
10#include "tag.h"
Linus Torvaldsc323ac72005-06-25 14:42:43 -070011#include "delta.h"
Linus Torvaldsa733cb62005-06-28 14:21:02 -070012#include "pack.h"
Nicolas Pitre3449f8c2008-02-28 00:25:17 -050013#include "pack-revindex.h"
Linus Torvaldsc38138c2005-06-26 20:27:56 -070014#include "csum-file.h"
Junio C Hamano1b0c7172006-03-29 22:55:43 -080015#include "tree-walk.h"
Junio C Hamanob5d97e62006-09-04 23:47:39 -070016#include "diff.h"
17#include "revision.h"
18#include "list-objects.h"
Jeff Hostetler9535ce72017-11-21 20:58:52 +000019#include "list-objects-filter-options.h"
Vicent Marti2834bc22013-10-24 14:01:06 -040020#include "pack-objects.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040021#include "progress.h"
Shawn O. Pearcef0a24aa2008-03-03 22:27:20 -050022#include "refs.h"
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +070023#include "streaming.h"
Johannes Sixt93749192010-04-08 09:15:39 +020024#include "thread-utils.h"
Vicent Marti6b8fda22013-12-21 09:00:09 -050025#include "pack-bitmap.h"
Jeff King28b8a732018-08-16 08:13:09 +020026#include "delta-islands.h"
Jeff Kingabcb8652014-10-15 18:42:09 -040027#include "reachable.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -040028#include "oid-array.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040029#include "strvec.h"
Gargi Sharmaec2dd322018-01-23 18:46:51 -050030#include "list.h"
Jonathan Tan0317f452017-08-18 15:20:19 -070031#include "packfile.h"
Elijah Newren87bed172023-04-11 00:41:53 -070032#include "object-file.h"
Elijah Newrena034e912023-05-16 06:34:06 +000033#include "object-store-ll.h"
Elijah Newrencbeab742023-02-24 00:09:33 +000034#include "replace-object.h"
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +020035#include "dir.h"
Derrick Stolee6a22d522018-08-20 16:52:08 +000036#include "midx.h"
Derrick Stoleeae417802019-02-22 14:25:07 -080037#include "trace2.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060038#include "shallow.h"
Jonathan Tane00549a2020-07-20 17:21:44 -070039#include "promisor-remote.h"
Taylor Blaub7573532022-05-20 19:17:52 -040040#include "pack-mtimes.h"
SZEDER Gábor49fd5512023-03-19 17:27:11 +010041#include "parse-options.h"
Nicolas Pitre8ecce682007-09-06 02:13:11 -040042
Ævar Arnfjörð Bjarmason7d089fb2021-05-27 02:52:51 +020043/*
44 * Objects we are going to pack are collected in the `to_pack` structure.
45 * It contains an array (dynamically expanded) of the object data, and a map
46 * that can resolve SHA1s to their position in the array.
47 */
48static struct packing_data to_pack;
49
50static inline struct object_entry *oe_delta(
51 const struct packing_data *pack,
52 const struct object_entry *e)
53{
54 if (!e->delta_idx)
55 return NULL;
56 if (e->ext_base)
57 return &pack->ext_bases[e->delta_idx - 1];
58 else
59 return &pack->objects[e->delta_idx - 1];
60}
61
62static inline unsigned long oe_delta_size(struct packing_data *pack,
63 const struct object_entry *e)
64{
65 if (e->delta_size_valid)
66 return e->delta_size_;
67
68 /*
69 * pack->delta_size[] can't be NULL because oe_set_delta_size()
70 * must have been called when a new delta is saved with
71 * oe_set_delta().
72 * If oe_delta() returns NULL (i.e. default state, which means
73 * delta_size_valid is also false), then the caller must never
74 * call oe_delta_size().
75 */
76 return pack->delta_size[e - pack->objects];
77}
78
79unsigned long oe_get_size_slow(struct packing_data *pack,
80 const struct object_entry *e);
81
82static inline unsigned long oe_size(struct packing_data *pack,
83 const struct object_entry *e)
84{
85 if (e->size_valid)
86 return e->size_;
87
88 return oe_get_size_slow(pack, e);
89}
90
91static inline void oe_set_delta(struct packing_data *pack,
92 struct object_entry *e,
93 struct object_entry *delta)
94{
95 if (delta)
96 e->delta_idx = (delta - pack->objects) + 1;
97 else
98 e->delta_idx = 0;
99}
100
101static inline struct object_entry *oe_delta_sibling(
102 const struct packing_data *pack,
103 const struct object_entry *e)
104{
105 if (e->delta_sibling_idx)
106 return &pack->objects[e->delta_sibling_idx - 1];
107 return NULL;
108}
109
110static inline struct object_entry *oe_delta_child(
111 const struct packing_data *pack,
112 const struct object_entry *e)
113{
114 if (e->delta_child_idx)
115 return &pack->objects[e->delta_child_idx - 1];
116 return NULL;
117}
118
119static inline void oe_set_delta_child(struct packing_data *pack,
120 struct object_entry *e,
121 struct object_entry *delta)
122{
123 if (delta)
124 e->delta_child_idx = (delta - pack->objects) + 1;
125 else
126 e->delta_child_idx = 0;
127}
128
129static inline void oe_set_delta_sibling(struct packing_data *pack,
130 struct object_entry *e,
131 struct object_entry *delta)
132{
133 if (delta)
134 e->delta_sibling_idx = (delta - pack->objects) + 1;
135 else
136 e->delta_sibling_idx = 0;
137}
138
139static inline void oe_set_size(struct packing_data *pack,
140 struct object_entry *e,
141 unsigned long size)
142{
143 if (size < pack->oe_size_limit) {
144 e->size_ = size;
145 e->size_valid = 1;
146 } else {
147 e->size_valid = 0;
148 if (oe_get_size_slow(pack, e) != size)
149 BUG("'size' is supposed to be the object size!");
150 }
151}
152
153static inline void oe_set_delta_size(struct packing_data *pack,
154 struct object_entry *e,
155 unsigned long size)
156{
157 if (size < pack->oe_delta_size_limit) {
158 e->delta_size_ = size;
159 e->delta_size_valid = 1;
160 } else {
161 packing_data_lock(pack);
162 if (!pack->delta_size)
163 ALLOC_ARRAY(pack->delta_size, pack->nr_alloc);
164 packing_data_unlock(pack);
165
166 pack->delta_size[e - pack->objects] = size;
167 e->delta_size_valid = 0;
168 }
169}
170
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +0200171#define IN_PACK(obj) oe_in_pack(&to_pack, obj)
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +0200172#define SIZE(obj) oe_size(&to_pack, obj)
173#define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size)
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +0200174#define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj)
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200175#define DELTA(obj) oe_delta(&to_pack, obj)
176#define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj)
177#define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj)
178#define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val)
Jeff King6a1e32d2018-08-21 15:07:05 -0400179#define SET_DELTA_EXT(obj, oid) oe_set_delta_ext(&to_pack, obj, oid)
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +0200180#define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val)
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200181#define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val)
182#define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val)
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +0200183
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +0700184static const char *pack_usage[] = {
Ævar Arnfjörð Bjarmason23a92352022-10-13 17:39:08 +0200185 N_("git pack-objects --stdout [<options>] [< <ref-list> | < <object-list>]"),
186 N_("git pack-objects [<options>] <base-name> [< <ref-list> | < <object-list>]"),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +0700187 NULL
188};
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700189
Nicolas Pitre79814f42007-11-01 23:43:24 -0400190static struct pack_idx_entry **written_list;
Nguyễn Thái Ngọc Duy5af05042018-04-15 17:36:18 +0200191static uint32_t nr_result, nr_written, nr_seen;
Jeff King6a1e32d2018-08-21 15:07:05 -0400192static struct bitmap_index *bitmap_git;
Jeff King28b8a732018-08-16 08:13:09 +0200193static uint32_t write_layer;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800194
David Rientjes96f1e582006-08-15 10:23:48 -0700195static int non_empty;
Nicolas Pitrea7de7132008-05-02 15:11:46 -0400196static int reuse_delta = 1, reuse_object = 1;
Junio C Hamanoe5e97142008-05-23 16:06:01 -0700197static int keep_unreachable, unpack_unreachable, include_tag;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200198static timestamp_t unpack_unreachable_expiration;
Jeff Kinge26a8c42016-06-13 00:38:04 -0400199static int pack_loose_unreachable;
Taylor Blaub7573532022-05-20 19:17:52 -0400200static int cruft;
201static timestamp_t cruft_expiration;
David Rientjes96f1e582006-08-15 10:23:48 -0700202static int local;
Jeff King56dfeb62016-07-29 00:11:31 -0400203static int have_non_local_packs;
David Rientjes96f1e582006-08-15 10:23:48 -0700204static int incremental;
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +0200205static int ignore_packed_keep_on_disk;
206static int ignore_packed_keep_in_core;
Nicolas Pitrebe6b1912006-09-21 00:09:44 -0400207static int allow_ofs_delta;
Junio C Hamanoebcfb372011-02-25 15:43:25 -0800208static struct pack_idx_option pack_idx_opts;
Dana L. Howd01fb922007-05-13 11:34:56 -0700209static const char *base_name;
Junio C Hamano024701f2006-02-12 13:01:54 -0800210static int progress = 1;
Jeff King4812a932006-07-23 01:50:30 -0400211static int window = 10;
Junio C Hamano568508e2011-10-28 14:48:40 -0700212static unsigned long pack_size_limit;
Theodore Ts'o618e6132007-05-08 09:28:26 -0400213static int depth = 50;
Nicolas Pitre43cc2b42008-12-11 15:36:47 -0500214static int delta_search_threads;
Junio C Hamanodf6d6102006-09-01 15:05:12 -0700215static int pack_to_stdout;
Derrick Stolee4f6d26b2019-01-16 10:25:58 -0800216static int sparse;
Jeff King6a1e32d2018-08-21 15:07:05 -0400217static int thin;
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700218static int num_preferred_base;
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400219static struct progress *progress_state;
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700220
Taylor Blau073b40e2023-12-14 17:24:12 -0500221static struct bitmapped_pack *reuse_packfiles;
222static size_t reuse_packfiles_nr;
Taylor Blaub96289a2023-12-14 17:24:20 -0500223static size_t reuse_packfiles_used_nr;
Vicent Marti6b8fda22013-12-21 09:00:09 -0500224static uint32_t reuse_packfile_objects;
Jeff Kingbb514de2019-12-18 12:25:45 +0100225static struct bitmap *reuse_packfile_bitmap;
Vicent Marti6b8fda22013-12-21 09:00:09 -0500226
Kirill Smelkov645c4322016-09-10 18:01:44 +0300227static int use_bitmap_index_default = 1;
228static int use_bitmap_index = -1;
Taylor Blau94107412023-12-14 17:24:42 -0500229static enum {
230 NO_PACK_REUSE = 0,
231 SINGLE_PACK_REUSE,
Taylor Blauaf626ac2023-12-14 17:24:44 -0500232 MULTI_PACK_REUSE,
Taylor Blau94107412023-12-14 17:24:42 -0500233} allow_pack_reuse = SINGLE_PACK_REUSE;
Jeff King25575012019-07-31 01:39:27 -0400234static enum {
235 WRITE_BITMAP_FALSE = 0,
236 WRITE_BITMAP_QUIET,
237 WRITE_BITMAP_TRUE,
238} write_bitmap_index;
Jeff Kingd4316602019-03-15 02:25:28 -0400239static uint16_t write_bitmap_options = BITMAP_OPT_HASH_CACHE;
Vicent Marti6b8fda22013-12-21 09:00:09 -0500240
Jonathan Tan0c16cd42017-12-08 15:27:16 +0000241static int exclude_promisor_objects;
242
Jeff King28b8a732018-08-16 08:13:09 +0200243static int use_delta_islands;
244
Martin Koegler074b2ee2007-05-28 23:20:58 +0200245static unsigned long delta_cache_size = 0;
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +0200246static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
Martin Koeglere3dfddb2007-05-28 23:20:59 +0200247static unsigned long cache_max_small_delta_size = 1000;
Martin Koegler074b2ee2007-05-28 23:20:58 +0200248
Brian Downinga97773c2007-07-12 08:07:46 -0500249static unsigned long window_memory_limit = 0;
250
Jonathan Tandd4b7322020-06-10 13:57:23 -0700251static struct string_list uri_protocols = STRING_LIST_INIT_NODUP;
252
Jeff Hostetler9535ce72017-11-21 20:58:52 +0000253enum missing_action {
Jonathan Tan0c16cd42017-12-08 15:27:16 +0000254 MA_ERROR = 0, /* fail if any missing objects are encountered */
255 MA_ALLOW_ANY, /* silently allow ALL missing objects */
256 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
Jeff Hostetler9535ce72017-11-21 20:58:52 +0000257};
258static enum missing_action arg_missing_action;
259static show_object_fn fn_show_object;
260
Jonathan Tandd4b7322020-06-10 13:57:23 -0700261struct configured_exclusion {
262 struct oidmap_entry e;
263 char *pack_hash_hex;
264 char *uri;
265};
266static struct oidmap configured_exclusions;
267
268static struct oidset excluded_by_config;
269
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800270/*
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800271 * stats
272 */
Shawn O. Pearce7cadf492007-03-06 20:44:24 -0500273static uint32_t written, written_delta;
274static uint32_t reused, reused_delta;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800275
Vicent Marti7cc8f972013-12-21 09:00:16 -0500276/*
277 * Indexed commits
278 */
279static struct commit **indexed_commits;
280static unsigned int indexed_commits_nr;
281static unsigned int indexed_commits_alloc;
282
283static void index_commit_for_bitmap(struct commit *commit)
284{
285 if (indexed_commits_nr >= indexed_commits_alloc) {
286 indexed_commits_alloc = (indexed_commits_alloc + 32) * 2;
René Scharfe2756ca42014-09-16 20:56:57 +0200287 REALLOC_ARRAY(indexed_commits, indexed_commits_alloc);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500288 }
289
290 indexed_commits[indexed_commits_nr++] = commit;
291}
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400292
Nicolas Pitre3613f9b2008-05-02 15:11:45 -0400293static void *get_delta(struct object_entry *entry)
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700294{
Nicolas Pitre3613f9b2008-05-02 15:11:45 -0400295 unsigned long size, base_size, delta_size;
296 void *buf, *base_buf, *delta_buf;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500297 enum object_type type;
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700298
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200299 buf = repo_read_object_file(the_repository, &entry->idx.oid, &type,
300 &size);
Nicolas Pitre3613f9b2008-05-02 15:11:45 -0400301 if (!buf)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +0200302 die(_("unable to read %s"), oid_to_hex(&entry->idx.oid));
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200303 base_buf = repo_read_object_file(the_repository,
304 &DELTA(entry)->idx.oid, &type,
305 &base_size);
Nicolas Pitre3613f9b2008-05-02 15:11:45 -0400306 if (!base_buf)
brian m. carlsone6a492b2017-05-06 22:10:11 +0000307 die("unable to read %s",
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200308 oid_to_hex(&DELTA(entry)->idx.oid));
Nicolas Pitre3613f9b2008-05-02 15:11:45 -0400309 delta_buf = diff_delta(base_buf, base_size,
Nicolas Pitredcde55b2005-06-29 02:49:56 -0400310 buf, size, &delta_size, 0);
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200311 /*
Elijah Newren15beaaa2019-11-05 17:07:23 +0000312 * We successfully computed this delta once but dropped it for
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200313 * memory reasons. Something is very wrong if this time we
314 * recompute and create a different delta.
315 */
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +0200316 if (!delta_buf || delta_size != DELTA_SIZE(entry))
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200317 BUG("delta size changed");
Nicolas Pitre3613f9b2008-05-02 15:11:45 -0400318 free(buf);
319 free(base_buf);
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700320 return delta_buf;
321}
322
Nicolas Pitre30ebb402008-05-02 15:11:49 -0400323static unsigned long do_compress(void **pptr, unsigned long size)
324{
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700325 git_zstream stream;
Nicolas Pitre30ebb402008-05-02 15:11:49 -0400326 void *in, *out;
327 unsigned long maxsize;
328
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700329 git_deflate_init(&stream, pack_compression_level);
Junio C Hamano225a6f12011-06-10 11:18:17 -0700330 maxsize = git_deflate_bound(&stream, size);
Nicolas Pitre30ebb402008-05-02 15:11:49 -0400331
332 in = *pptr;
333 out = xmalloc(maxsize);
334 *pptr = out;
335
336 stream.next_in = in;
337 stream.avail_in = size;
338 stream.next_out = out;
339 stream.avail_out = maxsize;
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700340 while (git_deflate(&stream, Z_FINISH) == Z_OK)
Nicolas Pitre30ebb402008-05-02 15:11:49 -0400341 ; /* nothing */
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700342 git_deflate_end(&stream);
Nicolas Pitre30ebb402008-05-02 15:11:49 -0400343
344 free(in);
345 return stream.total_out;
346}
347
brian m. carlson98a3bea2018-02-01 02:18:46 +0000348static unsigned long write_large_blob_data(struct git_istream *st, struct hashfile *f,
brian m. carlson188960b2017-10-15 22:07:01 +0000349 const struct object_id *oid)
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700350{
351 git_zstream stream;
352 unsigned char ibuf[1024 * 16];
353 unsigned char obuf[1024 * 16];
354 unsigned long olen = 0;
355
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700356 git_deflate_init(&stream, pack_compression_level);
357
358 for (;;) {
359 ssize_t readlen;
360 int zret = Z_OK;
361 readlen = read_istream(st, ibuf, sizeof(ibuf));
362 if (readlen == -1)
brian m. carlson188960b2017-10-15 22:07:01 +0000363 die(_("unable to read %s"), oid_to_hex(oid));
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700364
365 stream.next_in = ibuf;
366 stream.avail_in = readlen;
367 while ((stream.avail_in || readlen == 0) &&
368 (zret == Z_OK || zret == Z_BUF_ERROR)) {
369 stream.next_out = obuf;
370 stream.avail_out = sizeof(obuf);
371 zret = git_deflate(&stream, readlen ? 0 : Z_FINISH);
brian m. carlson98a3bea2018-02-01 02:18:46 +0000372 hashwrite(f, obuf, stream.next_out - obuf);
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700373 olen += stream.next_out - obuf;
374 }
375 if (stream.avail_in)
376 die(_("deflate error (%d)"), zret);
377 if (readlen == 0) {
378 if (zret != Z_STREAM_END)
379 die(_("deflate error (%d)"), zret);
380 break;
381 }
382 }
383 git_deflate_end(&stream);
384 return olen;
385}
386
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700387/*
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400388 * we are going to reuse the existing object data as is. make
389 * sure it is not corrupt.
390 */
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500391static int check_pack_inflate(struct packed_git *p,
392 struct pack_window **w_curs,
Shawn O. Pearce6777a592007-03-06 20:44:34 -0500393 off_t offset,
394 off_t len,
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500395 unsigned long expect)
396{
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700397 git_zstream stream;
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500398 unsigned char fakebuf[4096], *in;
399 int st;
400
401 memset(&stream, 0, sizeof(stream));
Linus Torvalds39c68542009-01-07 19:54:47 -0800402 git_inflate_init(&stream);
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500403 do {
404 in = use_pack(p, w_curs, offset, &stream.avail_in);
405 stream.next_in = in;
406 stream.next_out = fakebuf;
407 stream.avail_out = sizeof(fakebuf);
Linus Torvalds39c68542009-01-07 19:54:47 -0800408 st = git_inflate(&stream, Z_FINISH);
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500409 offset += stream.next_in - in;
410 } while (st == Z_OK || st == Z_BUF_ERROR);
Linus Torvalds39c68542009-01-07 19:54:47 -0800411 git_inflate_end(&stream);
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500412 return (st == Z_STREAM_END &&
413 stream.total_out == expect &&
414 stream.total_in == len) ? 0 : -1;
415}
416
brian m. carlson98a3bea2018-02-01 02:18:46 +0000417static void copy_pack_data(struct hashfile *f,
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500418 struct packed_git *p,
419 struct pack_window **w_curs,
Shawn O. Pearce6777a592007-03-06 20:44:34 -0500420 off_t offset,
421 off_t len)
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500422{
423 unsigned char *in;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700424 unsigned long avail;
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500425
426 while (len) {
427 in = use_pack(p, w_curs, offset, &avail);
428 if (avail > len)
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700429 avail = (unsigned long)len;
brian m. carlson98a3bea2018-02-01 02:18:46 +0000430 hashwrite(f, in, avail);
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500431 offset += avail;
432 len -= avail;
433 }
434}
435
Ævar Arnfjörð Bjarmason7d089fb2021-05-27 02:52:51 +0200436static inline int oe_size_greater_than(struct packing_data *pack,
437 const struct object_entry *lhs,
438 unsigned long rhs)
439{
440 if (lhs->size_valid)
441 return lhs->size_ > rhs;
442 if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
443 return 1;
444 return oe_get_size_slow(pack, lhs) > rhs;
445}
446
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700447/* Return 0 if we will bust the pack-size limit */
brian m. carlson98a3bea2018-02-01 02:18:46 +0000448static unsigned long write_no_reuse_object(struct hashfile *f, struct object_entry *entry,
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700449 unsigned long limit, int usable_delta)
450{
451 unsigned long size, datalen;
Jeff King2c5e2862017-03-24 13:26:50 -0400452 unsigned char header[MAX_PACK_OBJECT_HEADER],
453 dheader[MAX_PACK_OBJECT_HEADER];
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700454 unsigned hdrlen;
455 enum object_type type;
456 void *buf;
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700457 struct git_istream *st = NULL;
brian m. carlson41179102018-05-02 00:25:37 +0000458 const unsigned hashsz = the_hash_algo->rawsz;
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700459
460 if (!usable_delta) {
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +0200461 if (oe_type(entry) == OBJ_BLOB &&
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +0200462 oe_size_greater_than(&to_pack, entry, big_file_threshold) &&
Matheus Tavaresc8123e72020-01-30 17:32:20 -0300463 (st = open_istream(the_repository, &entry->idx.oid, &type,
464 &size, NULL)) != NULL)
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700465 buf = NULL;
466 else {
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200467 buf = repo_read_object_file(the_repository,
468 &entry->idx.oid, &type,
469 &size);
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700470 if (!buf)
brian m. carlsone6a492b2017-05-06 22:10:11 +0000471 die(_("unable to read %s"),
472 oid_to_hex(&entry->idx.oid));
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700473 }
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700474 /*
475 * make sure no cached delta data remains from a
476 * previous attempt before a pack split occurred.
477 */
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000478 FREE_AND_NULL(entry->delta_data);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700479 entry->z_delta_size = 0;
480 } else if (entry->delta_data) {
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +0200481 size = DELTA_SIZE(entry);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700482 buf = entry->delta_data;
483 entry->delta_data = NULL;
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200484 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700485 OBJ_OFS_DELTA : OBJ_REF_DELTA;
486 } else {
487 buf = get_delta(entry);
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +0200488 size = DELTA_SIZE(entry);
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200489 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700490 OBJ_OFS_DELTA : OBJ_REF_DELTA;
491 }
492
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700493 if (st) /* large blob case, just assume we don't compress well */
494 datalen = size;
495 else if (entry->z_delta_size)
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700496 datalen = entry->z_delta_size;
497 else
498 datalen = do_compress(&buf, size);
499
500 /*
501 * The object header is a byte of 'type' followed by zero or
502 * more bytes of length.
503 */
Jeff King7202a6f2017-03-24 13:26:40 -0400504 hdrlen = encode_in_pack_object_header(header, sizeof(header),
505 type, size);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700506
507 if (type == OBJ_OFS_DELTA) {
508 /*
509 * Deltas with relative base contain an additional
510 * encoding of the relative offset for the delta
511 * base from this object's position in the pack.
512 */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200513 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700514 unsigned pos = sizeof(dheader) - 1;
515 dheader[pos] = ofs & 127;
516 while (ofs >>= 7)
517 dheader[--pos] = 128 | (--ofs & 127);
brian m. carlson41179102018-05-02 00:25:37 +0000518 if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700519 if (st)
520 close_istream(st);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700521 free(buf);
522 return 0;
523 }
brian m. carlson98a3bea2018-02-01 02:18:46 +0000524 hashwrite(f, header, hdrlen);
525 hashwrite(f, dheader + pos, sizeof(dheader) - pos);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700526 hdrlen += sizeof(dheader) - pos;
527 } else if (type == OBJ_REF_DELTA) {
528 /*
529 * Deltas with a base reference contain
brian m. carlson41179102018-05-02 00:25:37 +0000530 * additional bytes for the base object ID.
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700531 */
brian m. carlson41179102018-05-02 00:25:37 +0000532 if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700533 if (st)
534 close_istream(st);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700535 free(buf);
536 return 0;
537 }
brian m. carlson98a3bea2018-02-01 02:18:46 +0000538 hashwrite(f, header, hdrlen);
Junio C Hamano42c8ce12018-05-30 14:04:10 +0900539 hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
brian m. carlson41179102018-05-02 00:25:37 +0000540 hdrlen += hashsz;
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700541 } else {
brian m. carlson41179102018-05-02 00:25:37 +0000542 if (limit && hdrlen + datalen + hashsz >= limit) {
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700543 if (st)
544 close_istream(st);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700545 free(buf);
546 return 0;
547 }
brian m. carlson98a3bea2018-02-01 02:18:46 +0000548 hashwrite(f, header, hdrlen);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700549 }
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700550 if (st) {
brian m. carlson188960b2017-10-15 22:07:01 +0000551 datalen = write_large_blob_data(st, f, &entry->idx.oid);
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700552 close_istream(st);
553 } else {
brian m. carlson98a3bea2018-02-01 02:18:46 +0000554 hashwrite(f, buf, datalen);
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700555 free(buf);
556 }
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700557
558 return hdrlen + datalen;
559}
560
561/* Return 0 if we will bust the pack-size limit */
brian m. carlson98a3bea2018-02-01 02:18:46 +0000562static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
Nguyễn Thái Ngọc Duyaf92a642016-07-13 17:44:03 +0200563 unsigned long limit, int usable_delta)
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700564{
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +0200565 struct packed_git *p = IN_PACK(entry);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700566 struct pack_window *w_curs = NULL;
Taylor Blau952fc682021-01-13 17:23:35 -0500567 uint32_t pos;
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700568 off_t offset;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +0200569 enum object_type type = oe_type(entry);
Nguyễn Thái Ngọc Duy211c61c2016-07-05 19:05:54 +0200570 off_t datalen;
Jeff King2c5e2862017-03-24 13:26:50 -0400571 unsigned char header[MAX_PACK_OBJECT_HEADER],
572 dheader[MAX_PACK_OBJECT_HEADER];
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700573 unsigned hdrlen;
brian m. carlson41179102018-05-02 00:25:37 +0000574 const unsigned hashsz = the_hash_algo->rawsz;
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +0200575 unsigned long entry_size = SIZE(entry);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700576
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200577 if (DELTA(entry))
578 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700579 OBJ_OFS_DELTA : OBJ_REF_DELTA;
Jeff King7202a6f2017-03-24 13:26:40 -0400580 hdrlen = encode_in_pack_object_header(header, sizeof(header),
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +0200581 type, entry_size);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700582
583 offset = entry->in_pack_offset;
Taylor Blau952fc682021-01-13 17:23:35 -0500584 if (offset_to_pack_pos(p, offset, &pos) < 0)
585 die(_("write_reuse_object: could not locate %s, expected at "
586 "offset %"PRIuMAX" in pack %s"),
587 oid_to_hex(&entry->idx.oid), (uintmax_t)offset,
588 p->pack_name);
589 datalen = pack_pos_to_offset(p, pos + 1) - offset;
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700590 if (!pack_to_stdout && p->index_version > 1 &&
Taylor Blau952fc682021-01-13 17:23:35 -0500591 check_pack_crc(p, &w_curs, offset, datalen,
592 pack_pos_to_index(p, pos))) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +0200593 error(_("bad packed object CRC for %s"),
brian m. carlsone6a492b2017-05-06 22:10:11 +0000594 oid_to_hex(&entry->idx.oid));
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700595 unuse_pack(&w_curs);
596 return write_no_reuse_object(f, entry, limit, usable_delta);
597 }
598
599 offset += entry->in_pack_header_size;
600 datalen -= entry->in_pack_header_size;
601
602 if (!pack_to_stdout && p->index_version == 1 &&
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +0200603 check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +0200604 error(_("corrupt packed object for %s"),
brian m. carlsone6a492b2017-05-06 22:10:11 +0000605 oid_to_hex(&entry->idx.oid));
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700606 unuse_pack(&w_curs);
607 return write_no_reuse_object(f, entry, limit, usable_delta);
608 }
609
610 if (type == OBJ_OFS_DELTA) {
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200611 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700612 unsigned pos = sizeof(dheader) - 1;
613 dheader[pos] = ofs & 127;
614 while (ofs >>= 7)
615 dheader[--pos] = 128 | (--ofs & 127);
brian m. carlson41179102018-05-02 00:25:37 +0000616 if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700617 unuse_pack(&w_curs);
618 return 0;
619 }
brian m. carlson98a3bea2018-02-01 02:18:46 +0000620 hashwrite(f, header, hdrlen);
621 hashwrite(f, dheader + pos, sizeof(dheader) - pos);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700622 hdrlen += sizeof(dheader) - pos;
623 reused_delta++;
624 } else if (type == OBJ_REF_DELTA) {
brian m. carlson41179102018-05-02 00:25:37 +0000625 if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700626 unuse_pack(&w_curs);
627 return 0;
628 }
brian m. carlson98a3bea2018-02-01 02:18:46 +0000629 hashwrite(f, header, hdrlen);
Junio C Hamano42c8ce12018-05-30 14:04:10 +0900630 hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
brian m. carlson41179102018-05-02 00:25:37 +0000631 hdrlen += hashsz;
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700632 reused_delta++;
633 } else {
brian m. carlson41179102018-05-02 00:25:37 +0000634 if (limit && hdrlen + datalen + hashsz >= limit) {
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700635 unuse_pack(&w_curs);
636 return 0;
637 }
brian m. carlson98a3bea2018-02-01 02:18:46 +0000638 hashwrite(f, header, hdrlen);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700639 }
640 copy_pack_data(f, p, &w_curs, offset, datalen);
641 unuse_pack(&w_curs);
642 reused++;
643 return hdrlen + datalen;
644}
645
646/* Return 0 if we will bust the pack-size limit */
brian m. carlson98a3bea2018-02-01 02:18:46 +0000647static off_t write_object(struct hashfile *f,
Nguyễn Thái Ngọc Duyaf92a642016-07-13 17:44:03 +0200648 struct object_entry *entry,
649 off_t write_offset)
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700650{
Nguyễn Thái Ngọc Duyaf92a642016-07-13 17:44:03 +0200651 unsigned long limit;
652 off_t len;
Nicolas Pitre2c5ef822008-05-02 15:11:48 -0400653 int usable_delta, to_reuse;
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700654
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400655 if (!pack_to_stdout)
656 crc32_begin(f);
657
Nicolas Pitrea2430dd2010-02-03 22:48:27 -0500658 /* apply size limit if limited packsize and not first object */
Nicolas Pitrea1e47602008-11-12 13:23:58 -0500659 if (!pack_size_limit || !nr_written)
660 limit = 0;
661 else if (pack_size_limit <= write_offset)
662 /*
663 * the earlier object did not fit the limit; avoid
664 * mistaking this with unlimited (i.e. limit = 0).
665 */
666 limit = 1;
667 else
668 limit = pack_size_limit - write_offset;
Nicolas Pitre2c5ef822008-05-02 15:11:48 -0400669
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200670 if (!DELTA(entry))
Nicolas Pitre2c5ef822008-05-02 15:11:48 -0400671 usable_delta = 0; /* no delta */
672 else if (!pack_size_limit)
673 usable_delta = 1; /* unlimited packfile */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200674 else if (DELTA(entry)->idx.offset == (off_t)-1)
Nicolas Pitre2c5ef822008-05-02 15:11:48 -0400675 usable_delta = 0; /* base was written to another pack */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200676 else if (DELTA(entry)->idx.offset)
Nicolas Pitre2c5ef822008-05-02 15:11:48 -0400677 usable_delta = 1; /* base already exists in this pack */
678 else
679 usable_delta = 0; /* base could end up in another pack */
680
Nicolas Pitrea7de7132008-05-02 15:11:46 -0400681 if (!reuse_object)
Nicolas Pitrefa736f72007-05-09 12:31:28 -0400682 to_reuse = 0; /* explicit */
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +0200683 else if (!IN_PACK(entry))
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -0800684 to_reuse = 0; /* can't reuse what we don't have */
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +0200685 else if (oe_type(entry) == OBJ_REF_DELTA ||
686 oe_type(entry) == OBJ_OFS_DELTA)
Dana L. How17b08f22007-05-13 12:06:18 -0700687 /* check_object() decided it for us ... */
688 to_reuse = usable_delta;
689 /* ... but pack split may override that */
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +0200690 else if (oe_type(entry) != entry->in_pack_type)
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -0800691 to_reuse = 0; /* pack has delta which is unusable */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200692 else if (DELTA(entry))
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -0800693 to_reuse = 0; /* we want to pack afresh */
694 else
695 to_reuse = 1; /* we have it in-pack undeltified,
696 * and we do not need to deltify it.
697 */
698
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700699 if (!to_reuse)
700 len = write_no_reuse_object(f, entry, limit, usable_delta);
701 else
702 len = write_reuse_object(f, entry, limit, usable_delta);
703 if (!len)
704 return 0;
Nicolas Pitreed4a9032008-05-02 15:11:50 -0400705
Dana L. How17b08f22007-05-13 12:06:18 -0700706 if (usable_delta)
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -0800707 written_delta++;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800708 written++;
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400709 if (!pack_to_stdout)
Geert Boschaa7e44b2007-06-01 15:18:05 -0400710 entry->idx.crc32 = crc32_end(f);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700711 return len;
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700712}
713
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800714enum write_one_status {
715 WRITE_ONE_SKIP = -1, /* already written */
716 WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */
717 WRITE_ONE_WRITTEN = 1, /* normal */
718 WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */
719};
720
brian m. carlson98a3bea2018-02-01 02:18:46 +0000721static enum write_one_status write_one(struct hashfile *f,
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800722 struct object_entry *e,
723 off_t *offset)
Junio C Hamano9d5ab962005-06-28 17:49:27 -0700724{
Nguyễn Thái Ngọc Duyaf92a642016-07-13 17:44:03 +0200725 off_t size;
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800726 int recursing;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400727
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800728 /*
729 * we set offset to 1 (which is an impossible value) to mark
730 * the fact that this object is involved in "write its base
731 * first before writing a deltified object" recursion.
732 */
733 recursing = (e->idx.offset == 1);
734 if (recursing) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +0200735 warning(_("recursive delta detected for object %s"),
brian m. carlsone6a492b2017-05-06 22:10:11 +0000736 oid_to_hex(&e->idx.oid));
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800737 return WRITE_ONE_RECURSIVE;
738 } else if (e->idx.offset || e->preferred_base) {
739 /* offset is non zero if object is written already. */
740 return WRITE_ONE_SKIP;
741 }
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400742
Nicolas Pitre720c9f72010-02-08 10:39:01 -0500743 /* if we are deltified, write out base object first. */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200744 if (DELTA(e)) {
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800745 e->idx.offset = 1; /* now recurse */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200746 switch (write_one(f, DELTA(e), offset)) {
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800747 case WRITE_ONE_RECURSIVE:
748 /* we cannot depend on this one */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200749 SET_DELTA(e, NULL);
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800750 break;
751 default:
752 break;
753 case WRITE_ONE_BREAK:
754 e->idx.offset = recursing;
755 return WRITE_ONE_BREAK;
756 }
757 }
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400758
Nicolas Pitre6ed7f252008-08-29 16:07:58 -0400759 e->idx.offset = *offset;
760 size = write_object(f, e, *offset);
Dana L. How17b08f22007-05-13 12:06:18 -0700761 if (!size) {
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800762 e->idx.offset = recursing;
763 return WRITE_ONE_BREAK;
Dana L. How17b08f22007-05-13 12:06:18 -0700764 }
Nicolas Pitre79814f42007-11-01 23:43:24 -0400765 written_list[nr_written++] = &e->idx;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400766
767 /* make sure off_t is sufficiently large not to wrap */
Erik Faye-Lundc03c8312010-10-05 09:24:10 +0200768 if (signed_add_overflows(*offset, size))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +0200769 die(_("pack too large for current definition of off_t"));
Nicolas Pitre6ed7f252008-08-29 16:07:58 -0400770 *offset += size;
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800771 return WRITE_ONE_WRITTEN;
Junio C Hamano9d5ab962005-06-28 17:49:27 -0700772}
773
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200774static int mark_tagged(const char *path UNUSED, const struct object_id *oid,
775 int flag UNUSED, void *cb_data UNUSED)
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700776{
brian m. carlson188960b2017-10-15 22:07:01 +0000777 struct object_id peeled;
Jeff King3a378762019-09-05 21:36:05 -0400778 struct object_entry *entry = packlist_find(&to_pack, oid);
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700779
780 if (entry)
781 entry->tagged = 1;
Jeff King36a31792021-01-20 14:44:43 -0500782 if (!peel_iterated_oid(oid, &peeled)) {
Jeff King3a378762019-09-05 21:36:05 -0400783 entry = packlist_find(&to_pack, &peeled);
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700784 if (entry)
785 entry->tagged = 1;
786 }
787 return 0;
788}
789
Ævar Arnfjörð Bjarmason7d089fb2021-05-27 02:52:51 +0200790static inline unsigned char oe_layer(struct packing_data *pack,
791 struct object_entry *e)
792{
793 if (!pack->layer)
794 return 0;
795 return pack->layer[e - pack->objects];
796}
797
Dan McGeebe126812011-10-18 00:21:21 -0500798static inline void add_to_write_order(struct object_entry **wo,
Dan McGee92bef1a2011-10-18 00:21:22 -0500799 unsigned int *endp,
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700800 struct object_entry *e)
801{
Christian Couderfe0ac2f2018-08-16 08:13:13 +0200802 if (e->filled || oe_layer(&to_pack, e) != write_layer)
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700803 return;
804 wo[(*endp)++] = e;
805 e->filled = 1;
806}
807
808static void add_descendants_to_write_order(struct object_entry **wo,
Dan McGee92bef1a2011-10-18 00:21:22 -0500809 unsigned int *endp,
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700810 struct object_entry *e)
811{
Dan McGeef3808722011-10-18 00:21:24 -0500812 int add_to_order = 1;
813 while (e) {
814 if (add_to_order) {
815 struct object_entry *s;
816 /* add this node... */
817 add_to_write_order(wo, endp, e);
818 /* all its siblings... */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200819 for (s = DELTA_SIBLING(e); s; s = DELTA_SIBLING(s)) {
Dan McGeef3808722011-10-18 00:21:24 -0500820 add_to_write_order(wo, endp, s);
821 }
822 }
823 /* drop down a level to add left subtree nodes if possible */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200824 if (DELTA_CHILD(e)) {
Dan McGeef3808722011-10-18 00:21:24 -0500825 add_to_order = 1;
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200826 e = DELTA_CHILD(e);
Dan McGeef3808722011-10-18 00:21:24 -0500827 } else {
828 add_to_order = 0;
829 /* our sibling might have some children, it is next */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200830 if (DELTA_SIBLING(e)) {
831 e = DELTA_SIBLING(e);
Dan McGeef3808722011-10-18 00:21:24 -0500832 continue;
833 }
834 /* go back to our parent node */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200835 e = DELTA(e);
836 while (e && !DELTA_SIBLING(e)) {
Dan McGeef3808722011-10-18 00:21:24 -0500837 /* we're on the right side of a subtree, keep
838 * going up until we can go right again */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200839 e = DELTA(e);
Dan McGeef3808722011-10-18 00:21:24 -0500840 }
841 if (!e) {
842 /* done- we hit our original root node */
843 return;
844 }
845 /* pass it off to sibling at this level */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200846 e = DELTA_SIBLING(e);
Dan McGeef3808722011-10-18 00:21:24 -0500847 }
848 };
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700849}
850
851static void add_family_to_write_order(struct object_entry **wo,
Dan McGee92bef1a2011-10-18 00:21:22 -0500852 unsigned int *endp,
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700853 struct object_entry *e)
854{
855 struct object_entry *root;
856
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200857 for (root = e; DELTA(root); root = DELTA(root))
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700858 ; /* nothing */
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700859 add_descendants_to_write_order(wo, endp, root);
860}
861
Christian Couderf64ba532018-08-16 08:13:08 +0200862static void compute_layer_order(struct object_entry **wo, unsigned int *wo_end)
863{
864 unsigned int i, last_untagged;
865 struct object_entry *objects = to_pack.objects;
866
867 for (i = 0; i < to_pack.nr_objects; i++) {
868 if (objects[i].tagged)
869 break;
870 add_to_write_order(wo, wo_end, &objects[i]);
871 }
872 last_untagged = i;
873
874 /*
875 * Then fill all the tagged tips.
876 */
877 for (; i < to_pack.nr_objects; i++) {
878 if (objects[i].tagged)
879 add_to_write_order(wo, wo_end, &objects[i]);
880 }
881
882 /*
883 * And then all remaining commits and tags.
884 */
885 for (i = last_untagged; i < to_pack.nr_objects; i++) {
886 if (oe_type(&objects[i]) != OBJ_COMMIT &&
887 oe_type(&objects[i]) != OBJ_TAG)
888 continue;
889 add_to_write_order(wo, wo_end, &objects[i]);
890 }
891
892 /*
893 * And then all the trees.
894 */
895 for (i = last_untagged; i < to_pack.nr_objects; i++) {
896 if (oe_type(&objects[i]) != OBJ_TREE)
897 continue;
898 add_to_write_order(wo, wo_end, &objects[i]);
899 }
900
901 /*
902 * Finally all the rest in really tight order
903 */
904 for (i = last_untagged; i < to_pack.nr_objects; i++) {
Christian Couderfe0ac2f2018-08-16 08:13:13 +0200905 if (!objects[i].filled && oe_layer(&to_pack, &objects[i]) == write_layer)
Christian Couderf64ba532018-08-16 08:13:08 +0200906 add_family_to_write_order(wo, wo_end, &objects[i]);
907 }
908}
909
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700910static struct object_entry **compute_write_order(void)
911{
Jeff King28b8a732018-08-16 08:13:09 +0200912 uint32_t max_layers = 1;
Christian Couderf64ba532018-08-16 08:13:08 +0200913 unsigned int i, wo_end;
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700914
Jeff Kingb32fa952016-02-22 17:44:25 -0500915 struct object_entry **wo;
Vicent Marti2834bc22013-10-24 14:01:06 -0400916 struct object_entry *objects = to_pack.objects;
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700917
Vicent Marti2834bc22013-10-24 14:01:06 -0400918 for (i = 0; i < to_pack.nr_objects; i++) {
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700919 objects[i].tagged = 0;
920 objects[i].filled = 0;
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200921 SET_DELTA_CHILD(&objects[i], NULL);
922 SET_DELTA_SIBLING(&objects[i], NULL);
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700923 }
924
925 /*
926 * Fully connect delta_child/delta_sibling network.
927 * Make sure delta_sibling is sorted in the original
928 * recency order.
929 */
Vicent Marti2834bc22013-10-24 14:01:06 -0400930 for (i = to_pack.nr_objects; i > 0;) {
Dan McGee92bef1a2011-10-18 00:21:22 -0500931 struct object_entry *e = &objects[--i];
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200932 if (!DELTA(e))
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700933 continue;
934 /* Mark me as the first child */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200935 e->delta_sibling_idx = DELTA(e)->delta_child_idx;
936 SET_DELTA_CHILD(DELTA(e), e);
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700937 }
938
939 /*
940 * Mark objects that are at the tip of tags.
941 */
Michael Haggertyd1552542015-05-25 18:38:38 +0000942 for_each_tag_ref(mark_tagged, NULL);
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700943
Eric Wong647982b2023-02-03 23:44:30 +0000944 if (use_delta_islands) {
Jeff King28b8a732018-08-16 08:13:09 +0200945 max_layers = compute_pack_layers(&to_pack);
Eric Wong647982b2023-02-03 23:44:30 +0000946 free_island_marks();
947 }
Jeff King28b8a732018-08-16 08:13:09 +0200948
Jeff Kingb32fa952016-02-22 17:44:25 -0500949 ALLOC_ARRAY(wo, to_pack.nr_objects);
Christian Couderf64ba532018-08-16 08:13:08 +0200950 wo_end = 0;
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700951
Jeff King28b8a732018-08-16 08:13:09 +0200952 for (; write_layer < max_layers; ++write_layer)
953 compute_layer_order(wo, &wo_end);
Dan McGee38d4deb2011-10-18 00:21:23 -0500954
Vicent Marti2834bc22013-10-24 14:01:06 -0400955 if (wo_end != to_pack.nr_objects)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +0200956 die(_("ordered %u objects, expected %"PRIu32),
957 wo_end, to_pack.nr_objects);
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700958
959 return wo;
960}
961
Jeff Kingbb514de2019-12-18 12:25:45 +0100962
963/*
964 * A reused set of objects. All objects in a chunk have the same
965 * relative position in the original packfile and the generated
966 * packfile.
967 */
968
969static struct reused_chunk {
970 /* The offset of the first object of this chunk in the original
971 * packfile. */
972 off_t original;
Han Xinbf120132021-03-23 11:20:50 +0800973 /* The difference for "original" minus the offset of the first object of
974 * this chunk in the generated packfile. */
Jeff Kingbb514de2019-12-18 12:25:45 +0100975 off_t difference;
976} *reused_chunks;
977static int reused_chunks_nr;
978static int reused_chunks_alloc;
979
980static void record_reused_object(off_t where, off_t offset)
Vicent Marti6b8fda22013-12-21 09:00:09 -0500981{
Jeff Kingbb514de2019-12-18 12:25:45 +0100982 if (reused_chunks_nr && reused_chunks[reused_chunks_nr-1].difference == offset)
983 return;
Vicent Marti6b8fda22013-12-21 09:00:09 -0500984
Jeff Kingbb514de2019-12-18 12:25:45 +0100985 ALLOC_GROW(reused_chunks, reused_chunks_nr + 1,
986 reused_chunks_alloc);
987 reused_chunks[reused_chunks_nr].original = where;
988 reused_chunks[reused_chunks_nr].difference = offset;
989 reused_chunks_nr++;
990}
Vicent Marti6b8fda22013-12-21 09:00:09 -0500991
Jeff Kingbb514de2019-12-18 12:25:45 +0100992/*
993 * Binary search to find the chunk that "where" is in. Note
994 * that we're not looking for an exact match, just the first
995 * chunk that contains it (which implicitly ends at the start
996 * of the next chunk.
997 */
998static off_t find_reused_offset(off_t where)
999{
1000 int lo = 0, hi = reused_chunks_nr;
1001 while (lo < hi) {
1002 int mi = lo + ((hi - lo) / 2);
1003 if (where == reused_chunks[mi].original)
1004 return reused_chunks[mi].difference;
1005 if (where < reused_chunks[mi].original)
1006 hi = mi;
1007 else
1008 lo = mi + 1;
Vicent Marti6b8fda22013-12-21 09:00:09 -05001009 }
1010
Jeff Kingbb514de2019-12-18 12:25:45 +01001011 /*
1012 * The first chunk starts at zero, so we can't have gone below
1013 * there.
1014 */
1015 assert(lo);
1016 return reused_chunks[lo-1].difference;
1017}
1018
Taylor Blau5e29c3f2023-12-14 17:24:07 -05001019static void write_reused_pack_one(struct packed_git *reuse_packfile,
1020 size_t pos, struct hashfile *out,
Taylor Blaud1d701e2023-12-14 17:24:09 -05001021 off_t pack_start,
Jeff Kingbb514de2019-12-18 12:25:45 +01001022 struct pack_window **w_curs)
1023{
1024 off_t offset, next, cur;
1025 enum object_type type;
1026 unsigned long size;
1027
Taylor Blau66cbd3e2021-01-13 17:23:39 -05001028 offset = pack_pos_to_offset(reuse_packfile, pos);
1029 next = pack_pos_to_offset(reuse_packfile, pos + 1);
Jeff Kingbb514de2019-12-18 12:25:45 +01001030
Taylor Blaud1d701e2023-12-14 17:24:09 -05001031 record_reused_object(offset,
1032 offset - (hashfile_total(out) - pack_start));
Jeff Kingbb514de2019-12-18 12:25:45 +01001033
1034 cur = offset;
1035 type = unpack_object_header(reuse_packfile, w_curs, &cur, &size);
1036 assert(type >= 0);
1037
1038 if (type == OBJ_OFS_DELTA) {
1039 off_t base_offset;
1040 off_t fixup;
1041
1042 unsigned char header[MAX_PACK_OBJECT_HEADER];
1043 unsigned len;
1044
1045 base_offset = get_delta_base(reuse_packfile, w_curs, &cur, type, offset);
1046 assert(base_offset != 0);
1047
1048 /* Convert to REF_DELTA if we must... */
1049 if (!allow_ofs_delta) {
Taylor Blau66cbd3e2021-01-13 17:23:39 -05001050 uint32_t base_pos;
Jeff Kingf66d4e02020-02-23 23:31:22 -05001051 struct object_id base_oid;
1052
Taylor Blau66cbd3e2021-01-13 17:23:39 -05001053 if (offset_to_pack_pos(reuse_packfile, base_offset, &base_pos) < 0)
1054 die(_("expected object at offset %"PRIuMAX" "
1055 "in pack %s"),
1056 (uintmax_t)base_offset,
1057 reuse_packfile->pack_name);
1058
Jeff Kingf66d4e02020-02-23 23:31:22 -05001059 nth_packed_object_id(&base_oid, reuse_packfile,
Taylor Blau66cbd3e2021-01-13 17:23:39 -05001060 pack_pos_to_index(reuse_packfile, base_pos));
Jeff Kingbb514de2019-12-18 12:25:45 +01001061
1062 len = encode_in_pack_object_header(header, sizeof(header),
1063 OBJ_REF_DELTA, size);
1064 hashwrite(out, header, len);
Junio C Hamanof8cb64e2020-03-26 17:11:20 -07001065 hashwrite(out, base_oid.hash, the_hash_algo->rawsz);
Jeff Kingbb514de2019-12-18 12:25:45 +01001066 copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur);
1067 return;
1068 }
1069
1070 /* Otherwise see if we need to rewrite the offset... */
1071 fixup = find_reused_offset(offset) -
1072 find_reused_offset(base_offset);
1073 if (fixup) {
1074 unsigned char ofs_header[10];
1075 unsigned i, ofs_len;
1076 off_t ofs = offset - base_offset - fixup;
1077
1078 len = encode_in_pack_object_header(header, sizeof(header),
1079 OBJ_OFS_DELTA, size);
1080
1081 i = sizeof(ofs_header) - 1;
1082 ofs_header[i] = ofs & 127;
1083 while (ofs >>= 7)
1084 ofs_header[--i] = 128 | (--ofs & 127);
1085
1086 ofs_len = sizeof(ofs_header) - i;
1087
1088 hashwrite(out, header, len);
1089 hashwrite(out, ofs_header + sizeof(ofs_header) - ofs_len, ofs_len);
1090 copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur);
1091 return;
1092 }
1093
1094 /* ...otherwise we have no fixup, and can write it verbatim */
1095 }
1096
1097 copy_pack_data(out, reuse_packfile, w_curs, offset, next - offset);
1098}
1099
Taylor Blau073b40e2023-12-14 17:24:12 -05001100static size_t write_reused_pack_verbatim(struct bitmapped_pack *reuse_packfile,
Taylor Blau5e29c3f2023-12-14 17:24:07 -05001101 struct hashfile *out,
Taylor Blauca0fd692023-12-14 17:24:17 -05001102 off_t pack_start,
Jeff Kingbb514de2019-12-18 12:25:45 +01001103 struct pack_window **w_curs)
1104{
Taylor Blauca0fd692023-12-14 17:24:17 -05001105 size_t pos = reuse_packfile->bitmap_pos;
1106 size_t end;
Jeff Kingbb514de2019-12-18 12:25:45 +01001107
Taylor Blauca0fd692023-12-14 17:24:17 -05001108 if (pos % BITS_IN_EWORD) {
1109 size_t word_pos = (pos / BITS_IN_EWORD);
1110 size_t offset = pos % BITS_IN_EWORD;
1111 size_t last;
1112 eword_t word = reuse_packfile_bitmap->words[word_pos];
Jeff Kingbb514de2019-12-18 12:25:45 +01001113
Taylor Blauca0fd692023-12-14 17:24:17 -05001114 if (offset + reuse_packfile->bitmap_nr < BITS_IN_EWORD)
1115 last = offset + reuse_packfile->bitmap_nr;
1116 else
1117 last = BITS_IN_EWORD;
Jeff Kingbb514de2019-12-18 12:25:45 +01001118
Taylor Blauca0fd692023-12-14 17:24:17 -05001119 for (; offset < last; offset++) {
1120 if (word >> offset == 0)
1121 return word_pos;
1122 if (!bitmap_get(reuse_packfile_bitmap,
1123 word_pos * BITS_IN_EWORD + offset))
1124 return word_pos;
1125 }
1126
1127 pos += BITS_IN_EWORD - (pos % BITS_IN_EWORD);
1128 }
1129
1130 /*
1131 * Now we're going to copy as many whole eword_t's as possible.
1132 * "end" is the index of the last whole eword_t we copy, but
1133 * there may be additional bits to process. Those are handled
1134 * individually by write_reused_pack().
1135 *
1136 * Begin by advancing to the first word boundary in range of the
1137 * bit positions occupied by objects in "reuse_packfile". Then
1138 * pick the last word boundary in the same range. If we have at
1139 * least one word's worth of bits to process, continue on.
1140 */
1141 end = reuse_packfile->bitmap_pos + reuse_packfile->bitmap_nr;
1142 if (end % BITS_IN_EWORD)
1143 end -= end % BITS_IN_EWORD;
1144 if (pos >= end)
1145 return reuse_packfile->bitmap_pos / BITS_IN_EWORD;
1146
1147 while (pos < end &&
1148 reuse_packfile_bitmap->words[pos / BITS_IN_EWORD] == (eword_t)~0)
1149 pos += BITS_IN_EWORD;
1150
1151 if (pos > end)
1152 pos = end;
1153
1154 if (reuse_packfile->bitmap_pos < pos) {
1155 off_t pack_start_off = pack_pos_to_offset(reuse_packfile->p, 0);
1156 off_t pack_end_off = pack_pos_to_offset(reuse_packfile->p,
1157 pos - reuse_packfile->bitmap_pos);
1158
1159 written += pos - reuse_packfile->bitmap_pos;
Jeff Kingbb514de2019-12-18 12:25:45 +01001160
1161 /* We're recording one chunk, not one object. */
Taylor Blauca0fd692023-12-14 17:24:17 -05001162 record_reused_object(pack_start_off,
1163 pack_start_off - (hashfile_total(out) - pack_start));
Jeff Kingbb514de2019-12-18 12:25:45 +01001164 hashflush(out);
Taylor Blau073b40e2023-12-14 17:24:12 -05001165 copy_pack_data(out, reuse_packfile->p, w_curs,
Taylor Blauca0fd692023-12-14 17:24:17 -05001166 pack_start_off, pack_end_off - pack_start_off);
Jeff Kingbb514de2019-12-18 12:25:45 +01001167
1168 display_progress(progress_state, written);
1169 }
Taylor Blauca0fd692023-12-14 17:24:17 -05001170 if (pos % BITS_IN_EWORD)
1171 BUG("attempted to jump past a word boundary to %"PRIuMAX,
1172 (uintmax_t)pos);
1173 return pos / BITS_IN_EWORD;
Jeff Kingbb514de2019-12-18 12:25:45 +01001174}
1175
Taylor Blau073b40e2023-12-14 17:24:12 -05001176static void write_reused_pack(struct bitmapped_pack *reuse_packfile,
Taylor Blau5e29c3f2023-12-14 17:24:07 -05001177 struct hashfile *f)
Jeff Kingbb514de2019-12-18 12:25:45 +01001178{
Taylor Blau48051252023-12-14 17:24:14 -05001179 size_t i = reuse_packfile->bitmap_pos / BITS_IN_EWORD;
Jeff Kingbb514de2019-12-18 12:25:45 +01001180 uint32_t offset;
Taylor Blaud1d701e2023-12-14 17:24:09 -05001181 off_t pack_start = hashfile_total(f) - sizeof(struct pack_header);
Jeff Kingbb514de2019-12-18 12:25:45 +01001182 struct pack_window *w_curs = NULL;
1183
1184 if (allow_ofs_delta)
Taylor Blaud1d701e2023-12-14 17:24:09 -05001185 i = write_reused_pack_verbatim(reuse_packfile, f, pack_start,
1186 &w_curs);
Jeff Kingbb514de2019-12-18 12:25:45 +01001187
1188 for (; i < reuse_packfile_bitmap->word_alloc; ++i) {
1189 eword_t word = reuse_packfile_bitmap->words[i];
1190 size_t pos = (i * BITS_IN_EWORD);
1191
1192 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1193 if ((word >> offset) == 0)
1194 break;
1195
1196 offset += ewah_bit_ctz64(word >> offset);
Taylor Blau48051252023-12-14 17:24:14 -05001197 if (pos + offset < reuse_packfile->bitmap_pos)
1198 continue;
1199 if (pos + offset >= reuse_packfile->bitmap_pos + reuse_packfile->bitmap_nr)
1200 goto done;
Taylor Blau0f533c72021-08-31 16:52:21 -04001201 /*
1202 * Can use bit positions directly, even for MIDX
1203 * bitmaps. See comment in try_partial_reuse()
1204 * for why.
1205 */
Taylor Blau48051252023-12-14 17:24:14 -05001206 write_reused_pack_one(reuse_packfile->p,
1207 pos + offset - reuse_packfile->bitmap_pos,
Taylor Blau073b40e2023-12-14 17:24:12 -05001208 f, pack_start, &w_curs);
Jeff Kingbb514de2019-12-18 12:25:45 +01001209 display_progress(progress_state, ++written);
1210 }
1211 }
1212
Taylor Blau48051252023-12-14 17:24:14 -05001213done:
Jeff Kingbb514de2019-12-18 12:25:45 +01001214 unuse_pack(&w_curs);
Vicent Marti6b8fda22013-12-21 09:00:09 -05001215}
1216
Jonathan Tandd4b7322020-06-10 13:57:23 -07001217static void write_excluded_by_configs(void)
1218{
1219 struct oidset_iter iter;
1220 const struct object_id *oid;
1221
1222 oidset_iter_init(&excluded_by_config, &iter);
1223 while ((oid = oidset_iter_next(&iter))) {
1224 struct configured_exclusion *ex =
1225 oidmap_get(&configured_exclusions, oid);
1226
1227 if (!ex)
1228 BUG("configured exclusion wasn't configured");
1229 write_in_full(1, ex->pack_hash_hex, strlen(ex->pack_hash_hex));
1230 write_in_full(1, " ", 1);
1231 write_in_full(1, ex->uri, strlen(ex->uri));
1232 write_in_full(1, "\n", 1);
1233 }
1234}
1235
Eric Wong9cea46c2016-04-28 07:28:55 +00001236static const char no_split_warning[] = N_(
1237"disabling bitmap writing, packs are split due to pack.packSizeLimit"
1238);
1239
Dana L. Howd01fb922007-05-13 11:34:56 -07001240static void write_pack_file(void)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001241{
Dana L. Howebe27b12007-05-13 12:09:16 -07001242 uint32_t i = 0, j;
brian m. carlson98a3bea2018-02-01 02:18:46 +00001243 struct hashfile *f;
Nicolas Pitre6ed7f252008-08-29 16:07:58 -04001244 off_t offset;
Dana L. Howebe27b12007-05-13 12:09:16 -07001245 uint32_t nr_remaining = nr_result;
Nicolas Pitref746bae2008-03-13 14:59:29 -04001246 time_t last_mtime = 0;
Junio C Hamano1b4bb162011-06-30 16:21:58 -07001247 struct object_entry **write_order;
Nicolas Pitre81a216a2007-04-16 12:31:05 -04001248
Nicolas Pitrebcd79542008-05-02 15:11:47 -04001249 if (progress > pack_to_stdout)
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +07001250 progress_state = start_progress(_("Writing objects"), nr_result);
Jeff Kingb32fa952016-02-22 17:44:25 -05001251 ALLOC_ARRAY(written_list, to_pack.nr_objects);
Junio C Hamano1b4bb162011-06-30 16:21:58 -07001252 write_order = compute_write_order();
Junio C Hamano183bdb22006-02-22 16:02:59 -08001253
Dana L. Howebe27b12007-05-13 12:09:16 -07001254 do {
brian m. carlson71b76722021-04-26 01:02:59 +00001255 unsigned char hash[GIT_MAX_RAWSZ];
Nicolas Pitre7ba502c2007-10-16 21:55:48 -04001256 char *pack_tmp_name = NULL;
Geert Boschaa7e44b2007-06-01 15:18:05 -04001257
Junio C Hamanocdf9db32011-10-28 11:52:14 -07001258 if (pack_to_stdout)
brian m. carlson98a3bea2018-02-01 02:18:46 +00001259 f = hashfd_throughput(1, "<stdout>", progress_state);
Junio C Hamanocdf9db32011-10-28 11:52:14 -07001260 else
1261 f = create_tmp_packfile(&pack_tmp_name);
Nicolas Pitrec553ca22007-04-09 01:06:33 -04001262
Junio C Hamanoc0ad4652011-10-28 11:40:48 -07001263 offset = write_pack_header(f, nr_remaining);
Vicent Marti6b8fda22013-12-21 09:00:09 -05001264
Taylor Blau073b40e2023-12-14 17:24:12 -05001265 if (reuse_packfiles_nr) {
Vicent Marti6b8fda22013-12-21 09:00:09 -05001266 assert(pack_to_stdout);
Taylor Blau073b40e2023-12-14 17:24:12 -05001267 for (j = 0; j < reuse_packfiles_nr; j++) {
1268 reused_chunks_nr = 0;
1269 write_reused_pack(&reuse_packfiles[j], f);
Taylor Blaub96289a2023-12-14 17:24:20 -05001270 if (reused_chunks_nr)
1271 reuse_packfiles_used_nr++;
Taylor Blau073b40e2023-12-14 17:24:12 -05001272 }
Jeff Kingbb514de2019-12-18 12:25:45 +01001273 offset = hashfile_total(f);
Vicent Marti6b8fda22013-12-21 09:00:09 -05001274 }
1275
Dana L. Howebe27b12007-05-13 12:09:16 -07001276 nr_written = 0;
Vicent Marti2834bc22013-10-24 14:01:06 -04001277 for (; i < to_pack.nr_objects; i++) {
Junio C Hamano1b4bb162011-06-30 16:21:58 -07001278 struct object_entry *e = write_order[i];
Junio C Hamanocddec4f2011-12-05 15:19:34 -08001279 if (write_one(f, e, &offset) == WRITE_ONE_BREAK)
Nicolas Pitre720c9f72010-02-08 10:39:01 -05001280 break;
1281 display_progress(progress_state, written);
1282 }
Dana L. Howebe27b12007-05-13 12:09:16 -07001283
Linus Torvalds54352bb2008-05-30 08:54:46 -07001284 if (pack_to_stdout) {
Neeraj Singh020406e2022-03-10 22:43:21 +00001285 /*
1286 * We never fsync when writing to stdout since we may
1287 * not be writing to an actual pack file. For instance,
1288 * the upload-pack code passes a pipe here. Calling
1289 * fsync on a pipe results in unnecessary
1290 * synchronization with the reader on some platforms.
1291 */
1292 finalize_hashfile(f, hash, FSYNC_COMPONENT_NONE,
1293 CSUM_HASH_IN_STREAM | CSUM_CLOSE);
Linus Torvalds54352bb2008-05-30 08:54:46 -07001294 } else if (nr_written == nr_remaining) {
Neeraj Singh020406e2022-03-10 22:43:21 +00001295 finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK,
1296 CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
Dana L. Howebe27b12007-05-13 12:09:16 -07001297 } else {
Neeraj Singh020406e2022-03-10 22:43:21 +00001298 /*
1299 * If we wrote the wrong number of entries in the
1300 * header, rewrite it like in fast-import.
1301 */
1302
1303 int fd = finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK, 0);
brian m. carlson71b76722021-04-26 01:02:59 +00001304 fixup_pack_header_footer(fd, hash, pack_tmp_name,
1305 nr_written, hash, offset);
Nicolas Pitre7ba502c2007-10-16 21:55:48 -04001306 close(fd);
Eric Wong9cea46c2016-04-28 07:28:55 +00001307 if (write_bitmap_index) {
Jeff King25575012019-07-31 01:39:27 -04001308 if (write_bitmap_index != WRITE_BITMAP_QUIET)
1309 warning(_(no_split_warning));
Eric Wong9cea46c2016-04-28 07:28:55 +00001310 write_bitmap_index = 0;
1311 }
Dana L. Howebe27b12007-05-13 12:09:16 -07001312 }
1313
1314 if (!pack_to_stdout) {
Nicolas Pitref746bae2008-03-13 14:59:29 -04001315 struct stat st;
Sun He58892712014-03-03 17:24:29 +08001316 struct strbuf tmpname = STRBUF_INIT;
Ævar Arnfjörð Bjarmason2ec02dd2021-09-09 19:24:56 -04001317 char *idx_tmp_name = NULL;
Dana L. Howd01fb922007-05-13 11:34:56 -07001318
Nicolas Pitref746bae2008-03-13 14:59:29 -04001319 /*
1320 * Packs are runtime accessed in their mtime
1321 * order since newer packs are more likely to contain
1322 * younger objects. So if we are creating multiple
1323 * packs then we should modify the mtime of later ones
1324 * to preserve this property.
1325 */
Junio C Hamano0e990532011-10-28 12:34:09 -07001326 if (stat(pack_tmp_name, &st) < 0) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02001327 warning_errno(_("failed to stat %s"), pack_tmp_name);
Nicolas Pitref746bae2008-03-13 14:59:29 -04001328 } else if (!last_mtime) {
1329 last_mtime = st.st_mtime;
1330 } else {
1331 struct utimbuf utb;
1332 utb.actime = st.st_atime;
1333 utb.modtime = --last_mtime;
Junio C Hamano0e990532011-10-28 12:34:09 -07001334 if (utime(pack_tmp_name, &utb) < 0)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02001335 warning_errno(_("failed utime() on %s"), pack_tmp_name);
Nicolas Pitref746bae2008-03-13 14:59:29 -04001336 }
1337
Ævar Arnfjörð Bjarmason66833f02021-09-09 19:24:37 -04001338 strbuf_addf(&tmpname, "%s-%s.", base_name,
1339 hash_to_hex(hash));
Vicent Marti7cc8f972013-12-21 09:00:16 -05001340
1341 if (write_bitmap_index) {
brian m. carlson71b76722021-04-26 01:02:59 +00001342 bitmap_writer_set_checksum(hash);
Nguyễn Thái Ngọc Duy06af3bb2018-04-14 17:35:04 +02001343 bitmap_writer_build_type_index(
1344 &to_pack, written_list, nr_written);
Vicent Marti7cc8f972013-12-21 09:00:16 -05001345 }
1346
Taylor Blaub7573532022-05-20 19:17:52 -04001347 if (cruft)
1348 pack_idx_opts.flags |= WRITE_MTIMES;
1349
Ævar Arnfjörð Bjarmason2ec02dd2021-09-09 19:24:56 -04001350 stage_tmp_packfiles(&tmpname, pack_tmp_name,
Junio C Hamano0e990532011-10-28 12:34:09 -07001351 written_list, nr_written,
Taylor Blau1c573cd2022-05-20 19:17:38 -04001352 &to_pack, &pack_idx_opts, hash,
1353 &idx_tmp_name);
Vicent Marti7cc8f972013-12-21 09:00:16 -05001354
1355 if (write_bitmap_index) {
Ævar Arnfjörð Bjarmason66833f02021-09-09 19:24:37 -04001356 size_t tmpname_len = tmpname.len;
Vicent Marti7cc8f972013-12-21 09:00:16 -05001357
Ævar Arnfjörð Bjarmason66833f02021-09-09 19:24:37 -04001358 strbuf_addstr(&tmpname, "bitmap");
Vicent Marti7cc8f972013-12-21 09:00:16 -05001359 stop_progress(&progress_state);
1360
1361 bitmap_writer_show_progress(progress);
Vicent Marti7cc8f972013-12-21 09:00:16 -05001362 bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1);
Taylor Blau3ba3d062021-08-24 12:15:54 -04001363 if (bitmap_writer_build(&to_pack) < 0)
1364 die(_("failed to write bitmap index"));
Vicent Martiae4f07f2013-12-21 09:00:45 -05001365 bitmap_writer_finish(written_list, nr_written,
Sun He58892712014-03-03 17:24:29 +08001366 tmpname.buf, write_bitmap_options);
Vicent Marti7cc8f972013-12-21 09:00:16 -05001367 write_bitmap_index = 0;
Ævar Arnfjörð Bjarmason66833f02021-09-09 19:24:37 -04001368 strbuf_setlen(&tmpname, tmpname_len);
Vicent Marti7cc8f972013-12-21 09:00:16 -05001369 }
1370
Ævar Arnfjörð Bjarmason4bc1fd62021-09-09 19:25:00 -04001371 rename_tmp_packfile_idx(&tmpname, &idx_tmp_name);
1372
Ævar Arnfjörð Bjarmason2ec02dd2021-09-09 19:24:56 -04001373 free(idx_tmp_name);
Sun He58892712014-03-03 17:24:29 +08001374 strbuf_release(&tmpname);
Nicolas Pitre7ba502c2007-10-16 21:55:48 -04001375 free(pack_tmp_name);
brian m. carlson71b76722021-04-26 01:02:59 +00001376 puts(hash_to_hex(hash));
Dana L. Howebe27b12007-05-13 12:09:16 -07001377 }
1378
1379 /* mark written objects as written to previous pack */
1380 for (j = 0; j < nr_written; j++) {
Nicolas Pitre79814f42007-11-01 23:43:24 -04001381 written_list[j]->offset = (off_t)-1;
Dana L. Howebe27b12007-05-13 12:09:16 -07001382 }
1383 nr_remaining -= nr_written;
Vicent Marti2834bc22013-10-24 14:01:06 -04001384 } while (nr_remaining && i < to_pack.nr_objects);
Dana L. Howebe27b12007-05-13 12:09:16 -07001385
1386 free(written_list);
Junio C Hamano1b4bb162011-06-30 16:21:58 -07001387 free(write_order);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -04001388 stop_progress(&progress_state);
Nicolas Pitre67c08ce2006-11-29 17:15:48 -05001389 if (written != nr_result)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02001390 die(_("wrote %"PRIu32" objects while expecting %"PRIu32),
1391 written, nr_result);
Jonathan Tan9ed87902019-04-11 10:36:26 -07001392 trace2_data_intmax("pack-objects", the_repository,
1393 "write_pack_file/wrote", nr_result);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001394}
1395
Junio C Hamanoa74db822007-05-19 00:39:31 -07001396static int no_try_delta(const char *path)
1397{
Junio C Hamano2aef63d2017-01-27 18:01:57 -08001398 static struct attr_check *check;
Junio C Hamanoa74db822007-05-19 00:39:31 -07001399
Junio C Hamano2aef63d2017-01-27 18:01:57 -08001400 if (!check)
1401 check = attr_check_initl("delta", NULL);
John Cai44451a22023-05-06 04:15:29 +00001402 git_check_attr(the_repository->index, path, check);
Junio C Hamano2aef63d2017-01-27 18:01:57 -08001403 if (ATTR_FALSE(check->items[0].value))
Junio C Hamanoa74db822007-05-19 00:39:31 -07001404 return 1;
1405 return 0;
1406}
1407
Jeff Kingce2bc422013-12-21 09:00:06 -05001408/*
1409 * When adding an object, check whether we have already added it
1410 * to our packing list. If so, we can skip. However, if we are
1411 * being asked to excludei t, but the previous mention was to include
1412 * it, make sure to adjust its flags and tweak our numbers accordingly.
1413 *
1414 * As an optimization, we pass out the index position where we would have
1415 * found the item, since that saves us from having to look it up again a
1416 * few lines later when we want to add the new entry.
1417 */
brian m. carlson188960b2017-10-15 22:07:01 +00001418static int have_duplicate_entry(const struct object_id *oid,
Jeff King3a378762019-09-05 21:36:05 -04001419 int exclude)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001420{
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001421 struct object_entry *entry;
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001422
Jeff King92fb0db2019-12-18 12:25:46 +01001423 if (reuse_packfile_bitmap &&
1424 bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid))
1425 return 1;
1426
Jeff King3a378762019-09-05 21:36:05 -04001427 entry = packlist_find(&to_pack, oid);
Jeff Kingce2bc422013-12-21 09:00:06 -05001428 if (!entry)
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001429 return 0;
Jeff Kingce2bc422013-12-21 09:00:06 -05001430
1431 if (exclude) {
1432 if (!entry->preferred_base)
1433 nr_result--;
1434 entry->preferred_base = 1;
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001435 }
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001436
Jeff Kingce2bc422013-12-21 09:00:06 -05001437 return 1;
1438}
1439
Jeff King6325da12021-02-22 21:25:20 -05001440static int want_found_object(const struct object_id *oid, int exclude,
1441 struct packed_git *p)
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001442{
1443 if (exclude)
1444 return 1;
1445 if (incremental)
1446 return 0;
1447
Taylor Blau40905112022-05-24 14:54:36 -04001448 if (!is_pack_valid(p))
1449 return -1;
1450
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001451 /*
1452 * When asked to do --local (do not include an object that appears in a
1453 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
1454 * an object that appears in a pack marked with .keep), finding a pack
1455 * that matches the criteria is sufficient for us to decide to omit it.
1456 * However, even if this pack does not satisfy the criteria, we need to
1457 * make sure no copy of this object appears in _any_ pack that makes us
1458 * to omit the object, so we need to check all the packs.
1459 *
Jeff King6325da12021-02-22 21:25:20 -05001460 * We can however first check whether these options can possibly matter;
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001461 * if they do not matter we know we want the object in generated pack.
1462 * Otherwise, we signal "-1" at the end to tell the caller that we do
1463 * not know either way, and it needs to check more packs.
1464 */
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001465
Jeff King6325da12021-02-22 21:25:20 -05001466 /*
1467 * Objects in packs borrowed from elsewhere are discarded regardless of
1468 * if they appear in other packs that weren't borrowed.
1469 */
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001470 if (local && !p->pack_local)
1471 return 0;
Jeff King6325da12021-02-22 21:25:20 -05001472
1473 /*
1474 * Then handle .keep first, as we have a fast(er) path there.
1475 */
1476 if (ignore_packed_keep_on_disk || ignore_packed_keep_in_core) {
1477 /*
1478 * Set the flags for the kept-pack cache to be the ones we want
1479 * to ignore.
1480 *
1481 * That is, if we are ignoring objects in on-disk keep packs,
1482 * then we want to search through the on-disk keep and ignore
1483 * the in-core ones.
1484 */
1485 unsigned flags = 0;
1486 if (ignore_packed_keep_on_disk)
1487 flags |= ON_DISK_KEEP_PACKS;
1488 if (ignore_packed_keep_in_core)
1489 flags |= IN_CORE_KEEP_PACKS;
1490
1491 if (ignore_packed_keep_on_disk && p->pack_keep)
1492 return 0;
1493 if (ignore_packed_keep_in_core && p->pack_keep_in_core)
1494 return 0;
1495 if (has_object_kept_pack(oid, flags))
1496 return 0;
1497 }
1498
1499 /*
1500 * At this point we know definitively that either we don't care about
1501 * keep-packs, or the object is not in one. Keep checking other
1502 * conditions...
1503 */
1504 if (!local || !have_non_local_packs)
1505 return 1;
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001506
1507 /* we don't know yet; keep looking for more packs */
1508 return -1;
1509}
1510
Jeff King6325da12021-02-22 21:25:20 -05001511static int want_object_in_pack_one(struct packed_git *p,
1512 const struct object_id *oid,
1513 int exclude,
1514 struct packed_git **found_pack,
1515 off_t *found_offset)
1516{
1517 off_t offset;
1518
1519 if (p == *found_pack)
1520 offset = *found_offset;
1521 else
1522 offset = find_pack_entry_one(oid->hash, p);
1523
1524 if (offset) {
1525 if (!*found_pack) {
1526 if (!is_pack_valid(p))
1527 return -1;
1528 *found_offset = offset;
1529 *found_pack = p;
1530 }
1531 return want_found_object(oid, exclude, p);
1532 }
1533 return -1;
1534}
1535
Jeff Kingce2bc422013-12-21 09:00:06 -05001536/*
1537 * Check whether we want the object in the pack (e.g., we do not want
1538 * objects found in non-local stores if the "--local" option was used).
1539 *
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001540 * If the caller already knows an existing pack it wants to take the object
1541 * from, that is passed in *found_pack and *found_offset; otherwise this
1542 * function finds if there is any pack that has the object and returns the pack
1543 * and its offset in these variables.
Jeff Kingce2bc422013-12-21 09:00:06 -05001544 */
brian m. carlson188960b2017-10-15 22:07:01 +00001545static int want_object_in_pack(const struct object_id *oid,
Jeff Kingce2bc422013-12-21 09:00:06 -05001546 int exclude,
1547 struct packed_git **found_pack,
1548 off_t *found_offset)
1549{
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001550 int want;
Olga Telezhnaya88658592017-09-30 17:51:01 +00001551 struct list_head *pos;
Derrick Stolee6a22d522018-08-20 16:52:08 +00001552 struct multi_pack_index *m;
Jeff Kingce2bc422013-12-21 09:00:06 -05001553
brian m. carlson6862ebb2018-05-02 00:25:34 +00001554 if (!exclude && local && has_loose_object_nonlocal(oid))
Brandon Caseydaae0622008-11-09 23:59:58 -06001555 return 0;
1556
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001557 /*
1558 * If we already know the pack object lives in, start checks from that
1559 * pack - in the usual case when neither --local was given nor .keep files
1560 * are present we will determine the answer right now.
1561 */
1562 if (*found_pack) {
Jeff King6325da12021-02-22 21:25:20 -05001563 want = want_found_object(oid, exclude, *found_pack);
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001564 if (want != -1)
1565 return want;
Taylor Blau40905112022-05-24 14:54:36 -04001566
1567 *found_pack = NULL;
1568 *found_offset = 0;
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001569 }
Derrick Stolee6a22d522018-08-20 16:52:08 +00001570
1571 for (m = get_multi_pack_index(the_repository); m; m = m->next) {
1572 struct pack_entry e;
Derrick Stolee64404a22019-04-29 09:18:55 -07001573 if (fill_midx_entry(the_repository, oid, &e, m)) {
Jeff King6325da12021-02-22 21:25:20 -05001574 want = want_object_in_pack_one(e.p, oid, exclude, found_pack, found_offset);
1575 if (want != -1)
1576 return want;
Derrick Stolee6a22d522018-08-20 16:52:08 +00001577 }
1578 }
1579
Stefan Bellera80d72d2018-03-23 18:20:59 +01001580 list_for_each(pos, get_packed_git_mru(the_repository)) {
Gargi Sharmaec2dd322018-01-23 18:46:51 -05001581 struct packed_git *p = list_entry(pos, struct packed_git, mru);
Jeff King6325da12021-02-22 21:25:20 -05001582 want = want_object_in_pack_one(p, oid, exclude, found_pack, found_offset);
1583 if (!exclude && want > 0)
1584 list_move(&p->mru,
1585 get_packed_git_mru(the_repository));
1586 if (want != -1)
1587 return want;
Linus Torvalds64560372005-10-13 15:38:28 -07001588 }
Linus Torvaldseb019372005-07-03 13:08:40 -07001589
Jonathan Tandd4b7322020-06-10 13:57:23 -07001590 if (uri_protocols.nr) {
1591 struct configured_exclusion *ex =
1592 oidmap_get(&configured_exclusions, oid);
1593 int i;
1594 const char *p;
1595
1596 if (ex) {
1597 for (i = 0; i < uri_protocols.nr; i++) {
1598 if (skip_prefix(ex->uri,
1599 uri_protocols.items[i].string,
1600 &p) &&
1601 *p == ':') {
1602 oidset_insert(&excluded_by_config, oid);
1603 return 0;
1604 }
1605 }
1606 }
1607 }
1608
Jeff Kingce2bc422013-12-21 09:00:06 -05001609 return 1;
1610}
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001611
Taylor Blaufa230902022-05-20 19:17:49 -04001612static struct object_entry *create_object_entry(const struct object_id *oid,
1613 enum object_type type,
1614 uint32_t hash,
1615 int exclude,
1616 int no_try_delta,
1617 struct packed_git *found_pack,
1618 off_t found_offset)
Jeff Kingce2bc422013-12-21 09:00:06 -05001619{
1620 struct object_entry *entry;
1621
Jeff King3a378762019-09-05 21:36:05 -04001622 entry = packlist_alloc(&to_pack, oid);
Linus Torvalds27225f22005-06-26 15:27:28 -07001623 entry->hash = hash;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02001624 oe_set_type(entry, type);
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001625 if (exclude)
1626 entry->preferred_base = 1;
Nicolas Pitre81a216a2007-04-16 12:31:05 -04001627 else
1628 nr_result++;
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001629 if (found_pack) {
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02001630 oe_set_in_pack(&to_pack, entry, found_pack);
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001631 entry->in_pack_offset = found_offset;
1632 }
Junio C Hamano7a979d92006-02-19 14:47:21 -08001633
Jeff Kingce2bc422013-12-21 09:00:06 -05001634 entry->no_try_delta = no_try_delta;
Taylor Blaufa230902022-05-20 19:17:49 -04001635
1636 return entry;
Jeff Kingce2bc422013-12-21 09:00:06 -05001637}
Junio C Hamano7a979d92006-02-19 14:47:21 -08001638
Jeff King373c67d2014-03-14 22:38:29 -04001639static const char no_closure_warning[] = N_(
1640"disabling bitmap writing, as some objects are not being packed"
1641);
1642
brian m. carlson188960b2017-10-15 22:07:01 +00001643static int add_object_entry(const struct object_id *oid, enum object_type type,
Jeff Kingce2bc422013-12-21 09:00:06 -05001644 const char *name, int exclude)
1645{
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001646 struct packed_git *found_pack = NULL;
1647 off_t found_offset = 0;
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001648
Nguyễn Thái Ngọc Duy5af05042018-04-15 17:36:18 +02001649 display_progress(progress_state, ++nr_seen);
1650
Jeff King3a378762019-09-05 21:36:05 -04001651 if (have_duplicate_entry(oid, exclude))
Jeff Kingce2bc422013-12-21 09:00:06 -05001652 return 0;
Junio C Hamanoa74db822007-05-19 00:39:31 -07001653
brian m. carlson188960b2017-10-15 22:07:01 +00001654 if (!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {
Jeff King373c67d2014-03-14 22:38:29 -04001655 /* The pack is missing an object, so it will not have closure */
1656 if (write_bitmap_index) {
Jeff King25575012019-07-31 01:39:27 -04001657 if (write_bitmap_index != WRITE_BITMAP_QUIET)
1658 warning(_(no_closure_warning));
Jeff King373c67d2014-03-14 22:38:29 -04001659 write_bitmap_index = 0;
1660 }
Jeff Kingce2bc422013-12-21 09:00:06 -05001661 return 0;
Jeff King373c67d2014-03-14 22:38:29 -04001662 }
Jeff Kingce2bc422013-12-21 09:00:06 -05001663
brian m. carlson188960b2017-10-15 22:07:01 +00001664 create_object_entry(oid, type, pack_name_hash(name),
Jeff Kingce2bc422013-12-21 09:00:06 -05001665 exclude, name && no_try_delta(name),
Jeff King3a378762019-09-05 21:36:05 -04001666 found_pack, found_offset);
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001667 return 1;
Junio C Hamano7a979d92006-02-19 14:47:21 -08001668}
1669
brian m. carlson20664962017-10-15 22:07:00 +00001670static int add_object_entry_from_bitmap(const struct object_id *oid,
Vicent Marti6b8fda22013-12-21 09:00:09 -05001671 enum object_type type,
Jeff Kingc50dca22023-02-24 01:39:22 -05001672 int flags UNUSED, uint32_t name_hash,
Vicent Marti6b8fda22013-12-21 09:00:09 -05001673 struct packed_git *pack, off_t offset)
1674{
Nguyễn Thái Ngọc Duy5af05042018-04-15 17:36:18 +02001675 display_progress(progress_state, ++nr_seen);
1676
Jeff King3a378762019-09-05 21:36:05 -04001677 if (have_duplicate_entry(oid, 0))
Vicent Marti6b8fda22013-12-21 09:00:09 -05001678 return 0;
1679
brian m. carlson188960b2017-10-15 22:07:01 +00001680 if (!want_object_in_pack(oid, 0, &pack, &offset))
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001681 return 0;
1682
Jeff King3a378762019-09-05 21:36:05 -04001683 create_object_entry(oid, type, name_hash, 0, 0, pack, offset);
Junio C Hamano7a979d92006-02-19 14:47:21 -08001684 return 1;
1685}
1686
1687struct pbase_tree_cache {
brian m. carlson188960b2017-10-15 22:07:01 +00001688 struct object_id oid;
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001689 int ref;
1690 int temporary;
1691 void *tree_data;
1692 unsigned long tree_size;
1693};
1694
1695static struct pbase_tree_cache *(pbase_tree_cache[256]);
brian m. carlson188960b2017-10-15 22:07:01 +00001696static int pbase_tree_cache_ix(const struct object_id *oid)
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001697{
brian m. carlson188960b2017-10-15 22:07:01 +00001698 return oid->hash[0] % ARRAY_SIZE(pbase_tree_cache);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001699}
1700static int pbase_tree_cache_ix_incr(int ix)
1701{
1702 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
1703}
1704
1705static struct pbase_tree {
1706 struct pbase_tree *next;
1707 /* This is a phony "cache" entry; we are not
Justin Lebar01689902014-03-31 15:11:46 -07001708 * going to evict it or find it through _get()
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001709 * mechanism -- this is for the toplevel node that
1710 * would almost always change with any commit.
1711 */
1712 struct pbase_tree_cache pcache;
1713} *pbase_tree;
1714
brian m. carlson188960b2017-10-15 22:07:01 +00001715static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001716{
1717 struct pbase_tree_cache *ent, *nent;
1718 void *data;
1719 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001720 enum object_type type;
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001721 int neigh;
brian m. carlson188960b2017-10-15 22:07:01 +00001722 int my_ix = pbase_tree_cache_ix(oid);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001723 int available_ix = -1;
1724
1725 /* pbase-tree-cache acts as a limited hashtable.
1726 * your object will be found at your index or within a few
1727 * slots after that slot if it is cached.
1728 */
1729 for (neigh = 0; neigh < 8; neigh++) {
1730 ent = pbase_tree_cache[my_ix];
Jeff King4a7e27e2018-08-28 17:22:40 -04001731 if (ent && oideq(&ent->oid, oid)) {
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001732 ent->ref++;
1733 return ent;
1734 }
1735 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
1736 ((0 <= available_ix) &&
1737 (!ent && pbase_tree_cache[available_ix])))
1738 available_ix = my_ix;
1739 if (!ent)
1740 break;
1741 my_ix = pbase_tree_cache_ix_incr(my_ix);
1742 }
1743
1744 /* Did not find one. Either we got a bogus request or
1745 * we need to read and perhaps cache.
1746 */
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +02001747 data = repo_read_object_file(the_repository, oid, &type, &size);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001748 if (!data)
1749 return NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001750 if (type != OBJ_TREE) {
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001751 free(data);
1752 return NULL;
1753 }
1754
1755 /* We need to either cache or return a throwaway copy */
1756
1757 if (available_ix < 0)
1758 ent = NULL;
1759 else {
1760 ent = pbase_tree_cache[available_ix];
1761 my_ix = available_ix;
1762 }
1763
1764 if (!ent) {
1765 nent = xmalloc(sizeof(*nent));
1766 nent->temporary = (available_ix < 0);
1767 }
1768 else {
1769 /* evict and reuse */
1770 free(ent->tree_data);
1771 nent = ent;
1772 }
brian m. carlson188960b2017-10-15 22:07:01 +00001773 oidcpy(&nent->oid, oid);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001774 nent->tree_data = data;
1775 nent->tree_size = size;
1776 nent->ref = 1;
1777 if (!nent->temporary)
1778 pbase_tree_cache[my_ix] = nent;
1779 return nent;
1780}
1781
1782static void pbase_tree_put(struct pbase_tree_cache *cache)
1783{
1784 if (!cache->temporary) {
1785 cache->ref--;
1786 return;
1787 }
1788 free(cache->tree_data);
1789 free(cache);
1790}
1791
René Scharfee65b8682023-02-05 11:42:27 +01001792static size_t name_cmp_len(const char *name)
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001793{
René Scharfee65b8682023-02-05 11:42:27 +01001794 return strcspn(name, "\n/");
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001795}
1796
1797static void add_pbase_object(struct tree_desc *tree,
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001798 const char *name,
René Scharfee65b8682023-02-05 11:42:27 +01001799 size_t cmplen,
Linus Torvaldsce0bd642006-06-05 12:03:31 -07001800 const char *fullname)
Junio C Hamano7a979d92006-02-19 14:47:21 -08001801{
Linus Torvalds4c068a92006-05-30 09:45:45 -07001802 struct name_entry entry;
Nicolas Pitre8a5a8d62007-04-16 12:28:10 -04001803 int cmp;
Linus Torvalds4c068a92006-05-30 09:45:45 -07001804
1805 while (tree_entry(tree,&entry)) {
Linus Torvalds1211be62007-08-17 09:56:54 -07001806 if (S_ISGITLINK(entry.mode))
1807 continue;
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +11001808 cmp = tree_entry_len(&entry) != cmplen ? 1 :
Nicolas Pitre8a5a8d62007-04-16 12:28:10 -04001809 memcmp(name, entry.path, cmplen);
1810 if (cmp > 0)
Junio C Hamano7a979d92006-02-19 14:47:21 -08001811 continue;
Nicolas Pitre8a5a8d62007-04-16 12:28:10 -04001812 if (cmp < 0)
1813 return;
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001814 if (name[cmplen] != '/') {
brian m. carlsonea82b2a2019-01-15 00:39:44 +00001815 add_object_entry(&entry.oid,
Linus Torvalds4d1012c2007-11-11 23:35:23 +00001816 object_type(entry.mode),
Junio C Hamanobc32fed2007-05-19 00:19:23 -07001817 fullname, 1);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001818 return;
1819 }
Nicolas Pitre8a5a8d62007-04-16 12:28:10 -04001820 if (S_ISDIR(entry.mode)) {
Junio C Hamano7a979d92006-02-19 14:47:21 -08001821 struct tree_desc sub;
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001822 struct pbase_tree_cache *tree;
1823 const char *down = name+cmplen+1;
René Scharfee65b8682023-02-05 11:42:27 +01001824 size_t downlen = name_cmp_len(down);
Junio C Hamano1d6b38c2006-02-22 22:10:24 -08001825
brian m. carlsonea82b2a2019-01-15 00:39:44 +00001826 tree = pbase_tree_get(&entry.oid);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001827 if (!tree)
1828 return;
Eric W. Biedermanefed6872023-10-01 21:40:28 -05001829 init_tree_desc(&sub, &tree->oid,
1830 tree->tree_data, tree->tree_size);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001831
Linus Torvaldsce0bd642006-06-05 12:03:31 -07001832 add_pbase_object(&sub, down, downlen, fullname);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001833 pbase_tree_put(tree);
1834 }
1835 }
1836}
1837
1838static unsigned *done_pbase_paths;
1839static int done_pbase_paths_num;
1840static int done_pbase_paths_alloc;
1841static int done_pbase_path_pos(unsigned hash)
1842{
1843 int lo = 0;
1844 int hi = done_pbase_paths_num;
1845 while (lo < hi) {
Derrick Stolee19716b22017-10-08 14:29:37 -04001846 int mi = lo + (hi - lo) / 2;
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001847 if (done_pbase_paths[mi] == hash)
1848 return mi;
1849 if (done_pbase_paths[mi] < hash)
1850 hi = mi;
1851 else
1852 lo = mi + 1;
1853 }
1854 return -lo-1;
1855}
1856
1857static int check_pbase_path(unsigned hash)
1858{
René Scharfec7b07802017-07-20 22:32:56 +02001859 int pos = done_pbase_path_pos(hash);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001860 if (0 <= pos)
1861 return 1;
1862 pos = -pos - 1;
Dmitry S. Dolzhenko25e19402014-03-04 02:31:49 +04001863 ALLOC_GROW(done_pbase_paths,
1864 done_pbase_paths_num + 1,
1865 done_pbase_paths_alloc);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001866 done_pbase_paths_num++;
1867 if (pos < done_pbase_paths_num)
René Scharfef331ab92017-07-15 22:00:45 +02001868 MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,
1869 done_pbase_paths_num - pos - 1);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001870 done_pbase_paths[pos] = hash;
1871 return 0;
1872}
1873
Junio C Hamanobc32fed2007-05-19 00:19:23 -07001874static void add_preferred_base_object(const char *name)
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001875{
1876 struct pbase_tree *it;
René Scharfee65b8682023-02-05 11:42:27 +01001877 size_t cmplen;
Vicent Marti68fb36e2013-10-24 14:01:29 -04001878 unsigned hash = pack_name_hash(name);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001879
Nicolas Pitre8a5a8d62007-04-16 12:28:10 -04001880 if (!num_preferred_base || check_pbase_path(hash))
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001881 return;
1882
Nicolas Pitre8a5a8d62007-04-16 12:28:10 -04001883 cmplen = name_cmp_len(name);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001884 for (it = pbase_tree; it; it = it->next) {
1885 if (cmplen == 0) {
brian m. carlson188960b2017-10-15 22:07:01 +00001886 add_object_entry(&it->pcache.oid, OBJ_TREE, NULL, 1);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001887 }
1888 else {
1889 struct tree_desc tree;
Eric W. Biedermanefed6872023-10-01 21:40:28 -05001890 init_tree_desc(&tree, &it->pcache.oid,
1891 it->pcache.tree_data, it->pcache.tree_size);
Linus Torvaldsce0bd642006-06-05 12:03:31 -07001892 add_pbase_object(&tree, name, cmplen, name);
Junio C Hamano7a979d92006-02-19 14:47:21 -08001893 }
1894 }
1895}
1896
brian m. carlson188960b2017-10-15 22:07:01 +00001897static void add_preferred_base(struct object_id *oid)
Junio C Hamano7a979d92006-02-19 14:47:21 -08001898{
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001899 struct pbase_tree *it;
1900 void *data;
1901 unsigned long size;
brian m. carlson188960b2017-10-15 22:07:01 +00001902 struct object_id tree_oid;
Junio C Hamano1d6b38c2006-02-22 22:10:24 -08001903
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07001904 if (window <= num_preferred_base++)
1905 return;
1906
Nguyễn Thái Ngọc Duyd3b47052019-06-27 16:28:47 +07001907 data = read_object_with_reference(the_repository, oid,
Ævar Arnfjörð Bjarmason6aea6ba2022-02-05 00:48:34 +01001908 OBJ_TREE, &size, &tree_oid);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001909 if (!data)
Junio C Hamano7a979d92006-02-19 14:47:21 -08001910 return;
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001911
1912 for (it = pbase_tree; it; it = it->next) {
Jeff King4a7e27e2018-08-28 17:22:40 -04001913 if (oideq(&it->pcache.oid, &tree_oid)) {
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001914 free(data);
1915 return;
1916 }
1917 }
1918
René Scharfeca56dad2021-03-13 17:17:22 +01001919 CALLOC_ARRAY(it, 1);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001920 it->next = pbase_tree;
1921 pbase_tree = it;
1922
brian m. carlson188960b2017-10-15 22:07:01 +00001923 oidcpy(&it->pcache.oid, &tree_oid);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001924 it->pcache.tree_data = data;
1925 it->pcache.tree_size = size;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001926}
1927
Nicolas Pitre0ef95f72009-09-03 21:54:03 -04001928static void cleanup_preferred_base(void)
1929{
1930 struct pbase_tree *it;
1931 unsigned i;
1932
1933 it = pbase_tree;
1934 pbase_tree = NULL;
1935 while (it) {
Brandon Williams095b3b22018-02-14 10:59:26 -08001936 struct pbase_tree *tmp = it;
1937 it = tmp->next;
1938 free(tmp->pcache.tree_data);
1939 free(tmp);
Nicolas Pitre0ef95f72009-09-03 21:54:03 -04001940 }
1941
1942 for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) {
1943 if (!pbase_tree_cache[i])
1944 continue;
1945 free(pbase_tree_cache[i]->tree_data);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001946 FREE_AND_NULL(pbase_tree_cache[i]);
Nicolas Pitre0ef95f72009-09-03 21:54:03 -04001947 }
1948
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001949 FREE_AND_NULL(done_pbase_paths);
Nicolas Pitre0ef95f72009-09-03 21:54:03 -04001950 done_pbase_paths_num = done_pbase_paths_alloc = 0;
1951}
1952
Jeff King2fa233a2018-09-18 23:49:07 -04001953/*
1954 * Return 1 iff the object specified by "delta" can be sent
1955 * literally as a delta against the base in "base_sha1". If
1956 * so, then *base_out will point to the entry in our packing
1957 * list, or NULL if we must use the external-base list.
1958 *
1959 * Depth value does not matter - find_deltas() will
1960 * never consider reused delta as the base object to
1961 * deltify other objects against, in order to avoid
1962 * circular deltas.
1963 */
Jeff King3f83fd52020-02-23 23:29:44 -05001964static int can_reuse_delta(const struct object_id *base_oid,
Jeff King2fa233a2018-09-18 23:49:07 -04001965 struct object_entry *delta,
1966 struct object_entry **base_out)
1967{
1968 struct object_entry *base;
Jeff King3df28ca2019-06-20 03:41:03 -04001969
Jeff King2fa233a2018-09-18 23:49:07 -04001970 /*
1971 * First see if we're already sending the base (or it's explicitly in
1972 * our "excluded" list).
1973 */
Jeff King3f83fd52020-02-23 23:29:44 -05001974 base = packlist_find(&to_pack, base_oid);
Jeff King2fa233a2018-09-18 23:49:07 -04001975 if (base) {
1976 if (!in_same_island(&delta->idx.oid, &base->idx.oid))
1977 return 0;
1978 *base_out = base;
1979 return 1;
1980 }
1981
1982 /*
1983 * Otherwise, reachability bitmaps may tell us if the receiver has it,
1984 * even if it was buried too deep in history to make it into the
1985 * packing list.
1986 */
Jeff King3f83fd52020-02-23 23:29:44 -05001987 if (thin && bitmap_has_oid_in_uninteresting(bitmap_git, base_oid)) {
Jeff King2fa233a2018-09-18 23:49:07 -04001988 if (use_delta_islands) {
Jeff King3f83fd52020-02-23 23:29:44 -05001989 if (!in_same_island(&delta->idx.oid, base_oid))
Jeff King2fa233a2018-09-18 23:49:07 -04001990 return 0;
1991 }
1992 *base_out = NULL;
1993 return 1;
1994 }
1995
1996 return 0;
1997}
1998
Jonathan Tane00549a2020-07-20 17:21:44 -07001999static void prefetch_to_pack(uint32_t object_index_start) {
2000 struct oid_array to_fetch = OID_ARRAY_INIT;
2001 uint32_t i;
2002
2003 for (i = object_index_start; i < to_pack.nr_objects; i++) {
2004 struct object_entry *entry = to_pack.objects + i;
2005
2006 if (!oid_object_info_extended(the_repository,
2007 &entry->idx.oid,
2008 NULL,
2009 OBJECT_INFO_FOR_PREFETCH))
2010 continue;
2011 oid_array_append(&to_fetch, &entry->idx.oid);
2012 }
2013 promisor_remote_get_direct(the_repository,
2014 to_fetch.oid, to_fetch.nr);
2015 oid_array_clear(&to_fetch);
2016}
2017
2018static void check_object(struct object_entry *entry, uint32_t object_index)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002019{
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002020 unsigned long canonical_size;
Jonathan Tan8d5cf952020-07-20 17:21:43 -07002021 enum object_type type;
2022 struct object_info oi = {.typep = &type, .sizep = &canonical_size};
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002023
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02002024 if (IN_PACK(entry)) {
2025 struct packed_git *p = IN_PACK(entry);
Shawn O. Pearce03e79c82006-12-23 02:34:08 -05002026 struct pack_window *w_curs = NULL;
Jeff King3f83fd52020-02-23 23:29:44 -05002027 int have_base = 0;
2028 struct object_id base_ref;
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002029 struct object_entry *base_entry;
2030 unsigned long used, used_0;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07002031 unsigned long avail;
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002032 off_t ofs;
2033 unsigned char *buf, c;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002034 enum object_type type;
Nguyễn Thái Ngọc Duy27a7d062018-04-14 17:35:09 +02002035 unsigned long in_pack_size;
Nicolas Pitre780e6e72006-09-22 21:25:04 -04002036
Shawn O. Pearce6777a592007-03-06 20:44:34 -05002037 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08002038
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002039 /*
Nicolas Pitrefa736f72007-05-09 12:31:28 -04002040 * We want in_pack_type even if we do not reuse delta
2041 * since non-delta representations could still be reused.
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08002042 */
Nicolas Pitre09ded042008-10-29 19:02:46 -04002043 used = unpack_object_header_buffer(buf, avail,
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002044 &type,
Nguyễn Thái Ngọc Duy27a7d062018-04-14 17:35:09 +02002045 &in_pack_size);
Nicolas Pitre03d66012008-10-29 19:02:48 -04002046 if (used == 0)
2047 goto give_up;
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08002048
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002049 if (type < 0)
2050 BUG("invalid type %d", type);
2051 entry->in_pack_type = type;
2052
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002053 /*
2054 * Determine if this is a delta and if so whether we can
2055 * reuse it or not. Otherwise let's find out as cheaply as
2056 * possible what the actual type and size for this object is.
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08002057 */
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002058 switch (entry->in_pack_type) {
2059 default:
2060 /* Not a delta hence we've already got all we need. */
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002061 oe_set_type(entry, entry->in_pack_type);
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002062 SET_SIZE(entry, in_pack_size);
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002063 entry->in_pack_header_size = used;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002064 if (oe_type(entry) < OBJ_COMMIT || oe_type(entry) > OBJ_BLOB)
Nicolas Pitre03d66012008-10-29 19:02:48 -04002065 goto give_up;
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002066 unuse_pack(&w_curs);
2067 return;
2068 case OBJ_REF_DELTA:
Jeff King3f83fd52020-02-23 23:29:44 -05002069 if (reuse_delta && !entry->preferred_base) {
2070 oidread(&base_ref,
2071 use_pack(p, &w_curs,
2072 entry->in_pack_offset + used,
2073 NULL));
2074 have_base = 1;
2075 }
brian m. carlson41179102018-05-02 00:25:37 +00002076 entry->in_pack_header_size = used + the_hash_algo->rawsz;
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002077 break;
2078 case OBJ_OFS_DELTA:
2079 buf = use_pack(p, &w_curs,
2080 entry->in_pack_offset + used, NULL);
2081 used_0 = 0;
2082 c = buf[used_0++];
2083 ofs = c & 127;
2084 while (c & 128) {
2085 ofs += 1;
Nicolas Pitre03d66012008-10-29 19:02:48 -04002086 if (!ofs || MSB(ofs, 7)) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002087 error(_("delta base offset overflow in pack for %s"),
brian m. carlsone6a492b2017-05-06 22:10:11 +00002088 oid_to_hex(&entry->idx.oid));
Nicolas Pitre03d66012008-10-29 19:02:48 -04002089 goto give_up;
2090 }
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002091 c = buf[used_0++];
2092 ofs = (ofs << 7) + (c & 127);
2093 }
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002094 ofs = entry->in_pack_offset - ofs;
Nicolas Pitre03d66012008-10-29 19:02:48 -04002095 if (ofs <= 0 || ofs >= entry->in_pack_offset) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002096 error(_("delta base offset out of bound for %s"),
brian m. carlsone6a492b2017-05-06 22:10:11 +00002097 oid_to_hex(&entry->idx.oid));
Nicolas Pitre03d66012008-10-29 19:02:48 -04002098 goto give_up;
2099 }
Nicolas Pitrea7de7132008-05-02 15:11:46 -04002100 if (reuse_delta && !entry->preferred_base) {
Taylor Blaueb3fd992021-01-13 17:23:47 -05002101 uint32_t pos;
2102 if (offset_to_pack_pos(p, ofs, &pos) < 0)
Nicolas Pitre08698b12008-10-29 19:02:49 -04002103 goto give_up;
Taylor Blaueb3fd992021-01-13 17:23:47 -05002104 if (!nth_packed_object_id(&base_ref, p,
2105 pack_pos_to_index(p, pos)))
Jeff King3f83fd52020-02-23 23:29:44 -05002106 have_base = 1;
Nicolas Pitre3449f8c2008-02-28 00:25:17 -05002107 }
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002108 entry->in_pack_header_size = used + used_0;
2109 break;
2110 }
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08002111
Jeff King3f83fd52020-02-23 23:29:44 -05002112 if (have_base &&
2113 can_reuse_delta(&base_ref, entry, &base_entry)) {
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002114 oe_set_type(entry, entry->in_pack_type);
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002115 SET_SIZE(entry, in_pack_size); /* delta size */
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +02002116 SET_DELTA_SIZE(entry, in_pack_size);
Jeff King6a1e32d2018-08-21 15:07:05 -04002117
2118 if (base_entry) {
2119 SET_DELTA(entry, base_entry);
2120 entry->delta_sibling_idx = base_entry->delta_child_idx;
2121 SET_DELTA_CHILD(base_entry, entry);
2122 } else {
Jeff Kinga93c1412020-02-23 23:30:07 -05002123 SET_DELTA_EXT(entry, &base_ref);
Jeff King6a1e32d2018-08-21 15:07:05 -04002124 }
2125
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002126 unuse_pack(&w_curs);
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08002127 return;
2128 }
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002129
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002130 if (oe_type(entry)) {
Nguyễn Thái Ngọc Duy27a7d062018-04-14 17:35:09 +02002131 off_t delta_pos;
2132
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002133 /*
2134 * This must be a delta and we already know what the
2135 * final object type is. Let's extract the actual
2136 * object size from the delta header.
2137 */
Nguyễn Thái Ngọc Duy27a7d062018-04-14 17:35:09 +02002138 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002139 canonical_size = get_size_from_delta(p, &w_curs, delta_pos);
2140 if (canonical_size == 0)
Nicolas Pitre03d66012008-10-29 19:02:48 -04002141 goto give_up;
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002142 SET_SIZE(entry, canonical_size);
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002143 unuse_pack(&w_curs);
2144 return;
2145 }
2146
2147 /*
2148 * No choice but to fall back to the recursive delta walk
Jeff Kingc93206b2019-01-07 03:34:12 -05002149 * with oid_object_info() to find about the object type
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002150 * at this point...
2151 */
Nicolas Pitre03d66012008-10-29 19:02:48 -04002152 give_up:
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002153 unuse_pack(&w_curs);
Junio C Hamano36e4d742005-06-27 03:34:06 -07002154 }
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08002155
Jonathan Tan8d5cf952020-07-20 17:21:43 -07002156 if (oid_object_info_extended(the_repository, &entry->idx.oid, &oi,
Jonathan Tane00549a2020-07-20 17:21:44 -07002157 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0) {
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +02002158 if (repo_has_promisor_remote(the_repository)) {
Jonathan Tane00549a2020-07-20 17:21:44 -07002159 prefetch_to_pack(object_index);
2160 if (oid_object_info_extended(the_repository, &entry->idx.oid, &oi,
2161 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0)
2162 type = -1;
2163 } else {
2164 type = -1;
2165 }
2166 }
Jonathan Tan8d5cf952020-07-20 17:21:43 -07002167 oe_set_type(entry, type);
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002168 if (entry->type_valid) {
2169 SET_SIZE(entry, canonical_size);
2170 } else {
2171 /*
2172 * Bad object type is checked in prepare_pack(). This is
2173 * to permit a missing preferred base object to be ignored
2174 * as a preferred base. Doing so can result in a larger
2175 * pack file, but the transfer will still take place.
2176 */
2177 }
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08002178}
2179
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002180static int pack_offset_sort(const void *_a, const void *_b)
2181{
2182 const struct object_entry *a = *(struct object_entry **)_a;
2183 const struct object_entry *b = *(struct object_entry **)_b;
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02002184 const struct packed_git *a_in_pack = IN_PACK(a);
2185 const struct packed_git *b_in_pack = IN_PACK(b);
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002186
2187 /* avoid filesystem trashing with loose objects */
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02002188 if (!a_in_pack && !b_in_pack)
brian m. carlsone6a492b2017-05-06 22:10:11 +00002189 return oidcmp(&a->idx.oid, &b->idx.oid);
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002190
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02002191 if (a_in_pack < b_in_pack)
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002192 return -1;
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02002193 if (a_in_pack > b_in_pack)
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002194 return 1;
2195 return a->in_pack_offset < b->in_pack_offset ? -1 :
2196 (a->in_pack_offset > b->in_pack_offset);
2197}
2198
Jeff King4cf21432016-08-11 05:26:36 -04002199/*
2200 * Drop an on-disk delta we were planning to reuse. Naively, this would
2201 * just involve blanking out the "delta" field, but we have to deal
2202 * with some extra book-keeping:
2203 *
2204 * 1. Removing ourselves from the delta_sibling linked list.
2205 *
2206 * 2. Updating our size/type to the non-delta representation. These were
2207 * either not recorded initially (size) or overwritten with the delta type
2208 * (type) when check_object() decided to reuse the delta.
Jeff King7dbabbb2017-01-27 19:09:59 -05002209 *
2210 * 3. Resetting our delta depth, as we are now a base object.
Jeff King4cf21432016-08-11 05:26:36 -04002211 */
2212static void drop_reused_delta(struct object_entry *entry)
2213{
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002214 unsigned *idx = &to_pack.objects[entry->delta_idx - 1].delta_child_idx;
Jeff King4cf21432016-08-11 05:26:36 -04002215 struct object_info oi = OBJECT_INFO_INIT;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002216 enum object_type type;
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002217 unsigned long size;
Jeff King4cf21432016-08-11 05:26:36 -04002218
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002219 while (*idx) {
2220 struct object_entry *oe = &to_pack.objects[*idx - 1];
2221
2222 if (oe == entry)
2223 *idx = oe->delta_sibling_idx;
Jeff King4cf21432016-08-11 05:26:36 -04002224 else
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002225 idx = &oe->delta_sibling_idx;
Jeff King4cf21432016-08-11 05:26:36 -04002226 }
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002227 SET_DELTA(entry, NULL);
Jeff King7dbabbb2017-01-27 19:09:59 -05002228 entry->depth = 0;
Jeff King4cf21432016-08-11 05:26:36 -04002229
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002230 oi.sizep = &size;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002231 oi.typep = &type;
Junio C Hamanoad635e82018-05-23 14:38:19 +09002232 if (packed_object_info(the_repository, IN_PACK(entry), entry->in_pack_offset, &oi) < 0) {
Jeff King4cf21432016-08-11 05:26:36 -04002233 /*
2234 * We failed to get the info from this pack for some reason;
Jeff Kingc93206b2019-01-07 03:34:12 -05002235 * fall back to oid_object_info, which may find another copy.
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002236 * And if that fails, the error will be recorded in oe_type(entry)
Jeff King4cf21432016-08-11 05:26:36 -04002237 * and dealt with in prepare_pack().
2238 */
Junio C Hamanoad635e82018-05-23 14:38:19 +09002239 oe_set_type(entry,
2240 oid_object_info(the_repository, &entry->idx.oid, &size));
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002241 } else {
2242 oe_set_type(entry, type);
Jeff King4cf21432016-08-11 05:26:36 -04002243 }
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002244 SET_SIZE(entry, size);
Jeff King4cf21432016-08-11 05:26:36 -04002245}
2246
2247/*
2248 * Follow the chain of deltas from this entry onward, throwing away any links
2249 * that cause us to hit a cycle (as determined by the DFS state flags in
2250 * the entries).
Jeff King7dbabbb2017-01-27 19:09:59 -05002251 *
2252 * We also detect too-long reused chains that would violate our --depth
2253 * limit.
Jeff King4cf21432016-08-11 05:26:36 -04002254 */
2255static void break_delta_chains(struct object_entry *entry)
2256{
Jeff King42b766d2017-01-27 17:05:36 -05002257 /*
2258 * The actual depth of each object we will write is stored as an int,
2259 * as it cannot exceed our int "depth" limit. But before we break
2260 * changes based no that limit, we may potentially go as deep as the
2261 * number of objects, which is elsewhere bounded to a uint32_t.
2262 */
2263 uint32_t total_depth;
2264 struct object_entry *cur, *next;
Jeff King4cf21432016-08-11 05:26:36 -04002265
Jeff King42b766d2017-01-27 17:05:36 -05002266 for (cur = entry, total_depth = 0;
2267 cur;
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002268 cur = DELTA(cur), total_depth++) {
Jeff King42b766d2017-01-27 17:05:36 -05002269 if (cur->dfs_state == DFS_DONE) {
2270 /*
2271 * We've already seen this object and know it isn't
2272 * part of a cycle. We do need to append its depth
2273 * to our count.
2274 */
2275 total_depth += cur->depth;
2276 break;
Jeff King7dbabbb2017-01-27 19:09:59 -05002277 }
2278
Jeff King4cf21432016-08-11 05:26:36 -04002279 /*
Jeff King42b766d2017-01-27 17:05:36 -05002280 * We break cycles before looping, so an ACTIVE state (or any
2281 * other cruft which made its way into the state variable)
2282 * is a bug.
Jeff King4cf21432016-08-11 05:26:36 -04002283 */
Jeff King42b766d2017-01-27 17:05:36 -05002284 if (cur->dfs_state != DFS_NONE)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002285 BUG("confusing delta dfs state in first pass: %d",
Jeff King42b766d2017-01-27 17:05:36 -05002286 cur->dfs_state);
2287
2288 /*
2289 * Now we know this is the first time we've seen the object. If
2290 * it's not a delta, we're done traversing, but we'll mark it
2291 * done to save time on future traversals.
2292 */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002293 if (!DELTA(cur)) {
Jeff King42b766d2017-01-27 17:05:36 -05002294 cur->dfs_state = DFS_DONE;
2295 break;
2296 }
2297
2298 /*
2299 * Mark ourselves as active and see if the next step causes
2300 * us to cycle to another active object. It's important to do
2301 * this _before_ we loop, because it impacts where we make the
2302 * cut, and thus how our total_depth counter works.
2303 * E.g., We may see a partial loop like:
2304 *
2305 * A -> B -> C -> D -> B
2306 *
2307 * Cutting B->C breaks the cycle. But now the depth of A is
2308 * only 1, and our total_depth counter is at 3. The size of the
2309 * error is always one less than the size of the cycle we
2310 * broke. Commits C and D were "lost" from A's chain.
2311 *
2312 * If we instead cut D->B, then the depth of A is correct at 3.
2313 * We keep all commits in the chain that we examined.
2314 */
2315 cur->dfs_state = DFS_ACTIVE;
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002316 if (DELTA(cur)->dfs_state == DFS_ACTIVE) {
Jeff King42b766d2017-01-27 17:05:36 -05002317 drop_reused_delta(cur);
2318 cur->dfs_state = DFS_DONE;
2319 break;
2320 }
Jeff King4cf21432016-08-11 05:26:36 -04002321 }
2322
Jeff King42b766d2017-01-27 17:05:36 -05002323 /*
2324 * And now that we've gone all the way to the bottom of the chain, we
2325 * need to clear the active flags and set the depth fields as
2326 * appropriate. Unlike the loop above, which can quit when it drops a
2327 * delta, we need to keep going to look for more depth cuts. So we need
2328 * an extra "next" pointer to keep going after we reset cur->delta.
2329 */
2330 for (cur = entry; cur; cur = next) {
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002331 next = DELTA(cur);
Jeff King4cf21432016-08-11 05:26:36 -04002332
Jeff King4cf21432016-08-11 05:26:36 -04002333 /*
Jeff King42b766d2017-01-27 17:05:36 -05002334 * We should have a chain of zero or more ACTIVE states down to
2335 * a final DONE. We can quit after the DONE, because either it
2336 * has no bases, or we've already handled them in a previous
2337 * call.
Jeff King4cf21432016-08-11 05:26:36 -04002338 */
Jeff King42b766d2017-01-27 17:05:36 -05002339 if (cur->dfs_state == DFS_DONE)
2340 break;
2341 else if (cur->dfs_state != DFS_ACTIVE)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002342 BUG("confusing delta dfs state in second pass: %d",
Jeff King42b766d2017-01-27 17:05:36 -05002343 cur->dfs_state);
2344
2345 /*
2346 * If the total_depth is more than depth, then we need to snip
2347 * the chain into two or more smaller chains that don't exceed
2348 * the maximum depth. Most of the resulting chains will contain
2349 * (depth + 1) entries (i.e., depth deltas plus one base), and
2350 * the last chain (i.e., the one containing entry) will contain
2351 * whatever entries are left over, namely
2352 * (total_depth % (depth + 1)) of them.
2353 *
2354 * Since we are iterating towards decreasing depth, we need to
2355 * decrement total_depth as we go, and we need to write to the
2356 * entry what its final depth will be after all of the
2357 * snipping. Since we're snipping into chains of length (depth
2358 * + 1) entries, the final depth of an entry will be its
2359 * original depth modulo (depth + 1). Any time we encounter an
2360 * entry whose final depth is supposed to be zero, we snip it
2361 * from its delta base, thereby making it so.
2362 */
2363 cur->depth = (total_depth--) % (depth + 1);
2364 if (!cur->depth)
2365 drop_reused_delta(cur);
2366
2367 cur->dfs_state = DFS_DONE;
Jeff King4cf21432016-08-11 05:26:36 -04002368 }
2369}
2370
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002371static void get_object_details(void)
2372{
Shawn O. Pearce7cadf492007-03-06 20:44:24 -05002373 uint32_t i;
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002374 struct object_entry **sorted_by_offset;
2375
Nguyễn Thái Ngọc Duy5af05042018-04-15 17:36:18 +02002376 if (progress)
2377 progress_state = start_progress(_("Counting objects"),
2378 to_pack.nr_objects);
2379
René Scharfeca56dad2021-03-13 17:17:22 +01002380 CALLOC_ARRAY(sorted_by_offset, to_pack.nr_objects);
Vicent Marti2834bc22013-10-24 14:01:06 -04002381 for (i = 0; i < to_pack.nr_objects; i++)
2382 sorted_by_offset[i] = to_pack.objects + i;
René Scharfe9ed0d8d2016-09-29 17:27:31 +02002383 QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002384
Vicent Marti2834bc22013-10-24 14:01:06 -04002385 for (i = 0; i < to_pack.nr_objects; i++) {
Junio C Hamano15366282011-04-05 10:44:11 -07002386 struct object_entry *entry = sorted_by_offset[i];
Jonathan Tane00549a2020-07-20 17:21:44 -07002387 check_object(entry, i);
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002388 if (entry->type_valid &&
2389 oe_size_greater_than(&to_pack, entry, big_file_threshold))
Junio C Hamano15366282011-04-05 10:44:11 -07002390 entry->no_try_delta = 1;
Nguyễn Thái Ngọc Duy5af05042018-04-15 17:36:18 +02002391 display_progress(progress_state, i + 1);
Junio C Hamano15366282011-04-05 10:44:11 -07002392 }
Nguyễn Thái Ngọc Duy5af05042018-04-15 17:36:18 +02002393 stop_progress(&progress_state);
Nicolas Pitre3449f8c2008-02-28 00:25:17 -05002394
Jeff King4cf21432016-08-11 05:26:36 -04002395 /*
2396 * This must happen in a second pass, since we rely on the delta
2397 * information for the whole list being completed.
2398 */
2399 for (i = 0; i < to_pack.nr_objects; i++)
2400 break_delta_chains(&to_pack.objects[i]);
2401
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002402 free(sorted_by_offset);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002403}
2404
Nicolas Pitreb9041662007-12-08 00:00:08 -05002405/*
2406 * We search for deltas in a list sorted by type, by filename hash, and then
2407 * by size, so that we see progressively smaller and smaller files.
2408 * That's because we prefer deltas to be from the bigger file
2409 * to the smaller -- deletes are potentially cheaper, but perhaps
2410 * more importantly, the bigger file is likely the more recent
2411 * one. The deepest deltas are therefore the oldest objects which are
2412 * less susceptible to be accessed often.
2413 */
Nicolas Pitre9668cf52007-04-16 12:29:54 -04002414static int type_size_sort(const void *_a, const void *_b)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002415{
Nicolas Pitre9668cf52007-04-16 12:29:54 -04002416 const struct object_entry *a = *(struct object_entry **)_a;
2417 const struct object_entry *b = *(struct object_entry **)_b;
Shahzad Lone33de80b2019-02-02 23:16:45 +00002418 const enum object_type a_type = oe_type(a);
2419 const enum object_type b_type = oe_type(b);
2420 const unsigned long a_size = SIZE(a);
2421 const unsigned long b_size = SIZE(b);
Nicolas Pitre9668cf52007-04-16 12:29:54 -04002422
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002423 if (a_type > b_type)
Linus Torvalds27225f22005-06-26 15:27:28 -07002424 return -1;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002425 if (a_type < b_type)
Nicolas Pitreb9041662007-12-08 00:00:08 -05002426 return 1;
Linus Torvalds27225f22005-06-26 15:27:28 -07002427 if (a->hash > b->hash)
Junio C Hamano7a979d92006-02-19 14:47:21 -08002428 return -1;
Nicolas Pitreb9041662007-12-08 00:00:08 -05002429 if (a->hash < b->hash)
2430 return 1;
Junio C Hamano7a979d92006-02-19 14:47:21 -08002431 if (a->preferred_base > b->preferred_base)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002432 return -1;
Nicolas Pitreb9041662007-12-08 00:00:08 -05002433 if (a->preferred_base < b->preferred_base)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002434 return 1;
Jeff King28b8a732018-08-16 08:13:09 +02002435 if (use_delta_islands) {
Shahzad Lone33de80b2019-02-02 23:16:45 +00002436 const int island_cmp = island_delta_cmp(&a->idx.oid, &b->idx.oid);
Jeff King28b8a732018-08-16 08:13:09 +02002437 if (island_cmp)
2438 return island_cmp;
2439 }
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002440 if (a_size > b_size)
Nicolas Pitreb9041662007-12-08 00:00:08 -05002441 return -1;
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002442 if (a_size < b_size)
Nicolas Pitreb9041662007-12-08 00:00:08 -05002443 return 1;
2444 return a < b ? -1 : (a > b); /* newest first */
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002445}
2446
2447struct unpacked {
2448 struct object_entry *entry;
2449 void *data;
Nicolas Pitref6c70812006-04-26 23:58:00 -04002450 struct delta_index *index;
Nicolas Pitre5a235b52007-07-12 17:07:59 -04002451 unsigned depth;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002452};
2453
Nicolas Pitred2506262007-08-15 22:46:01 -04002454static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
2455 unsigned long delta_size)
Martin Koegler074b2ee2007-05-28 23:20:58 +02002456{
2457 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
2458 return 0;
2459
Martin Koeglere3dfddb2007-05-28 23:20:59 +02002460 if (delta_size < cache_max_small_delta_size)
2461 return 1;
2462
Martin Koegler074b2ee2007-05-28 23:20:58 +02002463 /* cache delta, if objects are large enough compared to delta size */
2464 if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
2465 return 1;
2466
2467 return 0;
2468}
2469
Nguyễn Thái Ngọc Duyffbd51c2018-07-29 17:36:05 +02002470/* Protect delta_cache_size */
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01002471static pthread_mutex_t cache_mutex;
Nicolas Pitre3c701832007-09-10 11:10:11 -04002472#define cache_lock() pthread_mutex_lock(&cache_mutex)
2473#define cache_unlock() pthread_mutex_unlock(&cache_mutex)
2474
Nguyễn Thái Ngọc Duyffbd51c2018-07-29 17:36:05 +02002475/*
2476 * Protect object list partitioning (e.g. struct thread_param) and
2477 * progress_state
2478 */
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01002479static pthread_mutex_t progress_mutex;
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002480#define progress_lock() pthread_mutex_lock(&progress_mutex)
2481#define progress_unlock() pthread_mutex_unlock(&progress_mutex)
2482
Nguyễn Thái Ngọc Duyffbd51c2018-07-29 17:36:05 +02002483/*
2484 * Access to struct object_entry is unprotected since each thread owns
2485 * a portion of the main object list. Just don't access object entries
2486 * ahead in the list because they can be stolen and would need
2487 * progress_mutex for protection.
2488 */
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002489
Ævar Arnfjörð Bjarmason7d089fb2021-05-27 02:52:51 +02002490static inline int oe_size_less_than(struct packing_data *pack,
2491 const struct object_entry *lhs,
2492 unsigned long rhs)
2493{
2494 if (lhs->size_valid)
2495 return lhs->size_ < rhs;
2496 if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
2497 return 0;
2498 return oe_get_size_slow(pack, lhs) < rhs;
2499}
2500
2501static inline void oe_set_tree_depth(struct packing_data *pack,
2502 struct object_entry *e,
2503 unsigned int tree_depth)
2504{
2505 if (!pack->tree_depth)
2506 CALLOC_ARRAY(pack->tree_depth, pack->nr_alloc);
2507 pack->tree_depth[e - pack->objects] = tree_depth;
2508}
2509
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002510/*
2511 * Return the size of the object without doing any delta
2512 * reconstruction (so non-deltas are true object sizes, but deltas
2513 * return the size of the delta data).
2514 */
2515unsigned long oe_get_size_slow(struct packing_data *pack,
2516 const struct object_entry *e)
2517{
2518 struct packed_git *p;
2519 struct pack_window *w_curs;
2520 unsigned char *buf;
2521 enum object_type type;
2522 unsigned long used, avail, size;
2523
2524 if (e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {
Patrick Hoggedb673c2019-01-24 19:22:05 -05002525 packing_data_lock(&to_pack);
Junio C Hamanoad635e82018-05-23 14:38:19 +09002526 if (oid_object_info(the_repository, &e->idx.oid, &size) < 0)
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002527 die(_("unable to get size of %s"),
2528 oid_to_hex(&e->idx.oid));
Patrick Hoggedb673c2019-01-24 19:22:05 -05002529 packing_data_unlock(&to_pack);
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002530 return size;
2531 }
2532
2533 p = oe_in_pack(pack, e);
2534 if (!p)
2535 BUG("when e->type is a delta, it must belong to a pack");
2536
Patrick Hoggedb673c2019-01-24 19:22:05 -05002537 packing_data_lock(&to_pack);
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002538 w_curs = NULL;
2539 buf = use_pack(p, &w_curs, e->in_pack_offset, &avail);
2540 used = unpack_object_header_buffer(buf, avail, &type, &size);
2541 if (used == 0)
2542 die(_("unable to parse object header of %s"),
2543 oid_to_hex(&e->idx.oid));
2544
2545 unuse_pack(&w_curs);
Patrick Hoggedb673c2019-01-24 19:22:05 -05002546 packing_data_unlock(&to_pack);
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002547 return size;
2548}
2549
Nicolas Pitref6c70812006-04-26 23:58:00 -04002550static int try_delta(struct unpacked *trg, struct unpacked *src,
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002551 unsigned max_depth, unsigned long *mem_usage)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002552{
Nicolas Pitref6c70812006-04-26 23:58:00 -04002553 struct object_entry *trg_entry = trg->entry;
2554 struct object_entry *src_entry = src->entry;
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002555 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
Nicolas Pitrec83f0322007-07-12 14:33:21 -04002556 unsigned ref_depth;
Nicolas Pitre21666f12007-02-26 14:55:59 -05002557 enum object_type type;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002558 void *delta_buf;
2559
2560 /* Don't bother doing diffs between different types */
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002561 if (oe_type(trg_entry) != oe_type(src_entry))
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002562 return -1;
2563
Nicolas Pitref6c70812006-04-26 23:58:00 -04002564 /*
Jeff King15f07e02012-01-12 17:32:34 -05002565 * We do not bother to try a delta that we discarded on an
2566 * earlier try, but only when reusing delta data. Note that
2567 * src_entry that is marked as the preferred_base should always
2568 * be considered, as even if we produce a suboptimal delta against
2569 * it, we will still save the transfer cost, as we already know
2570 * the other side has it and we won't send src_entry at all.
Linus Torvalds51d1e832006-06-29 14:04:01 -07002571 */
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02002572 if (reuse_delta && IN_PACK(trg_entry) &&
2573 IN_PACK(trg_entry) == IN_PACK(src_entry) &&
Jeff King15f07e02012-01-12 17:32:34 -05002574 !src_entry->preferred_base &&
Junio C Hamanoe9195b52006-11-14 22:18:31 -08002575 trg_entry->in_pack_type != OBJ_REF_DELTA &&
2576 trg_entry->in_pack_type != OBJ_OFS_DELTA)
Linus Torvalds51d1e832006-06-29 14:04:01 -07002577 return 0;
2578
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002579 /* Let's not bust the allowed depth. */
Nicolas Pitre5a235b52007-07-12 17:07:59 -04002580 if (src->depth >= max_depth)
Linus Torvaldsd116a452005-06-25 20:17:59 -07002581 return 0;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002582
Nicolas Pitrec3b06a62006-05-16 16:29:14 -04002583 /* Now some size filtering heuristics. */
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002584 trg_size = SIZE(trg_entry);
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002585 if (!DELTA(trg_entry)) {
brian m. carlson41179102018-05-02 00:25:37 +00002586 max_size = trg_size/2 - the_hash_algo->rawsz;
Nicolas Pitrec83f0322007-07-12 14:33:21 -04002587 ref_depth = 1;
2588 } else {
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +02002589 max_size = DELTA_SIZE(trg_entry);
Nicolas Pitre5a235b52007-07-12 17:07:59 -04002590 ref_depth = trg->depth;
Nicolas Pitrec83f0322007-07-12 14:33:21 -04002591 }
Nicolas Pitre720fe222009-03-24 15:56:12 -04002592 max_size = (uint64_t)max_size * (max_depth - src->depth) /
Nicolas Pitrec83f0322007-07-12 14:33:21 -04002593 (max_depth - ref_depth + 1);
Nicolas Pitrec3b06a62006-05-16 16:29:14 -04002594 if (max_size == 0)
2595 return 0;
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002596 src_size = SIZE(src_entry);
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002597 sizediff = src_size < trg_size ? trg_size - src_size : 0;
Linus Torvalds27225f22005-06-26 15:27:28 -07002598 if (sizediff >= max_size)
Junio C Hamanof527cb82006-04-20 23:36:22 -07002599 return 0;
Brian Downinga1dab412007-07-12 07:55:47 -05002600 if (trg_size < src_size / 32)
2601 return 0;
Nicolas Pitref6c70812006-04-26 23:58:00 -04002602
Jeff King28b8a732018-08-16 08:13:09 +02002603 if (!in_same_island(&trg->entry->idx.oid, &src->entry->idx.oid))
2604 return 0;
2605
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002606 /* Load data if not already done */
2607 if (!trg->data) {
Patrick Hoggedb673c2019-01-24 19:22:05 -05002608 packing_data_lock(&to_pack);
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +02002609 trg->data = repo_read_object_file(the_repository,
2610 &trg_entry->idx.oid, &type,
2611 &sz);
Patrick Hoggedb673c2019-01-24 19:22:05 -05002612 packing_data_unlock(&to_pack);
Junio C Hamano2e3404c2007-08-25 01:26:47 -07002613 if (!trg->data)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002614 die(_("object %s cannot be read"),
brian m. carlsone6a492b2017-05-06 22:10:11 +00002615 oid_to_hex(&trg_entry->idx.oid));
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002616 if (sz != trg_size)
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01002617 die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
2618 oid_to_hex(&trg_entry->idx.oid), (uintmax_t)sz,
2619 (uintmax_t)trg_size);
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002620 *mem_usage += sz;
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002621 }
2622 if (!src->data) {
Patrick Hoggedb673c2019-01-24 19:22:05 -05002623 packing_data_lock(&to_pack);
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +02002624 src->data = repo_read_object_file(the_repository,
2625 &src_entry->idx.oid, &type,
2626 &sz);
Patrick Hoggedb673c2019-01-24 19:22:05 -05002627 packing_data_unlock(&to_pack);
Nicolas Pitre71064a92010-10-22 16:26:23 -04002628 if (!src->data) {
2629 if (src_entry->preferred_base) {
2630 static int warned = 0;
2631 if (!warned++)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002632 warning(_("object %s cannot be read"),
brian m. carlsone6a492b2017-05-06 22:10:11 +00002633 oid_to_hex(&src_entry->idx.oid));
Nicolas Pitre71064a92010-10-22 16:26:23 -04002634 /*
2635 * Those objects are not included in the
2636 * resulting pack. Be resilient and ignore
2637 * them if they can't be read, in case the
2638 * pack could be created nevertheless.
2639 */
2640 return 0;
2641 }
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002642 die(_("object %s cannot be read"),
brian m. carlsone6a492b2017-05-06 22:10:11 +00002643 oid_to_hex(&src_entry->idx.oid));
Nicolas Pitre71064a92010-10-22 16:26:23 -04002644 }
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002645 if (sz != src_size)
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01002646 die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
2647 oid_to_hex(&src_entry->idx.oid), (uintmax_t)sz,
2648 (uintmax_t)src_size);
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002649 *mem_usage += sz;
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002650 }
2651 if (!src->index) {
2652 src->index = create_delta_index(src->data, src_size);
Martin Koeglera588d882007-05-28 23:20:57 +02002653 if (!src->index) {
2654 static int warned = 0;
2655 if (!warned++)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002656 warning(_("suboptimal pack - out of memory"));
Martin Koeglera588d882007-05-28 23:20:57 +02002657 return 0;
2658 }
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002659 *mem_usage += sizeof_delta_index(src->index);
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002660 }
2661
2662 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002663 if (!delta_buf)
Linus Torvalds75c42d82005-06-25 19:30:20 -07002664 return 0;
Nicolas Pitref6c70812006-04-26 23:58:00 -04002665
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002666 if (DELTA(trg_entry)) {
Brian Downing848d7322007-07-08 23:45:21 -05002667 /* Prefer only shallower same-sized deltas. */
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +02002668 if (delta_size == DELTA_SIZE(trg_entry) &&
Nicolas Pitre5a235b52007-07-12 17:07:59 -04002669 src->depth + 1 >= trg->depth) {
Brian Downing848d7322007-07-08 23:45:21 -05002670 free(delta_buf);
2671 return 0;
2672 }
Nicolas Pitre9e2d57a2007-08-29 21:17:17 -04002673 }
2674
Nicolas Pitre3c701832007-09-10 11:10:11 -04002675 /*
2676 * Handle memory allocation outside of the cache
2677 * accounting lock. Compiler will optimize the strangeness
Dan McGee7eb151d2010-01-29 19:22:19 -06002678 * away when NO_PTHREADS is defined.
Nicolas Pitre3c701832007-09-10 11:10:11 -04002679 */
Jim Meyering8e0f7002008-01-31 18:26:32 +01002680 free(trg_entry->delta_data);
Nicolas Pitre3c701832007-09-10 11:10:11 -04002681 cache_lock();
Nicolas Pitre9e2d57a2007-08-29 21:17:17 -04002682 if (trg_entry->delta_data) {
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +02002683 delta_cache_size -= DELTA_SIZE(trg_entry);
Nicolas Pitre5a235b52007-07-12 17:07:59 -04002684 trg_entry->delta_data = NULL;
Martin Koegler074b2ee2007-05-28 23:20:58 +02002685 }
Nicolas Pitred2506262007-08-15 22:46:01 -04002686 if (delta_cacheable(src_size, trg_size, delta_size)) {
Nicolas Pitreb7a28f72007-12-07 20:27:52 -05002687 delta_cache_size += delta_size;
Nicolas Pitre3c701832007-09-10 11:10:11 -04002688 cache_unlock();
2689 trg_entry->delta_data = xrealloc(delta_buf, delta_size);
2690 } else {
2691 cache_unlock();
Martin Koegler074b2ee2007-05-28 23:20:58 +02002692 free(delta_buf);
Nicolas Pitre3c701832007-09-10 11:10:11 -04002693 }
2694
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002695 SET_DELTA(trg_entry, src_entry);
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +02002696 SET_DELTA_SIZE(trg_entry, delta_size);
Nicolas Pitreb7a28f72007-12-07 20:27:52 -05002697 trg->depth = src->depth + 1;
2698
Nicolas Pitref6c70812006-04-26 23:58:00 -04002699 return 1;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002700}
2701
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002702static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
Nicolas Pitreb2504a02006-02-22 16:00:08 -05002703{
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002704 struct object_entry *child = DELTA_CHILD(me);
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002705 unsigned int m = n;
2706 while (child) {
Shahzad Lone33de80b2019-02-02 23:16:45 +00002707 const unsigned int c = check_delta_limit(child, n + 1);
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002708 if (m < c)
2709 m = c;
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002710 child = DELTA_SIBLING(child);
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002711 }
2712 return m;
Nicolas Pitreb2504a02006-02-22 16:00:08 -05002713}
2714
Junio C Hamano75ad2352008-02-12 23:39:03 -08002715static unsigned long free_unpacked(struct unpacked *n)
Brian Downinga97773c2007-07-12 08:07:46 -05002716{
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002717 unsigned long freed_mem = sizeof_delta_index(n->index);
Brian Downinga97773c2007-07-12 08:07:46 -05002718 free_delta_index(n->index);
2719 n->index = NULL;
2720 if (n->data) {
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002721 freed_mem += SIZE(n->entry);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002722 FREE_AND_NULL(n->data);
Brian Downinga97773c2007-07-12 08:07:46 -05002723 }
2724 n->entry = NULL;
Nicolas Pitre7d7baa52007-07-12 22:27:12 -04002725 n->depth = 0;
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002726 return freed_mem;
Brian Downinga97773c2007-07-12 08:07:46 -05002727}
2728
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002729static void find_deltas(struct object_entry **list, unsigned *list_size,
Nicolas Pitree3349772007-09-06 02:13:10 -04002730 int window, int depth, unsigned *processed)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002731{
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002732 uint32_t i, idx = 0, count = 0;
Shawn O. Pearce7cadf492007-03-06 20:44:24 -05002733 struct unpacked *array;
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002734 unsigned long mem_usage = 0;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002735
René Scharfeca56dad2021-03-13 17:17:22 +01002736 CALLOC_ARRAY(array, window);
Junio C Hamano21fcd1b2006-02-11 17:54:18 -08002737
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002738 for (;;) {
Jeff King421b4882008-10-23 04:31:03 +00002739 struct object_entry *entry;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002740 struct unpacked *n = array + idx;
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002741 int j, max_depth, best_base = -1;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002742
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002743 progress_lock();
2744 if (!*list_size) {
2745 progress_unlock();
2746 break;
2747 }
Jeff King421b4882008-10-23 04:31:03 +00002748 entry = *list++;
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002749 (*list_size)--;
2750 if (!entry->preferred_base) {
2751 (*processed)++;
2752 display_progress(progress_state, *processed);
2753 }
2754 progress_unlock();
2755
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002756 mem_usage -= free_unpacked(n);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002757 n->entry = entry;
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08002758
Brian Downinga97773c2007-07-12 08:07:46 -05002759 while (window_memory_limit &&
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002760 mem_usage > window_memory_limit &&
Brian Downinga97773c2007-07-12 08:07:46 -05002761 count > 1) {
Shahzad Lone33de80b2019-02-02 23:16:45 +00002762 const uint32_t tail = (idx + window - count) % window;
Junio C Hamano75ad2352008-02-12 23:39:03 -08002763 mem_usage -= free_unpacked(array + tail);
Brian Downinga97773c2007-07-12 08:07:46 -05002764 count--;
2765 }
2766
Nicolas Pitre75d39852007-09-06 02:13:08 -04002767 /* We do not compute delta to *create* objects we are not
2768 * going to pack.
2769 */
2770 if (entry->preferred_base)
2771 goto next;
2772
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002773 /*
2774 * If the current object is at pack edge, take the depth the
2775 * objects that depend on the current object into account
2776 * otherwise they would become too deep.
2777 */
2778 max_depth = depth;
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002779 if (DELTA_CHILD(entry)) {
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002780 max_depth -= check_delta_limit(entry, 0);
2781 if (max_depth <= 0)
2782 goto next;
2783 }
2784
Linus Torvalds78817c12005-06-25 18:29:23 -07002785 j = window;
2786 while (--j > 0) {
Junio C Hamano77639872007-09-01 23:53:47 -07002787 int ret;
Shawn O. Pearce7cadf492007-03-06 20:44:24 -05002788 uint32_t other_idx = idx + j;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002789 struct unpacked *m;
Linus Torvalds78817c12005-06-25 18:29:23 -07002790 if (other_idx >= window)
2791 other_idx -= window;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002792 m = array + other_idx;
2793 if (!m->entry)
2794 break;
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002795 ret = try_delta(n, m, max_depth, &mem_usage);
Junio C Hamano77639872007-09-01 23:53:47 -07002796 if (ret < 0)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002797 break;
Junio C Hamano77639872007-09-01 23:53:47 -07002798 else if (ret > 0)
2799 best_base = other_idx;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002800 }
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002801
Nicolas Pitreed4a9032008-05-02 15:11:50 -04002802 /*
2803 * If we decided to cache the delta data, then it is best
2804 * to compress it right away. First because we have to do
2805 * it anyway, and doing it here while we're threaded will
2806 * save a lot of time in the non threaded write phase,
2807 * as well as allow for caching more deltas within
2808 * the same cache size limit.
2809 * ...
2810 * But only if not writing to stdout, since in that case
2811 * the network is most likely throttling writes anyway,
2812 * and therefore it is best to go to the write phase ASAP
2813 * instead, as we can afford spending more time compressing
2814 * between writes at that moment.
2815 */
2816 if (entry->delta_data && !pack_to_stdout) {
Nguyễn Thái Ngọc Duy0cb3c142018-04-14 17:35:07 +02002817 unsigned long size;
2818
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +02002819 size = do_compress(&entry->delta_data, DELTA_SIZE(entry));
Nguyễn Thái Ngọc Duy0cb3c142018-04-14 17:35:07 +02002820 if (size < (1U << OE_Z_DELTA_BITS)) {
2821 entry->z_delta_size = size;
2822 cache_lock();
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +02002823 delta_cache_size -= DELTA_SIZE(entry);
Nguyễn Thái Ngọc Duy0cb3c142018-04-14 17:35:07 +02002824 delta_cache_size += entry->z_delta_size;
2825 cache_unlock();
2826 } else {
2827 FREE_AND_NULL(entry->delta_data);
2828 entry->z_delta_size = 0;
2829 }
Nicolas Pitreed4a9032008-05-02 15:11:50 -04002830 }
2831
Junio C Hamano70ca1a32006-03-05 11:22:57 -08002832 /* if we made n a delta, and if n is already at max
2833 * depth, leaving it in the window is pointless. we
2834 * should evict it first.
Junio C Hamano70ca1a32006-03-05 11:22:57 -08002835 */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002836 if (DELTA(entry) && max_depth <= n->depth)
Junio C Hamano70ca1a32006-03-05 11:22:57 -08002837 continue;
Nicolas Pitreff457152006-05-15 13:47:16 -04002838
Junio C Hamano77639872007-09-01 23:53:47 -07002839 /*
2840 * Move the best delta base up in the window, after the
2841 * currently deltified object, to keep it longer. It will
2842 * be the first base object to be attempted next.
2843 */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002844 if (DELTA(entry)) {
Junio C Hamano77639872007-09-01 23:53:47 -07002845 struct unpacked swap = array[best_base];
2846 int dist = (window + idx - best_base) % window;
2847 int dst = best_base;
2848 while (dist--) {
2849 int src = (dst + 1) % window;
2850 array[dst] = array[src];
2851 dst = src;
2852 }
2853 array[dst] = swap;
2854 }
2855
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002856 next:
Linus Torvalds521a4f42005-06-26 13:43:41 -07002857 idx++;
Brian Downinga97773c2007-07-12 08:07:46 -05002858 if (count + 1 < window)
2859 count++;
Linus Torvalds521a4f42005-06-26 13:43:41 -07002860 if (idx >= window)
2861 idx = 0;
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002862 }
Sergey Vlasovadee7bd2005-08-08 22:46:58 +04002863
Nicolas Pitref6c70812006-04-26 23:58:00 -04002864 for (i = 0; i < window; ++i) {
Nicolas Pitreff457152006-05-15 13:47:16 -04002865 free_delta_index(array[i].index);
Sergey Vlasovadee7bd2005-08-08 22:46:58 +04002866 free(array[i].data);
Nicolas Pitref6c70812006-04-26 23:58:00 -04002867 }
Sergey Vlasovadee7bd2005-08-08 22:46:58 +04002868 free(array);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002869}
2870
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002871/*
Nguyễn Thái Ngọc Duyffbd51c2018-07-29 17:36:05 +02002872 * The main object list is split into smaller lists, each is handed to
2873 * one worker.
2874 *
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002875 * The main thread waits on the condition that (at least) one of the workers
2876 * has stopped working (which is indicated in the .working member of
2877 * struct thread_params).
Nguyễn Thái Ngọc Duyffbd51c2018-07-29 17:36:05 +02002878 *
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002879 * When a work thread has completed its work, it sets .working to 0 and
2880 * signals the main thread and waits on the condition that .data_ready
2881 * becomes 1.
Nguyễn Thái Ngọc Duyffbd51c2018-07-29 17:36:05 +02002882 *
2883 * The main thread steals half of the work from the worker that has
2884 * most work left to hand it to the idle worker.
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002885 */
2886
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002887struct thread_params {
2888 pthread_t thread;
2889 struct object_entry **list;
2890 unsigned list_size;
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002891 unsigned remaining;
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002892 int window;
2893 int depth;
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002894 int working;
2895 int data_ready;
2896 pthread_mutex_t mutex;
2897 pthread_cond_t cond;
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002898 unsigned *processed;
2899};
2900
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01002901static pthread_cond_t progress_cond;
2902
2903/*
2904 * Mutex and conditional variable can't be statically-initialized on Windows.
2905 */
2906static void init_threaded_search(void)
2907{
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01002908 pthread_mutex_init(&cache_mutex, NULL);
2909 pthread_mutex_init(&progress_mutex, NULL);
2910 pthread_cond_init(&progress_cond, NULL);
2911}
2912
2913static void cleanup_threaded_search(void)
2914{
2915 pthread_cond_destroy(&progress_cond);
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01002916 pthread_mutex_destroy(&cache_mutex);
2917 pthread_mutex_destroy(&progress_mutex);
2918}
Nicolas Pitrec2a33672007-09-10 00:06:09 -04002919
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002920static void *threaded_find_deltas(void *arg)
2921{
Nicolas Pitrec2a33672007-09-10 00:06:09 -04002922 struct thread_params *me = arg;
2923
Martin Ågren0c2ad002017-08-21 19:43:46 +02002924 progress_lock();
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002925 while (me->remaining) {
Martin Ågren0c2ad002017-08-21 19:43:46 +02002926 progress_unlock();
2927
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002928 find_deltas(me->list, &me->remaining,
Nicolas Pitrec2a33672007-09-10 00:06:09 -04002929 me->window, me->depth, me->processed);
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002930
2931 progress_lock();
2932 me->working = 0;
2933 pthread_cond_signal(&progress_cond);
2934 progress_unlock();
2935
2936 /*
2937 * We must not set ->data_ready before we wait on the
2938 * condition because the main thread may have set it to 1
2939 * before we get here. In order to be sure that new
2940 * work is available if we see 1 in ->data_ready, it
2941 * was initialized to 0 before this thread was spawned
2942 * and we reset it to 0 right away.
2943 */
2944 pthread_mutex_lock(&me->mutex);
2945 while (!me->data_ready)
2946 pthread_cond_wait(&me->cond, &me->mutex);
2947 me->data_ready = 0;
2948 pthread_mutex_unlock(&me->mutex);
Martin Ågren0c2ad002017-08-21 19:43:46 +02002949
2950 progress_lock();
Nicolas Pitrec2a33672007-09-10 00:06:09 -04002951 }
Martin Ågren0c2ad002017-08-21 19:43:46 +02002952 progress_unlock();
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002953 /* leave ->working 1 so that this doesn't get more work assigned */
2954 return NULL;
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002955}
2956
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002957static void ll_find_deltas(struct object_entry **list, unsigned list_size,
2958 int window, int depth, unsigned *processed)
2959{
Junio C Hamanodcda3612009-09-01 02:18:52 -07002960 struct thread_params *p;
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002961 int i, ret, active_threads = 0;
Nicolas Pitrec2a33672007-09-10 00:06:09 -04002962
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01002963 init_threaded_search();
2964
Nicolas Pitre367f4a42007-09-10 00:06:11 -04002965 if (delta_search_threads <= 1) {
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002966 find_deltas(list, &list_size, window, depth, processed);
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01002967 cleanup_threaded_search();
Nicolas Pitre367f4a42007-09-10 00:06:11 -04002968 return;
2969 }
Nicolas Pitre43cc2b42008-12-11 15:36:47 -05002970 if (progress > pack_to_stdout)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002971 fprintf_ln(stderr, _("Delta compression using up to %d threads"),
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02002972 delta_search_threads);
René Scharfeca56dad2021-03-13 17:17:22 +01002973 CALLOC_ARRAY(p, delta_search_threads);
Nicolas Pitre367f4a42007-09-10 00:06:11 -04002974
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002975 /* Partition the work amongst work threads. */
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002976 for (i = 0; i < delta_search_threads; i++) {
2977 unsigned sub_size = list_size / (delta_search_threads - i);
Nicolas Pitre59921b42007-09-10 00:06:10 -04002978
Nicolas Pitrebf874892008-12-13 15:06:40 -05002979 /* don't use too small segments or no deltas will be found */
2980 if (sub_size < 2*window && i+1 < delta_search_threads)
2981 sub_size = 0;
2982
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002983 p[i].window = window;
2984 p[i].depth = depth;
2985 p[i].processed = processed;
2986 p[i].working = 1;
2987 p[i].data_ready = 0;
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002988
2989 /* try to split chunks on "path" boundaries */
Nicolas Pitre6fc74702008-01-21 11:07:15 -05002990 while (sub_size && sub_size < list_size &&
2991 list[sub_size]->hash &&
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002992 list[sub_size]->hash == list[sub_size-1]->hash)
2993 sub_size++;
2994
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002995 p[i].list = list;
2996 p[i].list_size = sub_size;
2997 p[i].remaining = sub_size;
Nicolas Pitrec2a33672007-09-10 00:06:09 -04002998
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002999 list += sub_size;
3000 list_size -= sub_size;
3001 }
3002
Johannes Sixt50f22ad2007-12-16 20:45:34 +01003003 /* Start work threads. */
3004 for (i = 0; i < delta_search_threads; i++) {
3005 if (!p[i].list_size)
3006 continue;
Johannes Sixt68e6a4f2007-12-17 20:12:52 +01003007 pthread_mutex_init(&p[i].mutex, NULL);
3008 pthread_cond_init(&p[i].cond, NULL);
Johannes Sixt50f22ad2007-12-16 20:45:34 +01003009 ret = pthread_create(&p[i].thread, NULL,
3010 threaded_find_deltas, &p[i]);
3011 if (ret)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003012 die(_("unable to create thread: %s"), strerror(ret));
Johannes Sixt50f22ad2007-12-16 20:45:34 +01003013 active_threads++;
3014 }
3015
Nicolas Pitre384b32c2007-12-08 00:03:17 -05003016 /*
3017 * Now let's wait for work completion. Each time a thread is done
3018 * with its work, we steal half of the remaining work from the
3019 * thread with the largest number of unprocessed objects and give
3020 * it to that newly idle thread. This ensure good load balancing
3021 * until the remaining object list segments are simply too short
3022 * to be worth splitting anymore.
3023 */
Johannes Sixt50f22ad2007-12-16 20:45:34 +01003024 while (active_threads) {
3025 struct thread_params *target = NULL;
Nicolas Pitre384b32c2007-12-08 00:03:17 -05003026 struct thread_params *victim = NULL;
3027 unsigned sub_size = 0;
Nicolas Pitre384b32c2007-12-08 00:03:17 -05003028
3029 progress_lock();
Johannes Sixt50f22ad2007-12-16 20:45:34 +01003030 for (;;) {
3031 for (i = 0; !target && i < delta_search_threads; i++)
3032 if (!p[i].working)
3033 target = &p[i];
3034 if (target)
3035 break;
3036 pthread_cond_wait(&progress_cond, &progress_mutex);
3037 }
3038
Nicolas Pitre384b32c2007-12-08 00:03:17 -05003039 for (i = 0; i < delta_search_threads; i++)
3040 if (p[i].remaining > 2*window &&
3041 (!victim || victim->remaining < p[i].remaining))
3042 victim = &p[i];
3043 if (victim) {
3044 sub_size = victim->remaining / 2;
3045 list = victim->list + victim->list_size - sub_size;
3046 while (sub_size && list[0]->hash &&
3047 list[0]->hash == list[-1]->hash) {
3048 list++;
3049 sub_size--;
3050 }
Nicolas Pitreeb9688f2007-12-10 14:19:32 -05003051 if (!sub_size) {
3052 /*
3053 * It is possible for some "paths" to have
3054 * so many objects that no hash boundary
3055 * might be found. Let's just steal the
3056 * exact half in that case.
3057 */
3058 sub_size = victim->remaining / 2;
3059 list -= sub_size;
3060 }
Nicolas Pitre384b32c2007-12-08 00:03:17 -05003061 target->list = list;
3062 victim->list_size -= sub_size;
3063 victim->remaining -= sub_size;
Nicolas Pitrec2a33672007-09-10 00:06:09 -04003064 }
Nicolas Pitre384b32c2007-12-08 00:03:17 -05003065 target->list_size = sub_size;
3066 target->remaining = sub_size;
Johannes Sixt50f22ad2007-12-16 20:45:34 +01003067 target->working = 1;
3068 progress_unlock();
3069
3070 pthread_mutex_lock(&target->mutex);
3071 target->data_ready = 1;
3072 pthread_cond_signal(&target->cond);
3073 pthread_mutex_unlock(&target->mutex);
Nicolas Pitre384b32c2007-12-08 00:03:17 -05003074
3075 if (!sub_size) {
3076 pthread_join(target->thread, NULL);
Johannes Sixt50f22ad2007-12-16 20:45:34 +01003077 pthread_cond_destroy(&target->cond);
3078 pthread_mutex_destroy(&target->mutex);
Nicolas Pitre384b32c2007-12-08 00:03:17 -05003079 active_threads--;
3080 }
Johannes Sixt50f22ad2007-12-16 20:45:34 +01003081 }
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01003082 cleanup_threaded_search();
Junio C Hamanodcda3612009-09-01 02:18:52 -07003083 free(p);
Nicolas Pitre8ecce682007-09-06 02:13:11 -04003084}
3085
Jeff Kingff483022019-12-18 12:25:44 +01003086static int obj_is_packed(const struct object_id *oid)
3087{
Junio C Hamanoa14aebe2020-02-14 12:54:19 -08003088 return packlist_find(&to_pack, oid) ||
Jeff King92fb0db2019-12-18 12:25:46 +01003089 (reuse_packfile_bitmap &&
3090 bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid));
Jeff Kingff483022019-12-18 12:25:44 +01003091}
3092
Jeff Kingb773dde2016-09-05 17:52:26 -04003093static void add_tag_chain(const struct object_id *oid)
3094{
3095 struct tag *tag;
3096
3097 /*
3098 * We catch duplicates already in add_object_entry(), but we'd
3099 * prefer to do this extra check to avoid having to parse the
3100 * tag at all if we already know that it's being packed (e.g., if
3101 * it was included via bitmaps, we would not have parsed it
3102 * previously).
3103 */
Jeff Kingff483022019-12-18 12:25:44 +01003104 if (obj_is_packed(oid))
Jeff Kingb773dde2016-09-05 17:52:26 -04003105 return;
3106
Stefan Bellerce71efb2018-06-28 18:22:03 -07003107 tag = lookup_tag(the_repository, oid);
Jeff Kingb773dde2016-09-05 17:52:26 -04003108 while (1) {
3109 if (!tag || parse_tag(tag) || !tag->tagged)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003110 die(_("unable to pack objects reachable from tag %s"),
Jeff Kingb773dde2016-09-05 17:52:26 -04003111 oid_to_hex(oid));
3112
brian m. carlson188960b2017-10-15 22:07:01 +00003113 add_object_entry(&tag->object.oid, OBJ_TAG, NULL, 0);
Jeff Kingb773dde2016-09-05 17:52:26 -04003114
3115 if (tag->tagged->type != OBJ_TAG)
3116 return;
3117
3118 tag = (struct tag *)tag->tagged;
3119 }
3120}
3121
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02003122static int add_ref_tag(const char *tag UNUSED, const struct object_id *oid,
3123 int flag UNUSED, void *cb_data UNUSED)
Shawn O. Pearcef0a24aa2008-03-03 22:27:20 -05003124{
Michael Haggertyd1552542015-05-25 18:38:38 +00003125 struct object_id peeled;
Shawn O. Pearcef0a24aa2008-03-03 22:27:20 -05003126
Junio C Hamano77db59c2021-02-05 16:40:45 -08003127 if (!peel_iterated_oid(oid, &peeled) && obj_is_packed(&peeled))
Jeff Kingb773dde2016-09-05 17:52:26 -04003128 add_tag_chain(oid);
Shawn O. Pearcef0a24aa2008-03-03 22:27:20 -05003129 return 0;
3130}
3131
Junio C Hamanof3123c42005-10-22 01:28:13 -07003132static void prepare_pack(int window, int depth)
3133{
Nicolas Pitre9668cf52007-04-16 12:29:54 -04003134 struct object_entry **delta_list;
Ramsay Jones6e1c2342008-07-03 16:52:09 +01003135 uint32_t i, nr_deltas;
3136 unsigned n;
Nicolas Pitre9668cf52007-04-16 12:29:54 -04003137
Jeff King28b8a732018-08-16 08:13:09 +02003138 if (use_delta_islands)
Nguyễn Thái Ngọc Duy385cb642018-11-10 06:49:03 +01003139 resolve_tree_islands(the_repository, progress, &to_pack);
Jeff King28b8a732018-08-16 08:13:09 +02003140
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08003141 get_object_details();
Nicolas Pitre9668cf52007-04-16 12:29:54 -04003142
Nicolas Pitre0e8189e2008-10-31 11:31:08 -04003143 /*
3144 * If we're locally repacking then we need to be doubly careful
3145 * from now on in order to make sure no stealth corruption gets
3146 * propagated to the new pack. Clients receiving streamed packs
3147 * should validate everything they get anyway so no need to incur
3148 * the additional cost here in that case.
3149 */
3150 if (!pack_to_stdout)
3151 do_check_packed_object_crc = 1;
3152
Vicent Marti2834bc22013-10-24 14:01:06 -04003153 if (!to_pack.nr_objects || !window || !depth)
Nicolas Pitre9668cf52007-04-16 12:29:54 -04003154 return;
3155
Jeff Kingb32fa952016-02-22 17:44:25 -05003156 ALLOC_ARRAY(delta_list, to_pack.nr_objects);
Nicolas Pitre75d39852007-09-06 02:13:08 -04003157 nr_deltas = n = 0;
3158
Vicent Marti2834bc22013-10-24 14:01:06 -04003159 for (i = 0; i < to_pack.nr_objects; i++) {
3160 struct object_entry *entry = to_pack.objects + i;
Nicolas Pitre75d39852007-09-06 02:13:08 -04003161
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02003162 if (DELTA(entry))
Nicolas Pitre75d39852007-09-06 02:13:08 -04003163 /* This happens if we decided to reuse existing
Nicolas Pitrea7de7132008-05-02 15:11:46 -04003164 * delta from a pack. "reuse_delta &&" is implied.
Nicolas Pitre75d39852007-09-06 02:13:08 -04003165 */
3166 continue;
3167
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02003168 if (!entry->type_valid ||
3169 oe_size_less_than(&to_pack, entry, 50))
Nicolas Pitre75d39852007-09-06 02:13:08 -04003170 continue;
3171
3172 if (entry->no_try_delta)
3173 continue;
3174
Shawn O. Pearce6d6f9cd2008-08-12 11:31:06 -07003175 if (!entry->preferred_base) {
Nicolas Pitre75d39852007-09-06 02:13:08 -04003176 nr_deltas++;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02003177 if (oe_type(entry) < 0)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003178 die(_("unable to get type of object %s"),
brian m. carlsone6a492b2017-05-06 22:10:11 +00003179 oid_to_hex(&entry->idx.oid));
Nicolas Pitreeede9f42008-09-02 10:22:21 -04003180 } else {
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02003181 if (oe_type(entry) < 0) {
Nicolas Pitreeede9f42008-09-02 10:22:21 -04003182 /*
3183 * This object is not found, but we
3184 * don't have to include it anyway.
3185 */
3186 continue;
3187 }
Shawn O. Pearce6d6f9cd2008-08-12 11:31:06 -07003188 }
Nicolas Pitre75d39852007-09-06 02:13:08 -04003189
3190 delta_list[n++] = entry;
3191 }
3192
Nicolas Pitre2f8b8942007-10-16 21:55:47 -04003193 if (nr_deltas && n > 1) {
Nicolas Pitree3349772007-09-06 02:13:10 -04003194 unsigned nr_done = 0;
Jeff Kingbb514de2019-12-18 12:25:45 +01003195
Nicolas Pitree3349772007-09-06 02:13:10 -04003196 if (progress)
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +07003197 progress_state = start_progress(_("Compressing objects"),
Nicolas Pitredc6a0752007-10-30 14:57:32 -04003198 nr_deltas);
René Scharfe9ed0d8d2016-09-29 17:27:31 +02003199 QSORT(delta_list, n, type_size_sort);
Nicolas Pitre8ecce682007-09-06 02:13:11 -04003200 ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -04003201 stop_progress(&progress_state);
Nicolas Pitree3349772007-09-06 02:13:10 -04003202 if (nr_done != nr_deltas)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003203 die(_("inconsistency with delta count"));
Nicolas Pitre75d39852007-09-06 02:13:08 -04003204 }
Nicolas Pitre9668cf52007-04-16 12:29:54 -04003205 free(delta_list);
Junio C Hamanof3123c42005-10-22 01:28:13 -07003206}
3207
Glen Chooa4e7e312023-06-28 19:26:22 +00003208static int git_pack_config(const char *k, const char *v,
3209 const struct config_context *ctx, void *cb)
Jeff King4812a932006-07-23 01:50:30 -04003210{
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -04003211 if (!strcmp(k, "pack.window")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00003212 window = git_config_int(k, v, ctx->kvi);
Jeff King4812a932006-07-23 01:50:30 -04003213 return 0;
3214 }
Brian Downinga97773c2007-07-12 08:07:46 -05003215 if (!strcmp(k, "pack.windowmemory")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00003216 window_memory_limit = git_config_ulong(k, v, ctx->kvi);
Brian Downinga97773c2007-07-12 08:07:46 -05003217 return 0;
3218 }
3219 if (!strcmp(k, "pack.depth")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00003220 depth = git_config_int(k, v, ctx->kvi);
Theodore Ts'o842aaf92007-05-08 09:28:26 -04003221 return 0;
3222 }
Martin Koegler074b2ee2007-05-28 23:20:58 +02003223 if (!strcmp(k, "pack.deltacachesize")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00003224 max_delta_cache_size = git_config_int(k, v, ctx->kvi);
Martin Koegler074b2ee2007-05-28 23:20:58 +02003225 return 0;
3226 }
Martin Koeglere3dfddb2007-05-28 23:20:59 +02003227 if (!strcmp(k, "pack.deltacachelimit")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00003228 cache_max_small_delta_size = git_config_int(k, v, ctx->kvi);
Martin Koeglere3dfddb2007-05-28 23:20:59 +02003229 return 0;
3230 }
Vicent Martiae4f07f2013-12-21 09:00:45 -05003231 if (!strcmp(k, "pack.writebitmaphashcache")) {
3232 if (git_config_bool(k, v))
3233 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;
3234 else
3235 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;
3236 }
Abhradeep Chakraborty76f14b72022-08-14 16:55:09 +00003237
3238 if (!strcmp(k, "pack.writebitmaplookuptable")) {
3239 if (git_config_bool(k, v))
3240 write_bitmap_options |= BITMAP_OPT_LOOKUP_TABLE;
3241 else
3242 write_bitmap_options &= ~BITMAP_OPT_LOOKUP_TABLE;
3243 }
3244
Vicent Marti6b8fda22013-12-21 09:00:09 -05003245 if (!strcmp(k, "pack.usebitmaps")) {
Kirill Smelkov645c4322016-09-10 18:01:44 +03003246 use_bitmap_index_default = git_config_bool(k, v);
Vicent Marti6b8fda22013-12-21 09:00:09 -05003247 return 0;
3248 }
Jeff Kinge704fc72019-12-18 12:25:43 +01003249 if (!strcmp(k, "pack.allowpackreuse")) {
Taylor Blau94107412023-12-14 17:24:42 -05003250 int res = git_parse_maybe_bool_text(v);
3251 if (res < 0) {
3252 if (!strcasecmp(v, "single"))
3253 allow_pack_reuse = SINGLE_PACK_REUSE;
Taylor Blauaf626ac2023-12-14 17:24:44 -05003254 else if (!strcasecmp(v, "multi"))
3255 allow_pack_reuse = MULTI_PACK_REUSE;
Taylor Blau94107412023-12-14 17:24:42 -05003256 else
3257 die(_("invalid pack.allowPackReuse value: '%s'"), v);
3258 } else if (res) {
3259 allow_pack_reuse = SINGLE_PACK_REUSE;
3260 } else {
3261 allow_pack_reuse = NO_PACK_REUSE;
3262 }
Jeff Kinge704fc72019-12-18 12:25:43 +01003263 return 0;
3264 }
Nicolas Pitre693b86f2007-09-10 11:51:34 -04003265 if (!strcmp(k, "pack.threads")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00003266 delta_search_threads = git_config_int(k, v, ctx->kvi);
Andreas Ericsson833e3df2008-02-22 20:11:56 -06003267 if (delta_search_threads < 0)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003268 die(_("invalid number of threads specified (%d)"),
Nicolas Pitre693b86f2007-09-10 11:51:34 -04003269 delta_search_threads);
Nguyễn Thái Ngọc Duy9c897c52018-11-03 09:48:46 +01003270 if (!HAVE_THREADS && delta_search_threads != 1) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003271 warning(_("no threads support, ignoring %s"), k);
Ævar Arnfjörð Bjarmason2e96d812017-05-25 19:45:33 +00003272 delta_search_threads = 0;
3273 }
Nicolas Pitre693b86f2007-09-10 11:51:34 -04003274 return 0;
3275 }
Nicolas Pitre4d00bda2007-11-01 23:26:04 -04003276 if (!strcmp(k, "pack.indexversion")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00003277 pack_idx_opts.version = git_config_int(k, v, ctx->kvi);
Junio C Hamanoebcfb372011-02-25 15:43:25 -08003278 if (pack_idx_opts.version > 2)
Jiang Xinb4eda052022-06-17 18:03:09 +08003279 die(_("bad pack.indexVersion=%"PRIu32),
Junio C Hamanoebcfb372011-02-25 15:43:25 -08003280 pack_idx_opts.version);
Nicolas Pitre4d00bda2007-11-01 23:26:04 -04003281 return 0;
3282 }
Taylor Blauc9773342021-01-25 18:37:30 -05003283 if (!strcmp(k, "pack.writereverseindex")) {
3284 if (git_config_bool(k, v))
3285 pack_idx_opts.flags |= WRITE_REV;
3286 else
3287 pack_idx_opts.flags &= ~WRITE_REV;
3288 return 0;
3289 }
Jonathan Tandd4b7322020-06-10 13:57:23 -07003290 if (!strcmp(k, "uploadpack.blobpackfileuri")) {
Jeff Kingba176db2023-12-07 02:11:14 -05003291 struct configured_exclusion *ex;
Jonathan Tandd4b7322020-06-10 13:57:23 -07003292 const char *oid_end, *pack_end;
3293 /*
3294 * Stores the pack hash. This is not a true object ID, but is
3295 * of the same form.
3296 */
3297 struct object_id pack_hash;
3298
Jeff Kingba176db2023-12-07 02:11:14 -05003299 if (!v)
3300 return config_error_nonbool(k);
3301
3302 ex = xmalloc(sizeof(*ex));
Jonathan Tandd4b7322020-06-10 13:57:23 -07003303 if (parse_oid_hex(v, &ex->e.oid, &oid_end) ||
3304 *oid_end != ' ' ||
3305 parse_oid_hex(oid_end + 1, &pack_hash, &pack_end) ||
3306 *pack_end != ' ')
3307 die(_("value of uploadpack.blobpackfileuri must be "
3308 "of the form '<object-hash> <pack-hash> <uri>' (got '%s')"), v);
3309 if (oidmap_get(&configured_exclusions, &ex->e.oid))
3310 die(_("object already configured in another "
3311 "uploadpack.blobpackfileuri (got '%s')"), v);
3312 ex->pack_hash_hex = xcalloc(1, pack_end - oid_end);
3313 memcpy(ex->pack_hash_hex, oid_end + 1, pack_end - oid_end - 1);
3314 ex->uri = xstrdup(pack_end + 1);
3315 oidmap_put(&configured_exclusions, ex);
3316 }
Glen Chooa4e7e312023-06-28 19:26:22 +00003317 return git_default_config(k, v, ctx, cb);
Jeff King4812a932006-07-23 01:50:30 -04003318}
3319
Taylor Blau339bce22021-02-22 21:25:10 -05003320/* Counters for trace2 output when in --stdin-packs mode. */
3321static int stdin_packs_found_nr;
3322static int stdin_packs_hints_nr;
3323
3324static int add_object_entry_from_pack(const struct object_id *oid,
3325 struct packed_git *p,
3326 uint32_t pos,
3327 void *_data)
3328{
Taylor Blau339bce22021-02-22 21:25:10 -05003329 off_t ofs;
Taylor Blau50457592022-05-24 14:54:31 -04003330 enum object_type type = OBJ_NONE;
Taylor Blau339bce22021-02-22 21:25:10 -05003331
3332 display_progress(progress_state, ++nr_seen);
3333
3334 if (have_duplicate_entry(oid, 0))
3335 return 0;
3336
3337 ofs = nth_packed_object_offset(p, pos);
3338 if (!want_object_in_pack(oid, 0, &p, &ofs))
3339 return 0;
3340
Taylor Blau50457592022-05-24 14:54:31 -04003341 if (p) {
3342 struct rev_info *revs = _data;
3343 struct object_info oi = OBJECT_INFO_INIT;
Taylor Blau339bce22021-02-22 21:25:10 -05003344
Taylor Blau50457592022-05-24 14:54:31 -04003345 oi.typep = &type;
3346 if (packed_object_info(the_repository, p, ofs, &oi) < 0) {
3347 die(_("could not get type of object %s in pack %s"),
3348 oid_to_hex(oid), p->pack_name);
3349 } else if (type == OBJ_COMMIT) {
3350 /*
3351 * commits in included packs are used as starting points for the
3352 * subsequent revision walk
3353 */
3354 add_pending_oid(revs, NULL, oid, 0);
3355 }
3356
3357 stdin_packs_found_nr++;
3358 }
Taylor Blau339bce22021-02-22 21:25:10 -05003359
3360 create_object_entry(oid, type, 0, 0, 0, p, ofs);
3361
3362 return 0;
3363}
3364
Jeff Kingbe252d32023-02-24 01:39:24 -05003365static void show_commit_pack_hint(struct commit *commit UNUSED,
3366 void *data UNUSED)
Taylor Blau339bce22021-02-22 21:25:10 -05003367{
3368 /* nothing to do; commits don't have a namehash */
3369}
3370
3371static void show_object_pack_hint(struct object *object, const char *name,
Jeff Kingbe252d32023-02-24 01:39:24 -05003372 void *data UNUSED)
Taylor Blau339bce22021-02-22 21:25:10 -05003373{
3374 struct object_entry *oe = packlist_find(&to_pack, &object->oid);
3375 if (!oe)
3376 return;
3377
3378 /*
3379 * Our 'to_pack' list was constructed by iterating all objects packed in
3380 * included packs, and so doesn't have a non-zero hash field that you
3381 * would typically pick up during a reachability traversal.
3382 *
3383 * Make a best-effort attempt to fill in the ->hash and ->no_try_delta
3384 * here using a now in order to perhaps improve the delta selection
3385 * process.
3386 */
3387 oe->hash = pack_name_hash(name);
3388 oe->no_try_delta = name && no_try_delta(name);
3389
3390 stdin_packs_hints_nr++;
3391}
3392
3393static int pack_mtime_cmp(const void *_a, const void *_b)
3394{
3395 struct packed_git *a = ((const struct string_list_item*)_a)->util;
3396 struct packed_git *b = ((const struct string_list_item*)_b)->util;
3397
3398 /*
3399 * order packs by descending mtime so that objects are laid out
3400 * roughly as newest-to-oldest
3401 */
3402 if (a->mtime < b->mtime)
3403 return 1;
3404 else if (b->mtime < a->mtime)
3405 return -1;
3406 else
3407 return 0;
3408}
3409
3410static void read_packs_list_from_stdin(void)
3411{
3412 struct strbuf buf = STRBUF_INIT;
3413 struct string_list include_packs = STRING_LIST_INIT_DUP;
3414 struct string_list exclude_packs = STRING_LIST_INIT_DUP;
3415 struct string_list_item *item = NULL;
3416
3417 struct packed_git *p;
3418 struct rev_info revs;
3419
3420 repo_init_revisions(the_repository, &revs, NULL);
3421 /*
3422 * Use a revision walk to fill in the namehash of objects in the include
3423 * packs. To save time, we'll avoid traversing through objects that are
3424 * in excluded packs.
3425 *
3426 * That may cause us to avoid populating all of the namehash fields of
3427 * all included objects, but our goal is best-effort, since this is only
3428 * an optimization during delta selection.
3429 */
3430 revs.no_kept_objects = 1;
3431 revs.keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
3432 revs.blob_objects = 1;
3433 revs.tree_objects = 1;
3434 revs.tag_objects = 1;
Taylor Blau14e7b832021-03-19 11:40:52 -04003435 revs.ignore_missing_links = 1;
Taylor Blau339bce22021-02-22 21:25:10 -05003436
3437 while (strbuf_getline(&buf, stdin) != EOF) {
3438 if (!buf.len)
3439 continue;
3440
3441 if (*buf.buf == '^')
3442 string_list_append(&exclude_packs, buf.buf + 1);
3443 else
3444 string_list_append(&include_packs, buf.buf);
3445
3446 strbuf_reset(&buf);
3447 }
3448
3449 string_list_sort(&include_packs);
Patrick Steinhardt732194b2023-04-14 08:01:49 +02003450 string_list_remove_duplicates(&include_packs, 0);
Taylor Blau339bce22021-02-22 21:25:10 -05003451 string_list_sort(&exclude_packs);
Patrick Steinhardt732194b2023-04-14 08:01:49 +02003452 string_list_remove_duplicates(&exclude_packs, 0);
Taylor Blau339bce22021-02-22 21:25:10 -05003453
3454 for (p = get_all_packs(the_repository); p; p = p->next) {
3455 const char *pack_name = pack_basename(p);
3456
Patrick Steinhardt752b4652023-04-14 08:01:54 +02003457 if ((item = string_list_lookup(&include_packs, pack_name)))
3458 item->util = p;
3459 if ((item = string_list_lookup(&exclude_packs, pack_name)))
Taylor Blau339bce22021-02-22 21:25:10 -05003460 item->util = p;
3461 }
3462
3463 /*
Ævar Arnfjörð Bjarmason561fa032021-07-09 12:13:48 +02003464 * Arguments we got on stdin may not even be packs. First
3465 * check that to avoid segfaulting later on in
3466 * e.g. pack_mtime_cmp(), excluded packs are handled below.
3467 *
3468 * Since we first parsed our STDIN and then sorted the input
3469 * lines the pack we error on will be whatever line happens to
3470 * sort first. This is lazy, it's enough that we report one
3471 * bad case here, we don't need to report the first/last one,
3472 * or all of them.
3473 */
3474 for_each_string_list_item(item, &include_packs) {
3475 struct packed_git *p = item->util;
3476 if (!p)
3477 die(_("could not find pack '%s'"), item->string);
Taylor Blau50457592022-05-24 14:54:31 -04003478 if (!is_pack_valid(p))
3479 die(_("packfile %s cannot be accessed"), p->pack_name);
Ævar Arnfjörð Bjarmason561fa032021-07-09 12:13:48 +02003480 }
3481
3482 /*
3483 * Then, handle all of the excluded packs, marking them as
3484 * kept in-core so that later calls to add_object_entry()
3485 * discards any objects that are also found in excluded packs.
Taylor Blau339bce22021-02-22 21:25:10 -05003486 */
3487 for_each_string_list_item(item, &exclude_packs) {
3488 struct packed_git *p = item->util;
3489 if (!p)
3490 die(_("could not find pack '%s'"), item->string);
3491 p->pack_keep_in_core = 1;
3492 }
3493
3494 /*
3495 * Order packs by ascending mtime; use QSORT directly to access the
3496 * string_list_item's ->util pointer, which string_list_sort() does not
3497 * provide.
3498 */
3499 QSORT(include_packs.items, include_packs.nr, pack_mtime_cmp);
3500
3501 for_each_string_list_item(item, &include_packs) {
3502 struct packed_git *p = item->util;
Taylor Blau339bce22021-02-22 21:25:10 -05003503 for_each_object_in_pack(p,
3504 add_object_entry_from_pack,
3505 &revs,
3506 FOR_EACH_OBJECT_PACK_ORDER);
3507 }
3508
3509 if (prepare_revision_walk(&revs))
3510 die(_("revision walk setup failed"));
3511 traverse_commit_list(&revs,
3512 show_commit_pack_hint,
3513 show_object_pack_hint,
3514 NULL);
3515
3516 trace2_data_intmax("pack-objects", the_repository, "stdin_packs_found",
3517 stdin_packs_found_nr);
3518 trace2_data_intmax("pack-objects", the_repository, "stdin_packs_hints",
3519 stdin_packs_hints_nr);
3520
3521 strbuf_release(&buf);
3522 string_list_clear(&include_packs, 0);
3523 string_list_clear(&exclude_packs, 0);
3524}
3525
Taylor Blaub7573532022-05-20 19:17:52 -04003526static void add_cruft_object_entry(const struct object_id *oid, enum object_type type,
3527 struct packed_git *pack, off_t offset,
3528 const char *name, uint32_t mtime)
3529{
3530 struct object_entry *entry;
3531
3532 display_progress(progress_state, ++nr_seen);
3533
3534 entry = packlist_find(&to_pack, oid);
3535 if (entry) {
3536 if (name) {
3537 entry->hash = pack_name_hash(name);
3538 entry->no_try_delta = no_try_delta(name);
3539 }
3540 } else {
3541 if (!want_object_in_pack(oid, 0, &pack, &offset))
3542 return;
3543 if (!pack && type == OBJ_BLOB && !has_loose_object(oid)) {
3544 /*
3545 * If a traversed tree has a missing blob then we want
3546 * to avoid adding that missing object to our pack.
3547 *
3548 * This only applies to missing blobs, not trees,
3549 * because the traversal needs to parse sub-trees but
3550 * not blobs.
3551 *
3552 * Note we only perform this check when we couldn't
3553 * already find the object in a pack, so we're really
3554 * limited to "ensure non-tip blobs which don't exist in
3555 * packs do exist via loose objects". Confused?
3556 */
3557 return;
3558 }
3559
3560 entry = create_object_entry(oid, type, pack_name_hash(name),
3561 0, name && no_try_delta(name),
3562 pack, offset);
3563 }
3564
3565 if (mtime > oe_cruft_mtime(&to_pack, entry))
3566 oe_set_cruft_mtime(&to_pack, entry, mtime);
3567 return;
3568}
3569
Jeff Kingc50dca22023-02-24 01:39:22 -05003570static void show_cruft_object(struct object *obj, const char *name, void *data UNUSED)
Taylor Blaua7d49382022-05-20 19:18:00 -04003571{
3572 /*
3573 * if we did not record it earlier, it's at least as old as our
3574 * expiration value. Rather than find it exactly, just use that
3575 * value. This may bump it forward from its real mtime, but it
3576 * will still be "too old" next time we run with the same
3577 * expiration.
3578 *
3579 * if obj does appear in the packing list, this call is a noop (or may
3580 * set the namehash).
3581 */
3582 add_cruft_object_entry(&obj->oid, obj->type, NULL, 0, name, cruft_expiration);
3583}
3584
3585static void show_cruft_commit(struct commit *commit, void *data)
3586{
3587 show_cruft_object((struct object*)commit, NULL, data);
3588}
3589
Jeff Kingc50dca22023-02-24 01:39:22 -05003590static int cruft_include_check_obj(struct object *obj, void *data UNUSED)
Taylor Blaua7d49382022-05-20 19:18:00 -04003591{
3592 return !has_object_kept_pack(&obj->oid, IN_CORE_KEEP_PACKS);
3593}
3594
3595static int cruft_include_check(struct commit *commit, void *data)
3596{
3597 return cruft_include_check_obj((struct object*)commit, data);
3598}
3599
3600static void set_cruft_mtime(const struct object *object,
3601 struct packed_git *pack,
3602 off_t offset, time_t mtime)
3603{
3604 add_cruft_object_entry(&object->oid, object->type, pack, offset, NULL,
3605 mtime);
3606}
3607
Taylor Blaub7573532022-05-20 19:17:52 -04003608static void mark_pack_kept_in_core(struct string_list *packs, unsigned keep)
3609{
3610 struct string_list_item *item = NULL;
3611 for_each_string_list_item(item, packs) {
3612 struct packed_git *p = item->util;
3613 if (!p)
3614 die(_("could not find pack '%s'"), item->string);
3615 p->pack_keep_in_core = keep;
3616 }
3617}
3618
3619static void add_unreachable_loose_objects(void);
3620static void add_objects_in_unpacked_packs(void);
3621
3622static void enumerate_cruft_objects(void)
3623{
3624 if (progress)
3625 progress_state = start_progress(_("Enumerating cruft objects"), 0);
3626
3627 add_objects_in_unpacked_packs();
3628 add_unreachable_loose_objects();
3629
3630 stop_progress(&progress_state);
3631}
3632
Taylor Blaua7d49382022-05-20 19:18:00 -04003633static void enumerate_and_traverse_cruft_objects(struct string_list *fresh_packs)
3634{
3635 struct packed_git *p;
3636 struct rev_info revs;
3637 int ret;
3638
3639 repo_init_revisions(the_repository, &revs, NULL);
3640
3641 revs.tag_objects = 1;
3642 revs.tree_objects = 1;
3643 revs.blob_objects = 1;
3644
3645 revs.include_check = cruft_include_check;
3646 revs.include_check_obj = cruft_include_check_obj;
3647
3648 revs.ignore_missing_links = 1;
3649
3650 if (progress)
3651 progress_state = start_progress(_("Enumerating cruft objects"), 0);
3652 ret = add_unseen_recent_objects_to_traversal(&revs, cruft_expiration,
3653 set_cruft_mtime, 1);
3654 stop_progress(&progress_state);
3655
3656 if (ret)
3657 die(_("unable to add cruft objects"));
3658
3659 /*
3660 * Re-mark only the fresh packs as kept so that objects in
3661 * unknown packs do not halt the reachability traversal early.
3662 */
3663 for (p = get_all_packs(the_repository); p; p = p->next)
3664 p->pack_keep_in_core = 0;
3665 mark_pack_kept_in_core(fresh_packs, 1);
3666
3667 if (prepare_revision_walk(&revs))
3668 die(_("revision walk setup failed"));
3669 if (progress)
3670 progress_state = start_progress(_("Traversing cruft objects"), 0);
3671 nr_seen = 0;
3672 traverse_commit_list(&revs, show_cruft_commit, show_cruft_object, NULL);
3673
3674 stop_progress(&progress_state);
3675}
3676
Taylor Blaub7573532022-05-20 19:17:52 -04003677static void read_cruft_objects(void)
3678{
3679 struct strbuf buf = STRBUF_INIT;
3680 struct string_list discard_packs = STRING_LIST_INIT_DUP;
3681 struct string_list fresh_packs = STRING_LIST_INIT_DUP;
3682 struct packed_git *p;
3683
3684 ignore_packed_keep_in_core = 1;
3685
3686 while (strbuf_getline(&buf, stdin) != EOF) {
3687 if (!buf.len)
3688 continue;
3689
3690 if (*buf.buf == '-')
3691 string_list_append(&discard_packs, buf.buf + 1);
3692 else
3693 string_list_append(&fresh_packs, buf.buf);
Taylor Blaub7573532022-05-20 19:17:52 -04003694 }
3695
3696 string_list_sort(&discard_packs);
3697 string_list_sort(&fresh_packs);
3698
3699 for (p = get_all_packs(the_repository); p; p = p->next) {
3700 const char *pack_name = pack_basename(p);
3701 struct string_list_item *item;
3702
3703 item = string_list_lookup(&fresh_packs, pack_name);
3704 if (!item)
3705 item = string_list_lookup(&discard_packs, pack_name);
3706
3707 if (item) {
3708 item->util = p;
3709 } else {
3710 /*
3711 * This pack wasn't mentioned in either the "fresh" or
3712 * "discard" list, so the caller didn't know about it.
3713 *
3714 * Mark it as kept so that its objects are ignored by
3715 * add_unseen_recent_objects_to_traversal(). We'll
3716 * unmark it before starting the traversal so it doesn't
3717 * halt the traversal early.
3718 */
3719 p->pack_keep_in_core = 1;
3720 }
3721 }
3722
3723 mark_pack_kept_in_core(&fresh_packs, 1);
3724 mark_pack_kept_in_core(&discard_packs, 0);
3725
3726 if (cruft_expiration)
Taylor Blaua7d49382022-05-20 19:18:00 -04003727 enumerate_and_traverse_cruft_objects(&fresh_packs);
Taylor Blaub7573532022-05-20 19:17:52 -04003728 else
3729 enumerate_cruft_objects();
3730
3731 strbuf_release(&buf);
3732 string_list_clear(&discard_packs, 0);
3733 string_list_clear(&fresh_packs, 0);
3734}
3735
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003736static void read_object_list_from_stdin(void)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07003737{
brian m. carlson188960b2017-10-15 22:07:01 +00003738 char line[GIT_MAX_HEXSZ + 1 + PATH_MAX + 2];
3739 struct object_id oid;
3740 const char *p;
Nicolas Pitreb2504a02006-02-22 16:00:08 -05003741
Linus Torvaldsda93d122006-04-02 13:31:54 -07003742 for (;;) {
Linus Torvaldsda93d122006-04-02 13:31:54 -07003743 if (!fgets(line, sizeof(line), stdin)) {
3744 if (feof(stdin))
3745 break;
3746 if (!ferror(stdin))
Ævar Arnfjörð Bjarmason58677572021-12-07 12:05:51 +01003747 BUG("fgets returned NULL, not EOF, not error!");
Junio C Hamano687dd752006-04-03 23:41:09 -07003748 if (errno != EINTR)
Thomas Rastd824cbb2009-06-27 17:58:46 +02003749 die_errno("fgets");
Junio C Hamano687dd752006-04-03 23:41:09 -07003750 clearerr(stdin);
3751 continue;
Linus Torvaldsda93d122006-04-02 13:31:54 -07003752 }
Junio C Hamano7a979d92006-02-19 14:47:21 -08003753 if (line[0] == '-') {
brian m. carlson188960b2017-10-15 22:07:01 +00003754 if (get_oid_hex(line+1, &oid))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003755 die(_("expected edge object ID, got garbage:\n %s"),
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003756 line);
brian m. carlson188960b2017-10-15 22:07:01 +00003757 add_preferred_base(&oid);
Junio C Hamano7a979d92006-02-19 14:47:21 -08003758 continue;
Junio C Hamano21fcd1b2006-02-11 17:54:18 -08003759 }
brian m. carlson188960b2017-10-15 22:07:01 +00003760 if (parse_oid_hex(line, &oid, &p))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003761 die(_("expected object ID, got garbage:\n %s"), line);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003762
brian m. carlson188960b2017-10-15 22:07:01 +00003763 add_preferred_base_object(p + 1);
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02003764 add_object_entry(&oid, OBJ_NONE, p + 1, 0);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07003765 }
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003766}
3767
Jeff Kingc50dca22023-02-24 01:39:22 -05003768static void show_commit(struct commit *commit, void *data UNUSED)
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003769{
brian m. carlson188960b2017-10-15 22:07:01 +00003770 add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0);
Vicent Marti7cc8f972013-12-21 09:00:16 -05003771
3772 if (write_bitmap_index)
3773 index_commit_for_bitmap(commit);
Jeff King28b8a732018-08-16 08:13:09 +02003774
3775 if (use_delta_islands)
3776 propagate_island_marks(commit);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003777}
3778
Jeff Kingc50dca22023-02-24 01:39:22 -05003779static void show_object(struct object *obj, const char *name,
3780 void *data UNUSED)
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003781{
Linus Torvalds8d2dfc42009-04-10 17:27:58 -07003782 add_preferred_base_object(name);
brian m. carlson188960b2017-10-15 22:07:01 +00003783 add_object_entry(&obj->oid, obj->type, name, 0);
Jeff King28b8a732018-08-16 08:13:09 +02003784
3785 if (use_delta_islands) {
3786 const char *p;
Jeff King39490532018-11-20 04:50:53 -05003787 unsigned depth;
Jeff King28b8a732018-08-16 08:13:09 +02003788 struct object_entry *ent;
3789
Jeff King39490532018-11-20 04:50:53 -05003790 /* the empty string is a root tree, which is depth 0 */
3791 depth = *name ? 1 : 0;
Jeff King28b8a732018-08-16 08:13:09 +02003792 for (p = strchr(name, '/'); p; p = strchr(p + 1, '/'))
3793 depth++;
3794
Jeff King3a378762019-09-05 21:36:05 -04003795 ent = packlist_find(&to_pack, &obj->oid);
Christian Couder108f5302018-08-16 08:13:12 +02003796 if (ent && depth > oe_tree_depth(&to_pack, ent))
3797 oe_set_tree_depth(&to_pack, ent, depth);
Jeff King28b8a732018-08-16 08:13:09 +02003798 }
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003799}
3800
Jeff Hostetler9535ce72017-11-21 20:58:52 +00003801static void show_object__ma_allow_any(struct object *obj, const char *name, void *data)
3802{
3803 assert(arg_missing_action == MA_ALLOW_ANY);
3804
3805 /*
3806 * Quietly ignore ALL missing objects. This avoids problems with
3807 * staging them now and getting an odd error later.
3808 */
Jonathan Tanee472432020-08-05 16:06:51 -07003809 if (!has_object(the_repository, &obj->oid, 0))
Jeff Hostetler9535ce72017-11-21 20:58:52 +00003810 return;
3811
3812 show_object(obj, name, data);
3813}
3814
Jonathan Tan0c16cd42017-12-08 15:27:16 +00003815static void show_object__ma_allow_promisor(struct object *obj, const char *name, void *data)
3816{
3817 assert(arg_missing_action == MA_ALLOW_PROMISOR);
3818
3819 /*
3820 * Quietly ignore EXPECTED missing objects. This avoids problems with
3821 * staging them now and getting an odd error later.
3822 */
Jonathan Tanee472432020-08-05 16:06:51 -07003823 if (!has_object(the_repository, &obj->oid, 0) && is_promisor_object(&obj->oid))
Jonathan Tan0c16cd42017-12-08 15:27:16 +00003824 return;
3825
3826 show_object(obj, name, data);
3827}
3828
Jeff King34bf44f2023-08-31 17:21:28 -04003829static int option_parse_missing_action(const struct option *opt UNUSED,
Jeff Hostetler9535ce72017-11-21 20:58:52 +00003830 const char *arg, int unset)
3831{
3832 assert(arg);
3833 assert(!unset);
3834
3835 if (!strcmp(arg, "error")) {
3836 arg_missing_action = MA_ERROR;
3837 fn_show_object = show_object;
3838 return 0;
3839 }
3840
3841 if (!strcmp(arg, "allow-any")) {
3842 arg_missing_action = MA_ALLOW_ANY;
Jonathan Tan0c16cd42017-12-08 15:27:16 +00003843 fetch_if_missing = 0;
Jeff Hostetler9535ce72017-11-21 20:58:52 +00003844 fn_show_object = show_object__ma_allow_any;
3845 return 0;
3846 }
3847
Jonathan Tan0c16cd42017-12-08 15:27:16 +00003848 if (!strcmp(arg, "allow-promisor")) {
3849 arg_missing_action = MA_ALLOW_PROMISOR;
3850 fetch_if_missing = 0;
3851 fn_show_object = show_object__ma_allow_promisor;
3852 return 0;
3853 }
3854
Jean-Noël Avila1a8aea82022-01-31 22:07:47 +00003855 die(_("invalid value for '%s': '%s'"), "--missing", arg);
Jeff Hostetler9535ce72017-11-21 20:58:52 +00003856 return 0;
3857}
3858
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07003859static void show_edge(struct commit *commit)
3860{
brian m. carlson188960b2017-10-15 22:07:01 +00003861 add_preferred_base(&commit->object.oid);
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07003862}
3863
Taylor Blaua9fd2f22021-08-29 22:48:54 -04003864static int add_object_in_unpacked_pack(const struct object_id *oid,
3865 struct packed_git *pack,
3866 uint32_t pos,
Jeff Kingbe252d32023-02-24 01:39:24 -05003867 void *data UNUSED)
Junio C Hamano08cdfb12007-09-16 23:20:07 -07003868{
Taylor Blaub7573532022-05-20 19:17:52 -04003869 if (cruft) {
3870 off_t offset;
3871 time_t mtime;
3872
3873 if (pack->is_cruft) {
3874 if (load_pack_mtimes(pack) < 0)
3875 die(_("could not load cruft pack .mtimes"));
3876 mtime = nth_packed_mtime(pack, pos);
3877 } else {
3878 mtime = pack->mtime;
3879 }
3880 offset = nth_packed_object_offset(pack, pos);
3881
3882 add_cruft_object_entry(oid, OBJ_NONE, pack, offset,
3883 NULL, mtime);
3884 } else {
3885 add_object_entry(oid, OBJ_NONE, "", 0);
3886 }
Taylor Blaua9fd2f22021-08-29 22:48:54 -04003887 return 0;
Junio C Hamano08cdfb12007-09-16 23:20:07 -07003888}
3889
Jeff King7a2a7212019-05-09 17:31:16 -04003890static void add_objects_in_unpacked_packs(void)
Junio C Hamano08cdfb12007-09-16 23:20:07 -07003891{
Taylor Blaua9fd2f22021-08-29 22:48:54 -04003892 if (for_each_packed_object(add_object_in_unpacked_pack, NULL,
3893 FOR_EACH_OBJECT_PACK_ORDER |
3894 FOR_EACH_OBJECT_LOCAL_ONLY |
3895 FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS |
3896 FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS))
3897 die(_("cannot open pack index"));
Junio C Hamano08cdfb12007-09-16 23:20:07 -07003898}
3899
brian m. carlson76c1d9a2017-02-21 23:47:35 +00003900static int add_loose_object(const struct object_id *oid, const char *path,
Jeff Kingbe252d32023-02-24 01:39:24 -05003901 void *data UNUSED)
Jeff Kinge26a8c42016-06-13 00:38:04 -04003902{
Stefan Beller0df8e962018-04-25 11:20:59 -07003903 enum object_type type = oid_object_info(the_repository, oid, NULL);
Jeff Kinge26a8c42016-06-13 00:38:04 -04003904
3905 if (type < 0) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003906 warning(_("loose object at %s could not be examined"), path);
Jeff Kinge26a8c42016-06-13 00:38:04 -04003907 return 0;
3908 }
3909
Taylor Blaub7573532022-05-20 19:17:52 -04003910 if (cruft) {
3911 struct stat st;
3912 if (stat(path, &st) < 0) {
3913 if (errno == ENOENT)
3914 return 0;
3915 return error_errno("unable to stat %s", oid_to_hex(oid));
3916 }
3917
3918 add_cruft_object_entry(oid, type, NULL, 0, NULL,
3919 st.st_mtime);
3920 } else {
3921 add_object_entry(oid, type, "", 0);
3922 }
Jeff Kinge26a8c42016-06-13 00:38:04 -04003923 return 0;
3924}
3925
3926/*
3927 * We actually don't even have to worry about reachability here.
3928 * add_object_entry will weed out duplicates, so we just add every
3929 * loose object we find.
3930 */
3931static void add_unreachable_loose_objects(void)
3932{
3933 for_each_loose_file_in_objdir(get_object_directory(),
3934 add_loose_object,
3935 NULL, NULL, NULL);
3936}
3937
brian m. carlson188960b2017-10-15 22:07:01 +00003938static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
Brandon Casey094085e2009-03-21 17:26:11 -05003939{
3940 static struct packed_git *last_found = (void *)1;
3941 struct packed_git *p;
3942
Stefan Bellera80d72d2018-03-23 18:20:59 +01003943 p = (last_found != (void *)1) ? last_found :
Derrick Stolee454ea2e2018-08-20 16:52:04 +00003944 get_all_packs(the_repository);
Brandon Casey094085e2009-03-21 17:26:11 -05003945
3946 while (p) {
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02003947 if ((!p->pack_local || p->pack_keep ||
3948 p->pack_keep_in_core) &&
brian m. carlson188960b2017-10-15 22:07:01 +00003949 find_pack_entry_one(oid->hash, p)) {
Brandon Casey094085e2009-03-21 17:26:11 -05003950 last_found = p;
3951 return 1;
3952 }
3953 if (p == last_found)
Derrick Stolee454ea2e2018-08-20 16:52:04 +00003954 p = get_all_packs(the_repository);
Brandon Casey094085e2009-03-21 17:26:11 -05003955 else
3956 p = p->next;
3957 if (p == last_found)
3958 p = p->next;
3959 }
3960 return 0;
3961}
3962
Jeff Kingabcb8652014-10-15 18:42:09 -04003963/*
3964 * Store a list of sha1s that are should not be discarded
3965 * because they are either written too recently, or are
3966 * reachable from another object that was.
3967 *
3968 * This is filled by get_object_list.
3969 */
brian m. carlson910650d2017-03-31 01:40:00 +00003970static struct oid_array recent_objects;
Jeff Kingabcb8652014-10-15 18:42:09 -04003971
brian m. carlson4ce36212017-03-31 01:39:57 +00003972static int loosened_object_can_be_discarded(const struct object_id *oid,
Johannes Schindelindddbad72017-04-26 21:29:31 +02003973 timestamp_t mtime)
Jeff Kingd0d46ab2014-10-15 18:41:53 -04003974{
3975 if (!unpack_unreachable_expiration)
3976 return 0;
3977 if (mtime > unpack_unreachable_expiration)
3978 return 0;
brian m. carlson910650d2017-03-31 01:40:00 +00003979 if (oid_array_lookup(&recent_objects, oid) >= 0)
Jeff Kingabcb8652014-10-15 18:42:09 -04003980 return 0;
Jeff Kingd0d46ab2014-10-15 18:41:53 -04003981 return 1;
3982}
3983
Jeff King7a2a7212019-05-09 17:31:16 -04003984static void loosen_unused_packed_objects(void)
Nicolas Pitreca11b212008-05-14 01:33:53 -04003985{
3986 struct packed_git *p;
3987 uint32_t i;
Rafael Silvaa6431572021-04-21 21:32:12 +02003988 uint32_t loosened_objects_nr = 0;
brian m. carlson4ce36212017-03-31 01:39:57 +00003989 struct object_id oid;
Nicolas Pitreca11b212008-05-14 01:33:53 -04003990
Derrick Stolee454ea2e2018-08-20 16:52:04 +00003991 for (p = get_all_packs(the_repository); p; p = p->next) {
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02003992 if (!p->pack_local || p->pack_keep || p->pack_keep_in_core)
Nicolas Pitreca11b212008-05-14 01:33:53 -04003993 continue;
3994
3995 if (open_pack_index(p))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003996 die(_("cannot open pack index"));
Nicolas Pitreca11b212008-05-14 01:33:53 -04003997
3998 for (i = 0; i < p->num_objects; i++) {
Jeff King07636712020-02-23 23:27:36 -05003999 nth_packed_object_id(&oid, p, i);
Jeff King3a378762019-09-05 21:36:05 -04004000 if (!packlist_find(&to_pack, &oid) &&
brian m. carlson188960b2017-10-15 22:07:01 +00004001 !has_sha1_pack_kept_or_nonlocal(&oid) &&
Rafael Silvaa6431572021-04-21 21:32:12 +02004002 !loosened_object_can_be_discarded(&oid, p->mtime)) {
Patryk Obara4bdb70a2018-01-28 01:13:20 +01004003 if (force_object_loose(&oid, p->mtime))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004004 die(_("unable to force loose object"));
Rafael Silvaa6431572021-04-21 21:32:12 +02004005 loosened_objects_nr++;
4006 }
Nicolas Pitreca11b212008-05-14 01:33:53 -04004007 }
4008 }
Rafael Silvaa6431572021-04-21 21:32:12 +02004009
4010 trace2_data_intmax("pack-objects", the_repository,
4011 "loosen_unused_packed_objects/loosened", loosened_objects_nr);
Nicolas Pitreca11b212008-05-14 01:33:53 -04004012}
4013
Jeff King69e4b342014-04-02 02:39:17 -04004014/*
Kirill Smelkov645c4322016-09-10 18:01:44 +03004015 * This tracks any options which pack-reuse code expects to be on, or which a
4016 * reader of the pack might not understand, and which would therefore prevent
4017 * blind reuse of what we have on disk.
Jeff King69e4b342014-04-02 02:39:17 -04004018 */
4019static int pack_options_allow_reuse(void)
4020{
Taylor Blau94107412023-12-14 17:24:42 -05004021 return allow_pack_reuse != NO_PACK_REUSE &&
Jeff Kinge704fc72019-12-18 12:25:43 +01004022 pack_to_stdout &&
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004023 !ignore_packed_keep_on_disk &&
4024 !ignore_packed_keep_in_core &&
Jeff King9df4a602017-05-08 22:54:13 -04004025 (!local || !have_non_local_packs) &&
4026 !incremental;
Jeff King69e4b342014-04-02 02:39:17 -04004027}
4028
Vicent Marti6b8fda22013-12-21 09:00:09 -05004029static int get_object_list_from_bitmap(struct rev_info *revs)
4030{
Derrick Stolee09d4a792022-03-09 16:01:35 +00004031 if (!(bitmap_git = prepare_bitmap_walk(revs, 0)))
Vicent Marti6b8fda22013-12-21 09:00:09 -05004032 return -1;
4033
Taylor Blau35e156b2023-12-14 17:24:01 -05004034 if (pack_options_allow_reuse())
Taylor Blau073b40e2023-12-14 17:24:12 -05004035 reuse_partial_packfile_from_bitmap(bitmap_git,
4036 &reuse_packfiles,
4037 &reuse_packfiles_nr,
Taylor Blauaf626ac2023-12-14 17:24:44 -05004038 &reuse_packfile_bitmap,
4039 allow_pack_reuse == MULTI_PACK_REUSE);
Taylor Blau35e156b2023-12-14 17:24:01 -05004040
Taylor Blau073b40e2023-12-14 17:24:12 -05004041 if (reuse_packfiles) {
Taylor Blau35e156b2023-12-14 17:24:01 -05004042 reuse_packfile_objects = bitmap_popcount(reuse_packfile_bitmap);
4043 if (!reuse_packfile_objects)
4044 BUG("expected non-empty reuse bitmap");
4045
Vicent Marti6b8fda22013-12-21 09:00:09 -05004046 nr_result += reuse_packfile_objects;
Jeff King8e118e82021-04-11 23:41:18 -04004047 nr_seen += reuse_packfile_objects;
4048 display_progress(progress_state, nr_seen);
Vicent Marti6b8fda22013-12-21 09:00:09 -05004049 }
4050
Jeff King4eb707e2020-02-14 13:22:27 -05004051 traverse_bitmap_commit_list(bitmap_git, revs,
4052 &add_object_entry_from_bitmap);
Vicent Marti6b8fda22013-12-21 09:00:09 -05004053 return 0;
4054}
4055
Jeff Kingabcb8652014-10-15 18:42:09 -04004056static void record_recent_object(struct object *obj,
Jeff Kingbe252d32023-02-24 01:39:24 -05004057 const char *name UNUSED,
4058 void *data UNUSED)
Jeff Kingabcb8652014-10-15 18:42:09 -04004059{
brian m. carlson910650d2017-03-31 01:40:00 +00004060 oid_array_append(&recent_objects, &obj->oid);
Jeff Kingabcb8652014-10-15 18:42:09 -04004061}
4062
Jeff Kingbe252d32023-02-24 01:39:24 -05004063static void record_recent_commit(struct commit *commit, void *data UNUSED)
Jeff Kingabcb8652014-10-15 18:42:09 -04004064{
brian m. carlson910650d2017-03-31 01:40:00 +00004065 oid_array_append(&recent_objects, &commit->object.oid);
Jeff Kingabcb8652014-10-15 18:42:09 -04004066}
4067
Taylor Blau3f267a12021-03-31 21:32:14 -04004068static int mark_bitmap_preferred_tip(const char *refname,
Jeff King63e14ee2022-08-19 06:08:32 -04004069 const struct object_id *oid,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02004070 int flags UNUSED,
4071 void *data UNUSED)
Taylor Blau3f267a12021-03-31 21:32:14 -04004072{
4073 struct object_id peeled;
4074 struct object *object;
4075
4076 if (!peel_iterated_oid(oid, &peeled))
4077 oid = &peeled;
4078
4079 object = parse_object_or_die(oid, refname);
4080 if (object->type == OBJ_COMMIT)
4081 object->flags |= NEEDS_BITMAP;
4082
4083 return 0;
4084}
4085
4086static void mark_bitmap_preferred_tips(void)
4087{
4088 struct string_list_item *item;
4089 const struct string_list *preferred_tips;
4090
4091 preferred_tips = bitmap_preferred_tips(the_repository);
4092 if (!preferred_tips)
4093 return;
4094
4095 for_each_string_list_item(item, preferred_tips) {
4096 for_each_ref_in(item->string, mark_bitmap_preferred_tip, NULL);
4097 }
4098}
4099
Derrick Stolee80f6de42022-03-22 17:28:36 +00004100static void get_object_list(struct rev_info *revs, int ac, const char **av)
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004101{
Matthew DeVorebbcde412018-12-03 14:10:19 -08004102 struct setup_revision_opt s_r_opt = {
4103 .allow_exclude_promisor_objects = 1,
4104 };
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004105 char line[1000];
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004106 int flags = 0;
Derrick Stoleea4544b32018-11-06 12:34:47 -08004107 int save_warning;
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004108
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004109 save_commit_buffer = 0;
Derrick Stolee80f6de42022-03-22 17:28:36 +00004110 setup_revisions(ac, av, revs, &s_r_opt);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004111
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +07004112 /* make sure shallows are read */
Stefan Bellerc8813482018-05-17 15:51:46 -07004113 is_repository_shallow(the_repository);
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +07004114
Derrick Stoleea4544b32018-11-06 12:34:47 -08004115 save_warning = warn_on_object_refname_ambiguity;
4116 warn_on_object_refname_ambiguity = 0;
4117
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004118 while (fgets(line, sizeof(line), stdin) != NULL) {
4119 int len = strlen(line);
Jim Meyering872c9302008-01-04 18:37:41 +01004120 if (len && line[len - 1] == '\n')
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004121 line[--len] = 0;
4122 if (!len)
4123 break;
4124 if (*line == '-') {
4125 if (!strcmp(line, "--not")) {
4126 flags ^= UNINTERESTING;
Vicent Marti7cc8f972013-12-21 09:00:16 -05004127 write_bitmap_index = 0;
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004128 continue;
4129 }
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +07004130 if (starts_with(line, "--shallow ")) {
brian m. carlsone92b8482017-05-06 22:10:06 +00004131 struct object_id oid;
4132 if (get_oid_hex(line + 10, &oid))
Junio C Hamano42790002020-08-13 18:07:12 -07004133 die("not an object name '%s'", line + 10);
Stefan Beller19143f12018-05-17 15:51:44 -07004134 register_shallow(the_repository, &oid);
Jeff Kingf7f91082014-08-12 00:34:53 -04004135 use_bitmap_index = 0;
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +07004136 continue;
4137 }
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004138 die(_("not a rev '%s'"), line);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004139 }
Derrick Stolee80f6de42022-03-22 17:28:36 +00004140 if (handle_revision_arg(line, revs, flags, REVARG_CANNOT_BE_FILENAME))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004141 die(_("bad revision '%s'"), line);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004142 }
4143
Derrick Stoleea4544b32018-11-06 12:34:47 -08004144 warn_on_object_refname_ambiguity = save_warning;
4145
Derrick Stolee80f6de42022-03-22 17:28:36 +00004146 if (use_bitmap_index && !get_object_list_from_bitmap(revs))
Vicent Marti6b8fda22013-12-21 09:00:09 -05004147 return;
4148
Jeff King28b8a732018-08-16 08:13:09 +02004149 if (use_delta_islands)
Jeff Kingbdbdf422019-06-20 04:58:32 -04004150 load_delta_islands(the_repository, progress);
Jeff King28b8a732018-08-16 08:13:09 +02004151
Taylor Blau3f267a12021-03-31 21:32:14 -04004152 if (write_bitmap_index)
4153 mark_bitmap_preferred_tips();
4154
Derrick Stolee80f6de42022-03-22 17:28:36 +00004155 if (prepare_revision_walk(revs))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004156 die(_("revision walk setup failed"));
Derrick Stolee80f6de42022-03-22 17:28:36 +00004157 mark_edges_uninteresting(revs, show_edge, sparse);
Jeff Hostetler9535ce72017-11-21 20:58:52 +00004158
4159 if (!fn_show_object)
4160 fn_show_object = show_object;
Derrick Stolee80f6de42022-03-22 17:28:36 +00004161 traverse_commit_list(revs,
Derrick Stolee3e0370a2022-03-09 16:01:36 +00004162 show_commit, fn_show_object,
4163 NULL);
Junio C Hamano08cdfb12007-09-16 23:20:07 -07004164
Jeff Kingabcb8652014-10-15 18:42:09 -04004165 if (unpack_unreachable_expiration) {
Derrick Stolee80f6de42022-03-22 17:28:36 +00004166 revs->ignore_missing_links = 1;
4167 if (add_unseen_recent_objects_to_traversal(revs,
Taylor Blau2fb90402022-05-20 19:17:54 -04004168 unpack_unreachable_expiration, NULL, 0))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004169 die(_("unable to add recent objects"));
Derrick Stolee80f6de42022-03-22 17:28:36 +00004170 if (prepare_revision_walk(revs))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004171 die(_("revision walk setup failed"));
Derrick Stolee80f6de42022-03-22 17:28:36 +00004172 traverse_commit_list(revs, record_recent_commit,
Jeff Kingabcb8652014-10-15 18:42:09 -04004173 record_recent_object, NULL);
4174 }
4175
Junio C Hamano08cdfb12007-09-16 23:20:07 -07004176 if (keep_unreachable)
Jeff King7a2a7212019-05-09 17:31:16 -04004177 add_objects_in_unpacked_packs();
Jeff Kinge26a8c42016-06-13 00:38:04 -04004178 if (pack_loose_unreachable)
4179 add_unreachable_loose_objects();
Nicolas Pitreca11b212008-05-14 01:33:53 -04004180 if (unpack_unreachable)
Jeff King7a2a7212019-05-09 17:31:16 -04004181 loosen_unused_packed_objects();
Jeff Kingabcb8652014-10-15 18:42:09 -04004182
brian m. carlson910650d2017-03-31 01:40:00 +00004183 oid_array_clear(&recent_objects);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004184}
4185
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004186static void add_extra_kept_packs(const struct string_list *names)
4187{
4188 struct packed_git *p;
4189
4190 if (!names->nr)
4191 return;
4192
Derrick Stolee454ea2e2018-08-20 16:52:04 +00004193 for (p = get_all_packs(the_repository); p; p = p->next) {
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004194 const char *name = basename(p->pack_name);
4195 int i;
4196
4197 if (!p->pack_local)
4198 continue;
4199
4200 for (i = 0; i < names->nr; i++)
4201 if (!fspathcmp(name, names->items[i].string))
4202 break;
4203
4204 if (i < names->nr) {
4205 p->pack_keep_in_core = 1;
4206 ignore_packed_keep_in_core = 1;
4207 continue;
4208 }
4209 }
4210}
4211
René Scharfe36f76d22023-07-21 14:41:53 +02004212static int option_parse_quiet(const struct option *opt, const char *arg,
4213 int unset)
4214{
Jeff King66e33092023-08-31 17:21:07 -04004215 int *val = opt->value;
4216
René Scharfe36f76d22023-07-21 14:41:53 +02004217 BUG_ON_OPT_ARG(arg);
4218
4219 if (!unset)
Jeff King66e33092023-08-31 17:21:07 -04004220 *val = 0;
4221 else if (!*val)
4222 *val = 1;
René Scharfe36f76d22023-07-21 14:41:53 +02004223 return 0;
4224}
4225
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004226static int option_parse_index_version(const struct option *opt,
4227 const char *arg, int unset)
4228{
Jeff King66e33092023-08-31 17:21:07 -04004229 struct pack_idx_option *popts = opt->value;
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004230 char *c;
4231 const char *val = arg;
Jeff King517fe802018-11-05 01:45:42 -05004232
4233 BUG_ON_OPT_NEG(unset);
4234
Jeff King66e33092023-08-31 17:21:07 -04004235 popts->version = strtoul(val, &c, 10);
4236 if (popts->version > 2)
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004237 die(_("unsupported index version %s"), val);
4238 if (*c == ',' && c[1])
Jeff King66e33092023-08-31 17:21:07 -04004239 popts->off32_limit = strtoul(c+1, &c, 0);
4240 if (*c || popts->off32_limit & 0x80000000)
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004241 die(_("bad index version '%s'"), val);
4242 return 0;
4243}
4244
Jeff King34bf44f2023-08-31 17:21:28 -04004245static int option_parse_unpack_unreachable(const struct option *opt UNUSED,
Jeff King7e52f562012-04-07 06:30:09 -04004246 const char *arg, int unset)
4247{
4248 if (unset) {
4249 unpack_unreachable = 0;
4250 unpack_unreachable_expiration = 0;
4251 }
4252 else {
4253 unpack_unreachable = 1;
4254 if (arg)
4255 unpack_unreachable_expiration = approxidate(arg);
4256 }
4257 return 0;
4258}
4259
Jeff King34bf44f2023-08-31 17:21:28 -04004260static int option_parse_cruft_expiration(const struct option *opt UNUSED,
Taylor Blaub7573532022-05-20 19:17:52 -04004261 const char *arg, int unset)
4262{
4263 if (unset) {
4264 cruft = 0;
4265 cruft_expiration = 0;
4266 } else {
4267 cruft = 1;
4268 if (arg)
4269 cruft_expiration = approxidate(arg);
4270 }
4271 return 0;
4272}
4273
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004274int cmd_pack_objects(int argc, const char **argv, const char *prefix)
4275{
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004276 int use_internal_rev_list = 0;
brian m. carlson2dacf262014-12-24 23:05:40 +00004277 int shallow = 0;
Nicolas Pitre4f366272009-11-23 12:43:50 -05004278 int all_progress_implied = 0;
Jeff King22f9b7f2020-07-28 16:24:27 -04004279 struct strvec rp = STRVEC_INIT;
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004280 int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0;
Jeff Kingc90f9e12014-10-16 20:44:49 -04004281 int rev_list_index = 0;
Taylor Blau339bce22021-02-22 21:25:10 -05004282 int stdin_packs = 0;
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004283 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
René Scharfe0d5448a2022-11-29 13:25:05 +01004284 struct list_objects_filter_options filter_options =
4285 LIST_OBJECTS_FILTER_INIT;
Derrick Stolee80f6de42022-03-22 17:28:36 +00004286
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004287 struct option pack_objects_options[] = {
Jeff King66e33092023-08-31 17:21:07 -04004288 OPT_CALLBACK_F('q', "quiet", &progress, NULL,
René Scharfe36f76d22023-07-21 14:41:53 +02004289 N_("do not show progress meter"),
4290 PARSE_OPT_NOARG, option_parse_quiet),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004291 OPT_SET_INT(0, "progress", &progress,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004292 N_("show progress meter"), 1),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004293 OPT_SET_INT(0, "all-progress", &progress,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004294 N_("show progress meter during object writing phase"), 2),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004295 OPT_BOOL(0, "all-progress-implied",
4296 &all_progress_implied,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004297 N_("similar to --all-progress when progress meter is shown")),
Jeff King66e33092023-08-31 17:21:07 -04004298 OPT_CALLBACK_F(0, "index-version", &pack_idx_opts, N_("<version>[,<offset>]"),
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004299 N_("write the pack index file in the specified idx format version"),
Denton Liu203c8532020-04-28 04:36:28 -04004300 PARSE_OPT_NONEG, option_parse_index_version),
Charles Bailey2a514ed2015-06-21 19:25:44 +01004301 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit,
4302 N_("maximum size of each output pack file")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004303 OPT_BOOL(0, "local", &local,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004304 N_("ignore borrowed objects from alternate object store")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004305 OPT_BOOL(0, "incremental", &incremental,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004306 N_("ignore packed objects")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004307 OPT_INTEGER(0, "window", &window,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004308 N_("limit pack window by objects")),
Charles Bailey2a514ed2015-06-21 19:25:44 +01004309 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit,
4310 N_("limit pack window by memory in addition to object limit")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004311 OPT_INTEGER(0, "depth", &depth,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004312 N_("maximum length of delta chain allowed in the resulting pack")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004313 OPT_BOOL(0, "reuse-delta", &reuse_delta,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004314 N_("reuse existing deltas")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004315 OPT_BOOL(0, "reuse-object", &reuse_object,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004316 N_("reuse existing objects")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004317 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004318 N_("use OFS_DELTA objects")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004319 OPT_INTEGER(0, "threads", &delta_search_threads,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004320 N_("use threads when searching for best delta matches")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004321 OPT_BOOL(0, "non-empty", &non_empty,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004322 N_("do not create an empty pack output")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004323 OPT_BOOL(0, "revs", &use_internal_rev_list,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004324 N_("read revision arguments from standard input")),
Nguyễn Thái Ngọc Duy3e4a67b2018-05-20 17:42:58 +02004325 OPT_SET_INT_F(0, "unpacked", &rev_list_unpacked,
4326 N_("limit the objects to those that are not yet packed"),
4327 1, PARSE_OPT_NONEG),
4328 OPT_SET_INT_F(0, "all", &rev_list_all,
4329 N_("include objects reachable from any reference"),
4330 1, PARSE_OPT_NONEG),
4331 OPT_SET_INT_F(0, "reflog", &rev_list_reflog,
4332 N_("include objects referred by reflog entries"),
4333 1, PARSE_OPT_NONEG),
4334 OPT_SET_INT_F(0, "indexed-objects", &rev_list_index,
4335 N_("include objects referred to by the index"),
4336 1, PARSE_OPT_NONEG),
Taylor Blau339bce22021-02-22 21:25:10 -05004337 OPT_BOOL(0, "stdin-packs", &stdin_packs,
4338 N_("read packs from stdin")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004339 OPT_BOOL(0, "stdout", &pack_to_stdout,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004340 N_("output pack to stdout")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004341 OPT_BOOL(0, "include-tag", &include_tag,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004342 N_("include tag objects that refer to objects to be packed")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004343 OPT_BOOL(0, "keep-unreachable", &keep_unreachable,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004344 N_("keep unreachable objects")),
Jeff Kinge26a8c42016-06-13 00:38:04 -04004345 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable,
4346 N_("pack loose unreachable objects")),
Denton Liu203c8532020-04-28 04:36:28 -04004347 OPT_CALLBACK_F(0, "unpack-unreachable", NULL, N_("time"),
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004348 N_("unpack unreachable objects newer than <time>"),
Denton Liu203c8532020-04-28 04:36:28 -04004349 PARSE_OPT_OPTARG, option_parse_unpack_unreachable),
Taylor Blaub7573532022-05-20 19:17:52 -04004350 OPT_BOOL(0, "cruft", &cruft, N_("create a cruft pack")),
4351 OPT_CALLBACK_F(0, "cruft-expiration", NULL, N_("time"),
4352 N_("expire cruft objects older than <time>"),
4353 PARSE_OPT_OPTARG, option_parse_cruft_expiration),
Derrick Stolee4f6d26b2019-01-16 10:25:58 -08004354 OPT_BOOL(0, "sparse", &sparse,
4355 N_("use the sparse reachability algorithm")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004356 OPT_BOOL(0, "thin", &thin,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004357 N_("create thin packs")),
brian m. carlson2dacf262014-12-24 23:05:40 +00004358 OPT_BOOL(0, "shallow", &shallow,
4359 N_("create packs suitable for shallow fetches")),
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004360 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004361 N_("ignore packs that have companion .keep file")),
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004362 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
4363 N_("ignore this pack")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004364 OPT_INTEGER(0, "compression", &pack_compression_level,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004365 N_("pack compression level")),
René Scharfe3a5f3082023-07-21 14:41:56 +02004366 OPT_BOOL(0, "keep-true-parents", &grafts_keep_true_parents,
4367 N_("do not hide commits by grafts")),
Vicent Marti6b8fda22013-12-21 09:00:09 -05004368 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index,
4369 N_("use a bitmap index if available to speed up counting objects")),
Jeff King25575012019-07-31 01:39:27 -04004370 OPT_SET_INT(0, "write-bitmap-index", &write_bitmap_index,
4371 N_("write a bitmap index together with the pack index"),
4372 WRITE_BITMAP_TRUE),
4373 OPT_SET_INT_F(0, "write-bitmap-index-quiet",
4374 &write_bitmap_index,
4375 N_("write a bitmap index if possible"),
4376 WRITE_BITMAP_QUIET, PARSE_OPT_HIDDEN),
René Scharfe0d5448a2022-11-29 13:25:05 +01004377 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
Denton Liu203c8532020-04-28 04:36:28 -04004378 OPT_CALLBACK_F(0, "missing", NULL, N_("action"),
Jeff Hostetler9535ce72017-11-21 20:58:52 +00004379 N_("handling for missing objects"), PARSE_OPT_NONEG,
Denton Liu203c8532020-04-28 04:36:28 -04004380 option_parse_missing_action),
Jonathan Tan0c16cd42017-12-08 15:27:16 +00004381 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
4382 N_("do not pack objects in promisor packfiles")),
Jeff King28b8a732018-08-16 08:13:09 +02004383 OPT_BOOL(0, "delta-islands", &use_delta_islands,
4384 N_("respect islands during delta compression")),
Jonathan Tandd4b7322020-06-10 13:57:23 -07004385 OPT_STRING_LIST(0, "uri-protocol", &uri_protocols,
4386 N_("protocol"),
4387 N_("exclude any configured uploadpack.blobpackfileuri with this protocol")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004388 OPT_END(),
4389 };
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07004390
Nguyễn Thái Ngọc Duy0c6804a2018-04-14 17:35:02 +02004391 if (DFS_NUM_STATES > (1 << OE_DFS_STATE_BITS))
4392 BUG("too many dfs states, increase OE_DFS_STATE_BITS");
4393
Derrick Stoleed24eda42023-06-06 13:24:35 +00004394 disable_replace_refs();
Christian Couderdae556b2009-01-23 10:07:46 +01004395
Derrick Stolee2d657ab2020-03-20 12:38:10 +00004396 sparse = git_env_bool("GIT_TEST_PACK_SPARSE", -1);
Johannes Schindelin059fda12022-02-08 11:21:53 +00004397 if (the_repository->gitdir) {
4398 prepare_repo_settings(the_repository);
4399 if (sparse < 0)
4400 sparse = the_repository->settings.pack_use_sparse;
Taylor Blau23c1e712024-02-05 17:50:23 -05004401 if (the_repository->settings.pack_use_multi_pack_reuse)
4402 allow_pack_reuse = MULTI_PACK_REUSE;
Johannes Schindelin059fda12022-02-08 11:21:53 +00004403 }
Derrick Stolee7211b9e2019-08-13 11:37:43 -07004404
Junio C Hamanoebcfb372011-02-25 15:43:25 -08004405 reset_pack_idx_option(&pack_idx_opts);
Taylor Blaua8dd7e02023-04-12 18:20:33 -04004406 pack_idx_opts.flags |= WRITE_REV;
Johannes Schindelinef90d6d2008-05-14 18:46:53 +01004407 git_config(git_pack_config, NULL);
Taylor Blau9f7f10a2023-04-12 18:20:36 -04004408 if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX, 0))
4409 pack_idx_opts.flags &= ~WRITE_REV;
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004410
4411 progress = isatty(2);
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004412 argc = parse_options(argc, argv, prefix, pack_objects_options,
4413 pack_usage, 0);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004414
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004415 if (argc) {
4416 base_name = argv[0];
4417 argc--;
4418 }
4419 if (pack_to_stdout != !base_name || argc)
4420 usage_with_options(pack_usage, pack_objects_options);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004421
Jeff King6d52b6a2021-05-01 10:04:34 -04004422 if (depth < 0)
4423 depth = 0;
Nguyễn Thái Ngọc Duyb5c0cbd2018-04-14 17:35:03 +02004424 if (depth >= (1 << OE_DEPTH_BITS)) {
4425 warning(_("delta chain depth %d is too deep, forcing %d"),
4426 depth, (1 << OE_DEPTH_BITS) - 1);
4427 depth = (1 << OE_DEPTH_BITS) - 1;
4428 }
Nguyễn Thái Ngọc Duy0cb3c142018-04-14 17:35:07 +02004429 if (cache_max_small_delta_size >= (1U << OE_Z_DELTA_BITS)) {
4430 warning(_("pack.deltaCacheLimit is too high, forcing %d"),
4431 (1U << OE_Z_DELTA_BITS) - 1);
4432 cache_max_small_delta_size = (1U << OE_Z_DELTA_BITS) - 1;
4433 }
Jeff King953aa542021-05-01 10:03:37 -04004434 if (window < 0)
4435 window = 0;
Nguyễn Thái Ngọc Duyb5c0cbd2018-04-14 17:35:03 +02004436
Jeff King22f9b7f2020-07-28 16:24:27 -04004437 strvec_push(&rp, "pack-objects");
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004438 if (thin) {
4439 use_internal_rev_list = 1;
Jeff King22f9b7f2020-07-28 16:24:27 -04004440 strvec_push(&rp, shallow
brian m. carlson2dacf262014-12-24 23:05:40 +00004441 ? "--objects-edge-aggressive"
4442 : "--objects-edge");
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004443 } else
Jeff King22f9b7f2020-07-28 16:24:27 -04004444 strvec_push(&rp, "--objects");
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004445
4446 if (rev_list_all) {
4447 use_internal_rev_list = 1;
Jeff King22f9b7f2020-07-28 16:24:27 -04004448 strvec_push(&rp, "--all");
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004449 }
4450 if (rev_list_reflog) {
4451 use_internal_rev_list = 1;
Jeff King22f9b7f2020-07-28 16:24:27 -04004452 strvec_push(&rp, "--reflog");
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004453 }
Jeff Kingc90f9e12014-10-16 20:44:49 -04004454 if (rev_list_index) {
4455 use_internal_rev_list = 1;
Jeff King22f9b7f2020-07-28 16:24:27 -04004456 strvec_push(&rp, "--indexed-objects");
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004457 }
Taylor Blau339bce22021-02-22 21:25:10 -05004458 if (rev_list_unpacked && !stdin_packs) {
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004459 use_internal_rev_list = 1;
Jeff King22f9b7f2020-07-28 16:24:27 -04004460 strvec_push(&rp, "--unpacked");
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004461 }
4462
Jonathan Tan0c16cd42017-12-08 15:27:16 +00004463 if (exclude_promisor_objects) {
4464 use_internal_rev_list = 1;
4465 fetch_if_missing = 0;
Jeff King22f9b7f2020-07-28 16:24:27 -04004466 strvec_push(&rp, "--exclude-promisor-objects");
Jonathan Tan0c16cd42017-12-08 15:27:16 +00004467 }
Nguyễn Thái Ngọc Duy58bd77b2018-05-05 10:47:16 +02004468 if (unpack_unreachable || keep_unreachable || pack_loose_unreachable)
4469 use_internal_rev_list = 1;
Jonathan Tan0c16cd42017-12-08 15:27:16 +00004470
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004471 if (!reuse_object)
4472 reuse_delta = 0;
4473 if (pack_compression_level == -1)
4474 pack_compression_level = Z_DEFAULT_COMPRESSION;
4475 else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004476 die(_("bad pack compression level %d"), pack_compression_level);
Junio C Hamano0c45d252014-10-13 12:46:14 -07004477
4478 if (!delta_search_threads) /* --threads=0 means autodetect */
4479 delta_search_threads = online_cpus();
4480
Nguyễn Thái Ngọc Duy9c897c52018-11-03 09:48:46 +01004481 if (!HAVE_THREADS && delta_search_threads != 1)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004482 warning(_("no threads support, ignoring --threads"));
Taylor Blau61568ef2023-08-28 18:49:07 -04004483 if (!pack_to_stdout && !pack_size_limit)
Johannes Schindelin2b84b5a2008-02-05 14:25:04 +00004484 pack_size_limit = pack_size_limit_cfg;
Dana How01c12a22007-05-23 10:11:33 -07004485 if (pack_to_stdout && pack_size_limit)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004486 die(_("--max-pack-size cannot be used to build a pack for transfer"));
Nicolas Pitre07cf0f22010-02-03 22:48:28 -05004487 if (pack_size_limit && pack_size_limit < 1024*1024) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004488 warning(_("minimum pack size limit is 1 MiB"));
Nicolas Pitre07cf0f22010-02-03 22:48:28 -05004489 pack_size_limit = 1024*1024;
4490 }
Dana How01c12a22007-05-23 10:11:33 -07004491
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07004492 if (!pack_to_stdout && thin)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004493 die(_("--thin cannot be used to build an indexable pack"));
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004494
Nicolas Pitreca11b212008-05-14 01:33:53 -04004495 if (keep_unreachable && unpack_unreachable)
Jean-Noël Avila12909b62022-01-05 20:02:16 +00004496 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "--unpack-unreachable");
Jeff Kingb1e757f2014-10-16 20:44:54 -04004497 if (!rev_list_all || !rev_list_reflog || !rev_list_index)
4498 unpack_unreachable_expiration = 0;
Nicolas Pitreca11b212008-05-14 01:33:53 -04004499
Christian Couder6cfcabf2023-10-02 18:54:56 +02004500 if (stdin_packs && filter_options.choice)
4501 die(_("cannot use --filter with --stdin-packs"));
Jeff Hostetler9535ce72017-11-21 20:58:52 +00004502
Taylor Blau339bce22021-02-22 21:25:10 -05004503 if (stdin_packs && use_internal_rev_list)
4504 die(_("cannot use internal rev list with --stdin-packs"));
4505
Taylor Blaub7573532022-05-20 19:17:52 -04004506 if (cruft) {
4507 if (use_internal_rev_list)
4508 die(_("cannot use internal rev list with --cruft"));
4509 if (stdin_packs)
4510 die(_("cannot use --stdin-packs with --cruft"));
Taylor Blaub7573532022-05-20 19:17:52 -04004511 }
4512
Kirill Smelkov645c4322016-09-10 18:01:44 +03004513 /*
4514 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
4515 *
4516 * - to produce good pack (with bitmap index not-yet-packed objects are
4517 * packed in suboptimal order).
4518 *
4519 * - to use more robust pack-generation codepath (avoiding possible
4520 * bugs in bitmap code and possible bitmap index corruption).
4521 */
4522 if (!pack_to_stdout)
4523 use_bitmap_index_default = 0;
4524
4525 if (use_bitmap_index < 0)
4526 use_bitmap_index = use_bitmap_index_default;
4527
4528 /* "hard" reasons not to use bitmaps; these just won't work at all */
Stefan Bellerc8813482018-05-17 15:51:46 -07004529 if (!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) || is_repository_shallow(the_repository))
Vicent Marti6b8fda22013-12-21 09:00:09 -05004530 use_bitmap_index = 0;
4531
Vicent Marti7cc8f972013-12-21 09:00:16 -05004532 if (pack_to_stdout || !rev_list_all)
4533 write_bitmap_index = 0;
4534
Jeff King28b8a732018-08-16 08:13:09 +02004535 if (use_delta_islands)
Jeff King22f9b7f2020-07-28 16:24:27 -04004536 strvec_push(&rp, "--topo-order");
Jeff King28b8a732018-08-16 08:13:09 +02004537
Nicolas Pitre4f366272009-11-23 12:43:50 -05004538 if (progress && all_progress_implied)
4539 progress = 2;
4540
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004541 add_extra_kept_packs(&keep_pack_list);
4542 if (ignore_packed_keep_on_disk) {
Jeff King56dfeb62016-07-29 00:11:31 -04004543 struct packed_git *p;
Derrick Stolee454ea2e2018-08-20 16:52:04 +00004544 for (p = get_all_packs(the_repository); p; p = p->next)
Jeff King56dfeb62016-07-29 00:11:31 -04004545 if (p->pack_local && p->pack_keep)
4546 break;
4547 if (!p) /* no keep-able packs found */
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004548 ignore_packed_keep_on_disk = 0;
Jeff King56dfeb62016-07-29 00:11:31 -04004549 }
4550 if (local) {
4551 /*
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004552 * unlike ignore_packed_keep_on_disk above, we do not
4553 * want to unset "local" based on looking at packs, as
4554 * it also covers non-local objects
Jeff King56dfeb62016-07-29 00:11:31 -04004555 */
4556 struct packed_git *p;
Derrick Stolee454ea2e2018-08-20 16:52:04 +00004557 for (p = get_all_packs(the_repository); p; p = p->next) {
Jeff King56dfeb62016-07-29 00:11:31 -04004558 if (!p->pack_local) {
4559 have_non_local_packs = 1;
4560 break;
4561 }
4562 }
4563 }
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004564
Derrick Stoleeae417802019-02-22 14:25:07 -08004565 trace2_region_enter("pack-objects", "enumerate-objects",
4566 the_repository);
Nguyễn Thái Ngọc Duy7c141122018-11-10 06:49:08 +01004567 prepare_packing_data(the_repository, &to_pack);
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02004568
Taylor Blaub7573532022-05-20 19:17:52 -04004569 if (progress && !cruft)
Nguyễn Thái Ngọc Duy5af05042018-04-15 17:36:18 +02004570 progress_state = start_progress(_("Enumerating objects"), 0);
Taylor Blau339bce22021-02-22 21:25:10 -05004571 if (stdin_packs) {
4572 /* avoids adding objects in excluded packs */
4573 ignore_packed_keep_in_core = 1;
4574 read_packs_list_from_stdin();
4575 if (rev_list_unpacked)
4576 add_unreachable_loose_objects();
Taylor Blaub7573532022-05-20 19:17:52 -04004577 } else if (cruft) {
4578 read_cruft_objects();
Taylor Blau9e39acc2021-10-26 17:01:13 -04004579 } else if (!use_internal_rev_list) {
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004580 read_object_list_from_stdin();
Taylor Blau9e39acc2021-10-26 17:01:13 -04004581 } else {
Ævar Arnfjörð Bjarmason5cb28272022-03-28 17:43:18 +02004582 struct rev_info revs;
4583
4584 repo_init_revisions(the_repository, &revs, NULL);
René Scharfe0d5448a2022-11-29 13:25:05 +01004585 list_objects_filter_copy(&revs.filter, &filter_options);
Derrick Stolee80f6de42022-03-22 17:28:36 +00004586 get_object_list(&revs, rp.nr, rp.v);
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02004587 release_revisions(&revs);
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07004588 }
Nicolas Pitre0ef95f72009-09-03 21:54:03 -04004589 cleanup_preferred_base();
Michael Haggertyd1552542015-05-25 18:38:38 +00004590 if (include_tag && nr_result)
Jacob Vosmaerbe181532021-01-20 13:45:14 +01004591 for_each_tag_ref(add_ref_tag, NULL);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -04004592 stop_progress(&progress_state);
Derrick Stoleeae417802019-02-22 14:25:07 -08004593 trace2_region_leave("pack-objects", "enumerate-objects",
4594 the_repository);
Nicolas Pitre96a02f82007-04-18 14:27:45 -04004595
Junio C Hamanof0b0af12006-02-24 21:55:23 -08004596 if (non_empty && !nr_result)
Taylor Blau9e39acc2021-10-26 17:01:13 -04004597 goto cleanup;
Derrick Stoleeae417802019-02-22 14:25:07 -08004598 if (nr_result) {
4599 trace2_region_enter("pack-objects", "prepare-pack",
4600 the_repository);
Nicolas Pitref7ae6a92007-04-16 12:30:15 -04004601 prepare_pack(window, depth);
Derrick Stoleeae417802019-02-22 14:25:07 -08004602 trace2_region_leave("pack-objects", "prepare-pack",
4603 the_repository);
4604 }
4605
4606 trace2_region_enter("pack-objects", "write-pack-file", the_repository);
Jonathan Tandd4b7322020-06-10 13:57:23 -07004607 write_excluded_by_configs();
Dana L. Howd01fb922007-05-13 11:34:56 -07004608 write_pack_file();
Derrick Stoleeae417802019-02-22 14:25:07 -08004609 trace2_region_leave("pack-objects", "write-pack-file", the_repository);
4610
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08004611 if (progress)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004612 fprintf_ln(stderr,
4613 _("Total %"PRIu32" (delta %"PRIu32"),"
Jeff Kingbab28d92019-09-13 15:02:17 +02004614 " reused %"PRIu32" (delta %"PRIu32"),"
Taylor Blaub96289a2023-12-14 17:24:20 -05004615 " pack-reused %"PRIu32" (from %"PRIuMAX")"),
Jeff Kingbab28d92019-09-13 15:02:17 +02004616 written, written_delta, reused, reused_delta,
Taylor Blaub96289a2023-12-14 17:24:20 -05004617 reuse_packfile_objects,
4618 (uintmax_t)reuse_packfiles_used_nr);
Taylor Blau9e39acc2021-10-26 17:01:13 -04004619
Taylor Blau54393e42023-12-14 17:24:36 -05004620 trace2_data_intmax("pack-objects", the_repository, "written", written);
4621 trace2_data_intmax("pack-objects", the_repository, "written/delta", written_delta);
4622 trace2_data_intmax("pack-objects", the_repository, "reused", reused);
4623 trace2_data_intmax("pack-objects", the_repository, "reused/delta", reused_delta);
4624 trace2_data_intmax("pack-objects", the_repository, "pack-reused", reuse_packfile_objects);
4625 trace2_data_intmax("pack-objects", the_repository, "packs-reused", reuse_packfiles_used_nr);
Taylor Blau9e39acc2021-10-26 17:01:13 -04004626
4627cleanup:
Taylor Blau66f0c712023-12-14 17:23:39 -05004628 clear_packing_data(&to_pack);
René Scharfe0d5448a2022-11-29 13:25:05 +01004629 list_objects_filter_release(&filter_options);
Taylor Blau9e39acc2021-10-26 17:01:13 -04004630 strvec_clear(&rp);
4631
Linus Torvaldsc323ac72005-06-25 14:42:43 -07004632 return 0;
4633}