blob: d2a162d52804cf7fdf1fd1a11303f2380c24c9e3 [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 "blob.h"
10#include "commit.h"
11#include "tag.h"
12#include "tree.h"
Linus Torvaldsc323ac72005-06-25 14:42:43 -070013#include "delta.h"
Linus Torvaldsa733cb62005-06-28 14:21:02 -070014#include "pack.h"
Nicolas Pitre3449f8c2008-02-28 00:25:17 -050015#include "pack-revindex.h"
Linus Torvaldsc38138c2005-06-26 20:27:56 -070016#include "csum-file.h"
Junio C Hamano1b0c7172006-03-29 22:55:43 -080017#include "tree-walk.h"
Junio C Hamanob5d97e62006-09-04 23:47:39 -070018#include "diff.h"
19#include "revision.h"
20#include "list-objects.h"
Jeff Hostetler9535ce72017-11-21 20:58:52 +000021#include "list-objects-filter.h"
22#include "list-objects-filter-options.h"
Vicent Marti2834bc22013-10-24 14:01:06 -040023#include "pack-objects.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040024#include "progress.h"
Shawn O. Pearcef0a24aa2008-03-03 22:27:20 -050025#include "refs.h"
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +070026#include "streaming.h"
Johannes Sixt93749192010-04-08 09:15:39 +020027#include "thread-utils.h"
Vicent Marti6b8fda22013-12-21 09:00:09 -050028#include "pack-bitmap.h"
Jeff King28b8a732018-08-16 08:13:09 +020029#include "delta-islands.h"
Jeff Kingabcb8652014-10-15 18:42:09 -040030#include "reachable.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -040031#include "oid-array.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040032#include "strvec.h"
Gargi Sharmaec2dd322018-01-23 18:46:51 -050033#include "list.h"
Jonathan Tan0317f452017-08-18 15:20:19 -070034#include "packfile.h"
Elijah Newren87bed172023-04-11 00:41:53 -070035#include "object-file.h"
Elijah Newrena034e912023-05-16 06:34:06 +000036#include "object-store-ll.h"
Elijah Newrencbeab742023-02-24 00:09:33 +000037#include "replace-object.h"
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +020038#include "dir.h"
Derrick Stolee6a22d522018-08-20 16:52:08 +000039#include "midx.h"
Derrick Stoleeae417802019-02-22 14:25:07 -080040#include "trace2.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060041#include "shallow.h"
Jonathan Tane00549a2020-07-20 17:21:44 -070042#include "promisor-remote.h"
Taylor Blaub7573532022-05-20 19:17:52 -040043#include "pack-mtimes.h"
SZEDER Gábor49fd5512023-03-19 17:27:11 +010044#include "parse-options.h"
Nicolas Pitre8ecce682007-09-06 02:13:11 -040045
Ævar Arnfjörð Bjarmason7d089fb2021-05-27 02:52:51 +020046/*
47 * Objects we are going to pack are collected in the `to_pack` structure.
48 * It contains an array (dynamically expanded) of the object data, and a map
49 * that can resolve SHA1s to their position in the array.
50 */
51static struct packing_data to_pack;
52
53static inline struct object_entry *oe_delta(
54 const struct packing_data *pack,
55 const struct object_entry *e)
56{
57 if (!e->delta_idx)
58 return NULL;
59 if (e->ext_base)
60 return &pack->ext_bases[e->delta_idx - 1];
61 else
62 return &pack->objects[e->delta_idx - 1];
63}
64
65static inline unsigned long oe_delta_size(struct packing_data *pack,
66 const struct object_entry *e)
67{
68 if (e->delta_size_valid)
69 return e->delta_size_;
70
71 /*
72 * pack->delta_size[] can't be NULL because oe_set_delta_size()
73 * must have been called when a new delta is saved with
74 * oe_set_delta().
75 * If oe_delta() returns NULL (i.e. default state, which means
76 * delta_size_valid is also false), then the caller must never
77 * call oe_delta_size().
78 */
79 return pack->delta_size[e - pack->objects];
80}
81
82unsigned long oe_get_size_slow(struct packing_data *pack,
83 const struct object_entry *e);
84
85static inline unsigned long oe_size(struct packing_data *pack,
86 const struct object_entry *e)
87{
88 if (e->size_valid)
89 return e->size_;
90
91 return oe_get_size_slow(pack, e);
92}
93
94static inline void oe_set_delta(struct packing_data *pack,
95 struct object_entry *e,
96 struct object_entry *delta)
97{
98 if (delta)
99 e->delta_idx = (delta - pack->objects) + 1;
100 else
101 e->delta_idx = 0;
102}
103
104static inline struct object_entry *oe_delta_sibling(
105 const struct packing_data *pack,
106 const struct object_entry *e)
107{
108 if (e->delta_sibling_idx)
109 return &pack->objects[e->delta_sibling_idx - 1];
110 return NULL;
111}
112
113static inline struct object_entry *oe_delta_child(
114 const struct packing_data *pack,
115 const struct object_entry *e)
116{
117 if (e->delta_child_idx)
118 return &pack->objects[e->delta_child_idx - 1];
119 return NULL;
120}
121
122static inline void oe_set_delta_child(struct packing_data *pack,
123 struct object_entry *e,
124 struct object_entry *delta)
125{
126 if (delta)
127 e->delta_child_idx = (delta - pack->objects) + 1;
128 else
129 e->delta_child_idx = 0;
130}
131
132static inline void oe_set_delta_sibling(struct packing_data *pack,
133 struct object_entry *e,
134 struct object_entry *delta)
135{
136 if (delta)
137 e->delta_sibling_idx = (delta - pack->objects) + 1;
138 else
139 e->delta_sibling_idx = 0;
140}
141
142static inline void oe_set_size(struct packing_data *pack,
143 struct object_entry *e,
144 unsigned long size)
145{
146 if (size < pack->oe_size_limit) {
147 e->size_ = size;
148 e->size_valid = 1;
149 } else {
150 e->size_valid = 0;
151 if (oe_get_size_slow(pack, e) != size)
152 BUG("'size' is supposed to be the object size!");
153 }
154}
155
156static inline void oe_set_delta_size(struct packing_data *pack,
157 struct object_entry *e,
158 unsigned long size)
159{
160 if (size < pack->oe_delta_size_limit) {
161 e->delta_size_ = size;
162 e->delta_size_valid = 1;
163 } else {
164 packing_data_lock(pack);
165 if (!pack->delta_size)
166 ALLOC_ARRAY(pack->delta_size, pack->nr_alloc);
167 packing_data_unlock(pack);
168
169 pack->delta_size[e - pack->objects] = size;
170 e->delta_size_valid = 0;
171 }
172}
173
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +0200174#define IN_PACK(obj) oe_in_pack(&to_pack, obj)
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +0200175#define SIZE(obj) oe_size(&to_pack, obj)
176#define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size)
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +0200177#define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj)
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200178#define DELTA(obj) oe_delta(&to_pack, obj)
179#define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj)
180#define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj)
181#define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val)
Jeff King6a1e32d2018-08-21 15:07:05 -0400182#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 +0200183#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 +0200184#define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val)
185#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 +0200186
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +0700187static const char *pack_usage[] = {
Ævar Arnfjörð Bjarmason23a92352022-10-13 17:39:08 +0200188 N_("git pack-objects --stdout [<options>] [< <ref-list> | < <object-list>]"),
189 N_("git pack-objects [<options>] <base-name> [< <ref-list> | < <object-list>]"),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +0700190 NULL
191};
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700192
Nicolas Pitre79814f42007-11-01 23:43:24 -0400193static struct pack_idx_entry **written_list;
Nguyễn Thái Ngọc Duy5af05042018-04-15 17:36:18 +0200194static uint32_t nr_result, nr_written, nr_seen;
Jeff King6a1e32d2018-08-21 15:07:05 -0400195static struct bitmap_index *bitmap_git;
Jeff King28b8a732018-08-16 08:13:09 +0200196static uint32_t write_layer;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800197
David Rientjes96f1e582006-08-15 10:23:48 -0700198static int non_empty;
Nicolas Pitrea7de7132008-05-02 15:11:46 -0400199static int reuse_delta = 1, reuse_object = 1;
Junio C Hamanoe5e97142008-05-23 16:06:01 -0700200static int keep_unreachable, unpack_unreachable, include_tag;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200201static timestamp_t unpack_unreachable_expiration;
Jeff Kinge26a8c42016-06-13 00:38:04 -0400202static int pack_loose_unreachable;
Taylor Blaub7573532022-05-20 19:17:52 -0400203static int cruft;
204static timestamp_t cruft_expiration;
David Rientjes96f1e582006-08-15 10:23:48 -0700205static int local;
Jeff King56dfeb62016-07-29 00:11:31 -0400206static int have_non_local_packs;
David Rientjes96f1e582006-08-15 10:23:48 -0700207static int incremental;
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +0200208static int ignore_packed_keep_on_disk;
209static int ignore_packed_keep_in_core;
Nicolas Pitrebe6b1912006-09-21 00:09:44 -0400210static int allow_ofs_delta;
Junio C Hamanoebcfb372011-02-25 15:43:25 -0800211static struct pack_idx_option pack_idx_opts;
Dana L. Howd01fb922007-05-13 11:34:56 -0700212static const char *base_name;
Junio C Hamano024701f2006-02-12 13:01:54 -0800213static int progress = 1;
Jeff King4812a932006-07-23 01:50:30 -0400214static int window = 10;
Junio C Hamano568508e2011-10-28 14:48:40 -0700215static unsigned long pack_size_limit;
Theodore Ts'o618e6132007-05-08 09:28:26 -0400216static int depth = 50;
Nicolas Pitre43cc2b42008-12-11 15:36:47 -0500217static int delta_search_threads;
Junio C Hamanodf6d6102006-09-01 15:05:12 -0700218static int pack_to_stdout;
Derrick Stolee4f6d26b2019-01-16 10:25:58 -0800219static int sparse;
Jeff King6a1e32d2018-08-21 15:07:05 -0400220static int thin;
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700221static int num_preferred_base;
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400222static struct progress *progress_state;
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700223
Vicent Marti6b8fda22013-12-21 09:00:09 -0500224static struct packed_git *reuse_packfile;
225static uint32_t reuse_packfile_objects;
Jeff Kingbb514de2019-12-18 12:25:45 +0100226static struct bitmap *reuse_packfile_bitmap;
Vicent Marti6b8fda22013-12-21 09:00:09 -0500227
Kirill Smelkov645c4322016-09-10 18:01:44 +0300228static int use_bitmap_index_default = 1;
229static int use_bitmap_index = -1;
Jeff Kinge704fc72019-12-18 12:25:43 +0100230static int allow_pack_reuse = 1;
Jeff King25575012019-07-31 01:39:27 -0400231static enum {
232 WRITE_BITMAP_FALSE = 0,
233 WRITE_BITMAP_QUIET,
234 WRITE_BITMAP_TRUE,
235} write_bitmap_index;
Jeff Kingd4316602019-03-15 02:25:28 -0400236static uint16_t write_bitmap_options = BITMAP_OPT_HASH_CACHE;
Vicent Marti6b8fda22013-12-21 09:00:09 -0500237
Jonathan Tan0c16cd42017-12-08 15:27:16 +0000238static int exclude_promisor_objects;
239
Jeff King28b8a732018-08-16 08:13:09 +0200240static int use_delta_islands;
241
Martin Koegler074b2ee2007-05-28 23:20:58 +0200242static unsigned long delta_cache_size = 0;
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +0200243static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
Martin Koeglere3dfddb2007-05-28 23:20:59 +0200244static unsigned long cache_max_small_delta_size = 1000;
Martin Koegler074b2ee2007-05-28 23:20:58 +0200245
Brian Downinga97773c2007-07-12 08:07:46 -0500246static unsigned long window_memory_limit = 0;
247
Jonathan Tandd4b7322020-06-10 13:57:23 -0700248static struct string_list uri_protocols = STRING_LIST_INIT_NODUP;
249
Jeff Hostetler9535ce72017-11-21 20:58:52 +0000250enum missing_action {
Jonathan Tan0c16cd42017-12-08 15:27:16 +0000251 MA_ERROR = 0, /* fail if any missing objects are encountered */
252 MA_ALLOW_ANY, /* silently allow ALL missing objects */
253 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
Jeff Hostetler9535ce72017-11-21 20:58:52 +0000254};
255static enum missing_action arg_missing_action;
256static show_object_fn fn_show_object;
257
Jonathan Tandd4b7322020-06-10 13:57:23 -0700258struct configured_exclusion {
259 struct oidmap_entry e;
260 char *pack_hash_hex;
261 char *uri;
262};
263static struct oidmap configured_exclusions;
264
265static struct oidset excluded_by_config;
266
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800267/*
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800268 * stats
269 */
Shawn O. Pearce7cadf492007-03-06 20:44:24 -0500270static uint32_t written, written_delta;
271static uint32_t reused, reused_delta;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800272
Vicent Marti7cc8f972013-12-21 09:00:16 -0500273/*
274 * Indexed commits
275 */
276static struct commit **indexed_commits;
277static unsigned int indexed_commits_nr;
278static unsigned int indexed_commits_alloc;
279
280static void index_commit_for_bitmap(struct commit *commit)
281{
282 if (indexed_commits_nr >= indexed_commits_alloc) {
283 indexed_commits_alloc = (indexed_commits_alloc + 32) * 2;
René Scharfe2756ca42014-09-16 20:56:57 +0200284 REALLOC_ARRAY(indexed_commits, indexed_commits_alloc);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500285 }
286
287 indexed_commits[indexed_commits_nr++] = commit;
288}
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400289
Nicolas Pitre3613f9b2008-05-02 15:11:45 -0400290static void *get_delta(struct object_entry *entry)
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700291{
Nicolas Pitre3613f9b2008-05-02 15:11:45 -0400292 unsigned long size, base_size, delta_size;
293 void *buf, *base_buf, *delta_buf;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500294 enum object_type type;
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700295
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200296 buf = repo_read_object_file(the_repository, &entry->idx.oid, &type,
297 &size);
Nicolas Pitre3613f9b2008-05-02 15:11:45 -0400298 if (!buf)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +0200299 die(_("unable to read %s"), oid_to_hex(&entry->idx.oid));
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200300 base_buf = repo_read_object_file(the_repository,
301 &DELTA(entry)->idx.oid, &type,
302 &base_size);
Nicolas Pitre3613f9b2008-05-02 15:11:45 -0400303 if (!base_buf)
brian m. carlsone6a492b2017-05-06 22:10:11 +0000304 die("unable to read %s",
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200305 oid_to_hex(&DELTA(entry)->idx.oid));
Nicolas Pitre3613f9b2008-05-02 15:11:45 -0400306 delta_buf = diff_delta(base_buf, base_size,
Nicolas Pitredcde55b2005-06-29 02:49:56 -0400307 buf, size, &delta_size, 0);
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200308 /*
Elijah Newren15beaaa2019-11-05 17:07:23 +0000309 * We successfully computed this delta once but dropped it for
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200310 * memory reasons. Something is very wrong if this time we
311 * recompute and create a different delta.
312 */
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +0200313 if (!delta_buf || delta_size != DELTA_SIZE(entry))
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200314 BUG("delta size changed");
Nicolas Pitre3613f9b2008-05-02 15:11:45 -0400315 free(buf);
316 free(base_buf);
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700317 return delta_buf;
318}
319
Nicolas Pitre30ebb402008-05-02 15:11:49 -0400320static unsigned long do_compress(void **pptr, unsigned long size)
321{
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700322 git_zstream stream;
Nicolas Pitre30ebb402008-05-02 15:11:49 -0400323 void *in, *out;
324 unsigned long maxsize;
325
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700326 git_deflate_init(&stream, pack_compression_level);
Junio C Hamano225a6f12011-06-10 11:18:17 -0700327 maxsize = git_deflate_bound(&stream, size);
Nicolas Pitre30ebb402008-05-02 15:11:49 -0400328
329 in = *pptr;
330 out = xmalloc(maxsize);
331 *pptr = out;
332
333 stream.next_in = in;
334 stream.avail_in = size;
335 stream.next_out = out;
336 stream.avail_out = maxsize;
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700337 while (git_deflate(&stream, Z_FINISH) == Z_OK)
Nicolas Pitre30ebb402008-05-02 15:11:49 -0400338 ; /* nothing */
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700339 git_deflate_end(&stream);
Nicolas Pitre30ebb402008-05-02 15:11:49 -0400340
341 free(in);
342 return stream.total_out;
343}
344
brian m. carlson98a3bea2018-02-01 02:18:46 +0000345static unsigned long write_large_blob_data(struct git_istream *st, struct hashfile *f,
brian m. carlson188960b2017-10-15 22:07:01 +0000346 const struct object_id *oid)
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700347{
348 git_zstream stream;
349 unsigned char ibuf[1024 * 16];
350 unsigned char obuf[1024 * 16];
351 unsigned long olen = 0;
352
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700353 git_deflate_init(&stream, pack_compression_level);
354
355 for (;;) {
356 ssize_t readlen;
357 int zret = Z_OK;
358 readlen = read_istream(st, ibuf, sizeof(ibuf));
359 if (readlen == -1)
brian m. carlson188960b2017-10-15 22:07:01 +0000360 die(_("unable to read %s"), oid_to_hex(oid));
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700361
362 stream.next_in = ibuf;
363 stream.avail_in = readlen;
364 while ((stream.avail_in || readlen == 0) &&
365 (zret == Z_OK || zret == Z_BUF_ERROR)) {
366 stream.next_out = obuf;
367 stream.avail_out = sizeof(obuf);
368 zret = git_deflate(&stream, readlen ? 0 : Z_FINISH);
brian m. carlson98a3bea2018-02-01 02:18:46 +0000369 hashwrite(f, obuf, stream.next_out - obuf);
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700370 olen += stream.next_out - obuf;
371 }
372 if (stream.avail_in)
373 die(_("deflate error (%d)"), zret);
374 if (readlen == 0) {
375 if (zret != Z_STREAM_END)
376 die(_("deflate error (%d)"), zret);
377 break;
378 }
379 }
380 git_deflate_end(&stream);
381 return olen;
382}
383
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700384/*
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400385 * we are going to reuse the existing object data as is. make
386 * sure it is not corrupt.
387 */
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500388static int check_pack_inflate(struct packed_git *p,
389 struct pack_window **w_curs,
Shawn O. Pearce6777a592007-03-06 20:44:34 -0500390 off_t offset,
391 off_t len,
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500392 unsigned long expect)
393{
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700394 git_zstream stream;
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500395 unsigned char fakebuf[4096], *in;
396 int st;
397
398 memset(&stream, 0, sizeof(stream));
Linus Torvalds39c68542009-01-07 19:54:47 -0800399 git_inflate_init(&stream);
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500400 do {
401 in = use_pack(p, w_curs, offset, &stream.avail_in);
402 stream.next_in = in;
403 stream.next_out = fakebuf;
404 stream.avail_out = sizeof(fakebuf);
Linus Torvalds39c68542009-01-07 19:54:47 -0800405 st = git_inflate(&stream, Z_FINISH);
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500406 offset += stream.next_in - in;
407 } while (st == Z_OK || st == Z_BUF_ERROR);
Linus Torvalds39c68542009-01-07 19:54:47 -0800408 git_inflate_end(&stream);
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500409 return (st == Z_STREAM_END &&
410 stream.total_out == expect &&
411 stream.total_in == len) ? 0 : -1;
412}
413
brian m. carlson98a3bea2018-02-01 02:18:46 +0000414static void copy_pack_data(struct hashfile *f,
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500415 struct packed_git *p,
416 struct pack_window **w_curs,
Shawn O. Pearce6777a592007-03-06 20:44:34 -0500417 off_t offset,
418 off_t len)
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500419{
420 unsigned char *in;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700421 unsigned long avail;
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500422
423 while (len) {
424 in = use_pack(p, w_curs, offset, &avail);
425 if (avail > len)
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700426 avail = (unsigned long)len;
brian m. carlson98a3bea2018-02-01 02:18:46 +0000427 hashwrite(f, in, avail);
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500428 offset += avail;
429 len -= avail;
430 }
431}
432
Ævar Arnfjörð Bjarmason7d089fb2021-05-27 02:52:51 +0200433static inline int oe_size_greater_than(struct packing_data *pack,
434 const struct object_entry *lhs,
435 unsigned long rhs)
436{
437 if (lhs->size_valid)
438 return lhs->size_ > rhs;
439 if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
440 return 1;
441 return oe_get_size_slow(pack, lhs) > rhs;
442}
443
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700444/* Return 0 if we will bust the pack-size limit */
brian m. carlson98a3bea2018-02-01 02:18:46 +0000445static 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 +0700446 unsigned long limit, int usable_delta)
447{
448 unsigned long size, datalen;
Jeff King2c5e2862017-03-24 13:26:50 -0400449 unsigned char header[MAX_PACK_OBJECT_HEADER],
450 dheader[MAX_PACK_OBJECT_HEADER];
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700451 unsigned hdrlen;
452 enum object_type type;
453 void *buf;
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700454 struct git_istream *st = NULL;
brian m. carlson41179102018-05-02 00:25:37 +0000455 const unsigned hashsz = the_hash_algo->rawsz;
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700456
457 if (!usable_delta) {
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +0200458 if (oe_type(entry) == OBJ_BLOB &&
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +0200459 oe_size_greater_than(&to_pack, entry, big_file_threshold) &&
Matheus Tavaresc8123e72020-01-30 17:32:20 -0300460 (st = open_istream(the_repository, &entry->idx.oid, &type,
461 &size, NULL)) != NULL)
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700462 buf = NULL;
463 else {
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200464 buf = repo_read_object_file(the_repository,
465 &entry->idx.oid, &type,
466 &size);
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700467 if (!buf)
brian m. carlsone6a492b2017-05-06 22:10:11 +0000468 die(_("unable to read %s"),
469 oid_to_hex(&entry->idx.oid));
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700470 }
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700471 /*
472 * make sure no cached delta data remains from a
473 * previous attempt before a pack split occurred.
474 */
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000475 FREE_AND_NULL(entry->delta_data);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700476 entry->z_delta_size = 0;
477 } else if (entry->delta_data) {
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +0200478 size = DELTA_SIZE(entry);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700479 buf = entry->delta_data;
480 entry->delta_data = NULL;
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200481 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700482 OBJ_OFS_DELTA : OBJ_REF_DELTA;
483 } else {
484 buf = get_delta(entry);
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +0200485 size = DELTA_SIZE(entry);
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200486 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700487 OBJ_OFS_DELTA : OBJ_REF_DELTA;
488 }
489
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700490 if (st) /* large blob case, just assume we don't compress well */
491 datalen = size;
492 else if (entry->z_delta_size)
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700493 datalen = entry->z_delta_size;
494 else
495 datalen = do_compress(&buf, size);
496
497 /*
498 * The object header is a byte of 'type' followed by zero or
499 * more bytes of length.
500 */
Jeff King7202a6f2017-03-24 13:26:40 -0400501 hdrlen = encode_in_pack_object_header(header, sizeof(header),
502 type, size);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700503
504 if (type == OBJ_OFS_DELTA) {
505 /*
506 * Deltas with relative base contain an additional
507 * encoding of the relative offset for the delta
508 * base from this object's position in the pack.
509 */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200510 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700511 unsigned pos = sizeof(dheader) - 1;
512 dheader[pos] = ofs & 127;
513 while (ofs >>= 7)
514 dheader[--pos] = 128 | (--ofs & 127);
brian m. carlson41179102018-05-02 00:25:37 +0000515 if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700516 if (st)
517 close_istream(st);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700518 free(buf);
519 return 0;
520 }
brian m. carlson98a3bea2018-02-01 02:18:46 +0000521 hashwrite(f, header, hdrlen);
522 hashwrite(f, dheader + pos, sizeof(dheader) - pos);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700523 hdrlen += sizeof(dheader) - pos;
524 } else if (type == OBJ_REF_DELTA) {
525 /*
526 * Deltas with a base reference contain
brian m. carlson41179102018-05-02 00:25:37 +0000527 * additional bytes for the base object ID.
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700528 */
brian m. carlson41179102018-05-02 00:25:37 +0000529 if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700530 if (st)
531 close_istream(st);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700532 free(buf);
533 return 0;
534 }
brian m. carlson98a3bea2018-02-01 02:18:46 +0000535 hashwrite(f, header, hdrlen);
Junio C Hamano42c8ce12018-05-30 14:04:10 +0900536 hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
brian m. carlson41179102018-05-02 00:25:37 +0000537 hdrlen += hashsz;
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700538 } else {
brian m. carlson41179102018-05-02 00:25:37 +0000539 if (limit && hdrlen + datalen + hashsz >= limit) {
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700540 if (st)
541 close_istream(st);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700542 free(buf);
543 return 0;
544 }
brian m. carlson98a3bea2018-02-01 02:18:46 +0000545 hashwrite(f, header, hdrlen);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700546 }
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700547 if (st) {
brian m. carlson188960b2017-10-15 22:07:01 +0000548 datalen = write_large_blob_data(st, f, &entry->idx.oid);
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700549 close_istream(st);
550 } else {
brian m. carlson98a3bea2018-02-01 02:18:46 +0000551 hashwrite(f, buf, datalen);
Nguyễn Thái Ngọc Duycf2ba132012-05-26 17:28:01 +0700552 free(buf);
553 }
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700554
555 return hdrlen + datalen;
556}
557
558/* Return 0 if we will bust the pack-size limit */
brian m. carlson98a3bea2018-02-01 02:18:46 +0000559static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
Nguyễn Thái Ngọc Duyaf92a642016-07-13 17:44:03 +0200560 unsigned long limit, int usable_delta)
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700561{
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +0200562 struct packed_git *p = IN_PACK(entry);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700563 struct pack_window *w_curs = NULL;
Taylor Blau952fc682021-01-13 17:23:35 -0500564 uint32_t pos;
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700565 off_t offset;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +0200566 enum object_type type = oe_type(entry);
Nguyễn Thái Ngọc Duy211c61c2016-07-05 19:05:54 +0200567 off_t datalen;
Jeff King2c5e2862017-03-24 13:26:50 -0400568 unsigned char header[MAX_PACK_OBJECT_HEADER],
569 dheader[MAX_PACK_OBJECT_HEADER];
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700570 unsigned hdrlen;
brian m. carlson41179102018-05-02 00:25:37 +0000571 const unsigned hashsz = the_hash_algo->rawsz;
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +0200572 unsigned long entry_size = SIZE(entry);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700573
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200574 if (DELTA(entry))
575 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700576 OBJ_OFS_DELTA : OBJ_REF_DELTA;
Jeff King7202a6f2017-03-24 13:26:40 -0400577 hdrlen = encode_in_pack_object_header(header, sizeof(header),
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +0200578 type, entry_size);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700579
580 offset = entry->in_pack_offset;
Taylor Blau952fc682021-01-13 17:23:35 -0500581 if (offset_to_pack_pos(p, offset, &pos) < 0)
582 die(_("write_reuse_object: could not locate %s, expected at "
583 "offset %"PRIuMAX" in pack %s"),
584 oid_to_hex(&entry->idx.oid), (uintmax_t)offset,
585 p->pack_name);
586 datalen = pack_pos_to_offset(p, pos + 1) - offset;
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700587 if (!pack_to_stdout && p->index_version > 1 &&
Taylor Blau952fc682021-01-13 17:23:35 -0500588 check_pack_crc(p, &w_curs, offset, datalen,
589 pack_pos_to_index(p, pos))) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +0200590 error(_("bad packed object CRC for %s"),
brian m. carlsone6a492b2017-05-06 22:10:11 +0000591 oid_to_hex(&entry->idx.oid));
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700592 unuse_pack(&w_curs);
593 return write_no_reuse_object(f, entry, limit, usable_delta);
594 }
595
596 offset += entry->in_pack_header_size;
597 datalen -= entry->in_pack_header_size;
598
599 if (!pack_to_stdout && p->index_version == 1 &&
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +0200600 check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +0200601 error(_("corrupt packed object for %s"),
brian m. carlsone6a492b2017-05-06 22:10:11 +0000602 oid_to_hex(&entry->idx.oid));
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700603 unuse_pack(&w_curs);
604 return write_no_reuse_object(f, entry, limit, usable_delta);
605 }
606
607 if (type == OBJ_OFS_DELTA) {
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200608 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700609 unsigned pos = sizeof(dheader) - 1;
610 dheader[pos] = ofs & 127;
611 while (ofs >>= 7)
612 dheader[--pos] = 128 | (--ofs & 127);
brian m. carlson41179102018-05-02 00:25:37 +0000613 if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700614 unuse_pack(&w_curs);
615 return 0;
616 }
brian m. carlson98a3bea2018-02-01 02:18:46 +0000617 hashwrite(f, header, hdrlen);
618 hashwrite(f, dheader + pos, sizeof(dheader) - pos);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700619 hdrlen += sizeof(dheader) - pos;
620 reused_delta++;
621 } else if (type == OBJ_REF_DELTA) {
brian m. carlson41179102018-05-02 00:25:37 +0000622 if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700623 unuse_pack(&w_curs);
624 return 0;
625 }
brian m. carlson98a3bea2018-02-01 02:18:46 +0000626 hashwrite(f, header, hdrlen);
Junio C Hamano42c8ce12018-05-30 14:04:10 +0900627 hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
brian m. carlson41179102018-05-02 00:25:37 +0000628 hdrlen += hashsz;
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700629 reused_delta++;
630 } else {
brian m. carlson41179102018-05-02 00:25:37 +0000631 if (limit && hdrlen + datalen + hashsz >= limit) {
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700632 unuse_pack(&w_curs);
633 return 0;
634 }
brian m. carlson98a3bea2018-02-01 02:18:46 +0000635 hashwrite(f, header, hdrlen);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700636 }
637 copy_pack_data(f, p, &w_curs, offset, datalen);
638 unuse_pack(&w_curs);
639 reused++;
640 return hdrlen + datalen;
641}
642
643/* Return 0 if we will bust the pack-size limit */
brian m. carlson98a3bea2018-02-01 02:18:46 +0000644static off_t write_object(struct hashfile *f,
Nguyễn Thái Ngọc Duyaf92a642016-07-13 17:44:03 +0200645 struct object_entry *entry,
646 off_t write_offset)
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700647{
Nguyễn Thái Ngọc Duyaf92a642016-07-13 17:44:03 +0200648 unsigned long limit;
649 off_t len;
Nicolas Pitre2c5ef822008-05-02 15:11:48 -0400650 int usable_delta, to_reuse;
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700651
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400652 if (!pack_to_stdout)
653 crc32_begin(f);
654
Nicolas Pitrea2430dd2010-02-03 22:48:27 -0500655 /* apply size limit if limited packsize and not first object */
Nicolas Pitrea1e47602008-11-12 13:23:58 -0500656 if (!pack_size_limit || !nr_written)
657 limit = 0;
658 else if (pack_size_limit <= write_offset)
659 /*
660 * the earlier object did not fit the limit; avoid
661 * mistaking this with unlimited (i.e. limit = 0).
662 */
663 limit = 1;
664 else
665 limit = pack_size_limit - write_offset;
Nicolas Pitre2c5ef822008-05-02 15:11:48 -0400666
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200667 if (!DELTA(entry))
Nicolas Pitre2c5ef822008-05-02 15:11:48 -0400668 usable_delta = 0; /* no delta */
669 else if (!pack_size_limit)
670 usable_delta = 1; /* unlimited packfile */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200671 else if (DELTA(entry)->idx.offset == (off_t)-1)
Nicolas Pitre2c5ef822008-05-02 15:11:48 -0400672 usable_delta = 0; /* base was written to another pack */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200673 else if (DELTA(entry)->idx.offset)
Nicolas Pitre2c5ef822008-05-02 15:11:48 -0400674 usable_delta = 1; /* base already exists in this pack */
675 else
676 usable_delta = 0; /* base could end up in another pack */
677
Nicolas Pitrea7de7132008-05-02 15:11:46 -0400678 if (!reuse_object)
Nicolas Pitrefa736f72007-05-09 12:31:28 -0400679 to_reuse = 0; /* explicit */
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +0200680 else if (!IN_PACK(entry))
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -0800681 to_reuse = 0; /* can't reuse what we don't have */
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +0200682 else if (oe_type(entry) == OBJ_REF_DELTA ||
683 oe_type(entry) == OBJ_OFS_DELTA)
Dana L. How17b08f22007-05-13 12:06:18 -0700684 /* check_object() decided it for us ... */
685 to_reuse = usable_delta;
686 /* ... but pack split may override that */
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +0200687 else if (oe_type(entry) != entry->in_pack_type)
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -0800688 to_reuse = 0; /* pack has delta which is unusable */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200689 else if (DELTA(entry))
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -0800690 to_reuse = 0; /* we want to pack afresh */
691 else
692 to_reuse = 1; /* we have it in-pack undeltified,
693 * and we do not need to deltify it.
694 */
695
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700696 if (!to_reuse)
697 len = write_no_reuse_object(f, entry, limit, usable_delta);
698 else
699 len = write_reuse_object(f, entry, limit, usable_delta);
700 if (!len)
701 return 0;
Nicolas Pitreed4a9032008-05-02 15:11:50 -0400702
Dana L. How17b08f22007-05-13 12:06:18 -0700703 if (usable_delta)
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -0800704 written_delta++;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800705 written++;
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400706 if (!pack_to_stdout)
Geert Boschaa7e44b2007-06-01 15:18:05 -0400707 entry->idx.crc32 = crc32_end(f);
Nguyễn Thái Ngọc Duyc9018b02012-05-16 19:02:10 +0700708 return len;
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700709}
710
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800711enum write_one_status {
712 WRITE_ONE_SKIP = -1, /* already written */
713 WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */
714 WRITE_ONE_WRITTEN = 1, /* normal */
715 WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */
716};
717
brian m. carlson98a3bea2018-02-01 02:18:46 +0000718static enum write_one_status write_one(struct hashfile *f,
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800719 struct object_entry *e,
720 off_t *offset)
Junio C Hamano9d5ab962005-06-28 17:49:27 -0700721{
Nguyễn Thái Ngọc Duyaf92a642016-07-13 17:44:03 +0200722 off_t size;
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800723 int recursing;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400724
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800725 /*
726 * we set offset to 1 (which is an impossible value) to mark
727 * the fact that this object is involved in "write its base
728 * first before writing a deltified object" recursion.
729 */
730 recursing = (e->idx.offset == 1);
731 if (recursing) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +0200732 warning(_("recursive delta detected for object %s"),
brian m. carlsone6a492b2017-05-06 22:10:11 +0000733 oid_to_hex(&e->idx.oid));
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800734 return WRITE_ONE_RECURSIVE;
735 } else if (e->idx.offset || e->preferred_base) {
736 /* offset is non zero if object is written already. */
737 return WRITE_ONE_SKIP;
738 }
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400739
Nicolas Pitre720c9f72010-02-08 10:39:01 -0500740 /* if we are deltified, write out base object first. */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200741 if (DELTA(e)) {
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800742 e->idx.offset = 1; /* now recurse */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200743 switch (write_one(f, DELTA(e), offset)) {
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800744 case WRITE_ONE_RECURSIVE:
745 /* we cannot depend on this one */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200746 SET_DELTA(e, NULL);
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800747 break;
748 default:
749 break;
750 case WRITE_ONE_BREAK:
751 e->idx.offset = recursing;
752 return WRITE_ONE_BREAK;
753 }
754 }
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400755
Nicolas Pitre6ed7f252008-08-29 16:07:58 -0400756 e->idx.offset = *offset;
757 size = write_object(f, e, *offset);
Dana L. How17b08f22007-05-13 12:06:18 -0700758 if (!size) {
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800759 e->idx.offset = recursing;
760 return WRITE_ONE_BREAK;
Dana L. How17b08f22007-05-13 12:06:18 -0700761 }
Nicolas Pitre79814f42007-11-01 23:43:24 -0400762 written_list[nr_written++] = &e->idx;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400763
764 /* make sure off_t is sufficiently large not to wrap */
Erik Faye-Lundc03c8312010-10-05 09:24:10 +0200765 if (signed_add_overflows(*offset, size))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +0200766 die(_("pack too large for current definition of off_t"));
Nicolas Pitre6ed7f252008-08-29 16:07:58 -0400767 *offset += size;
Junio C Hamanof63c79d2011-11-16 22:04:03 -0800768 return WRITE_ONE_WRITTEN;
Junio C Hamano9d5ab962005-06-28 17:49:27 -0700769}
770
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200771static int mark_tagged(const char *path UNUSED, const struct object_id *oid,
772 int flag UNUSED, void *cb_data UNUSED)
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700773{
brian m. carlson188960b2017-10-15 22:07:01 +0000774 struct object_id peeled;
Jeff King3a378762019-09-05 21:36:05 -0400775 struct object_entry *entry = packlist_find(&to_pack, oid);
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700776
777 if (entry)
778 entry->tagged = 1;
Jeff King36a31792021-01-20 14:44:43 -0500779 if (!peel_iterated_oid(oid, &peeled)) {
Jeff King3a378762019-09-05 21:36:05 -0400780 entry = packlist_find(&to_pack, &peeled);
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700781 if (entry)
782 entry->tagged = 1;
783 }
784 return 0;
785}
786
Ævar Arnfjörð Bjarmason7d089fb2021-05-27 02:52:51 +0200787static inline unsigned char oe_layer(struct packing_data *pack,
788 struct object_entry *e)
789{
790 if (!pack->layer)
791 return 0;
792 return pack->layer[e - pack->objects];
793}
794
Dan McGeebe126812011-10-18 00:21:21 -0500795static inline void add_to_write_order(struct object_entry **wo,
Dan McGee92bef1a2011-10-18 00:21:22 -0500796 unsigned int *endp,
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700797 struct object_entry *e)
798{
Christian Couderfe0ac2f2018-08-16 08:13:13 +0200799 if (e->filled || oe_layer(&to_pack, e) != write_layer)
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700800 return;
801 wo[(*endp)++] = e;
802 e->filled = 1;
803}
804
805static void add_descendants_to_write_order(struct object_entry **wo,
Dan McGee92bef1a2011-10-18 00:21:22 -0500806 unsigned int *endp,
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700807 struct object_entry *e)
808{
Dan McGeef3808722011-10-18 00:21:24 -0500809 int add_to_order = 1;
810 while (e) {
811 if (add_to_order) {
812 struct object_entry *s;
813 /* add this node... */
814 add_to_write_order(wo, endp, e);
815 /* all its siblings... */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200816 for (s = DELTA_SIBLING(e); s; s = DELTA_SIBLING(s)) {
Dan McGeef3808722011-10-18 00:21:24 -0500817 add_to_write_order(wo, endp, s);
818 }
819 }
820 /* drop down a level to add left subtree nodes if possible */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200821 if (DELTA_CHILD(e)) {
Dan McGeef3808722011-10-18 00:21:24 -0500822 add_to_order = 1;
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200823 e = DELTA_CHILD(e);
Dan McGeef3808722011-10-18 00:21:24 -0500824 } else {
825 add_to_order = 0;
826 /* our sibling might have some children, it is next */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200827 if (DELTA_SIBLING(e)) {
828 e = DELTA_SIBLING(e);
Dan McGeef3808722011-10-18 00:21:24 -0500829 continue;
830 }
831 /* go back to our parent node */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200832 e = DELTA(e);
833 while (e && !DELTA_SIBLING(e)) {
Dan McGeef3808722011-10-18 00:21:24 -0500834 /* we're on the right side of a subtree, keep
835 * going up until we can go right again */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200836 e = DELTA(e);
Dan McGeef3808722011-10-18 00:21:24 -0500837 }
838 if (!e) {
839 /* done- we hit our original root node */
840 return;
841 }
842 /* pass it off to sibling at this level */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200843 e = DELTA_SIBLING(e);
Dan McGeef3808722011-10-18 00:21:24 -0500844 }
845 };
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700846}
847
848static void add_family_to_write_order(struct object_entry **wo,
Dan McGee92bef1a2011-10-18 00:21:22 -0500849 unsigned int *endp,
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700850 struct object_entry *e)
851{
852 struct object_entry *root;
853
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200854 for (root = e; DELTA(root); root = DELTA(root))
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700855 ; /* nothing */
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700856 add_descendants_to_write_order(wo, endp, root);
857}
858
Christian Couderf64ba532018-08-16 08:13:08 +0200859static void compute_layer_order(struct object_entry **wo, unsigned int *wo_end)
860{
861 unsigned int i, last_untagged;
862 struct object_entry *objects = to_pack.objects;
863
864 for (i = 0; i < to_pack.nr_objects; i++) {
865 if (objects[i].tagged)
866 break;
867 add_to_write_order(wo, wo_end, &objects[i]);
868 }
869 last_untagged = i;
870
871 /*
872 * Then fill all the tagged tips.
873 */
874 for (; i < to_pack.nr_objects; i++) {
875 if (objects[i].tagged)
876 add_to_write_order(wo, wo_end, &objects[i]);
877 }
878
879 /*
880 * And then all remaining commits and tags.
881 */
882 for (i = last_untagged; i < to_pack.nr_objects; i++) {
883 if (oe_type(&objects[i]) != OBJ_COMMIT &&
884 oe_type(&objects[i]) != OBJ_TAG)
885 continue;
886 add_to_write_order(wo, wo_end, &objects[i]);
887 }
888
889 /*
890 * And then all the trees.
891 */
892 for (i = last_untagged; i < to_pack.nr_objects; i++) {
893 if (oe_type(&objects[i]) != OBJ_TREE)
894 continue;
895 add_to_write_order(wo, wo_end, &objects[i]);
896 }
897
898 /*
899 * Finally all the rest in really tight order
900 */
901 for (i = last_untagged; i < to_pack.nr_objects; i++) {
Christian Couderfe0ac2f2018-08-16 08:13:13 +0200902 if (!objects[i].filled && oe_layer(&to_pack, &objects[i]) == write_layer)
Christian Couderf64ba532018-08-16 08:13:08 +0200903 add_family_to_write_order(wo, wo_end, &objects[i]);
904 }
905}
906
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700907static struct object_entry **compute_write_order(void)
908{
Jeff King28b8a732018-08-16 08:13:09 +0200909 uint32_t max_layers = 1;
Christian Couderf64ba532018-08-16 08:13:08 +0200910 unsigned int i, wo_end;
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700911
Jeff Kingb32fa952016-02-22 17:44:25 -0500912 struct object_entry **wo;
Vicent Marti2834bc22013-10-24 14:01:06 -0400913 struct object_entry *objects = to_pack.objects;
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700914
Vicent Marti2834bc22013-10-24 14:01:06 -0400915 for (i = 0; i < to_pack.nr_objects; i++) {
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700916 objects[i].tagged = 0;
917 objects[i].filled = 0;
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200918 SET_DELTA_CHILD(&objects[i], NULL);
919 SET_DELTA_SIBLING(&objects[i], NULL);
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700920 }
921
922 /*
923 * Fully connect delta_child/delta_sibling network.
924 * Make sure delta_sibling is sorted in the original
925 * recency order.
926 */
Vicent Marti2834bc22013-10-24 14:01:06 -0400927 for (i = to_pack.nr_objects; i > 0;) {
Dan McGee92bef1a2011-10-18 00:21:22 -0500928 struct object_entry *e = &objects[--i];
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200929 if (!DELTA(e))
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700930 continue;
931 /* Mark me as the first child */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +0200932 e->delta_sibling_idx = DELTA(e)->delta_child_idx;
933 SET_DELTA_CHILD(DELTA(e), e);
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700934 }
935
936 /*
937 * Mark objects that are at the tip of tags.
938 */
Michael Haggertyd1552542015-05-25 18:38:38 +0000939 for_each_tag_ref(mark_tagged, NULL);
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700940
Eric Wong647982b2023-02-03 23:44:30 +0000941 if (use_delta_islands) {
Jeff King28b8a732018-08-16 08:13:09 +0200942 max_layers = compute_pack_layers(&to_pack);
Eric Wong647982b2023-02-03 23:44:30 +0000943 free_island_marks();
944 }
Jeff King28b8a732018-08-16 08:13:09 +0200945
Jeff Kingb32fa952016-02-22 17:44:25 -0500946 ALLOC_ARRAY(wo, to_pack.nr_objects);
Christian Couderf64ba532018-08-16 08:13:08 +0200947 wo_end = 0;
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700948
Jeff King28b8a732018-08-16 08:13:09 +0200949 for (; write_layer < max_layers; ++write_layer)
950 compute_layer_order(wo, &wo_end);
Dan McGee38d4deb2011-10-18 00:21:23 -0500951
Vicent Marti2834bc22013-10-24 14:01:06 -0400952 if (wo_end != to_pack.nr_objects)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +0200953 die(_("ordered %u objects, expected %"PRIu32),
954 wo_end, to_pack.nr_objects);
Junio C Hamano1b4bb162011-06-30 16:21:58 -0700955
956 return wo;
957}
958
Jeff Kingbb514de2019-12-18 12:25:45 +0100959
960/*
961 * A reused set of objects. All objects in a chunk have the same
962 * relative position in the original packfile and the generated
963 * packfile.
964 */
965
966static struct reused_chunk {
967 /* The offset of the first object of this chunk in the original
968 * packfile. */
969 off_t original;
Han Xinbf120132021-03-23 11:20:50 +0800970 /* The difference for "original" minus the offset of the first object of
971 * this chunk in the generated packfile. */
Jeff Kingbb514de2019-12-18 12:25:45 +0100972 off_t difference;
973} *reused_chunks;
974static int reused_chunks_nr;
975static int reused_chunks_alloc;
976
977static void record_reused_object(off_t where, off_t offset)
Vicent Marti6b8fda22013-12-21 09:00:09 -0500978{
Jeff Kingbb514de2019-12-18 12:25:45 +0100979 if (reused_chunks_nr && reused_chunks[reused_chunks_nr-1].difference == offset)
980 return;
Vicent Marti6b8fda22013-12-21 09:00:09 -0500981
Jeff Kingbb514de2019-12-18 12:25:45 +0100982 ALLOC_GROW(reused_chunks, reused_chunks_nr + 1,
983 reused_chunks_alloc);
984 reused_chunks[reused_chunks_nr].original = where;
985 reused_chunks[reused_chunks_nr].difference = offset;
986 reused_chunks_nr++;
987}
Vicent Marti6b8fda22013-12-21 09:00:09 -0500988
Jeff Kingbb514de2019-12-18 12:25:45 +0100989/*
990 * Binary search to find the chunk that "where" is in. Note
991 * that we're not looking for an exact match, just the first
992 * chunk that contains it (which implicitly ends at the start
993 * of the next chunk.
994 */
995static off_t find_reused_offset(off_t where)
996{
997 int lo = 0, hi = reused_chunks_nr;
998 while (lo < hi) {
999 int mi = lo + ((hi - lo) / 2);
1000 if (where == reused_chunks[mi].original)
1001 return reused_chunks[mi].difference;
1002 if (where < reused_chunks[mi].original)
1003 hi = mi;
1004 else
1005 lo = mi + 1;
Vicent Marti6b8fda22013-12-21 09:00:09 -05001006 }
1007
Jeff Kingbb514de2019-12-18 12:25:45 +01001008 /*
1009 * The first chunk starts at zero, so we can't have gone below
1010 * there.
1011 */
1012 assert(lo);
1013 return reused_chunks[lo-1].difference;
1014}
1015
1016static void write_reused_pack_one(size_t pos, struct hashfile *out,
1017 struct pack_window **w_curs)
1018{
1019 off_t offset, next, cur;
1020 enum object_type type;
1021 unsigned long size;
1022
Taylor Blau66cbd3e2021-01-13 17:23:39 -05001023 offset = pack_pos_to_offset(reuse_packfile, pos);
1024 next = pack_pos_to_offset(reuse_packfile, pos + 1);
Jeff Kingbb514de2019-12-18 12:25:45 +01001025
1026 record_reused_object(offset, offset - hashfile_total(out));
1027
1028 cur = offset;
1029 type = unpack_object_header(reuse_packfile, w_curs, &cur, &size);
1030 assert(type >= 0);
1031
1032 if (type == OBJ_OFS_DELTA) {
1033 off_t base_offset;
1034 off_t fixup;
1035
1036 unsigned char header[MAX_PACK_OBJECT_HEADER];
1037 unsigned len;
1038
1039 base_offset = get_delta_base(reuse_packfile, w_curs, &cur, type, offset);
1040 assert(base_offset != 0);
1041
1042 /* Convert to REF_DELTA if we must... */
1043 if (!allow_ofs_delta) {
Taylor Blau66cbd3e2021-01-13 17:23:39 -05001044 uint32_t base_pos;
Jeff Kingf66d4e02020-02-23 23:31:22 -05001045 struct object_id base_oid;
1046
Taylor Blau66cbd3e2021-01-13 17:23:39 -05001047 if (offset_to_pack_pos(reuse_packfile, base_offset, &base_pos) < 0)
1048 die(_("expected object at offset %"PRIuMAX" "
1049 "in pack %s"),
1050 (uintmax_t)base_offset,
1051 reuse_packfile->pack_name);
1052
Jeff Kingf66d4e02020-02-23 23:31:22 -05001053 nth_packed_object_id(&base_oid, reuse_packfile,
Taylor Blau66cbd3e2021-01-13 17:23:39 -05001054 pack_pos_to_index(reuse_packfile, base_pos));
Jeff Kingbb514de2019-12-18 12:25:45 +01001055
1056 len = encode_in_pack_object_header(header, sizeof(header),
1057 OBJ_REF_DELTA, size);
1058 hashwrite(out, header, len);
Junio C Hamanof8cb64e2020-03-26 17:11:20 -07001059 hashwrite(out, base_oid.hash, the_hash_algo->rawsz);
Jeff Kingbb514de2019-12-18 12:25:45 +01001060 copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur);
1061 return;
1062 }
1063
1064 /* Otherwise see if we need to rewrite the offset... */
1065 fixup = find_reused_offset(offset) -
1066 find_reused_offset(base_offset);
1067 if (fixup) {
1068 unsigned char ofs_header[10];
1069 unsigned i, ofs_len;
1070 off_t ofs = offset - base_offset - fixup;
1071
1072 len = encode_in_pack_object_header(header, sizeof(header),
1073 OBJ_OFS_DELTA, size);
1074
1075 i = sizeof(ofs_header) - 1;
1076 ofs_header[i] = ofs & 127;
1077 while (ofs >>= 7)
1078 ofs_header[--i] = 128 | (--ofs & 127);
1079
1080 ofs_len = sizeof(ofs_header) - i;
1081
1082 hashwrite(out, header, len);
1083 hashwrite(out, ofs_header + sizeof(ofs_header) - ofs_len, ofs_len);
1084 copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur);
1085 return;
1086 }
1087
1088 /* ...otherwise we have no fixup, and can write it verbatim */
1089 }
1090
1091 copy_pack_data(out, reuse_packfile, w_curs, offset, next - offset);
1092}
1093
1094static size_t write_reused_pack_verbatim(struct hashfile *out,
1095 struct pack_window **w_curs)
1096{
1097 size_t pos = 0;
1098
1099 while (pos < reuse_packfile_bitmap->word_alloc &&
1100 reuse_packfile_bitmap->words[pos] == (eword_t)~0)
1101 pos++;
1102
1103 if (pos) {
1104 off_t to_write;
1105
1106 written = (pos * BITS_IN_EWORD);
Taylor Blau6a5c10c2021-01-13 17:23:43 -05001107 to_write = pack_pos_to_offset(reuse_packfile, written)
Jeff Kingbb514de2019-12-18 12:25:45 +01001108 - sizeof(struct pack_header);
1109
1110 /* We're recording one chunk, not one object. */
1111 record_reused_object(sizeof(struct pack_header), 0);
1112 hashflush(out);
1113 copy_pack_data(out, reuse_packfile, w_curs,
1114 sizeof(struct pack_header), to_write);
1115
1116 display_progress(progress_state, written);
1117 }
1118 return pos;
1119}
1120
1121static void write_reused_pack(struct hashfile *f)
1122{
1123 size_t i = 0;
1124 uint32_t offset;
1125 struct pack_window *w_curs = NULL;
1126
1127 if (allow_ofs_delta)
1128 i = write_reused_pack_verbatim(f, &w_curs);
1129
1130 for (; i < reuse_packfile_bitmap->word_alloc; ++i) {
1131 eword_t word = reuse_packfile_bitmap->words[i];
1132 size_t pos = (i * BITS_IN_EWORD);
1133
1134 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1135 if ((word >> offset) == 0)
1136 break;
1137
1138 offset += ewah_bit_ctz64(word >> offset);
Taylor Blau0f533c72021-08-31 16:52:21 -04001139 /*
1140 * Can use bit positions directly, even for MIDX
1141 * bitmaps. See comment in try_partial_reuse()
1142 * for why.
1143 */
Jeff Kingbb514de2019-12-18 12:25:45 +01001144 write_reused_pack_one(pos + offset, f, &w_curs);
1145 display_progress(progress_state, ++written);
1146 }
1147 }
1148
1149 unuse_pack(&w_curs);
Vicent Marti6b8fda22013-12-21 09:00:09 -05001150}
1151
Jonathan Tandd4b7322020-06-10 13:57:23 -07001152static void write_excluded_by_configs(void)
1153{
1154 struct oidset_iter iter;
1155 const struct object_id *oid;
1156
1157 oidset_iter_init(&excluded_by_config, &iter);
1158 while ((oid = oidset_iter_next(&iter))) {
1159 struct configured_exclusion *ex =
1160 oidmap_get(&configured_exclusions, oid);
1161
1162 if (!ex)
1163 BUG("configured exclusion wasn't configured");
1164 write_in_full(1, ex->pack_hash_hex, strlen(ex->pack_hash_hex));
1165 write_in_full(1, " ", 1);
1166 write_in_full(1, ex->uri, strlen(ex->uri));
1167 write_in_full(1, "\n", 1);
1168 }
1169}
1170
Eric Wong9cea46c2016-04-28 07:28:55 +00001171static const char no_split_warning[] = N_(
1172"disabling bitmap writing, packs are split due to pack.packSizeLimit"
1173);
1174
Dana L. Howd01fb922007-05-13 11:34:56 -07001175static void write_pack_file(void)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001176{
Dana L. Howebe27b12007-05-13 12:09:16 -07001177 uint32_t i = 0, j;
brian m. carlson98a3bea2018-02-01 02:18:46 +00001178 struct hashfile *f;
Nicolas Pitre6ed7f252008-08-29 16:07:58 -04001179 off_t offset;
Dana L. Howebe27b12007-05-13 12:09:16 -07001180 uint32_t nr_remaining = nr_result;
Nicolas Pitref746bae2008-03-13 14:59:29 -04001181 time_t last_mtime = 0;
Junio C Hamano1b4bb162011-06-30 16:21:58 -07001182 struct object_entry **write_order;
Nicolas Pitre81a216a2007-04-16 12:31:05 -04001183
Nicolas Pitrebcd79542008-05-02 15:11:47 -04001184 if (progress > pack_to_stdout)
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +07001185 progress_state = start_progress(_("Writing objects"), nr_result);
Jeff Kingb32fa952016-02-22 17:44:25 -05001186 ALLOC_ARRAY(written_list, to_pack.nr_objects);
Junio C Hamano1b4bb162011-06-30 16:21:58 -07001187 write_order = compute_write_order();
Junio C Hamano183bdb22006-02-22 16:02:59 -08001188
Dana L. Howebe27b12007-05-13 12:09:16 -07001189 do {
brian m. carlson71b76722021-04-26 01:02:59 +00001190 unsigned char hash[GIT_MAX_RAWSZ];
Nicolas Pitre7ba502c2007-10-16 21:55:48 -04001191 char *pack_tmp_name = NULL;
Geert Boschaa7e44b2007-06-01 15:18:05 -04001192
Junio C Hamanocdf9db32011-10-28 11:52:14 -07001193 if (pack_to_stdout)
brian m. carlson98a3bea2018-02-01 02:18:46 +00001194 f = hashfd_throughput(1, "<stdout>", progress_state);
Junio C Hamanocdf9db32011-10-28 11:52:14 -07001195 else
1196 f = create_tmp_packfile(&pack_tmp_name);
Nicolas Pitrec553ca22007-04-09 01:06:33 -04001197
Junio C Hamanoc0ad4652011-10-28 11:40:48 -07001198 offset = write_pack_header(f, nr_remaining);
Vicent Marti6b8fda22013-12-21 09:00:09 -05001199
1200 if (reuse_packfile) {
Vicent Marti6b8fda22013-12-21 09:00:09 -05001201 assert(pack_to_stdout);
Jeff Kingbb514de2019-12-18 12:25:45 +01001202 write_reused_pack(f);
1203 offset = hashfile_total(f);
Vicent Marti6b8fda22013-12-21 09:00:09 -05001204 }
1205
Dana L. Howebe27b12007-05-13 12:09:16 -07001206 nr_written = 0;
Vicent Marti2834bc22013-10-24 14:01:06 -04001207 for (; i < to_pack.nr_objects; i++) {
Junio C Hamano1b4bb162011-06-30 16:21:58 -07001208 struct object_entry *e = write_order[i];
Junio C Hamanocddec4f2011-12-05 15:19:34 -08001209 if (write_one(f, e, &offset) == WRITE_ONE_BREAK)
Nicolas Pitre720c9f72010-02-08 10:39:01 -05001210 break;
1211 display_progress(progress_state, written);
1212 }
Dana L. Howebe27b12007-05-13 12:09:16 -07001213
Linus Torvalds54352bb2008-05-30 08:54:46 -07001214 if (pack_to_stdout) {
Neeraj Singh020406e2022-03-10 22:43:21 +00001215 /*
1216 * We never fsync when writing to stdout since we may
1217 * not be writing to an actual pack file. For instance,
1218 * the upload-pack code passes a pipe here. Calling
1219 * fsync on a pipe results in unnecessary
1220 * synchronization with the reader on some platforms.
1221 */
1222 finalize_hashfile(f, hash, FSYNC_COMPONENT_NONE,
1223 CSUM_HASH_IN_STREAM | CSUM_CLOSE);
Linus Torvalds54352bb2008-05-30 08:54:46 -07001224 } else if (nr_written == nr_remaining) {
Neeraj Singh020406e2022-03-10 22:43:21 +00001225 finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK,
1226 CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
Dana L. Howebe27b12007-05-13 12:09:16 -07001227 } else {
Neeraj Singh020406e2022-03-10 22:43:21 +00001228 /*
1229 * If we wrote the wrong number of entries in the
1230 * header, rewrite it like in fast-import.
1231 */
1232
1233 int fd = finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK, 0);
brian m. carlson71b76722021-04-26 01:02:59 +00001234 fixup_pack_header_footer(fd, hash, pack_tmp_name,
1235 nr_written, hash, offset);
Nicolas Pitre7ba502c2007-10-16 21:55:48 -04001236 close(fd);
Eric Wong9cea46c2016-04-28 07:28:55 +00001237 if (write_bitmap_index) {
Jeff King25575012019-07-31 01:39:27 -04001238 if (write_bitmap_index != WRITE_BITMAP_QUIET)
1239 warning(_(no_split_warning));
Eric Wong9cea46c2016-04-28 07:28:55 +00001240 write_bitmap_index = 0;
1241 }
Dana L. Howebe27b12007-05-13 12:09:16 -07001242 }
1243
1244 if (!pack_to_stdout) {
Nicolas Pitref746bae2008-03-13 14:59:29 -04001245 struct stat st;
Sun He58892712014-03-03 17:24:29 +08001246 struct strbuf tmpname = STRBUF_INIT;
Ævar Arnfjörð Bjarmason2ec02dd2021-09-09 19:24:56 -04001247 char *idx_tmp_name = NULL;
Dana L. Howd01fb922007-05-13 11:34:56 -07001248
Nicolas Pitref746bae2008-03-13 14:59:29 -04001249 /*
1250 * Packs are runtime accessed in their mtime
1251 * order since newer packs are more likely to contain
1252 * younger objects. So if we are creating multiple
1253 * packs then we should modify the mtime of later ones
1254 * to preserve this property.
1255 */
Junio C Hamano0e990532011-10-28 12:34:09 -07001256 if (stat(pack_tmp_name, &st) < 0) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02001257 warning_errno(_("failed to stat %s"), pack_tmp_name);
Nicolas Pitref746bae2008-03-13 14:59:29 -04001258 } else if (!last_mtime) {
1259 last_mtime = st.st_mtime;
1260 } else {
1261 struct utimbuf utb;
1262 utb.actime = st.st_atime;
1263 utb.modtime = --last_mtime;
Junio C Hamano0e990532011-10-28 12:34:09 -07001264 if (utime(pack_tmp_name, &utb) < 0)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02001265 warning_errno(_("failed utime() on %s"), pack_tmp_name);
Nicolas Pitref746bae2008-03-13 14:59:29 -04001266 }
1267
Ævar Arnfjörð Bjarmason66833f02021-09-09 19:24:37 -04001268 strbuf_addf(&tmpname, "%s-%s.", base_name,
1269 hash_to_hex(hash));
Vicent Marti7cc8f972013-12-21 09:00:16 -05001270
1271 if (write_bitmap_index) {
brian m. carlson71b76722021-04-26 01:02:59 +00001272 bitmap_writer_set_checksum(hash);
Nguyễn Thái Ngọc Duy06af3bb2018-04-14 17:35:04 +02001273 bitmap_writer_build_type_index(
1274 &to_pack, written_list, nr_written);
Vicent Marti7cc8f972013-12-21 09:00:16 -05001275 }
1276
Taylor Blaub7573532022-05-20 19:17:52 -04001277 if (cruft)
1278 pack_idx_opts.flags |= WRITE_MTIMES;
1279
Ævar Arnfjörð Bjarmason2ec02dd2021-09-09 19:24:56 -04001280 stage_tmp_packfiles(&tmpname, pack_tmp_name,
Junio C Hamano0e990532011-10-28 12:34:09 -07001281 written_list, nr_written,
Taylor Blau1c573cd2022-05-20 19:17:38 -04001282 &to_pack, &pack_idx_opts, hash,
1283 &idx_tmp_name);
Vicent Marti7cc8f972013-12-21 09:00:16 -05001284
1285 if (write_bitmap_index) {
Ævar Arnfjörð Bjarmason66833f02021-09-09 19:24:37 -04001286 size_t tmpname_len = tmpname.len;
Vicent Marti7cc8f972013-12-21 09:00:16 -05001287
Ævar Arnfjörð Bjarmason66833f02021-09-09 19:24:37 -04001288 strbuf_addstr(&tmpname, "bitmap");
Vicent Marti7cc8f972013-12-21 09:00:16 -05001289 stop_progress(&progress_state);
1290
1291 bitmap_writer_show_progress(progress);
Vicent Marti7cc8f972013-12-21 09:00:16 -05001292 bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1);
Taylor Blau3ba3d062021-08-24 12:15:54 -04001293 if (bitmap_writer_build(&to_pack) < 0)
1294 die(_("failed to write bitmap index"));
Vicent Martiae4f07f2013-12-21 09:00:45 -05001295 bitmap_writer_finish(written_list, nr_written,
Sun He58892712014-03-03 17:24:29 +08001296 tmpname.buf, write_bitmap_options);
Vicent Marti7cc8f972013-12-21 09:00:16 -05001297 write_bitmap_index = 0;
Ævar Arnfjörð Bjarmason66833f02021-09-09 19:24:37 -04001298 strbuf_setlen(&tmpname, tmpname_len);
Vicent Marti7cc8f972013-12-21 09:00:16 -05001299 }
1300
Ævar Arnfjörð Bjarmason4bc1fd62021-09-09 19:25:00 -04001301 rename_tmp_packfile_idx(&tmpname, &idx_tmp_name);
1302
Ævar Arnfjörð Bjarmason2ec02dd2021-09-09 19:24:56 -04001303 free(idx_tmp_name);
Sun He58892712014-03-03 17:24:29 +08001304 strbuf_release(&tmpname);
Nicolas Pitre7ba502c2007-10-16 21:55:48 -04001305 free(pack_tmp_name);
brian m. carlson71b76722021-04-26 01:02:59 +00001306 puts(hash_to_hex(hash));
Dana L. Howebe27b12007-05-13 12:09:16 -07001307 }
1308
1309 /* mark written objects as written to previous pack */
1310 for (j = 0; j < nr_written; j++) {
Nicolas Pitre79814f42007-11-01 23:43:24 -04001311 written_list[j]->offset = (off_t)-1;
Dana L. Howebe27b12007-05-13 12:09:16 -07001312 }
1313 nr_remaining -= nr_written;
Vicent Marti2834bc22013-10-24 14:01:06 -04001314 } while (nr_remaining && i < to_pack.nr_objects);
Dana L. Howebe27b12007-05-13 12:09:16 -07001315
1316 free(written_list);
Junio C Hamano1b4bb162011-06-30 16:21:58 -07001317 free(write_order);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -04001318 stop_progress(&progress_state);
Nicolas Pitre67c08ce2006-11-29 17:15:48 -05001319 if (written != nr_result)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02001320 die(_("wrote %"PRIu32" objects while expecting %"PRIu32),
1321 written, nr_result);
Jonathan Tan9ed87902019-04-11 10:36:26 -07001322 trace2_data_intmax("pack-objects", the_repository,
1323 "write_pack_file/wrote", nr_result);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001324}
1325
Junio C Hamanoa74db822007-05-19 00:39:31 -07001326static int no_try_delta(const char *path)
1327{
Junio C Hamano2aef63d2017-01-27 18:01:57 -08001328 static struct attr_check *check;
Junio C Hamanoa74db822007-05-19 00:39:31 -07001329
Junio C Hamano2aef63d2017-01-27 18:01:57 -08001330 if (!check)
1331 check = attr_check_initl("delta", NULL);
John Cai44451a22023-05-06 04:15:29 +00001332 git_check_attr(the_repository->index, path, check);
Junio C Hamano2aef63d2017-01-27 18:01:57 -08001333 if (ATTR_FALSE(check->items[0].value))
Junio C Hamanoa74db822007-05-19 00:39:31 -07001334 return 1;
1335 return 0;
1336}
1337
Jeff Kingce2bc422013-12-21 09:00:06 -05001338/*
1339 * When adding an object, check whether we have already added it
1340 * to our packing list. If so, we can skip. However, if we are
1341 * being asked to excludei t, but the previous mention was to include
1342 * it, make sure to adjust its flags and tweak our numbers accordingly.
1343 *
1344 * As an optimization, we pass out the index position where we would have
1345 * found the item, since that saves us from having to look it up again a
1346 * few lines later when we want to add the new entry.
1347 */
brian m. carlson188960b2017-10-15 22:07:01 +00001348static int have_duplicate_entry(const struct object_id *oid,
Jeff King3a378762019-09-05 21:36:05 -04001349 int exclude)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001350{
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001351 struct object_entry *entry;
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001352
Jeff King92fb0db2019-12-18 12:25:46 +01001353 if (reuse_packfile_bitmap &&
1354 bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid))
1355 return 1;
1356
Jeff King3a378762019-09-05 21:36:05 -04001357 entry = packlist_find(&to_pack, oid);
Jeff Kingce2bc422013-12-21 09:00:06 -05001358 if (!entry)
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001359 return 0;
Jeff Kingce2bc422013-12-21 09:00:06 -05001360
1361 if (exclude) {
1362 if (!entry->preferred_base)
1363 nr_result--;
1364 entry->preferred_base = 1;
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001365 }
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001366
Jeff Kingce2bc422013-12-21 09:00:06 -05001367 return 1;
1368}
1369
Jeff King6325da12021-02-22 21:25:20 -05001370static int want_found_object(const struct object_id *oid, int exclude,
1371 struct packed_git *p)
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001372{
1373 if (exclude)
1374 return 1;
1375 if (incremental)
1376 return 0;
1377
Taylor Blau40905112022-05-24 14:54:36 -04001378 if (!is_pack_valid(p))
1379 return -1;
1380
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001381 /*
1382 * When asked to do --local (do not include an object that appears in a
1383 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
1384 * an object that appears in a pack marked with .keep), finding a pack
1385 * that matches the criteria is sufficient for us to decide to omit it.
1386 * However, even if this pack does not satisfy the criteria, we need to
1387 * make sure no copy of this object appears in _any_ pack that makes us
1388 * to omit the object, so we need to check all the packs.
1389 *
Jeff King6325da12021-02-22 21:25:20 -05001390 * We can however first check whether these options can possibly matter;
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001391 * if they do not matter we know we want the object in generated pack.
1392 * Otherwise, we signal "-1" at the end to tell the caller that we do
1393 * not know either way, and it needs to check more packs.
1394 */
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001395
Jeff King6325da12021-02-22 21:25:20 -05001396 /*
1397 * Objects in packs borrowed from elsewhere are discarded regardless of
1398 * if they appear in other packs that weren't borrowed.
1399 */
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001400 if (local && !p->pack_local)
1401 return 0;
Jeff King6325da12021-02-22 21:25:20 -05001402
1403 /*
1404 * Then handle .keep first, as we have a fast(er) path there.
1405 */
1406 if (ignore_packed_keep_on_disk || ignore_packed_keep_in_core) {
1407 /*
1408 * Set the flags for the kept-pack cache to be the ones we want
1409 * to ignore.
1410 *
1411 * That is, if we are ignoring objects in on-disk keep packs,
1412 * then we want to search through the on-disk keep and ignore
1413 * the in-core ones.
1414 */
1415 unsigned flags = 0;
1416 if (ignore_packed_keep_on_disk)
1417 flags |= ON_DISK_KEEP_PACKS;
1418 if (ignore_packed_keep_in_core)
1419 flags |= IN_CORE_KEEP_PACKS;
1420
1421 if (ignore_packed_keep_on_disk && p->pack_keep)
1422 return 0;
1423 if (ignore_packed_keep_in_core && p->pack_keep_in_core)
1424 return 0;
1425 if (has_object_kept_pack(oid, flags))
1426 return 0;
1427 }
1428
1429 /*
1430 * At this point we know definitively that either we don't care about
1431 * keep-packs, or the object is not in one. Keep checking other
1432 * conditions...
1433 */
1434 if (!local || !have_non_local_packs)
1435 return 1;
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001436
1437 /* we don't know yet; keep looking for more packs */
1438 return -1;
1439}
1440
Jeff King6325da12021-02-22 21:25:20 -05001441static int want_object_in_pack_one(struct packed_git *p,
1442 const struct object_id *oid,
1443 int exclude,
1444 struct packed_git **found_pack,
1445 off_t *found_offset)
1446{
1447 off_t offset;
1448
1449 if (p == *found_pack)
1450 offset = *found_offset;
1451 else
1452 offset = find_pack_entry_one(oid->hash, p);
1453
1454 if (offset) {
1455 if (!*found_pack) {
1456 if (!is_pack_valid(p))
1457 return -1;
1458 *found_offset = offset;
1459 *found_pack = p;
1460 }
1461 return want_found_object(oid, exclude, p);
1462 }
1463 return -1;
1464}
1465
Jeff Kingce2bc422013-12-21 09:00:06 -05001466/*
1467 * Check whether we want the object in the pack (e.g., we do not want
1468 * objects found in non-local stores if the "--local" option was used).
1469 *
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001470 * If the caller already knows an existing pack it wants to take the object
1471 * from, that is passed in *found_pack and *found_offset; otherwise this
1472 * function finds if there is any pack that has the object and returns the pack
1473 * and its offset in these variables.
Jeff Kingce2bc422013-12-21 09:00:06 -05001474 */
brian m. carlson188960b2017-10-15 22:07:01 +00001475static int want_object_in_pack(const struct object_id *oid,
Jeff Kingce2bc422013-12-21 09:00:06 -05001476 int exclude,
1477 struct packed_git **found_pack,
1478 off_t *found_offset)
1479{
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001480 int want;
Olga Telezhnaya88658592017-09-30 17:51:01 +00001481 struct list_head *pos;
Derrick Stolee6a22d522018-08-20 16:52:08 +00001482 struct multi_pack_index *m;
Jeff Kingce2bc422013-12-21 09:00:06 -05001483
brian m. carlson6862ebb2018-05-02 00:25:34 +00001484 if (!exclude && local && has_loose_object_nonlocal(oid))
Brandon Caseydaae0622008-11-09 23:59:58 -06001485 return 0;
1486
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001487 /*
1488 * If we already know the pack object lives in, start checks from that
1489 * pack - in the usual case when neither --local was given nor .keep files
1490 * are present we will determine the answer right now.
1491 */
1492 if (*found_pack) {
Jeff King6325da12021-02-22 21:25:20 -05001493 want = want_found_object(oid, exclude, *found_pack);
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001494 if (want != -1)
1495 return want;
Taylor Blau40905112022-05-24 14:54:36 -04001496
1497 *found_pack = NULL;
1498 *found_offset = 0;
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001499 }
Derrick Stolee6a22d522018-08-20 16:52:08 +00001500
1501 for (m = get_multi_pack_index(the_repository); m; m = m->next) {
1502 struct pack_entry e;
Derrick Stolee64404a22019-04-29 09:18:55 -07001503 if (fill_midx_entry(the_repository, oid, &e, m)) {
Jeff King6325da12021-02-22 21:25:20 -05001504 want = want_object_in_pack_one(e.p, oid, exclude, found_pack, found_offset);
1505 if (want != -1)
1506 return want;
Derrick Stolee6a22d522018-08-20 16:52:08 +00001507 }
1508 }
1509
Stefan Bellera80d72d2018-03-23 18:20:59 +01001510 list_for_each(pos, get_packed_git_mru(the_repository)) {
Gargi Sharmaec2dd322018-01-23 18:46:51 -05001511 struct packed_git *p = list_entry(pos, struct packed_git, mru);
Jeff King6325da12021-02-22 21:25:20 -05001512 want = want_object_in_pack_one(p, oid, exclude, found_pack, found_offset);
1513 if (!exclude && want > 0)
1514 list_move(&p->mru,
1515 get_packed_git_mru(the_repository));
1516 if (want != -1)
1517 return want;
Linus Torvalds64560372005-10-13 15:38:28 -07001518 }
Linus Torvaldseb019372005-07-03 13:08:40 -07001519
Jonathan Tandd4b7322020-06-10 13:57:23 -07001520 if (uri_protocols.nr) {
1521 struct configured_exclusion *ex =
1522 oidmap_get(&configured_exclusions, oid);
1523 int i;
1524 const char *p;
1525
1526 if (ex) {
1527 for (i = 0; i < uri_protocols.nr; i++) {
1528 if (skip_prefix(ex->uri,
1529 uri_protocols.items[i].string,
1530 &p) &&
1531 *p == ':') {
1532 oidset_insert(&excluded_by_config, oid);
1533 return 0;
1534 }
1535 }
1536 }
1537 }
1538
Jeff Kingce2bc422013-12-21 09:00:06 -05001539 return 1;
1540}
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001541
Taylor Blaufa230902022-05-20 19:17:49 -04001542static struct object_entry *create_object_entry(const struct object_id *oid,
1543 enum object_type type,
1544 uint32_t hash,
1545 int exclude,
1546 int no_try_delta,
1547 struct packed_git *found_pack,
1548 off_t found_offset)
Jeff Kingce2bc422013-12-21 09:00:06 -05001549{
1550 struct object_entry *entry;
1551
Jeff King3a378762019-09-05 21:36:05 -04001552 entry = packlist_alloc(&to_pack, oid);
Linus Torvalds27225f22005-06-26 15:27:28 -07001553 entry->hash = hash;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02001554 oe_set_type(entry, type);
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001555 if (exclude)
1556 entry->preferred_base = 1;
Nicolas Pitre81a216a2007-04-16 12:31:05 -04001557 else
1558 nr_result++;
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001559 if (found_pack) {
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02001560 oe_set_in_pack(&to_pack, entry, found_pack);
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001561 entry->in_pack_offset = found_offset;
1562 }
Junio C Hamano7a979d92006-02-19 14:47:21 -08001563
Jeff Kingce2bc422013-12-21 09:00:06 -05001564 entry->no_try_delta = no_try_delta;
Taylor Blaufa230902022-05-20 19:17:49 -04001565
1566 return entry;
Jeff Kingce2bc422013-12-21 09:00:06 -05001567}
Junio C Hamano7a979d92006-02-19 14:47:21 -08001568
Jeff King373c67d2014-03-14 22:38:29 -04001569static const char no_closure_warning[] = N_(
1570"disabling bitmap writing, as some objects are not being packed"
1571);
1572
brian m. carlson188960b2017-10-15 22:07:01 +00001573static int add_object_entry(const struct object_id *oid, enum object_type type,
Jeff Kingce2bc422013-12-21 09:00:06 -05001574 const char *name, int exclude)
1575{
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001576 struct packed_git *found_pack = NULL;
1577 off_t found_offset = 0;
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001578
Nguyễn Thái Ngọc Duy5af05042018-04-15 17:36:18 +02001579 display_progress(progress_state, ++nr_seen);
1580
Jeff King3a378762019-09-05 21:36:05 -04001581 if (have_duplicate_entry(oid, exclude))
Jeff Kingce2bc422013-12-21 09:00:06 -05001582 return 0;
Junio C Hamanoa74db822007-05-19 00:39:31 -07001583
brian m. carlson188960b2017-10-15 22:07:01 +00001584 if (!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {
Jeff King373c67d2014-03-14 22:38:29 -04001585 /* The pack is missing an object, so it will not have closure */
1586 if (write_bitmap_index) {
Jeff King25575012019-07-31 01:39:27 -04001587 if (write_bitmap_index != WRITE_BITMAP_QUIET)
1588 warning(_(no_closure_warning));
Jeff King373c67d2014-03-14 22:38:29 -04001589 write_bitmap_index = 0;
1590 }
Jeff Kingce2bc422013-12-21 09:00:06 -05001591 return 0;
Jeff King373c67d2014-03-14 22:38:29 -04001592 }
Jeff Kingce2bc422013-12-21 09:00:06 -05001593
brian m. carlson188960b2017-10-15 22:07:01 +00001594 create_object_entry(oid, type, pack_name_hash(name),
Jeff Kingce2bc422013-12-21 09:00:06 -05001595 exclude, name && no_try_delta(name),
Jeff King3a378762019-09-05 21:36:05 -04001596 found_pack, found_offset);
Nicolas Pitre29b734e2007-04-10 22:54:36 -04001597 return 1;
Junio C Hamano7a979d92006-02-19 14:47:21 -08001598}
1599
brian m. carlson20664962017-10-15 22:07:00 +00001600static int add_object_entry_from_bitmap(const struct object_id *oid,
Vicent Marti6b8fda22013-12-21 09:00:09 -05001601 enum object_type type,
Jeff Kingc50dca22023-02-24 01:39:22 -05001602 int flags UNUSED, uint32_t name_hash,
Vicent Marti6b8fda22013-12-21 09:00:09 -05001603 struct packed_git *pack, off_t offset)
1604{
Nguyễn Thái Ngọc Duy5af05042018-04-15 17:36:18 +02001605 display_progress(progress_state, ++nr_seen);
1606
Jeff King3a378762019-09-05 21:36:05 -04001607 if (have_duplicate_entry(oid, 0))
Vicent Marti6b8fda22013-12-21 09:00:09 -05001608 return 0;
1609
brian m. carlson188960b2017-10-15 22:07:01 +00001610 if (!want_object_in_pack(oid, 0, &pack, &offset))
Kirill Smelkov702d1b92016-09-10 18:01:10 +03001611 return 0;
1612
Jeff King3a378762019-09-05 21:36:05 -04001613 create_object_entry(oid, type, name_hash, 0, 0, pack, offset);
Junio C Hamano7a979d92006-02-19 14:47:21 -08001614 return 1;
1615}
1616
1617struct pbase_tree_cache {
brian m. carlson188960b2017-10-15 22:07:01 +00001618 struct object_id oid;
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001619 int ref;
1620 int temporary;
1621 void *tree_data;
1622 unsigned long tree_size;
1623};
1624
1625static struct pbase_tree_cache *(pbase_tree_cache[256]);
brian m. carlson188960b2017-10-15 22:07:01 +00001626static int pbase_tree_cache_ix(const struct object_id *oid)
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001627{
brian m. carlson188960b2017-10-15 22:07:01 +00001628 return oid->hash[0] % ARRAY_SIZE(pbase_tree_cache);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001629}
1630static int pbase_tree_cache_ix_incr(int ix)
1631{
1632 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
1633}
1634
1635static struct pbase_tree {
1636 struct pbase_tree *next;
1637 /* This is a phony "cache" entry; we are not
Justin Lebar01689902014-03-31 15:11:46 -07001638 * going to evict it or find it through _get()
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001639 * mechanism -- this is for the toplevel node that
1640 * would almost always change with any commit.
1641 */
1642 struct pbase_tree_cache pcache;
1643} *pbase_tree;
1644
brian m. carlson188960b2017-10-15 22:07:01 +00001645static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001646{
1647 struct pbase_tree_cache *ent, *nent;
1648 void *data;
1649 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001650 enum object_type type;
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001651 int neigh;
brian m. carlson188960b2017-10-15 22:07:01 +00001652 int my_ix = pbase_tree_cache_ix(oid);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001653 int available_ix = -1;
1654
1655 /* pbase-tree-cache acts as a limited hashtable.
1656 * your object will be found at your index or within a few
1657 * slots after that slot if it is cached.
1658 */
1659 for (neigh = 0; neigh < 8; neigh++) {
1660 ent = pbase_tree_cache[my_ix];
Jeff King4a7e27e2018-08-28 17:22:40 -04001661 if (ent && oideq(&ent->oid, oid)) {
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001662 ent->ref++;
1663 return ent;
1664 }
1665 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
1666 ((0 <= available_ix) &&
1667 (!ent && pbase_tree_cache[available_ix])))
1668 available_ix = my_ix;
1669 if (!ent)
1670 break;
1671 my_ix = pbase_tree_cache_ix_incr(my_ix);
1672 }
1673
1674 /* Did not find one. Either we got a bogus request or
1675 * we need to read and perhaps cache.
1676 */
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +02001677 data = repo_read_object_file(the_repository, oid, &type, &size);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001678 if (!data)
1679 return NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001680 if (type != OBJ_TREE) {
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001681 free(data);
1682 return NULL;
1683 }
1684
1685 /* We need to either cache or return a throwaway copy */
1686
1687 if (available_ix < 0)
1688 ent = NULL;
1689 else {
1690 ent = pbase_tree_cache[available_ix];
1691 my_ix = available_ix;
1692 }
1693
1694 if (!ent) {
1695 nent = xmalloc(sizeof(*nent));
1696 nent->temporary = (available_ix < 0);
1697 }
1698 else {
1699 /* evict and reuse */
1700 free(ent->tree_data);
1701 nent = ent;
1702 }
brian m. carlson188960b2017-10-15 22:07:01 +00001703 oidcpy(&nent->oid, oid);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001704 nent->tree_data = data;
1705 nent->tree_size = size;
1706 nent->ref = 1;
1707 if (!nent->temporary)
1708 pbase_tree_cache[my_ix] = nent;
1709 return nent;
1710}
1711
1712static void pbase_tree_put(struct pbase_tree_cache *cache)
1713{
1714 if (!cache->temporary) {
1715 cache->ref--;
1716 return;
1717 }
1718 free(cache->tree_data);
1719 free(cache);
1720}
1721
René Scharfee65b8682023-02-05 11:42:27 +01001722static size_t name_cmp_len(const char *name)
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001723{
René Scharfee65b8682023-02-05 11:42:27 +01001724 return strcspn(name, "\n/");
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001725}
1726
1727static void add_pbase_object(struct tree_desc *tree,
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001728 const char *name,
René Scharfee65b8682023-02-05 11:42:27 +01001729 size_t cmplen,
Linus Torvaldsce0bd642006-06-05 12:03:31 -07001730 const char *fullname)
Junio C Hamano7a979d92006-02-19 14:47:21 -08001731{
Linus Torvalds4c068a92006-05-30 09:45:45 -07001732 struct name_entry entry;
Nicolas Pitre8a5a8d62007-04-16 12:28:10 -04001733 int cmp;
Linus Torvalds4c068a92006-05-30 09:45:45 -07001734
1735 while (tree_entry(tree,&entry)) {
Linus Torvalds1211be62007-08-17 09:56:54 -07001736 if (S_ISGITLINK(entry.mode))
1737 continue;
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +11001738 cmp = tree_entry_len(&entry) != cmplen ? 1 :
Nicolas Pitre8a5a8d62007-04-16 12:28:10 -04001739 memcmp(name, entry.path, cmplen);
1740 if (cmp > 0)
Junio C Hamano7a979d92006-02-19 14:47:21 -08001741 continue;
Nicolas Pitre8a5a8d62007-04-16 12:28:10 -04001742 if (cmp < 0)
1743 return;
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001744 if (name[cmplen] != '/') {
brian m. carlsonea82b2a2019-01-15 00:39:44 +00001745 add_object_entry(&entry.oid,
Linus Torvalds4d1012c2007-11-11 23:35:23 +00001746 object_type(entry.mode),
Junio C Hamanobc32fed2007-05-19 00:19:23 -07001747 fullname, 1);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001748 return;
1749 }
Nicolas Pitre8a5a8d62007-04-16 12:28:10 -04001750 if (S_ISDIR(entry.mode)) {
Junio C Hamano7a979d92006-02-19 14:47:21 -08001751 struct tree_desc sub;
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001752 struct pbase_tree_cache *tree;
1753 const char *down = name+cmplen+1;
René Scharfee65b8682023-02-05 11:42:27 +01001754 size_t downlen = name_cmp_len(down);
Junio C Hamano1d6b38c2006-02-22 22:10:24 -08001755
brian m. carlsonea82b2a2019-01-15 00:39:44 +00001756 tree = pbase_tree_get(&entry.oid);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001757 if (!tree)
1758 return;
Linus Torvalds6fda5e52007-03-21 10:08:25 -07001759 init_tree_desc(&sub, tree->tree_data, tree->tree_size);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001760
Linus Torvaldsce0bd642006-06-05 12:03:31 -07001761 add_pbase_object(&sub, down, downlen, fullname);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001762 pbase_tree_put(tree);
1763 }
1764 }
1765}
1766
1767static unsigned *done_pbase_paths;
1768static int done_pbase_paths_num;
1769static int done_pbase_paths_alloc;
1770static int done_pbase_path_pos(unsigned hash)
1771{
1772 int lo = 0;
1773 int hi = done_pbase_paths_num;
1774 while (lo < hi) {
Derrick Stolee19716b22017-10-08 14:29:37 -04001775 int mi = lo + (hi - lo) / 2;
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001776 if (done_pbase_paths[mi] == hash)
1777 return mi;
1778 if (done_pbase_paths[mi] < hash)
1779 hi = mi;
1780 else
1781 lo = mi + 1;
1782 }
1783 return -lo-1;
1784}
1785
1786static int check_pbase_path(unsigned hash)
1787{
René Scharfec7b07802017-07-20 22:32:56 +02001788 int pos = done_pbase_path_pos(hash);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001789 if (0 <= pos)
1790 return 1;
1791 pos = -pos - 1;
Dmitry S. Dolzhenko25e19402014-03-04 02:31:49 +04001792 ALLOC_GROW(done_pbase_paths,
1793 done_pbase_paths_num + 1,
1794 done_pbase_paths_alloc);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001795 done_pbase_paths_num++;
1796 if (pos < done_pbase_paths_num)
René Scharfef331ab92017-07-15 22:00:45 +02001797 MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,
1798 done_pbase_paths_num - pos - 1);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001799 done_pbase_paths[pos] = hash;
1800 return 0;
1801}
1802
Junio C Hamanobc32fed2007-05-19 00:19:23 -07001803static void add_preferred_base_object(const char *name)
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001804{
1805 struct pbase_tree *it;
René Scharfee65b8682023-02-05 11:42:27 +01001806 size_t cmplen;
Vicent Marti68fb36e2013-10-24 14:01:29 -04001807 unsigned hash = pack_name_hash(name);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001808
Nicolas Pitre8a5a8d62007-04-16 12:28:10 -04001809 if (!num_preferred_base || check_pbase_path(hash))
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001810 return;
1811
Nicolas Pitre8a5a8d62007-04-16 12:28:10 -04001812 cmplen = name_cmp_len(name);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001813 for (it = pbase_tree; it; it = it->next) {
1814 if (cmplen == 0) {
brian m. carlson188960b2017-10-15 22:07:01 +00001815 add_object_entry(&it->pcache.oid, OBJ_TREE, NULL, 1);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001816 }
1817 else {
1818 struct tree_desc tree;
Linus Torvalds6fda5e52007-03-21 10:08:25 -07001819 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
Linus Torvaldsce0bd642006-06-05 12:03:31 -07001820 add_pbase_object(&tree, name, cmplen, name);
Junio C Hamano7a979d92006-02-19 14:47:21 -08001821 }
1822 }
1823}
1824
brian m. carlson188960b2017-10-15 22:07:01 +00001825static void add_preferred_base(struct object_id *oid)
Junio C Hamano7a979d92006-02-19 14:47:21 -08001826{
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001827 struct pbase_tree *it;
1828 void *data;
1829 unsigned long size;
brian m. carlson188960b2017-10-15 22:07:01 +00001830 struct object_id tree_oid;
Junio C Hamano1d6b38c2006-02-22 22:10:24 -08001831
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07001832 if (window <= num_preferred_base++)
1833 return;
1834
Nguyễn Thái Ngọc Duyd3b47052019-06-27 16:28:47 +07001835 data = read_object_with_reference(the_repository, oid,
Ævar Arnfjörð Bjarmason6aea6ba2022-02-05 00:48:34 +01001836 OBJ_TREE, &size, &tree_oid);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001837 if (!data)
Junio C Hamano7a979d92006-02-19 14:47:21 -08001838 return;
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001839
1840 for (it = pbase_tree; it; it = it->next) {
Jeff King4a7e27e2018-08-28 17:22:40 -04001841 if (oideq(&it->pcache.oid, &tree_oid)) {
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001842 free(data);
1843 return;
1844 }
1845 }
1846
René Scharfeca56dad2021-03-13 17:17:22 +01001847 CALLOC_ARRAY(it, 1);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001848 it->next = pbase_tree;
1849 pbase_tree = it;
1850
brian m. carlson188960b2017-10-15 22:07:01 +00001851 oidcpy(&it->pcache.oid, &tree_oid);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001852 it->pcache.tree_data = data;
1853 it->pcache.tree_size = size;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001854}
1855
Nicolas Pitre0ef95f72009-09-03 21:54:03 -04001856static void cleanup_preferred_base(void)
1857{
1858 struct pbase_tree *it;
1859 unsigned i;
1860
1861 it = pbase_tree;
1862 pbase_tree = NULL;
1863 while (it) {
Brandon Williams095b3b22018-02-14 10:59:26 -08001864 struct pbase_tree *tmp = it;
1865 it = tmp->next;
1866 free(tmp->pcache.tree_data);
1867 free(tmp);
Nicolas Pitre0ef95f72009-09-03 21:54:03 -04001868 }
1869
1870 for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) {
1871 if (!pbase_tree_cache[i])
1872 continue;
1873 free(pbase_tree_cache[i]->tree_data);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001874 FREE_AND_NULL(pbase_tree_cache[i]);
Nicolas Pitre0ef95f72009-09-03 21:54:03 -04001875 }
1876
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001877 FREE_AND_NULL(done_pbase_paths);
Nicolas Pitre0ef95f72009-09-03 21:54:03 -04001878 done_pbase_paths_num = done_pbase_paths_alloc = 0;
1879}
1880
Jeff King2fa233a2018-09-18 23:49:07 -04001881/*
1882 * Return 1 iff the object specified by "delta" can be sent
1883 * literally as a delta against the base in "base_sha1". If
1884 * so, then *base_out will point to the entry in our packing
1885 * list, or NULL if we must use the external-base list.
1886 *
1887 * Depth value does not matter - find_deltas() will
1888 * never consider reused delta as the base object to
1889 * deltify other objects against, in order to avoid
1890 * circular deltas.
1891 */
Jeff King3f83fd52020-02-23 23:29:44 -05001892static int can_reuse_delta(const struct object_id *base_oid,
Jeff King2fa233a2018-09-18 23:49:07 -04001893 struct object_entry *delta,
1894 struct object_entry **base_out)
1895{
1896 struct object_entry *base;
Jeff King3df28ca2019-06-20 03:41:03 -04001897
Jeff King2fa233a2018-09-18 23:49:07 -04001898 /*
1899 * First see if we're already sending the base (or it's explicitly in
1900 * our "excluded" list).
1901 */
Jeff King3f83fd52020-02-23 23:29:44 -05001902 base = packlist_find(&to_pack, base_oid);
Jeff King2fa233a2018-09-18 23:49:07 -04001903 if (base) {
1904 if (!in_same_island(&delta->idx.oid, &base->idx.oid))
1905 return 0;
1906 *base_out = base;
1907 return 1;
1908 }
1909
1910 /*
1911 * Otherwise, reachability bitmaps may tell us if the receiver has it,
1912 * even if it was buried too deep in history to make it into the
1913 * packing list.
1914 */
Jeff King3f83fd52020-02-23 23:29:44 -05001915 if (thin && bitmap_has_oid_in_uninteresting(bitmap_git, base_oid)) {
Jeff King2fa233a2018-09-18 23:49:07 -04001916 if (use_delta_islands) {
Jeff King3f83fd52020-02-23 23:29:44 -05001917 if (!in_same_island(&delta->idx.oid, base_oid))
Jeff King2fa233a2018-09-18 23:49:07 -04001918 return 0;
1919 }
1920 *base_out = NULL;
1921 return 1;
1922 }
1923
1924 return 0;
1925}
1926
Jonathan Tane00549a2020-07-20 17:21:44 -07001927static void prefetch_to_pack(uint32_t object_index_start) {
1928 struct oid_array to_fetch = OID_ARRAY_INIT;
1929 uint32_t i;
1930
1931 for (i = object_index_start; i < to_pack.nr_objects; i++) {
1932 struct object_entry *entry = to_pack.objects + i;
1933
1934 if (!oid_object_info_extended(the_repository,
1935 &entry->idx.oid,
1936 NULL,
1937 OBJECT_INFO_FOR_PREFETCH))
1938 continue;
1939 oid_array_append(&to_fetch, &entry->idx.oid);
1940 }
1941 promisor_remote_get_direct(the_repository,
1942 to_fetch.oid, to_fetch.nr);
1943 oid_array_clear(&to_fetch);
1944}
1945
1946static void check_object(struct object_entry *entry, uint32_t object_index)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001947{
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02001948 unsigned long canonical_size;
Jonathan Tan8d5cf952020-07-20 17:21:43 -07001949 enum object_type type;
1950 struct object_info oi = {.typep = &type, .sizep = &canonical_size};
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02001951
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02001952 if (IN_PACK(entry)) {
1953 struct packed_git *p = IN_PACK(entry);
Shawn O. Pearce03e79c82006-12-23 02:34:08 -05001954 struct pack_window *w_curs = NULL;
Jeff King3f83fd52020-02-23 23:29:44 -05001955 int have_base = 0;
1956 struct object_id base_ref;
Nicolas Pitre5c49c112007-04-16 12:32:13 -04001957 struct object_entry *base_entry;
1958 unsigned long used, used_0;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001959 unsigned long avail;
Nicolas Pitre5c49c112007-04-16 12:32:13 -04001960 off_t ofs;
1961 unsigned char *buf, c;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02001962 enum object_type type;
Nguyễn Thái Ngọc Duy27a7d062018-04-14 17:35:09 +02001963 unsigned long in_pack_size;
Nicolas Pitre780e6e72006-09-22 21:25:04 -04001964
Shawn O. Pearce6777a592007-03-06 20:44:34 -05001965 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08001966
Nicolas Pitre5c49c112007-04-16 12:32:13 -04001967 /*
Nicolas Pitrefa736f72007-05-09 12:31:28 -04001968 * We want in_pack_type even if we do not reuse delta
1969 * since non-delta representations could still be reused.
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08001970 */
Nicolas Pitre09ded042008-10-29 19:02:46 -04001971 used = unpack_object_header_buffer(buf, avail,
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02001972 &type,
Nguyễn Thái Ngọc Duy27a7d062018-04-14 17:35:09 +02001973 &in_pack_size);
Nicolas Pitre03d66012008-10-29 19:02:48 -04001974 if (used == 0)
1975 goto give_up;
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08001976
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02001977 if (type < 0)
1978 BUG("invalid type %d", type);
1979 entry->in_pack_type = type;
1980
Nicolas Pitre5c49c112007-04-16 12:32:13 -04001981 /*
1982 * Determine if this is a delta and if so whether we can
1983 * reuse it or not. Otherwise let's find out as cheaply as
1984 * possible what the actual type and size for this object is.
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08001985 */
Nicolas Pitre5c49c112007-04-16 12:32:13 -04001986 switch (entry->in_pack_type) {
1987 default:
1988 /* Not a delta hence we've already got all we need. */
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02001989 oe_set_type(entry, entry->in_pack_type);
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02001990 SET_SIZE(entry, in_pack_size);
Nicolas Pitre5c49c112007-04-16 12:32:13 -04001991 entry->in_pack_header_size = used;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02001992 if (oe_type(entry) < OBJ_COMMIT || oe_type(entry) > OBJ_BLOB)
Nicolas Pitre03d66012008-10-29 19:02:48 -04001993 goto give_up;
Nicolas Pitre5c49c112007-04-16 12:32:13 -04001994 unuse_pack(&w_curs);
1995 return;
1996 case OBJ_REF_DELTA:
Jeff King3f83fd52020-02-23 23:29:44 -05001997 if (reuse_delta && !entry->preferred_base) {
1998 oidread(&base_ref,
1999 use_pack(p, &w_curs,
2000 entry->in_pack_offset + used,
2001 NULL));
2002 have_base = 1;
2003 }
brian m. carlson41179102018-05-02 00:25:37 +00002004 entry->in_pack_header_size = used + the_hash_algo->rawsz;
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002005 break;
2006 case OBJ_OFS_DELTA:
2007 buf = use_pack(p, &w_curs,
2008 entry->in_pack_offset + used, NULL);
2009 used_0 = 0;
2010 c = buf[used_0++];
2011 ofs = c & 127;
2012 while (c & 128) {
2013 ofs += 1;
Nicolas Pitre03d66012008-10-29 19:02:48 -04002014 if (!ofs || MSB(ofs, 7)) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002015 error(_("delta base offset overflow in pack for %s"),
brian m. carlsone6a492b2017-05-06 22:10:11 +00002016 oid_to_hex(&entry->idx.oid));
Nicolas Pitre03d66012008-10-29 19:02:48 -04002017 goto give_up;
2018 }
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002019 c = buf[used_0++];
2020 ofs = (ofs << 7) + (c & 127);
2021 }
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002022 ofs = entry->in_pack_offset - ofs;
Nicolas Pitre03d66012008-10-29 19:02:48 -04002023 if (ofs <= 0 || ofs >= entry->in_pack_offset) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002024 error(_("delta base offset out of bound for %s"),
brian m. carlsone6a492b2017-05-06 22:10:11 +00002025 oid_to_hex(&entry->idx.oid));
Nicolas Pitre03d66012008-10-29 19:02:48 -04002026 goto give_up;
2027 }
Nicolas Pitrea7de7132008-05-02 15:11:46 -04002028 if (reuse_delta && !entry->preferred_base) {
Taylor Blaueb3fd992021-01-13 17:23:47 -05002029 uint32_t pos;
2030 if (offset_to_pack_pos(p, ofs, &pos) < 0)
Nicolas Pitre08698b12008-10-29 19:02:49 -04002031 goto give_up;
Taylor Blaueb3fd992021-01-13 17:23:47 -05002032 if (!nth_packed_object_id(&base_ref, p,
2033 pack_pos_to_index(p, pos)))
Jeff King3f83fd52020-02-23 23:29:44 -05002034 have_base = 1;
Nicolas Pitre3449f8c2008-02-28 00:25:17 -05002035 }
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002036 entry->in_pack_header_size = used + used_0;
2037 break;
2038 }
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08002039
Jeff King3f83fd52020-02-23 23:29:44 -05002040 if (have_base &&
2041 can_reuse_delta(&base_ref, entry, &base_entry)) {
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002042 oe_set_type(entry, entry->in_pack_type);
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002043 SET_SIZE(entry, in_pack_size); /* delta size */
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +02002044 SET_DELTA_SIZE(entry, in_pack_size);
Jeff King6a1e32d2018-08-21 15:07:05 -04002045
2046 if (base_entry) {
2047 SET_DELTA(entry, base_entry);
2048 entry->delta_sibling_idx = base_entry->delta_child_idx;
2049 SET_DELTA_CHILD(base_entry, entry);
2050 } else {
Jeff Kinga93c1412020-02-23 23:30:07 -05002051 SET_DELTA_EXT(entry, &base_ref);
Jeff King6a1e32d2018-08-21 15:07:05 -04002052 }
2053
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002054 unuse_pack(&w_curs);
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08002055 return;
2056 }
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002057
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002058 if (oe_type(entry)) {
Nguyễn Thái Ngọc Duy27a7d062018-04-14 17:35:09 +02002059 off_t delta_pos;
2060
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002061 /*
2062 * This must be a delta and we already know what the
2063 * final object type is. Let's extract the actual
2064 * object size from the delta header.
2065 */
Nguyễn Thái Ngọc Duy27a7d062018-04-14 17:35:09 +02002066 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002067 canonical_size = get_size_from_delta(p, &w_curs, delta_pos);
2068 if (canonical_size == 0)
Nicolas Pitre03d66012008-10-29 19:02:48 -04002069 goto give_up;
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002070 SET_SIZE(entry, canonical_size);
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002071 unuse_pack(&w_curs);
2072 return;
2073 }
2074
2075 /*
2076 * No choice but to fall back to the recursive delta walk
Jeff Kingc93206b2019-01-07 03:34:12 -05002077 * with oid_object_info() to find about the object type
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002078 * at this point...
2079 */
Nicolas Pitre03d66012008-10-29 19:02:48 -04002080 give_up:
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002081 unuse_pack(&w_curs);
Junio C Hamano36e4d742005-06-27 03:34:06 -07002082 }
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08002083
Jonathan Tan8d5cf952020-07-20 17:21:43 -07002084 if (oid_object_info_extended(the_repository, &entry->idx.oid, &oi,
Jonathan Tane00549a2020-07-20 17:21:44 -07002085 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0) {
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +02002086 if (repo_has_promisor_remote(the_repository)) {
Jonathan Tane00549a2020-07-20 17:21:44 -07002087 prefetch_to_pack(object_index);
2088 if (oid_object_info_extended(the_repository, &entry->idx.oid, &oi,
2089 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0)
2090 type = -1;
2091 } else {
2092 type = -1;
2093 }
2094 }
Jonathan Tan8d5cf952020-07-20 17:21:43 -07002095 oe_set_type(entry, type);
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002096 if (entry->type_valid) {
2097 SET_SIZE(entry, canonical_size);
2098 } else {
2099 /*
2100 * Bad object type is checked in prepare_pack(). This is
2101 * to permit a missing preferred base object to be ignored
2102 * as a preferred base. Doing so can result in a larger
2103 * pack file, but the transfer will still take place.
2104 */
2105 }
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08002106}
2107
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002108static int pack_offset_sort(const void *_a, const void *_b)
2109{
2110 const struct object_entry *a = *(struct object_entry **)_a;
2111 const struct object_entry *b = *(struct object_entry **)_b;
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02002112 const struct packed_git *a_in_pack = IN_PACK(a);
2113 const struct packed_git *b_in_pack = IN_PACK(b);
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002114
2115 /* avoid filesystem trashing with loose objects */
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02002116 if (!a_in_pack && !b_in_pack)
brian m. carlsone6a492b2017-05-06 22:10:11 +00002117 return oidcmp(&a->idx.oid, &b->idx.oid);
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002118
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02002119 if (a_in_pack < b_in_pack)
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002120 return -1;
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02002121 if (a_in_pack > b_in_pack)
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002122 return 1;
2123 return a->in_pack_offset < b->in_pack_offset ? -1 :
2124 (a->in_pack_offset > b->in_pack_offset);
2125}
2126
Jeff King4cf21432016-08-11 05:26:36 -04002127/*
2128 * Drop an on-disk delta we were planning to reuse. Naively, this would
2129 * just involve blanking out the "delta" field, but we have to deal
2130 * with some extra book-keeping:
2131 *
2132 * 1. Removing ourselves from the delta_sibling linked list.
2133 *
2134 * 2. Updating our size/type to the non-delta representation. These were
2135 * either not recorded initially (size) or overwritten with the delta type
2136 * (type) when check_object() decided to reuse the delta.
Jeff King7dbabbb2017-01-27 19:09:59 -05002137 *
2138 * 3. Resetting our delta depth, as we are now a base object.
Jeff King4cf21432016-08-11 05:26:36 -04002139 */
2140static void drop_reused_delta(struct object_entry *entry)
2141{
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002142 unsigned *idx = &to_pack.objects[entry->delta_idx - 1].delta_child_idx;
Jeff King4cf21432016-08-11 05:26:36 -04002143 struct object_info oi = OBJECT_INFO_INIT;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002144 enum object_type type;
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002145 unsigned long size;
Jeff King4cf21432016-08-11 05:26:36 -04002146
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002147 while (*idx) {
2148 struct object_entry *oe = &to_pack.objects[*idx - 1];
2149
2150 if (oe == entry)
2151 *idx = oe->delta_sibling_idx;
Jeff King4cf21432016-08-11 05:26:36 -04002152 else
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002153 idx = &oe->delta_sibling_idx;
Jeff King4cf21432016-08-11 05:26:36 -04002154 }
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002155 SET_DELTA(entry, NULL);
Jeff King7dbabbb2017-01-27 19:09:59 -05002156 entry->depth = 0;
Jeff King4cf21432016-08-11 05:26:36 -04002157
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002158 oi.sizep = &size;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002159 oi.typep = &type;
Junio C Hamanoad635e82018-05-23 14:38:19 +09002160 if (packed_object_info(the_repository, IN_PACK(entry), entry->in_pack_offset, &oi) < 0) {
Jeff King4cf21432016-08-11 05:26:36 -04002161 /*
2162 * We failed to get the info from this pack for some reason;
Jeff Kingc93206b2019-01-07 03:34:12 -05002163 * fall back to oid_object_info, which may find another copy.
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002164 * And if that fails, the error will be recorded in oe_type(entry)
Jeff King4cf21432016-08-11 05:26:36 -04002165 * and dealt with in prepare_pack().
2166 */
Junio C Hamanoad635e82018-05-23 14:38:19 +09002167 oe_set_type(entry,
2168 oid_object_info(the_repository, &entry->idx.oid, &size));
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002169 } else {
2170 oe_set_type(entry, type);
Jeff King4cf21432016-08-11 05:26:36 -04002171 }
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002172 SET_SIZE(entry, size);
Jeff King4cf21432016-08-11 05:26:36 -04002173}
2174
2175/*
2176 * Follow the chain of deltas from this entry onward, throwing away any links
2177 * that cause us to hit a cycle (as determined by the DFS state flags in
2178 * the entries).
Jeff King7dbabbb2017-01-27 19:09:59 -05002179 *
2180 * We also detect too-long reused chains that would violate our --depth
2181 * limit.
Jeff King4cf21432016-08-11 05:26:36 -04002182 */
2183static void break_delta_chains(struct object_entry *entry)
2184{
Jeff King42b766d2017-01-27 17:05:36 -05002185 /*
2186 * The actual depth of each object we will write is stored as an int,
2187 * as it cannot exceed our int "depth" limit. But before we break
2188 * changes based no that limit, we may potentially go as deep as the
2189 * number of objects, which is elsewhere bounded to a uint32_t.
2190 */
2191 uint32_t total_depth;
2192 struct object_entry *cur, *next;
Jeff King4cf21432016-08-11 05:26:36 -04002193
Jeff King42b766d2017-01-27 17:05:36 -05002194 for (cur = entry, total_depth = 0;
2195 cur;
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002196 cur = DELTA(cur), total_depth++) {
Jeff King42b766d2017-01-27 17:05:36 -05002197 if (cur->dfs_state == DFS_DONE) {
2198 /*
2199 * We've already seen this object and know it isn't
2200 * part of a cycle. We do need to append its depth
2201 * to our count.
2202 */
2203 total_depth += cur->depth;
2204 break;
Jeff King7dbabbb2017-01-27 19:09:59 -05002205 }
2206
Jeff King4cf21432016-08-11 05:26:36 -04002207 /*
Jeff King42b766d2017-01-27 17:05:36 -05002208 * We break cycles before looping, so an ACTIVE state (or any
2209 * other cruft which made its way into the state variable)
2210 * is a bug.
Jeff King4cf21432016-08-11 05:26:36 -04002211 */
Jeff King42b766d2017-01-27 17:05:36 -05002212 if (cur->dfs_state != DFS_NONE)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002213 BUG("confusing delta dfs state in first pass: %d",
Jeff King42b766d2017-01-27 17:05:36 -05002214 cur->dfs_state);
2215
2216 /*
2217 * Now we know this is the first time we've seen the object. If
2218 * it's not a delta, we're done traversing, but we'll mark it
2219 * done to save time on future traversals.
2220 */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002221 if (!DELTA(cur)) {
Jeff King42b766d2017-01-27 17:05:36 -05002222 cur->dfs_state = DFS_DONE;
2223 break;
2224 }
2225
2226 /*
2227 * Mark ourselves as active and see if the next step causes
2228 * us to cycle to another active object. It's important to do
2229 * this _before_ we loop, because it impacts where we make the
2230 * cut, and thus how our total_depth counter works.
2231 * E.g., We may see a partial loop like:
2232 *
2233 * A -> B -> C -> D -> B
2234 *
2235 * Cutting B->C breaks the cycle. But now the depth of A is
2236 * only 1, and our total_depth counter is at 3. The size of the
2237 * error is always one less than the size of the cycle we
2238 * broke. Commits C and D were "lost" from A's chain.
2239 *
2240 * If we instead cut D->B, then the depth of A is correct at 3.
2241 * We keep all commits in the chain that we examined.
2242 */
2243 cur->dfs_state = DFS_ACTIVE;
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002244 if (DELTA(cur)->dfs_state == DFS_ACTIVE) {
Jeff King42b766d2017-01-27 17:05:36 -05002245 drop_reused_delta(cur);
2246 cur->dfs_state = DFS_DONE;
2247 break;
2248 }
Jeff King4cf21432016-08-11 05:26:36 -04002249 }
2250
Jeff King42b766d2017-01-27 17:05:36 -05002251 /*
2252 * And now that we've gone all the way to the bottom of the chain, we
2253 * need to clear the active flags and set the depth fields as
2254 * appropriate. Unlike the loop above, which can quit when it drops a
2255 * delta, we need to keep going to look for more depth cuts. So we need
2256 * an extra "next" pointer to keep going after we reset cur->delta.
2257 */
2258 for (cur = entry; cur; cur = next) {
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002259 next = DELTA(cur);
Jeff King4cf21432016-08-11 05:26:36 -04002260
Jeff King4cf21432016-08-11 05:26:36 -04002261 /*
Jeff King42b766d2017-01-27 17:05:36 -05002262 * We should have a chain of zero or more ACTIVE states down to
2263 * a final DONE. We can quit after the DONE, because either it
2264 * has no bases, or we've already handled them in a previous
2265 * call.
Jeff King4cf21432016-08-11 05:26:36 -04002266 */
Jeff King42b766d2017-01-27 17:05:36 -05002267 if (cur->dfs_state == DFS_DONE)
2268 break;
2269 else if (cur->dfs_state != DFS_ACTIVE)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002270 BUG("confusing delta dfs state in second pass: %d",
Jeff King42b766d2017-01-27 17:05:36 -05002271 cur->dfs_state);
2272
2273 /*
2274 * If the total_depth is more than depth, then we need to snip
2275 * the chain into two or more smaller chains that don't exceed
2276 * the maximum depth. Most of the resulting chains will contain
2277 * (depth + 1) entries (i.e., depth deltas plus one base), and
2278 * the last chain (i.e., the one containing entry) will contain
2279 * whatever entries are left over, namely
2280 * (total_depth % (depth + 1)) of them.
2281 *
2282 * Since we are iterating towards decreasing depth, we need to
2283 * decrement total_depth as we go, and we need to write to the
2284 * entry what its final depth will be after all of the
2285 * snipping. Since we're snipping into chains of length (depth
2286 * + 1) entries, the final depth of an entry will be its
2287 * original depth modulo (depth + 1). Any time we encounter an
2288 * entry whose final depth is supposed to be zero, we snip it
2289 * from its delta base, thereby making it so.
2290 */
2291 cur->depth = (total_depth--) % (depth + 1);
2292 if (!cur->depth)
2293 drop_reused_delta(cur);
2294
2295 cur->dfs_state = DFS_DONE;
Jeff King4cf21432016-08-11 05:26:36 -04002296 }
2297}
2298
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002299static void get_object_details(void)
2300{
Shawn O. Pearce7cadf492007-03-06 20:44:24 -05002301 uint32_t i;
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002302 struct object_entry **sorted_by_offset;
2303
Nguyễn Thái Ngọc Duy5af05042018-04-15 17:36:18 +02002304 if (progress)
2305 progress_state = start_progress(_("Counting objects"),
2306 to_pack.nr_objects);
2307
René Scharfeca56dad2021-03-13 17:17:22 +01002308 CALLOC_ARRAY(sorted_by_offset, to_pack.nr_objects);
Vicent Marti2834bc22013-10-24 14:01:06 -04002309 for (i = 0; i < to_pack.nr_objects; i++)
2310 sorted_by_offset[i] = to_pack.objects + i;
René Scharfe9ed0d8d2016-09-29 17:27:31 +02002311 QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002312
Vicent Marti2834bc22013-10-24 14:01:06 -04002313 for (i = 0; i < to_pack.nr_objects; i++) {
Junio C Hamano15366282011-04-05 10:44:11 -07002314 struct object_entry *entry = sorted_by_offset[i];
Jonathan Tane00549a2020-07-20 17:21:44 -07002315 check_object(entry, i);
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002316 if (entry->type_valid &&
2317 oe_size_greater_than(&to_pack, entry, big_file_threshold))
Junio C Hamano15366282011-04-05 10:44:11 -07002318 entry->no_try_delta = 1;
Nguyễn Thái Ngọc Duy5af05042018-04-15 17:36:18 +02002319 display_progress(progress_state, i + 1);
Junio C Hamano15366282011-04-05 10:44:11 -07002320 }
Nguyễn Thái Ngọc Duy5af05042018-04-15 17:36:18 +02002321 stop_progress(&progress_state);
Nicolas Pitre3449f8c2008-02-28 00:25:17 -05002322
Jeff King4cf21432016-08-11 05:26:36 -04002323 /*
2324 * This must happen in a second pass, since we rely on the delta
2325 * information for the whole list being completed.
2326 */
2327 for (i = 0; i < to_pack.nr_objects; i++)
2328 break_delta_chains(&to_pack.objects[i]);
2329
Nicolas Pitre5c49c112007-04-16 12:32:13 -04002330 free(sorted_by_offset);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002331}
2332
Nicolas Pitreb9041662007-12-08 00:00:08 -05002333/*
2334 * We search for deltas in a list sorted by type, by filename hash, and then
2335 * by size, so that we see progressively smaller and smaller files.
2336 * That's because we prefer deltas to be from the bigger file
2337 * to the smaller -- deletes are potentially cheaper, but perhaps
2338 * more importantly, the bigger file is likely the more recent
2339 * one. The deepest deltas are therefore the oldest objects which are
2340 * less susceptible to be accessed often.
2341 */
Nicolas Pitre9668cf52007-04-16 12:29:54 -04002342static int type_size_sort(const void *_a, const void *_b)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002343{
Nicolas Pitre9668cf52007-04-16 12:29:54 -04002344 const struct object_entry *a = *(struct object_entry **)_a;
2345 const struct object_entry *b = *(struct object_entry **)_b;
Shahzad Lone33de80b2019-02-02 23:16:45 +00002346 const enum object_type a_type = oe_type(a);
2347 const enum object_type b_type = oe_type(b);
2348 const unsigned long a_size = SIZE(a);
2349 const unsigned long b_size = SIZE(b);
Nicolas Pitre9668cf52007-04-16 12:29:54 -04002350
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002351 if (a_type > b_type)
Linus Torvalds27225f22005-06-26 15:27:28 -07002352 return -1;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002353 if (a_type < b_type)
Nicolas Pitreb9041662007-12-08 00:00:08 -05002354 return 1;
Linus Torvalds27225f22005-06-26 15:27:28 -07002355 if (a->hash > b->hash)
Junio C Hamano7a979d92006-02-19 14:47:21 -08002356 return -1;
Nicolas Pitreb9041662007-12-08 00:00:08 -05002357 if (a->hash < b->hash)
2358 return 1;
Junio C Hamano7a979d92006-02-19 14:47:21 -08002359 if (a->preferred_base > b->preferred_base)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002360 return -1;
Nicolas Pitreb9041662007-12-08 00:00:08 -05002361 if (a->preferred_base < b->preferred_base)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002362 return 1;
Jeff King28b8a732018-08-16 08:13:09 +02002363 if (use_delta_islands) {
Shahzad Lone33de80b2019-02-02 23:16:45 +00002364 const int island_cmp = island_delta_cmp(&a->idx.oid, &b->idx.oid);
Jeff King28b8a732018-08-16 08:13:09 +02002365 if (island_cmp)
2366 return island_cmp;
2367 }
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002368 if (a_size > b_size)
Nicolas Pitreb9041662007-12-08 00:00:08 -05002369 return -1;
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002370 if (a_size < b_size)
Nicolas Pitreb9041662007-12-08 00:00:08 -05002371 return 1;
2372 return a < b ? -1 : (a > b); /* newest first */
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002373}
2374
2375struct unpacked {
2376 struct object_entry *entry;
2377 void *data;
Nicolas Pitref6c70812006-04-26 23:58:00 -04002378 struct delta_index *index;
Nicolas Pitre5a235b52007-07-12 17:07:59 -04002379 unsigned depth;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002380};
2381
Nicolas Pitred2506262007-08-15 22:46:01 -04002382static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
2383 unsigned long delta_size)
Martin Koegler074b2ee2007-05-28 23:20:58 +02002384{
2385 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
2386 return 0;
2387
Martin Koeglere3dfddb2007-05-28 23:20:59 +02002388 if (delta_size < cache_max_small_delta_size)
2389 return 1;
2390
Martin Koegler074b2ee2007-05-28 23:20:58 +02002391 /* cache delta, if objects are large enough compared to delta size */
2392 if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
2393 return 1;
2394
2395 return 0;
2396}
2397
Nguyễn Thái Ngọc Duyffbd51c2018-07-29 17:36:05 +02002398/* Protect delta_cache_size */
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01002399static pthread_mutex_t cache_mutex;
Nicolas Pitre3c701832007-09-10 11:10:11 -04002400#define cache_lock() pthread_mutex_lock(&cache_mutex)
2401#define cache_unlock() pthread_mutex_unlock(&cache_mutex)
2402
Nguyễn Thái Ngọc Duyffbd51c2018-07-29 17:36:05 +02002403/*
2404 * Protect object list partitioning (e.g. struct thread_param) and
2405 * progress_state
2406 */
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01002407static pthread_mutex_t progress_mutex;
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002408#define progress_lock() pthread_mutex_lock(&progress_mutex)
2409#define progress_unlock() pthread_mutex_unlock(&progress_mutex)
2410
Nguyễn Thái Ngọc Duyffbd51c2018-07-29 17:36:05 +02002411/*
2412 * Access to struct object_entry is unprotected since each thread owns
2413 * a portion of the main object list. Just don't access object entries
2414 * ahead in the list because they can be stolen and would need
2415 * progress_mutex for protection.
2416 */
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002417
Ævar Arnfjörð Bjarmason7d089fb2021-05-27 02:52:51 +02002418static inline int oe_size_less_than(struct packing_data *pack,
2419 const struct object_entry *lhs,
2420 unsigned long rhs)
2421{
2422 if (lhs->size_valid)
2423 return lhs->size_ < rhs;
2424 if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
2425 return 0;
2426 return oe_get_size_slow(pack, lhs) < rhs;
2427}
2428
2429static inline void oe_set_tree_depth(struct packing_data *pack,
2430 struct object_entry *e,
2431 unsigned int tree_depth)
2432{
2433 if (!pack->tree_depth)
2434 CALLOC_ARRAY(pack->tree_depth, pack->nr_alloc);
2435 pack->tree_depth[e - pack->objects] = tree_depth;
2436}
2437
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002438/*
2439 * Return the size of the object without doing any delta
2440 * reconstruction (so non-deltas are true object sizes, but deltas
2441 * return the size of the delta data).
2442 */
2443unsigned long oe_get_size_slow(struct packing_data *pack,
2444 const struct object_entry *e)
2445{
2446 struct packed_git *p;
2447 struct pack_window *w_curs;
2448 unsigned char *buf;
2449 enum object_type type;
2450 unsigned long used, avail, size;
2451
2452 if (e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {
Patrick Hoggedb673c2019-01-24 19:22:05 -05002453 packing_data_lock(&to_pack);
Junio C Hamanoad635e82018-05-23 14:38:19 +09002454 if (oid_object_info(the_repository, &e->idx.oid, &size) < 0)
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002455 die(_("unable to get size of %s"),
2456 oid_to_hex(&e->idx.oid));
Patrick Hoggedb673c2019-01-24 19:22:05 -05002457 packing_data_unlock(&to_pack);
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002458 return size;
2459 }
2460
2461 p = oe_in_pack(pack, e);
2462 if (!p)
2463 BUG("when e->type is a delta, it must belong to a pack");
2464
Patrick Hoggedb673c2019-01-24 19:22:05 -05002465 packing_data_lock(&to_pack);
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002466 w_curs = NULL;
2467 buf = use_pack(p, &w_curs, e->in_pack_offset, &avail);
2468 used = unpack_object_header_buffer(buf, avail, &type, &size);
2469 if (used == 0)
2470 die(_("unable to parse object header of %s"),
2471 oid_to_hex(&e->idx.oid));
2472
2473 unuse_pack(&w_curs);
Patrick Hoggedb673c2019-01-24 19:22:05 -05002474 packing_data_unlock(&to_pack);
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002475 return size;
2476}
2477
Nicolas Pitref6c70812006-04-26 23:58:00 -04002478static int try_delta(struct unpacked *trg, struct unpacked *src,
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002479 unsigned max_depth, unsigned long *mem_usage)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002480{
Nicolas Pitref6c70812006-04-26 23:58:00 -04002481 struct object_entry *trg_entry = trg->entry;
2482 struct object_entry *src_entry = src->entry;
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002483 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
Nicolas Pitrec83f0322007-07-12 14:33:21 -04002484 unsigned ref_depth;
Nicolas Pitre21666f12007-02-26 14:55:59 -05002485 enum object_type type;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002486 void *delta_buf;
2487
2488 /* Don't bother doing diffs between different types */
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02002489 if (oe_type(trg_entry) != oe_type(src_entry))
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002490 return -1;
2491
Nicolas Pitref6c70812006-04-26 23:58:00 -04002492 /*
Jeff King15f07e02012-01-12 17:32:34 -05002493 * We do not bother to try a delta that we discarded on an
2494 * earlier try, but only when reusing delta data. Note that
2495 * src_entry that is marked as the preferred_base should always
2496 * be considered, as even if we produce a suboptimal delta against
2497 * it, we will still save the transfer cost, as we already know
2498 * the other side has it and we won't send src_entry at all.
Linus Torvalds51d1e832006-06-29 14:04:01 -07002499 */
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02002500 if (reuse_delta && IN_PACK(trg_entry) &&
2501 IN_PACK(trg_entry) == IN_PACK(src_entry) &&
Jeff King15f07e02012-01-12 17:32:34 -05002502 !src_entry->preferred_base &&
Junio C Hamanoe9195b52006-11-14 22:18:31 -08002503 trg_entry->in_pack_type != OBJ_REF_DELTA &&
2504 trg_entry->in_pack_type != OBJ_OFS_DELTA)
Linus Torvalds51d1e832006-06-29 14:04:01 -07002505 return 0;
2506
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002507 /* Let's not bust the allowed depth. */
Nicolas Pitre5a235b52007-07-12 17:07:59 -04002508 if (src->depth >= max_depth)
Linus Torvaldsd116a452005-06-25 20:17:59 -07002509 return 0;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002510
Nicolas Pitrec3b06a62006-05-16 16:29:14 -04002511 /* Now some size filtering heuristics. */
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002512 trg_size = SIZE(trg_entry);
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002513 if (!DELTA(trg_entry)) {
brian m. carlson41179102018-05-02 00:25:37 +00002514 max_size = trg_size/2 - the_hash_algo->rawsz;
Nicolas Pitrec83f0322007-07-12 14:33:21 -04002515 ref_depth = 1;
2516 } else {
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +02002517 max_size = DELTA_SIZE(trg_entry);
Nicolas Pitre5a235b52007-07-12 17:07:59 -04002518 ref_depth = trg->depth;
Nicolas Pitrec83f0322007-07-12 14:33:21 -04002519 }
Nicolas Pitre720fe222009-03-24 15:56:12 -04002520 max_size = (uint64_t)max_size * (max_depth - src->depth) /
Nicolas Pitrec83f0322007-07-12 14:33:21 -04002521 (max_depth - ref_depth + 1);
Nicolas Pitrec3b06a62006-05-16 16:29:14 -04002522 if (max_size == 0)
2523 return 0;
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002524 src_size = SIZE(src_entry);
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002525 sizediff = src_size < trg_size ? trg_size - src_size : 0;
Linus Torvalds27225f22005-06-26 15:27:28 -07002526 if (sizediff >= max_size)
Junio C Hamanof527cb82006-04-20 23:36:22 -07002527 return 0;
Brian Downinga1dab412007-07-12 07:55:47 -05002528 if (trg_size < src_size / 32)
2529 return 0;
Nicolas Pitref6c70812006-04-26 23:58:00 -04002530
Jeff King28b8a732018-08-16 08:13:09 +02002531 if (!in_same_island(&trg->entry->idx.oid, &src->entry->idx.oid))
2532 return 0;
2533
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002534 /* Load data if not already done */
2535 if (!trg->data) {
Patrick Hoggedb673c2019-01-24 19:22:05 -05002536 packing_data_lock(&to_pack);
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +02002537 trg->data = repo_read_object_file(the_repository,
2538 &trg_entry->idx.oid, &type,
2539 &sz);
Patrick Hoggedb673c2019-01-24 19:22:05 -05002540 packing_data_unlock(&to_pack);
Junio C Hamano2e3404c2007-08-25 01:26:47 -07002541 if (!trg->data)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002542 die(_("object %s cannot be read"),
brian m. carlsone6a492b2017-05-06 22:10:11 +00002543 oid_to_hex(&trg_entry->idx.oid));
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002544 if (sz != trg_size)
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01002545 die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
2546 oid_to_hex(&trg_entry->idx.oid), (uintmax_t)sz,
2547 (uintmax_t)trg_size);
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002548 *mem_usage += sz;
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002549 }
2550 if (!src->data) {
Patrick Hoggedb673c2019-01-24 19:22:05 -05002551 packing_data_lock(&to_pack);
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +02002552 src->data = repo_read_object_file(the_repository,
2553 &src_entry->idx.oid, &type,
2554 &sz);
Patrick Hoggedb673c2019-01-24 19:22:05 -05002555 packing_data_unlock(&to_pack);
Nicolas Pitre71064a92010-10-22 16:26:23 -04002556 if (!src->data) {
2557 if (src_entry->preferred_base) {
2558 static int warned = 0;
2559 if (!warned++)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002560 warning(_("object %s cannot be read"),
brian m. carlsone6a492b2017-05-06 22:10:11 +00002561 oid_to_hex(&src_entry->idx.oid));
Nicolas Pitre71064a92010-10-22 16:26:23 -04002562 /*
2563 * Those objects are not included in the
2564 * resulting pack. Be resilient and ignore
2565 * them if they can't be read, in case the
2566 * pack could be created nevertheless.
2567 */
2568 return 0;
2569 }
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002570 die(_("object %s cannot be read"),
brian m. carlsone6a492b2017-05-06 22:10:11 +00002571 oid_to_hex(&src_entry->idx.oid));
Nicolas Pitre71064a92010-10-22 16:26:23 -04002572 }
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002573 if (sz != src_size)
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01002574 die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
2575 oid_to_hex(&src_entry->idx.oid), (uintmax_t)sz,
2576 (uintmax_t)src_size);
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002577 *mem_usage += sz;
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002578 }
2579 if (!src->index) {
2580 src->index = create_delta_index(src->data, src_size);
Martin Koeglera588d882007-05-28 23:20:57 +02002581 if (!src->index) {
2582 static int warned = 0;
2583 if (!warned++)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002584 warning(_("suboptimal pack - out of memory"));
Martin Koeglera588d882007-05-28 23:20:57 +02002585 return 0;
2586 }
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002587 *mem_usage += sizeof_delta_index(src->index);
Nicolas Pitre560b25a2006-06-30 22:55:30 -04002588 }
2589
2590 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002591 if (!delta_buf)
Linus Torvalds75c42d82005-06-25 19:30:20 -07002592 return 0;
Nicolas Pitref6c70812006-04-26 23:58:00 -04002593
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002594 if (DELTA(trg_entry)) {
Brian Downing848d7322007-07-08 23:45:21 -05002595 /* Prefer only shallower same-sized deltas. */
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +02002596 if (delta_size == DELTA_SIZE(trg_entry) &&
Nicolas Pitre5a235b52007-07-12 17:07:59 -04002597 src->depth + 1 >= trg->depth) {
Brian Downing848d7322007-07-08 23:45:21 -05002598 free(delta_buf);
2599 return 0;
2600 }
Nicolas Pitre9e2d57a2007-08-29 21:17:17 -04002601 }
2602
Nicolas Pitre3c701832007-09-10 11:10:11 -04002603 /*
2604 * Handle memory allocation outside of the cache
2605 * accounting lock. Compiler will optimize the strangeness
Dan McGee7eb151d2010-01-29 19:22:19 -06002606 * away when NO_PTHREADS is defined.
Nicolas Pitre3c701832007-09-10 11:10:11 -04002607 */
Jim Meyering8e0f7002008-01-31 18:26:32 +01002608 free(trg_entry->delta_data);
Nicolas Pitre3c701832007-09-10 11:10:11 -04002609 cache_lock();
Nicolas Pitre9e2d57a2007-08-29 21:17:17 -04002610 if (trg_entry->delta_data) {
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +02002611 delta_cache_size -= DELTA_SIZE(trg_entry);
Nicolas Pitre5a235b52007-07-12 17:07:59 -04002612 trg_entry->delta_data = NULL;
Martin Koegler074b2ee2007-05-28 23:20:58 +02002613 }
Nicolas Pitred2506262007-08-15 22:46:01 -04002614 if (delta_cacheable(src_size, trg_size, delta_size)) {
Nicolas Pitreb7a28f72007-12-07 20:27:52 -05002615 delta_cache_size += delta_size;
Nicolas Pitre3c701832007-09-10 11:10:11 -04002616 cache_unlock();
2617 trg_entry->delta_data = xrealloc(delta_buf, delta_size);
2618 } else {
2619 cache_unlock();
Martin Koegler074b2ee2007-05-28 23:20:58 +02002620 free(delta_buf);
Nicolas Pitre3c701832007-09-10 11:10:11 -04002621 }
2622
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002623 SET_DELTA(trg_entry, src_entry);
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +02002624 SET_DELTA_SIZE(trg_entry, delta_size);
Nicolas Pitreb7a28f72007-12-07 20:27:52 -05002625 trg->depth = src->depth + 1;
2626
Nicolas Pitref6c70812006-04-26 23:58:00 -04002627 return 1;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002628}
2629
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002630static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
Nicolas Pitreb2504a02006-02-22 16:00:08 -05002631{
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002632 struct object_entry *child = DELTA_CHILD(me);
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002633 unsigned int m = n;
2634 while (child) {
Shahzad Lone33de80b2019-02-02 23:16:45 +00002635 const unsigned int c = check_delta_limit(child, n + 1);
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002636 if (m < c)
2637 m = c;
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002638 child = DELTA_SIBLING(child);
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002639 }
2640 return m;
Nicolas Pitreb2504a02006-02-22 16:00:08 -05002641}
2642
Junio C Hamano75ad2352008-02-12 23:39:03 -08002643static unsigned long free_unpacked(struct unpacked *n)
Brian Downinga97773c2007-07-12 08:07:46 -05002644{
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002645 unsigned long freed_mem = sizeof_delta_index(n->index);
Brian Downinga97773c2007-07-12 08:07:46 -05002646 free_delta_index(n->index);
2647 n->index = NULL;
2648 if (n->data) {
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02002649 freed_mem += SIZE(n->entry);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002650 FREE_AND_NULL(n->data);
Brian Downinga97773c2007-07-12 08:07:46 -05002651 }
2652 n->entry = NULL;
Nicolas Pitre7d7baa52007-07-12 22:27:12 -04002653 n->depth = 0;
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002654 return freed_mem;
Brian Downinga97773c2007-07-12 08:07:46 -05002655}
2656
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002657static void find_deltas(struct object_entry **list, unsigned *list_size,
Nicolas Pitree3349772007-09-06 02:13:10 -04002658 int window, int depth, unsigned *processed)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002659{
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002660 uint32_t i, idx = 0, count = 0;
Shawn O. Pearce7cadf492007-03-06 20:44:24 -05002661 struct unpacked *array;
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002662 unsigned long mem_usage = 0;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002663
René Scharfeca56dad2021-03-13 17:17:22 +01002664 CALLOC_ARRAY(array, window);
Junio C Hamano21fcd1b2006-02-11 17:54:18 -08002665
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002666 for (;;) {
Jeff King421b4882008-10-23 04:31:03 +00002667 struct object_entry *entry;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002668 struct unpacked *n = array + idx;
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002669 int j, max_depth, best_base = -1;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002670
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002671 progress_lock();
2672 if (!*list_size) {
2673 progress_unlock();
2674 break;
2675 }
Jeff King421b4882008-10-23 04:31:03 +00002676 entry = *list++;
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002677 (*list_size)--;
2678 if (!entry->preferred_base) {
2679 (*processed)++;
2680 display_progress(progress_state, *processed);
2681 }
2682 progress_unlock();
2683
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002684 mem_usage -= free_unpacked(n);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002685 n->entry = entry;
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08002686
Brian Downinga97773c2007-07-12 08:07:46 -05002687 while (window_memory_limit &&
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002688 mem_usage > window_memory_limit &&
Brian Downinga97773c2007-07-12 08:07:46 -05002689 count > 1) {
Shahzad Lone33de80b2019-02-02 23:16:45 +00002690 const uint32_t tail = (idx + window - count) % window;
Junio C Hamano75ad2352008-02-12 23:39:03 -08002691 mem_usage -= free_unpacked(array + tail);
Brian Downinga97773c2007-07-12 08:07:46 -05002692 count--;
2693 }
2694
Nicolas Pitre75d39852007-09-06 02:13:08 -04002695 /* We do not compute delta to *create* objects we are not
2696 * going to pack.
2697 */
2698 if (entry->preferred_base)
2699 goto next;
2700
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002701 /*
2702 * If the current object is at pack edge, take the depth the
2703 * objects that depend on the current object into account
2704 * otherwise they would become too deep.
2705 */
2706 max_depth = depth;
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002707 if (DELTA_CHILD(entry)) {
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002708 max_depth -= check_delta_limit(entry, 0);
2709 if (max_depth <= 0)
2710 goto next;
2711 }
2712
Linus Torvalds78817c12005-06-25 18:29:23 -07002713 j = window;
2714 while (--j > 0) {
Junio C Hamano77639872007-09-01 23:53:47 -07002715 int ret;
Shawn O. Pearce7cadf492007-03-06 20:44:24 -05002716 uint32_t other_idx = idx + j;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002717 struct unpacked *m;
Linus Torvalds78817c12005-06-25 18:29:23 -07002718 if (other_idx >= window)
2719 other_idx -= window;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002720 m = array + other_idx;
2721 if (!m->entry)
2722 break;
Nicolas Pitreef0316f2007-09-06 02:13:09 -04002723 ret = try_delta(n, m, max_depth, &mem_usage);
Junio C Hamano77639872007-09-01 23:53:47 -07002724 if (ret < 0)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002725 break;
Junio C Hamano77639872007-09-01 23:53:47 -07002726 else if (ret > 0)
2727 best_base = other_idx;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002728 }
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002729
Nicolas Pitreed4a9032008-05-02 15:11:50 -04002730 /*
2731 * If we decided to cache the delta data, then it is best
2732 * to compress it right away. First because we have to do
2733 * it anyway, and doing it here while we're threaded will
2734 * save a lot of time in the non threaded write phase,
2735 * as well as allow for caching more deltas within
2736 * the same cache size limit.
2737 * ...
2738 * But only if not writing to stdout, since in that case
2739 * the network is most likely throttling writes anyway,
2740 * and therefore it is best to go to the write phase ASAP
2741 * instead, as we can afford spending more time compressing
2742 * between writes at that moment.
2743 */
2744 if (entry->delta_data && !pack_to_stdout) {
Nguyễn Thái Ngọc Duy0cb3c142018-04-14 17:35:07 +02002745 unsigned long size;
2746
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +02002747 size = do_compress(&entry->delta_data, DELTA_SIZE(entry));
Nguyễn Thái Ngọc Duy0cb3c142018-04-14 17:35:07 +02002748 if (size < (1U << OE_Z_DELTA_BITS)) {
2749 entry->z_delta_size = size;
2750 cache_lock();
Nguyễn Thái Ngọc Duy0aca34e2018-04-14 17:35:11 +02002751 delta_cache_size -= DELTA_SIZE(entry);
Nguyễn Thái Ngọc Duy0cb3c142018-04-14 17:35:07 +02002752 delta_cache_size += entry->z_delta_size;
2753 cache_unlock();
2754 } else {
2755 FREE_AND_NULL(entry->delta_data);
2756 entry->z_delta_size = 0;
2757 }
Nicolas Pitreed4a9032008-05-02 15:11:50 -04002758 }
2759
Junio C Hamano70ca1a32006-03-05 11:22:57 -08002760 /* if we made n a delta, and if n is already at max
2761 * depth, leaving it in the window is pointless. we
2762 * should evict it first.
Junio C Hamano70ca1a32006-03-05 11:22:57 -08002763 */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002764 if (DELTA(entry) && max_depth <= n->depth)
Junio C Hamano70ca1a32006-03-05 11:22:57 -08002765 continue;
Nicolas Pitreff457152006-05-15 13:47:16 -04002766
Junio C Hamano77639872007-09-01 23:53:47 -07002767 /*
2768 * Move the best delta base up in the window, after the
2769 * currently deltified object, to keep it longer. It will
2770 * be the first base object to be attempted next.
2771 */
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02002772 if (DELTA(entry)) {
Junio C Hamano77639872007-09-01 23:53:47 -07002773 struct unpacked swap = array[best_base];
2774 int dist = (window + idx - best_base) % window;
2775 int dst = best_base;
2776 while (dist--) {
2777 int src = (dst + 1) % window;
2778 array[dst] = array[src];
2779 dst = src;
2780 }
2781 array[dst] = swap;
2782 }
2783
Nicolas Pitre898b14c2007-04-16 12:29:16 -04002784 next:
Linus Torvalds521a4f42005-06-26 13:43:41 -07002785 idx++;
Brian Downinga97773c2007-07-12 08:07:46 -05002786 if (count + 1 < window)
2787 count++;
Linus Torvalds521a4f42005-06-26 13:43:41 -07002788 if (idx >= window)
2789 idx = 0;
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002790 }
Sergey Vlasovadee7bd2005-08-08 22:46:58 +04002791
Nicolas Pitref6c70812006-04-26 23:58:00 -04002792 for (i = 0; i < window; ++i) {
Nicolas Pitreff457152006-05-15 13:47:16 -04002793 free_delta_index(array[i].index);
Sergey Vlasovadee7bd2005-08-08 22:46:58 +04002794 free(array[i].data);
Nicolas Pitref6c70812006-04-26 23:58:00 -04002795 }
Sergey Vlasovadee7bd2005-08-08 22:46:58 +04002796 free(array);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002797}
2798
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002799/*
Nguyễn Thái Ngọc Duyffbd51c2018-07-29 17:36:05 +02002800 * The main object list is split into smaller lists, each is handed to
2801 * one worker.
2802 *
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002803 * The main thread waits on the condition that (at least) one of the workers
2804 * has stopped working (which is indicated in the .working member of
2805 * struct thread_params).
Nguyễn Thái Ngọc Duyffbd51c2018-07-29 17:36:05 +02002806 *
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002807 * When a work thread has completed its work, it sets .working to 0 and
2808 * signals the main thread and waits on the condition that .data_ready
2809 * becomes 1.
Nguyễn Thái Ngọc Duyffbd51c2018-07-29 17:36:05 +02002810 *
2811 * The main thread steals half of the work from the worker that has
2812 * most work left to hand it to the idle worker.
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002813 */
2814
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002815struct thread_params {
2816 pthread_t thread;
2817 struct object_entry **list;
2818 unsigned list_size;
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002819 unsigned remaining;
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002820 int window;
2821 int depth;
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002822 int working;
2823 int data_ready;
2824 pthread_mutex_t mutex;
2825 pthread_cond_t cond;
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002826 unsigned *processed;
2827};
2828
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01002829static pthread_cond_t progress_cond;
2830
2831/*
2832 * Mutex and conditional variable can't be statically-initialized on Windows.
2833 */
2834static void init_threaded_search(void)
2835{
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01002836 pthread_mutex_init(&cache_mutex, NULL);
2837 pthread_mutex_init(&progress_mutex, NULL);
2838 pthread_cond_init(&progress_cond, NULL);
2839}
2840
2841static void cleanup_threaded_search(void)
2842{
2843 pthread_cond_destroy(&progress_cond);
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01002844 pthread_mutex_destroy(&cache_mutex);
2845 pthread_mutex_destroy(&progress_mutex);
2846}
Nicolas Pitrec2a33672007-09-10 00:06:09 -04002847
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002848static void *threaded_find_deltas(void *arg)
2849{
Nicolas Pitrec2a33672007-09-10 00:06:09 -04002850 struct thread_params *me = arg;
2851
Martin Ågren0c2ad002017-08-21 19:43:46 +02002852 progress_lock();
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002853 while (me->remaining) {
Martin Ågren0c2ad002017-08-21 19:43:46 +02002854 progress_unlock();
2855
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002856 find_deltas(me->list, &me->remaining,
Nicolas Pitrec2a33672007-09-10 00:06:09 -04002857 me->window, me->depth, me->processed);
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002858
2859 progress_lock();
2860 me->working = 0;
2861 pthread_cond_signal(&progress_cond);
2862 progress_unlock();
2863
2864 /*
2865 * We must not set ->data_ready before we wait on the
2866 * condition because the main thread may have set it to 1
2867 * before we get here. In order to be sure that new
2868 * work is available if we see 1 in ->data_ready, it
2869 * was initialized to 0 before this thread was spawned
2870 * and we reset it to 0 right away.
2871 */
2872 pthread_mutex_lock(&me->mutex);
2873 while (!me->data_ready)
2874 pthread_cond_wait(&me->cond, &me->mutex);
2875 me->data_ready = 0;
2876 pthread_mutex_unlock(&me->mutex);
Martin Ågren0c2ad002017-08-21 19:43:46 +02002877
2878 progress_lock();
Nicolas Pitrec2a33672007-09-10 00:06:09 -04002879 }
Martin Ågren0c2ad002017-08-21 19:43:46 +02002880 progress_unlock();
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002881 /* leave ->working 1 so that this doesn't get more work assigned */
2882 return NULL;
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002883}
2884
Nicolas Pitre8ecce682007-09-06 02:13:11 -04002885static void ll_find_deltas(struct object_entry **list, unsigned list_size,
2886 int window, int depth, unsigned *processed)
2887{
Junio C Hamanodcda3612009-09-01 02:18:52 -07002888 struct thread_params *p;
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002889 int i, ret, active_threads = 0;
Nicolas Pitrec2a33672007-09-10 00:06:09 -04002890
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01002891 init_threaded_search();
2892
Nicolas Pitre367f4a42007-09-10 00:06:11 -04002893 if (delta_search_threads <= 1) {
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002894 find_deltas(list, &list_size, window, depth, processed);
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01002895 cleanup_threaded_search();
Nicolas Pitre367f4a42007-09-10 00:06:11 -04002896 return;
2897 }
Nicolas Pitre43cc2b42008-12-11 15:36:47 -05002898 if (progress > pack_to_stdout)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002899 fprintf_ln(stderr, _("Delta compression using up to %d threads"),
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02002900 delta_search_threads);
René Scharfeca56dad2021-03-13 17:17:22 +01002901 CALLOC_ARRAY(p, delta_search_threads);
Nicolas Pitre367f4a42007-09-10 00:06:11 -04002902
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002903 /* Partition the work amongst work threads. */
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002904 for (i = 0; i < delta_search_threads; i++) {
2905 unsigned sub_size = list_size / (delta_search_threads - i);
Nicolas Pitre59921b42007-09-10 00:06:10 -04002906
Nicolas Pitrebf874892008-12-13 15:06:40 -05002907 /* don't use too small segments or no deltas will be found */
2908 if (sub_size < 2*window && i+1 < delta_search_threads)
2909 sub_size = 0;
2910
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002911 p[i].window = window;
2912 p[i].depth = depth;
2913 p[i].processed = processed;
2914 p[i].working = 1;
2915 p[i].data_ready = 0;
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002916
2917 /* try to split chunks on "path" boundaries */
Nicolas Pitre6fc74702008-01-21 11:07:15 -05002918 while (sub_size && sub_size < list_size &&
2919 list[sub_size]->hash &&
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002920 list[sub_size]->hash == list[sub_size-1]->hash)
2921 sub_size++;
2922
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002923 p[i].list = list;
2924 p[i].list_size = sub_size;
2925 p[i].remaining = sub_size;
Nicolas Pitrec2a33672007-09-10 00:06:09 -04002926
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002927 list += sub_size;
2928 list_size -= sub_size;
2929 }
2930
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002931 /* Start work threads. */
2932 for (i = 0; i < delta_search_threads; i++) {
2933 if (!p[i].list_size)
2934 continue;
Johannes Sixt68e6a4f2007-12-17 20:12:52 +01002935 pthread_mutex_init(&p[i].mutex, NULL);
2936 pthread_cond_init(&p[i].cond, NULL);
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002937 ret = pthread_create(&p[i].thread, NULL,
2938 threaded_find_deltas, &p[i]);
2939 if (ret)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02002940 die(_("unable to create thread: %s"), strerror(ret));
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002941 active_threads++;
2942 }
2943
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002944 /*
2945 * Now let's wait for work completion. Each time a thread is done
2946 * with its work, we steal half of the remaining work from the
2947 * thread with the largest number of unprocessed objects and give
2948 * it to that newly idle thread. This ensure good load balancing
2949 * until the remaining object list segments are simply too short
2950 * to be worth splitting anymore.
2951 */
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002952 while (active_threads) {
2953 struct thread_params *target = NULL;
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002954 struct thread_params *victim = NULL;
2955 unsigned sub_size = 0;
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002956
2957 progress_lock();
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002958 for (;;) {
2959 for (i = 0; !target && i < delta_search_threads; i++)
2960 if (!p[i].working)
2961 target = &p[i];
2962 if (target)
2963 break;
2964 pthread_cond_wait(&progress_cond, &progress_mutex);
2965 }
2966
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002967 for (i = 0; i < delta_search_threads; i++)
2968 if (p[i].remaining > 2*window &&
2969 (!victim || victim->remaining < p[i].remaining))
2970 victim = &p[i];
2971 if (victim) {
2972 sub_size = victim->remaining / 2;
2973 list = victim->list + victim->list_size - sub_size;
2974 while (sub_size && list[0]->hash &&
2975 list[0]->hash == list[-1]->hash) {
2976 list++;
2977 sub_size--;
2978 }
Nicolas Pitreeb9688f2007-12-10 14:19:32 -05002979 if (!sub_size) {
2980 /*
2981 * It is possible for some "paths" to have
2982 * so many objects that no hash boundary
2983 * might be found. Let's just steal the
2984 * exact half in that case.
2985 */
2986 sub_size = victim->remaining / 2;
2987 list -= sub_size;
2988 }
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002989 target->list = list;
2990 victim->list_size -= sub_size;
2991 victim->remaining -= sub_size;
Nicolas Pitrec2a33672007-09-10 00:06:09 -04002992 }
Nicolas Pitre384b32c2007-12-08 00:03:17 -05002993 target->list_size = sub_size;
2994 target->remaining = sub_size;
Johannes Sixt50f22ad2007-12-16 20:45:34 +01002995 target->working = 1;
2996 progress_unlock();
2997
2998 pthread_mutex_lock(&target->mutex);
2999 target->data_ready = 1;
3000 pthread_cond_signal(&target->cond);
3001 pthread_mutex_unlock(&target->mutex);
Nicolas Pitre384b32c2007-12-08 00:03:17 -05003002
3003 if (!sub_size) {
3004 pthread_join(target->thread, NULL);
Johannes Sixt50f22ad2007-12-16 20:45:34 +01003005 pthread_cond_destroy(&target->cond);
3006 pthread_mutex_destroy(&target->mutex);
Nicolas Pitre384b32c2007-12-08 00:03:17 -05003007 active_threads--;
3008 }
Johannes Sixt50f22ad2007-12-16 20:45:34 +01003009 }
Andrzej K. Haczewski44626dc2010-01-15 21:12:20 +01003010 cleanup_threaded_search();
Junio C Hamanodcda3612009-09-01 02:18:52 -07003011 free(p);
Nicolas Pitre8ecce682007-09-06 02:13:11 -04003012}
3013
Jeff Kingff483022019-12-18 12:25:44 +01003014static int obj_is_packed(const struct object_id *oid)
3015{
Junio C Hamanoa14aebe2020-02-14 12:54:19 -08003016 return packlist_find(&to_pack, oid) ||
Jeff King92fb0db2019-12-18 12:25:46 +01003017 (reuse_packfile_bitmap &&
3018 bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid));
Jeff Kingff483022019-12-18 12:25:44 +01003019}
3020
Jeff Kingb773dde2016-09-05 17:52:26 -04003021static void add_tag_chain(const struct object_id *oid)
3022{
3023 struct tag *tag;
3024
3025 /*
3026 * We catch duplicates already in add_object_entry(), but we'd
3027 * prefer to do this extra check to avoid having to parse the
3028 * tag at all if we already know that it's being packed (e.g., if
3029 * it was included via bitmaps, we would not have parsed it
3030 * previously).
3031 */
Jeff Kingff483022019-12-18 12:25:44 +01003032 if (obj_is_packed(oid))
Jeff Kingb773dde2016-09-05 17:52:26 -04003033 return;
3034
Stefan Bellerce71efb2018-06-28 18:22:03 -07003035 tag = lookup_tag(the_repository, oid);
Jeff Kingb773dde2016-09-05 17:52:26 -04003036 while (1) {
3037 if (!tag || parse_tag(tag) || !tag->tagged)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003038 die(_("unable to pack objects reachable from tag %s"),
Jeff Kingb773dde2016-09-05 17:52:26 -04003039 oid_to_hex(oid));
3040
brian m. carlson188960b2017-10-15 22:07:01 +00003041 add_object_entry(&tag->object.oid, OBJ_TAG, NULL, 0);
Jeff Kingb773dde2016-09-05 17:52:26 -04003042
3043 if (tag->tagged->type != OBJ_TAG)
3044 return;
3045
3046 tag = (struct tag *)tag->tagged;
3047 }
3048}
3049
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02003050static int add_ref_tag(const char *tag UNUSED, const struct object_id *oid,
3051 int flag UNUSED, void *cb_data UNUSED)
Shawn O. Pearcef0a24aa2008-03-03 22:27:20 -05003052{
Michael Haggertyd1552542015-05-25 18:38:38 +00003053 struct object_id peeled;
Shawn O. Pearcef0a24aa2008-03-03 22:27:20 -05003054
Junio C Hamano77db59c2021-02-05 16:40:45 -08003055 if (!peel_iterated_oid(oid, &peeled) && obj_is_packed(&peeled))
Jeff Kingb773dde2016-09-05 17:52:26 -04003056 add_tag_chain(oid);
Shawn O. Pearcef0a24aa2008-03-03 22:27:20 -05003057 return 0;
3058}
3059
Junio C Hamanof3123c42005-10-22 01:28:13 -07003060static void prepare_pack(int window, int depth)
3061{
Nicolas Pitre9668cf52007-04-16 12:29:54 -04003062 struct object_entry **delta_list;
Ramsay Jones6e1c2342008-07-03 16:52:09 +01003063 uint32_t i, nr_deltas;
3064 unsigned n;
Nicolas Pitre9668cf52007-04-16 12:29:54 -04003065
Jeff King28b8a732018-08-16 08:13:09 +02003066 if (use_delta_islands)
Nguyễn Thái Ngọc Duy385cb642018-11-10 06:49:03 +01003067 resolve_tree_islands(the_repository, progress, &to_pack);
Jeff King28b8a732018-08-16 08:13:09 +02003068
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08003069 get_object_details();
Nicolas Pitre9668cf52007-04-16 12:29:54 -04003070
Nicolas Pitre0e8189e2008-10-31 11:31:08 -04003071 /*
3072 * If we're locally repacking then we need to be doubly careful
3073 * from now on in order to make sure no stealth corruption gets
3074 * propagated to the new pack. Clients receiving streamed packs
3075 * should validate everything they get anyway so no need to incur
3076 * the additional cost here in that case.
3077 */
3078 if (!pack_to_stdout)
3079 do_check_packed_object_crc = 1;
3080
Vicent Marti2834bc22013-10-24 14:01:06 -04003081 if (!to_pack.nr_objects || !window || !depth)
Nicolas Pitre9668cf52007-04-16 12:29:54 -04003082 return;
3083
Jeff Kingb32fa952016-02-22 17:44:25 -05003084 ALLOC_ARRAY(delta_list, to_pack.nr_objects);
Nicolas Pitre75d39852007-09-06 02:13:08 -04003085 nr_deltas = n = 0;
3086
Vicent Marti2834bc22013-10-24 14:01:06 -04003087 for (i = 0; i < to_pack.nr_objects; i++) {
3088 struct object_entry *entry = to_pack.objects + i;
Nicolas Pitre75d39852007-09-06 02:13:08 -04003089
Nguyễn Thái Ngọc Duy898eba52018-04-14 17:35:06 +02003090 if (DELTA(entry))
Nicolas Pitre75d39852007-09-06 02:13:08 -04003091 /* This happens if we decided to reuse existing
Nicolas Pitrea7de7132008-05-02 15:11:46 -04003092 * delta from a pack. "reuse_delta &&" is implied.
Nicolas Pitre75d39852007-09-06 02:13:08 -04003093 */
3094 continue;
3095
Nguyễn Thái Ngọc Duyac77d0c2018-04-14 17:35:10 +02003096 if (!entry->type_valid ||
3097 oe_size_less_than(&to_pack, entry, 50))
Nicolas Pitre75d39852007-09-06 02:13:08 -04003098 continue;
3099
3100 if (entry->no_try_delta)
3101 continue;
3102
Shawn O. Pearce6d6f9cd2008-08-12 11:31:06 -07003103 if (!entry->preferred_base) {
Nicolas Pitre75d39852007-09-06 02:13:08 -04003104 nr_deltas++;
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02003105 if (oe_type(entry) < 0)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003106 die(_("unable to get type of object %s"),
brian m. carlsone6a492b2017-05-06 22:10:11 +00003107 oid_to_hex(&entry->idx.oid));
Nicolas Pitreeede9f42008-09-02 10:22:21 -04003108 } else {
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02003109 if (oe_type(entry) < 0) {
Nicolas Pitreeede9f42008-09-02 10:22:21 -04003110 /*
3111 * This object is not found, but we
3112 * don't have to include it anyway.
3113 */
3114 continue;
3115 }
Shawn O. Pearce6d6f9cd2008-08-12 11:31:06 -07003116 }
Nicolas Pitre75d39852007-09-06 02:13:08 -04003117
3118 delta_list[n++] = entry;
3119 }
3120
Nicolas Pitre2f8b8942007-10-16 21:55:47 -04003121 if (nr_deltas && n > 1) {
Nicolas Pitree3349772007-09-06 02:13:10 -04003122 unsigned nr_done = 0;
Jeff Kingbb514de2019-12-18 12:25:45 +01003123
Nicolas Pitree3349772007-09-06 02:13:10 -04003124 if (progress)
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +07003125 progress_state = start_progress(_("Compressing objects"),
Nicolas Pitredc6a0752007-10-30 14:57:32 -04003126 nr_deltas);
René Scharfe9ed0d8d2016-09-29 17:27:31 +02003127 QSORT(delta_list, n, type_size_sort);
Nicolas Pitre8ecce682007-09-06 02:13:11 -04003128 ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -04003129 stop_progress(&progress_state);
Nicolas Pitree3349772007-09-06 02:13:10 -04003130 if (nr_done != nr_deltas)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003131 die(_("inconsistency with delta count"));
Nicolas Pitre75d39852007-09-06 02:13:08 -04003132 }
Nicolas Pitre9668cf52007-04-16 12:29:54 -04003133 free(delta_list);
Junio C Hamanof3123c42005-10-22 01:28:13 -07003134}
3135
Glen Chooa4e7e312023-06-28 19:26:22 +00003136static int git_pack_config(const char *k, const char *v,
3137 const struct config_context *ctx, void *cb)
Jeff King4812a932006-07-23 01:50:30 -04003138{
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -04003139 if (!strcmp(k, "pack.window")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00003140 window = git_config_int(k, v, ctx->kvi);
Jeff King4812a932006-07-23 01:50:30 -04003141 return 0;
3142 }
Brian Downinga97773c2007-07-12 08:07:46 -05003143 if (!strcmp(k, "pack.windowmemory")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00003144 window_memory_limit = git_config_ulong(k, v, ctx->kvi);
Brian Downinga97773c2007-07-12 08:07:46 -05003145 return 0;
3146 }
3147 if (!strcmp(k, "pack.depth")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00003148 depth = git_config_int(k, v, ctx->kvi);
Theodore Ts'o842aaf92007-05-08 09:28:26 -04003149 return 0;
3150 }
Martin Koegler074b2ee2007-05-28 23:20:58 +02003151 if (!strcmp(k, "pack.deltacachesize")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00003152 max_delta_cache_size = git_config_int(k, v, ctx->kvi);
Martin Koegler074b2ee2007-05-28 23:20:58 +02003153 return 0;
3154 }
Martin Koeglere3dfddb2007-05-28 23:20:59 +02003155 if (!strcmp(k, "pack.deltacachelimit")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00003156 cache_max_small_delta_size = git_config_int(k, v, ctx->kvi);
Martin Koeglere3dfddb2007-05-28 23:20:59 +02003157 return 0;
3158 }
Vicent Martiae4f07f2013-12-21 09:00:45 -05003159 if (!strcmp(k, "pack.writebitmaphashcache")) {
3160 if (git_config_bool(k, v))
3161 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;
3162 else
3163 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;
3164 }
Abhradeep Chakraborty76f14b72022-08-14 16:55:09 +00003165
3166 if (!strcmp(k, "pack.writebitmaplookuptable")) {
3167 if (git_config_bool(k, v))
3168 write_bitmap_options |= BITMAP_OPT_LOOKUP_TABLE;
3169 else
3170 write_bitmap_options &= ~BITMAP_OPT_LOOKUP_TABLE;
3171 }
3172
Vicent Marti6b8fda22013-12-21 09:00:09 -05003173 if (!strcmp(k, "pack.usebitmaps")) {
Kirill Smelkov645c4322016-09-10 18:01:44 +03003174 use_bitmap_index_default = git_config_bool(k, v);
Vicent Marti6b8fda22013-12-21 09:00:09 -05003175 return 0;
3176 }
Jeff Kinge704fc72019-12-18 12:25:43 +01003177 if (!strcmp(k, "pack.allowpackreuse")) {
3178 allow_pack_reuse = git_config_bool(k, v);
3179 return 0;
3180 }
Nicolas Pitre693b86f2007-09-10 11:51:34 -04003181 if (!strcmp(k, "pack.threads")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00003182 delta_search_threads = git_config_int(k, v, ctx->kvi);
Andreas Ericsson833e3df2008-02-22 20:11:56 -06003183 if (delta_search_threads < 0)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003184 die(_("invalid number of threads specified (%d)"),
Nicolas Pitre693b86f2007-09-10 11:51:34 -04003185 delta_search_threads);
Nguyễn Thái Ngọc Duy9c897c52018-11-03 09:48:46 +01003186 if (!HAVE_THREADS && delta_search_threads != 1) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003187 warning(_("no threads support, ignoring %s"), k);
Ævar Arnfjörð Bjarmason2e96d812017-05-25 19:45:33 +00003188 delta_search_threads = 0;
3189 }
Nicolas Pitre693b86f2007-09-10 11:51:34 -04003190 return 0;
3191 }
Nicolas Pitre4d00bda2007-11-01 23:26:04 -04003192 if (!strcmp(k, "pack.indexversion")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00003193 pack_idx_opts.version = git_config_int(k, v, ctx->kvi);
Junio C Hamanoebcfb372011-02-25 15:43:25 -08003194 if (pack_idx_opts.version > 2)
Jiang Xinb4eda052022-06-17 18:03:09 +08003195 die(_("bad pack.indexVersion=%"PRIu32),
Junio C Hamanoebcfb372011-02-25 15:43:25 -08003196 pack_idx_opts.version);
Nicolas Pitre4d00bda2007-11-01 23:26:04 -04003197 return 0;
3198 }
Taylor Blauc9773342021-01-25 18:37:30 -05003199 if (!strcmp(k, "pack.writereverseindex")) {
3200 if (git_config_bool(k, v))
3201 pack_idx_opts.flags |= WRITE_REV;
3202 else
3203 pack_idx_opts.flags &= ~WRITE_REV;
3204 return 0;
3205 }
Jonathan Tandd4b7322020-06-10 13:57:23 -07003206 if (!strcmp(k, "uploadpack.blobpackfileuri")) {
3207 struct configured_exclusion *ex = xmalloc(sizeof(*ex));
3208 const char *oid_end, *pack_end;
3209 /*
3210 * Stores the pack hash. This is not a true object ID, but is
3211 * of the same form.
3212 */
3213 struct object_id pack_hash;
3214
3215 if (parse_oid_hex(v, &ex->e.oid, &oid_end) ||
3216 *oid_end != ' ' ||
3217 parse_oid_hex(oid_end + 1, &pack_hash, &pack_end) ||
3218 *pack_end != ' ')
3219 die(_("value of uploadpack.blobpackfileuri must be "
3220 "of the form '<object-hash> <pack-hash> <uri>' (got '%s')"), v);
3221 if (oidmap_get(&configured_exclusions, &ex->e.oid))
3222 die(_("object already configured in another "
3223 "uploadpack.blobpackfileuri (got '%s')"), v);
3224 ex->pack_hash_hex = xcalloc(1, pack_end - oid_end);
3225 memcpy(ex->pack_hash_hex, oid_end + 1, pack_end - oid_end - 1);
3226 ex->uri = xstrdup(pack_end + 1);
3227 oidmap_put(&configured_exclusions, ex);
3228 }
Glen Chooa4e7e312023-06-28 19:26:22 +00003229 return git_default_config(k, v, ctx, cb);
Jeff King4812a932006-07-23 01:50:30 -04003230}
3231
Taylor Blau339bce22021-02-22 21:25:10 -05003232/* Counters for trace2 output when in --stdin-packs mode. */
3233static int stdin_packs_found_nr;
3234static int stdin_packs_hints_nr;
3235
3236static int add_object_entry_from_pack(const struct object_id *oid,
3237 struct packed_git *p,
3238 uint32_t pos,
3239 void *_data)
3240{
Taylor Blau339bce22021-02-22 21:25:10 -05003241 off_t ofs;
Taylor Blau50457592022-05-24 14:54:31 -04003242 enum object_type type = OBJ_NONE;
Taylor Blau339bce22021-02-22 21:25:10 -05003243
3244 display_progress(progress_state, ++nr_seen);
3245
3246 if (have_duplicate_entry(oid, 0))
3247 return 0;
3248
3249 ofs = nth_packed_object_offset(p, pos);
3250 if (!want_object_in_pack(oid, 0, &p, &ofs))
3251 return 0;
3252
Taylor Blau50457592022-05-24 14:54:31 -04003253 if (p) {
3254 struct rev_info *revs = _data;
3255 struct object_info oi = OBJECT_INFO_INIT;
Taylor Blau339bce22021-02-22 21:25:10 -05003256
Taylor Blau50457592022-05-24 14:54:31 -04003257 oi.typep = &type;
3258 if (packed_object_info(the_repository, p, ofs, &oi) < 0) {
3259 die(_("could not get type of object %s in pack %s"),
3260 oid_to_hex(oid), p->pack_name);
3261 } else if (type == OBJ_COMMIT) {
3262 /*
3263 * commits in included packs are used as starting points for the
3264 * subsequent revision walk
3265 */
3266 add_pending_oid(revs, NULL, oid, 0);
3267 }
3268
3269 stdin_packs_found_nr++;
3270 }
Taylor Blau339bce22021-02-22 21:25:10 -05003271
3272 create_object_entry(oid, type, 0, 0, 0, p, ofs);
3273
3274 return 0;
3275}
3276
Jeff Kingbe252d32023-02-24 01:39:24 -05003277static void show_commit_pack_hint(struct commit *commit UNUSED,
3278 void *data UNUSED)
Taylor Blau339bce22021-02-22 21:25:10 -05003279{
3280 /* nothing to do; commits don't have a namehash */
3281}
3282
3283static void show_object_pack_hint(struct object *object, const char *name,
Jeff Kingbe252d32023-02-24 01:39:24 -05003284 void *data UNUSED)
Taylor Blau339bce22021-02-22 21:25:10 -05003285{
3286 struct object_entry *oe = packlist_find(&to_pack, &object->oid);
3287 if (!oe)
3288 return;
3289
3290 /*
3291 * Our 'to_pack' list was constructed by iterating all objects packed in
3292 * included packs, and so doesn't have a non-zero hash field that you
3293 * would typically pick up during a reachability traversal.
3294 *
3295 * Make a best-effort attempt to fill in the ->hash and ->no_try_delta
3296 * here using a now in order to perhaps improve the delta selection
3297 * process.
3298 */
3299 oe->hash = pack_name_hash(name);
3300 oe->no_try_delta = name && no_try_delta(name);
3301
3302 stdin_packs_hints_nr++;
3303}
3304
3305static int pack_mtime_cmp(const void *_a, const void *_b)
3306{
3307 struct packed_git *a = ((const struct string_list_item*)_a)->util;
3308 struct packed_git *b = ((const struct string_list_item*)_b)->util;
3309
3310 /*
3311 * order packs by descending mtime so that objects are laid out
3312 * roughly as newest-to-oldest
3313 */
3314 if (a->mtime < b->mtime)
3315 return 1;
3316 else if (b->mtime < a->mtime)
3317 return -1;
3318 else
3319 return 0;
3320}
3321
3322static void read_packs_list_from_stdin(void)
3323{
3324 struct strbuf buf = STRBUF_INIT;
3325 struct string_list include_packs = STRING_LIST_INIT_DUP;
3326 struct string_list exclude_packs = STRING_LIST_INIT_DUP;
3327 struct string_list_item *item = NULL;
3328
3329 struct packed_git *p;
3330 struct rev_info revs;
3331
3332 repo_init_revisions(the_repository, &revs, NULL);
3333 /*
3334 * Use a revision walk to fill in the namehash of objects in the include
3335 * packs. To save time, we'll avoid traversing through objects that are
3336 * in excluded packs.
3337 *
3338 * That may cause us to avoid populating all of the namehash fields of
3339 * all included objects, but our goal is best-effort, since this is only
3340 * an optimization during delta selection.
3341 */
3342 revs.no_kept_objects = 1;
3343 revs.keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
3344 revs.blob_objects = 1;
3345 revs.tree_objects = 1;
3346 revs.tag_objects = 1;
Taylor Blau14e7b832021-03-19 11:40:52 -04003347 revs.ignore_missing_links = 1;
Taylor Blau339bce22021-02-22 21:25:10 -05003348
3349 while (strbuf_getline(&buf, stdin) != EOF) {
3350 if (!buf.len)
3351 continue;
3352
3353 if (*buf.buf == '^')
3354 string_list_append(&exclude_packs, buf.buf + 1);
3355 else
3356 string_list_append(&include_packs, buf.buf);
3357
3358 strbuf_reset(&buf);
3359 }
3360
3361 string_list_sort(&include_packs);
Patrick Steinhardt732194b2023-04-14 08:01:49 +02003362 string_list_remove_duplicates(&include_packs, 0);
Taylor Blau339bce22021-02-22 21:25:10 -05003363 string_list_sort(&exclude_packs);
Patrick Steinhardt732194b2023-04-14 08:01:49 +02003364 string_list_remove_duplicates(&exclude_packs, 0);
Taylor Blau339bce22021-02-22 21:25:10 -05003365
3366 for (p = get_all_packs(the_repository); p; p = p->next) {
3367 const char *pack_name = pack_basename(p);
3368
Patrick Steinhardt752b4652023-04-14 08:01:54 +02003369 if ((item = string_list_lookup(&include_packs, pack_name)))
3370 item->util = p;
3371 if ((item = string_list_lookup(&exclude_packs, pack_name)))
Taylor Blau339bce22021-02-22 21:25:10 -05003372 item->util = p;
3373 }
3374
3375 /*
Ævar Arnfjörð Bjarmason561fa032021-07-09 12:13:48 +02003376 * Arguments we got on stdin may not even be packs. First
3377 * check that to avoid segfaulting later on in
3378 * e.g. pack_mtime_cmp(), excluded packs are handled below.
3379 *
3380 * Since we first parsed our STDIN and then sorted the input
3381 * lines the pack we error on will be whatever line happens to
3382 * sort first. This is lazy, it's enough that we report one
3383 * bad case here, we don't need to report the first/last one,
3384 * or all of them.
3385 */
3386 for_each_string_list_item(item, &include_packs) {
3387 struct packed_git *p = item->util;
3388 if (!p)
3389 die(_("could not find pack '%s'"), item->string);
Taylor Blau50457592022-05-24 14:54:31 -04003390 if (!is_pack_valid(p))
3391 die(_("packfile %s cannot be accessed"), p->pack_name);
Ævar Arnfjörð Bjarmason561fa032021-07-09 12:13:48 +02003392 }
3393
3394 /*
3395 * Then, handle all of the excluded packs, marking them as
3396 * kept in-core so that later calls to add_object_entry()
3397 * discards any objects that are also found in excluded packs.
Taylor Blau339bce22021-02-22 21:25:10 -05003398 */
3399 for_each_string_list_item(item, &exclude_packs) {
3400 struct packed_git *p = item->util;
3401 if (!p)
3402 die(_("could not find pack '%s'"), item->string);
3403 p->pack_keep_in_core = 1;
3404 }
3405
3406 /*
3407 * Order packs by ascending mtime; use QSORT directly to access the
3408 * string_list_item's ->util pointer, which string_list_sort() does not
3409 * provide.
3410 */
3411 QSORT(include_packs.items, include_packs.nr, pack_mtime_cmp);
3412
3413 for_each_string_list_item(item, &include_packs) {
3414 struct packed_git *p = item->util;
Taylor Blau339bce22021-02-22 21:25:10 -05003415 for_each_object_in_pack(p,
3416 add_object_entry_from_pack,
3417 &revs,
3418 FOR_EACH_OBJECT_PACK_ORDER);
3419 }
3420
3421 if (prepare_revision_walk(&revs))
3422 die(_("revision walk setup failed"));
3423 traverse_commit_list(&revs,
3424 show_commit_pack_hint,
3425 show_object_pack_hint,
3426 NULL);
3427
3428 trace2_data_intmax("pack-objects", the_repository, "stdin_packs_found",
3429 stdin_packs_found_nr);
3430 trace2_data_intmax("pack-objects", the_repository, "stdin_packs_hints",
3431 stdin_packs_hints_nr);
3432
3433 strbuf_release(&buf);
3434 string_list_clear(&include_packs, 0);
3435 string_list_clear(&exclude_packs, 0);
3436}
3437
Taylor Blaub7573532022-05-20 19:17:52 -04003438static void add_cruft_object_entry(const struct object_id *oid, enum object_type type,
3439 struct packed_git *pack, off_t offset,
3440 const char *name, uint32_t mtime)
3441{
3442 struct object_entry *entry;
3443
3444 display_progress(progress_state, ++nr_seen);
3445
3446 entry = packlist_find(&to_pack, oid);
3447 if (entry) {
3448 if (name) {
3449 entry->hash = pack_name_hash(name);
3450 entry->no_try_delta = no_try_delta(name);
3451 }
3452 } else {
3453 if (!want_object_in_pack(oid, 0, &pack, &offset))
3454 return;
3455 if (!pack && type == OBJ_BLOB && !has_loose_object(oid)) {
3456 /*
3457 * If a traversed tree has a missing blob then we want
3458 * to avoid adding that missing object to our pack.
3459 *
3460 * This only applies to missing blobs, not trees,
3461 * because the traversal needs to parse sub-trees but
3462 * not blobs.
3463 *
3464 * Note we only perform this check when we couldn't
3465 * already find the object in a pack, so we're really
3466 * limited to "ensure non-tip blobs which don't exist in
3467 * packs do exist via loose objects". Confused?
3468 */
3469 return;
3470 }
3471
3472 entry = create_object_entry(oid, type, pack_name_hash(name),
3473 0, name && no_try_delta(name),
3474 pack, offset);
3475 }
3476
3477 if (mtime > oe_cruft_mtime(&to_pack, entry))
3478 oe_set_cruft_mtime(&to_pack, entry, mtime);
3479 return;
3480}
3481
Jeff Kingc50dca22023-02-24 01:39:22 -05003482static void show_cruft_object(struct object *obj, const char *name, void *data UNUSED)
Taylor Blaua7d49382022-05-20 19:18:00 -04003483{
3484 /*
3485 * if we did not record it earlier, it's at least as old as our
3486 * expiration value. Rather than find it exactly, just use that
3487 * value. This may bump it forward from its real mtime, but it
3488 * will still be "too old" next time we run with the same
3489 * expiration.
3490 *
3491 * if obj does appear in the packing list, this call is a noop (or may
3492 * set the namehash).
3493 */
3494 add_cruft_object_entry(&obj->oid, obj->type, NULL, 0, name, cruft_expiration);
3495}
3496
3497static void show_cruft_commit(struct commit *commit, void *data)
3498{
3499 show_cruft_object((struct object*)commit, NULL, data);
3500}
3501
Jeff Kingc50dca22023-02-24 01:39:22 -05003502static int cruft_include_check_obj(struct object *obj, void *data UNUSED)
Taylor Blaua7d49382022-05-20 19:18:00 -04003503{
3504 return !has_object_kept_pack(&obj->oid, IN_CORE_KEEP_PACKS);
3505}
3506
3507static int cruft_include_check(struct commit *commit, void *data)
3508{
3509 return cruft_include_check_obj((struct object*)commit, data);
3510}
3511
3512static void set_cruft_mtime(const struct object *object,
3513 struct packed_git *pack,
3514 off_t offset, time_t mtime)
3515{
3516 add_cruft_object_entry(&object->oid, object->type, pack, offset, NULL,
3517 mtime);
3518}
3519
Taylor Blaub7573532022-05-20 19:17:52 -04003520static void mark_pack_kept_in_core(struct string_list *packs, unsigned keep)
3521{
3522 struct string_list_item *item = NULL;
3523 for_each_string_list_item(item, packs) {
3524 struct packed_git *p = item->util;
3525 if (!p)
3526 die(_("could not find pack '%s'"), item->string);
3527 p->pack_keep_in_core = keep;
3528 }
3529}
3530
3531static void add_unreachable_loose_objects(void);
3532static void add_objects_in_unpacked_packs(void);
3533
3534static void enumerate_cruft_objects(void)
3535{
3536 if (progress)
3537 progress_state = start_progress(_("Enumerating cruft objects"), 0);
3538
3539 add_objects_in_unpacked_packs();
3540 add_unreachable_loose_objects();
3541
3542 stop_progress(&progress_state);
3543}
3544
Taylor Blaua7d49382022-05-20 19:18:00 -04003545static void enumerate_and_traverse_cruft_objects(struct string_list *fresh_packs)
3546{
3547 struct packed_git *p;
3548 struct rev_info revs;
3549 int ret;
3550
3551 repo_init_revisions(the_repository, &revs, NULL);
3552
3553 revs.tag_objects = 1;
3554 revs.tree_objects = 1;
3555 revs.blob_objects = 1;
3556
3557 revs.include_check = cruft_include_check;
3558 revs.include_check_obj = cruft_include_check_obj;
3559
3560 revs.ignore_missing_links = 1;
3561
3562 if (progress)
3563 progress_state = start_progress(_("Enumerating cruft objects"), 0);
3564 ret = add_unseen_recent_objects_to_traversal(&revs, cruft_expiration,
3565 set_cruft_mtime, 1);
3566 stop_progress(&progress_state);
3567
3568 if (ret)
3569 die(_("unable to add cruft objects"));
3570
3571 /*
3572 * Re-mark only the fresh packs as kept so that objects in
3573 * unknown packs do not halt the reachability traversal early.
3574 */
3575 for (p = get_all_packs(the_repository); p; p = p->next)
3576 p->pack_keep_in_core = 0;
3577 mark_pack_kept_in_core(fresh_packs, 1);
3578
3579 if (prepare_revision_walk(&revs))
3580 die(_("revision walk setup failed"));
3581 if (progress)
3582 progress_state = start_progress(_("Traversing cruft objects"), 0);
3583 nr_seen = 0;
3584 traverse_commit_list(&revs, show_cruft_commit, show_cruft_object, NULL);
3585
3586 stop_progress(&progress_state);
3587}
3588
Taylor Blaub7573532022-05-20 19:17:52 -04003589static void read_cruft_objects(void)
3590{
3591 struct strbuf buf = STRBUF_INIT;
3592 struct string_list discard_packs = STRING_LIST_INIT_DUP;
3593 struct string_list fresh_packs = STRING_LIST_INIT_DUP;
3594 struct packed_git *p;
3595
3596 ignore_packed_keep_in_core = 1;
3597
3598 while (strbuf_getline(&buf, stdin) != EOF) {
3599 if (!buf.len)
3600 continue;
3601
3602 if (*buf.buf == '-')
3603 string_list_append(&discard_packs, buf.buf + 1);
3604 else
3605 string_list_append(&fresh_packs, buf.buf);
3606 strbuf_reset(&buf);
3607 }
3608
3609 string_list_sort(&discard_packs);
3610 string_list_sort(&fresh_packs);
3611
3612 for (p = get_all_packs(the_repository); p; p = p->next) {
3613 const char *pack_name = pack_basename(p);
3614 struct string_list_item *item;
3615
3616 item = string_list_lookup(&fresh_packs, pack_name);
3617 if (!item)
3618 item = string_list_lookup(&discard_packs, pack_name);
3619
3620 if (item) {
3621 item->util = p;
3622 } else {
3623 /*
3624 * This pack wasn't mentioned in either the "fresh" or
3625 * "discard" list, so the caller didn't know about it.
3626 *
3627 * Mark it as kept so that its objects are ignored by
3628 * add_unseen_recent_objects_to_traversal(). We'll
3629 * unmark it before starting the traversal so it doesn't
3630 * halt the traversal early.
3631 */
3632 p->pack_keep_in_core = 1;
3633 }
3634 }
3635
3636 mark_pack_kept_in_core(&fresh_packs, 1);
3637 mark_pack_kept_in_core(&discard_packs, 0);
3638
3639 if (cruft_expiration)
Taylor Blaua7d49382022-05-20 19:18:00 -04003640 enumerate_and_traverse_cruft_objects(&fresh_packs);
Taylor Blaub7573532022-05-20 19:17:52 -04003641 else
3642 enumerate_cruft_objects();
3643
3644 strbuf_release(&buf);
3645 string_list_clear(&discard_packs, 0);
3646 string_list_clear(&fresh_packs, 0);
3647}
3648
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003649static void read_object_list_from_stdin(void)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07003650{
brian m. carlson188960b2017-10-15 22:07:01 +00003651 char line[GIT_MAX_HEXSZ + 1 + PATH_MAX + 2];
3652 struct object_id oid;
3653 const char *p;
Nicolas Pitreb2504a02006-02-22 16:00:08 -05003654
Linus Torvaldsda93d122006-04-02 13:31:54 -07003655 for (;;) {
Linus Torvaldsda93d122006-04-02 13:31:54 -07003656 if (!fgets(line, sizeof(line), stdin)) {
3657 if (feof(stdin))
3658 break;
3659 if (!ferror(stdin))
Ævar Arnfjörð Bjarmason58677572021-12-07 12:05:51 +01003660 BUG("fgets returned NULL, not EOF, not error!");
Junio C Hamano687dd752006-04-03 23:41:09 -07003661 if (errno != EINTR)
Thomas Rastd824cbb2009-06-27 17:58:46 +02003662 die_errno("fgets");
Junio C Hamano687dd752006-04-03 23:41:09 -07003663 clearerr(stdin);
3664 continue;
Linus Torvaldsda93d122006-04-02 13:31:54 -07003665 }
Junio C Hamano7a979d92006-02-19 14:47:21 -08003666 if (line[0] == '-') {
brian m. carlson188960b2017-10-15 22:07:01 +00003667 if (get_oid_hex(line+1, &oid))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003668 die(_("expected edge object ID, got garbage:\n %s"),
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003669 line);
brian m. carlson188960b2017-10-15 22:07:01 +00003670 add_preferred_base(&oid);
Junio C Hamano7a979d92006-02-19 14:47:21 -08003671 continue;
Junio C Hamano21fcd1b2006-02-11 17:54:18 -08003672 }
brian m. carlson188960b2017-10-15 22:07:01 +00003673 if (parse_oid_hex(line, &oid, &p))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003674 die(_("expected object ID, got garbage:\n %s"), line);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003675
brian m. carlson188960b2017-10-15 22:07:01 +00003676 add_preferred_base_object(p + 1);
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +02003677 add_object_entry(&oid, OBJ_NONE, p + 1, 0);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07003678 }
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003679}
3680
Jeff Kingc50dca22023-02-24 01:39:22 -05003681static void show_commit(struct commit *commit, void *data UNUSED)
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003682{
brian m. carlson188960b2017-10-15 22:07:01 +00003683 add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0);
Vicent Marti7cc8f972013-12-21 09:00:16 -05003684
3685 if (write_bitmap_index)
3686 index_commit_for_bitmap(commit);
Jeff King28b8a732018-08-16 08:13:09 +02003687
3688 if (use_delta_islands)
3689 propagate_island_marks(commit);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003690}
3691
Jeff Kingc50dca22023-02-24 01:39:22 -05003692static void show_object(struct object *obj, const char *name,
3693 void *data UNUSED)
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003694{
Linus Torvalds8d2dfc42009-04-10 17:27:58 -07003695 add_preferred_base_object(name);
brian m. carlson188960b2017-10-15 22:07:01 +00003696 add_object_entry(&obj->oid, obj->type, name, 0);
Jeff King28b8a732018-08-16 08:13:09 +02003697
3698 if (use_delta_islands) {
3699 const char *p;
Jeff King39490532018-11-20 04:50:53 -05003700 unsigned depth;
Jeff King28b8a732018-08-16 08:13:09 +02003701 struct object_entry *ent;
3702
Jeff King39490532018-11-20 04:50:53 -05003703 /* the empty string is a root tree, which is depth 0 */
3704 depth = *name ? 1 : 0;
Jeff King28b8a732018-08-16 08:13:09 +02003705 for (p = strchr(name, '/'); p; p = strchr(p + 1, '/'))
3706 depth++;
3707
Jeff King3a378762019-09-05 21:36:05 -04003708 ent = packlist_find(&to_pack, &obj->oid);
Christian Couder108f5302018-08-16 08:13:12 +02003709 if (ent && depth > oe_tree_depth(&to_pack, ent))
3710 oe_set_tree_depth(&to_pack, ent, depth);
Jeff King28b8a732018-08-16 08:13:09 +02003711 }
Junio C Hamanob5d97e62006-09-04 23:47:39 -07003712}
3713
Jeff Hostetler9535ce72017-11-21 20:58:52 +00003714static void show_object__ma_allow_any(struct object *obj, const char *name, void *data)
3715{
3716 assert(arg_missing_action == MA_ALLOW_ANY);
3717
3718 /*
3719 * Quietly ignore ALL missing objects. This avoids problems with
3720 * staging them now and getting an odd error later.
3721 */
Jonathan Tanee472432020-08-05 16:06:51 -07003722 if (!has_object(the_repository, &obj->oid, 0))
Jeff Hostetler9535ce72017-11-21 20:58:52 +00003723 return;
3724
3725 show_object(obj, name, data);
3726}
3727
Jonathan Tan0c16cd42017-12-08 15:27:16 +00003728static void show_object__ma_allow_promisor(struct object *obj, const char *name, void *data)
3729{
3730 assert(arg_missing_action == MA_ALLOW_PROMISOR);
3731
3732 /*
3733 * Quietly ignore EXPECTED missing objects. This avoids problems with
3734 * staging them now and getting an odd error later.
3735 */
Jonathan Tanee472432020-08-05 16:06:51 -07003736 if (!has_object(the_repository, &obj->oid, 0) && is_promisor_object(&obj->oid))
Jonathan Tan0c16cd42017-12-08 15:27:16 +00003737 return;
3738
3739 show_object(obj, name, data);
3740}
3741
Jeff Hostetler9535ce72017-11-21 20:58:52 +00003742static int option_parse_missing_action(const struct option *opt,
3743 const char *arg, int unset)
3744{
3745 assert(arg);
3746 assert(!unset);
3747
3748 if (!strcmp(arg, "error")) {
3749 arg_missing_action = MA_ERROR;
3750 fn_show_object = show_object;
3751 return 0;
3752 }
3753
3754 if (!strcmp(arg, "allow-any")) {
3755 arg_missing_action = MA_ALLOW_ANY;
Jonathan Tan0c16cd42017-12-08 15:27:16 +00003756 fetch_if_missing = 0;
Jeff Hostetler9535ce72017-11-21 20:58:52 +00003757 fn_show_object = show_object__ma_allow_any;
3758 return 0;
3759 }
3760
Jonathan Tan0c16cd42017-12-08 15:27:16 +00003761 if (!strcmp(arg, "allow-promisor")) {
3762 arg_missing_action = MA_ALLOW_PROMISOR;
3763 fetch_if_missing = 0;
3764 fn_show_object = show_object__ma_allow_promisor;
3765 return 0;
3766 }
3767
Jean-Noël Avila1a8aea82022-01-31 22:07:47 +00003768 die(_("invalid value for '%s': '%s'"), "--missing", arg);
Jeff Hostetler9535ce72017-11-21 20:58:52 +00003769 return 0;
3770}
3771
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07003772static void show_edge(struct commit *commit)
3773{
brian m. carlson188960b2017-10-15 22:07:01 +00003774 add_preferred_base(&commit->object.oid);
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07003775}
3776
Taylor Blaua9fd2f22021-08-29 22:48:54 -04003777static int add_object_in_unpacked_pack(const struct object_id *oid,
3778 struct packed_git *pack,
3779 uint32_t pos,
Jeff Kingbe252d32023-02-24 01:39:24 -05003780 void *data UNUSED)
Junio C Hamano08cdfb12007-09-16 23:20:07 -07003781{
Taylor Blaub7573532022-05-20 19:17:52 -04003782 if (cruft) {
3783 off_t offset;
3784 time_t mtime;
3785
3786 if (pack->is_cruft) {
3787 if (load_pack_mtimes(pack) < 0)
3788 die(_("could not load cruft pack .mtimes"));
3789 mtime = nth_packed_mtime(pack, pos);
3790 } else {
3791 mtime = pack->mtime;
3792 }
3793 offset = nth_packed_object_offset(pack, pos);
3794
3795 add_cruft_object_entry(oid, OBJ_NONE, pack, offset,
3796 NULL, mtime);
3797 } else {
3798 add_object_entry(oid, OBJ_NONE, "", 0);
3799 }
Taylor Blaua9fd2f22021-08-29 22:48:54 -04003800 return 0;
Junio C Hamano08cdfb12007-09-16 23:20:07 -07003801}
3802
Jeff King7a2a7212019-05-09 17:31:16 -04003803static void add_objects_in_unpacked_packs(void)
Junio C Hamano08cdfb12007-09-16 23:20:07 -07003804{
Taylor Blaua9fd2f22021-08-29 22:48:54 -04003805 if (for_each_packed_object(add_object_in_unpacked_pack, NULL,
3806 FOR_EACH_OBJECT_PACK_ORDER |
3807 FOR_EACH_OBJECT_LOCAL_ONLY |
3808 FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS |
3809 FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS))
3810 die(_("cannot open pack index"));
Junio C Hamano08cdfb12007-09-16 23:20:07 -07003811}
3812
brian m. carlson76c1d9a2017-02-21 23:47:35 +00003813static int add_loose_object(const struct object_id *oid, const char *path,
Jeff Kingbe252d32023-02-24 01:39:24 -05003814 void *data UNUSED)
Jeff Kinge26a8c42016-06-13 00:38:04 -04003815{
Stefan Beller0df8e962018-04-25 11:20:59 -07003816 enum object_type type = oid_object_info(the_repository, oid, NULL);
Jeff Kinge26a8c42016-06-13 00:38:04 -04003817
3818 if (type < 0) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003819 warning(_("loose object at %s could not be examined"), path);
Jeff Kinge26a8c42016-06-13 00:38:04 -04003820 return 0;
3821 }
3822
Taylor Blaub7573532022-05-20 19:17:52 -04003823 if (cruft) {
3824 struct stat st;
3825 if (stat(path, &st) < 0) {
3826 if (errno == ENOENT)
3827 return 0;
3828 return error_errno("unable to stat %s", oid_to_hex(oid));
3829 }
3830
3831 add_cruft_object_entry(oid, type, NULL, 0, NULL,
3832 st.st_mtime);
3833 } else {
3834 add_object_entry(oid, type, "", 0);
3835 }
Jeff Kinge26a8c42016-06-13 00:38:04 -04003836 return 0;
3837}
3838
3839/*
3840 * We actually don't even have to worry about reachability here.
3841 * add_object_entry will weed out duplicates, so we just add every
3842 * loose object we find.
3843 */
3844static void add_unreachable_loose_objects(void)
3845{
3846 for_each_loose_file_in_objdir(get_object_directory(),
3847 add_loose_object,
3848 NULL, NULL, NULL);
3849}
3850
brian m. carlson188960b2017-10-15 22:07:01 +00003851static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
Brandon Casey094085e2009-03-21 17:26:11 -05003852{
3853 static struct packed_git *last_found = (void *)1;
3854 struct packed_git *p;
3855
Stefan Bellera80d72d2018-03-23 18:20:59 +01003856 p = (last_found != (void *)1) ? last_found :
Derrick Stolee454ea2e2018-08-20 16:52:04 +00003857 get_all_packs(the_repository);
Brandon Casey094085e2009-03-21 17:26:11 -05003858
3859 while (p) {
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02003860 if ((!p->pack_local || p->pack_keep ||
3861 p->pack_keep_in_core) &&
brian m. carlson188960b2017-10-15 22:07:01 +00003862 find_pack_entry_one(oid->hash, p)) {
Brandon Casey094085e2009-03-21 17:26:11 -05003863 last_found = p;
3864 return 1;
3865 }
3866 if (p == last_found)
Derrick Stolee454ea2e2018-08-20 16:52:04 +00003867 p = get_all_packs(the_repository);
Brandon Casey094085e2009-03-21 17:26:11 -05003868 else
3869 p = p->next;
3870 if (p == last_found)
3871 p = p->next;
3872 }
3873 return 0;
3874}
3875
Jeff Kingabcb8652014-10-15 18:42:09 -04003876/*
3877 * Store a list of sha1s that are should not be discarded
3878 * because they are either written too recently, or are
3879 * reachable from another object that was.
3880 *
3881 * This is filled by get_object_list.
3882 */
brian m. carlson910650d2017-03-31 01:40:00 +00003883static struct oid_array recent_objects;
Jeff Kingabcb8652014-10-15 18:42:09 -04003884
brian m. carlson4ce36212017-03-31 01:39:57 +00003885static int loosened_object_can_be_discarded(const struct object_id *oid,
Johannes Schindelindddbad72017-04-26 21:29:31 +02003886 timestamp_t mtime)
Jeff Kingd0d46ab2014-10-15 18:41:53 -04003887{
3888 if (!unpack_unreachable_expiration)
3889 return 0;
3890 if (mtime > unpack_unreachable_expiration)
3891 return 0;
brian m. carlson910650d2017-03-31 01:40:00 +00003892 if (oid_array_lookup(&recent_objects, oid) >= 0)
Jeff Kingabcb8652014-10-15 18:42:09 -04003893 return 0;
Jeff Kingd0d46ab2014-10-15 18:41:53 -04003894 return 1;
3895}
3896
Jeff King7a2a7212019-05-09 17:31:16 -04003897static void loosen_unused_packed_objects(void)
Nicolas Pitreca11b212008-05-14 01:33:53 -04003898{
3899 struct packed_git *p;
3900 uint32_t i;
Rafael Silvaa6431572021-04-21 21:32:12 +02003901 uint32_t loosened_objects_nr = 0;
brian m. carlson4ce36212017-03-31 01:39:57 +00003902 struct object_id oid;
Nicolas Pitreca11b212008-05-14 01:33:53 -04003903
Derrick Stolee454ea2e2018-08-20 16:52:04 +00003904 for (p = get_all_packs(the_repository); p; p = p->next) {
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02003905 if (!p->pack_local || p->pack_keep || p->pack_keep_in_core)
Nicolas Pitreca11b212008-05-14 01:33:53 -04003906 continue;
3907
3908 if (open_pack_index(p))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003909 die(_("cannot open pack index"));
Nicolas Pitreca11b212008-05-14 01:33:53 -04003910
3911 for (i = 0; i < p->num_objects; i++) {
Jeff King07636712020-02-23 23:27:36 -05003912 nth_packed_object_id(&oid, p, i);
Jeff King3a378762019-09-05 21:36:05 -04003913 if (!packlist_find(&to_pack, &oid) &&
brian m. carlson188960b2017-10-15 22:07:01 +00003914 !has_sha1_pack_kept_or_nonlocal(&oid) &&
Rafael Silvaa6431572021-04-21 21:32:12 +02003915 !loosened_object_can_be_discarded(&oid, p->mtime)) {
Patryk Obara4bdb70a2018-01-28 01:13:20 +01003916 if (force_object_loose(&oid, p->mtime))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02003917 die(_("unable to force loose object"));
Rafael Silvaa6431572021-04-21 21:32:12 +02003918 loosened_objects_nr++;
3919 }
Nicolas Pitreca11b212008-05-14 01:33:53 -04003920 }
3921 }
Rafael Silvaa6431572021-04-21 21:32:12 +02003922
3923 trace2_data_intmax("pack-objects", the_repository,
3924 "loosen_unused_packed_objects/loosened", loosened_objects_nr);
Nicolas Pitreca11b212008-05-14 01:33:53 -04003925}
3926
Jeff King69e4b342014-04-02 02:39:17 -04003927/*
Kirill Smelkov645c4322016-09-10 18:01:44 +03003928 * This tracks any options which pack-reuse code expects to be on, or which a
3929 * reader of the pack might not understand, and which would therefore prevent
3930 * blind reuse of what we have on disk.
Jeff King69e4b342014-04-02 02:39:17 -04003931 */
3932static int pack_options_allow_reuse(void)
3933{
Jeff Kinge704fc72019-12-18 12:25:43 +01003934 return allow_pack_reuse &&
3935 pack_to_stdout &&
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02003936 !ignore_packed_keep_on_disk &&
3937 !ignore_packed_keep_in_core &&
Jeff King9df4a602017-05-08 22:54:13 -04003938 (!local || !have_non_local_packs) &&
3939 !incremental;
Jeff King69e4b342014-04-02 02:39:17 -04003940}
3941
Vicent Marti6b8fda22013-12-21 09:00:09 -05003942static int get_object_list_from_bitmap(struct rev_info *revs)
3943{
Derrick Stolee09d4a792022-03-09 16:01:35 +00003944 if (!(bitmap_git = prepare_bitmap_walk(revs, 0)))
Vicent Marti6b8fda22013-12-21 09:00:09 -05003945 return -1;
3946
Jeff King69e4b342014-04-02 02:39:17 -04003947 if (pack_options_allow_reuse() &&
3948 !reuse_partial_packfile_from_bitmap(
Jonathan Tan3ae5fa02018-06-07 12:04:13 -07003949 bitmap_git,
Vicent Marti6b8fda22013-12-21 09:00:09 -05003950 &reuse_packfile,
3951 &reuse_packfile_objects,
Jeff Kingbb514de2019-12-18 12:25:45 +01003952 &reuse_packfile_bitmap)) {
Vicent Marti6b8fda22013-12-21 09:00:09 -05003953 assert(reuse_packfile_objects);
3954 nr_result += reuse_packfile_objects;
Jeff King8e118e82021-04-11 23:41:18 -04003955 nr_seen += reuse_packfile_objects;
3956 display_progress(progress_state, nr_seen);
Vicent Marti6b8fda22013-12-21 09:00:09 -05003957 }
3958
Jeff King4eb707e2020-02-14 13:22:27 -05003959 traverse_bitmap_commit_list(bitmap_git, revs,
3960 &add_object_entry_from_bitmap);
Vicent Marti6b8fda22013-12-21 09:00:09 -05003961 return 0;
3962}
3963
Jeff Kingabcb8652014-10-15 18:42:09 -04003964static void record_recent_object(struct object *obj,
Jeff Kingbe252d32023-02-24 01:39:24 -05003965 const char *name UNUSED,
3966 void *data UNUSED)
Jeff Kingabcb8652014-10-15 18:42:09 -04003967{
brian m. carlson910650d2017-03-31 01:40:00 +00003968 oid_array_append(&recent_objects, &obj->oid);
Jeff Kingabcb8652014-10-15 18:42:09 -04003969}
3970
Jeff Kingbe252d32023-02-24 01:39:24 -05003971static void record_recent_commit(struct commit *commit, void *data UNUSED)
Jeff Kingabcb8652014-10-15 18:42:09 -04003972{
brian m. carlson910650d2017-03-31 01:40:00 +00003973 oid_array_append(&recent_objects, &commit->object.oid);
Jeff Kingabcb8652014-10-15 18:42:09 -04003974}
3975
Taylor Blau3f267a12021-03-31 21:32:14 -04003976static int mark_bitmap_preferred_tip(const char *refname,
Jeff King63e14ee2022-08-19 06:08:32 -04003977 const struct object_id *oid,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02003978 int flags UNUSED,
3979 void *data UNUSED)
Taylor Blau3f267a12021-03-31 21:32:14 -04003980{
3981 struct object_id peeled;
3982 struct object *object;
3983
3984 if (!peel_iterated_oid(oid, &peeled))
3985 oid = &peeled;
3986
3987 object = parse_object_or_die(oid, refname);
3988 if (object->type == OBJ_COMMIT)
3989 object->flags |= NEEDS_BITMAP;
3990
3991 return 0;
3992}
3993
3994static void mark_bitmap_preferred_tips(void)
3995{
3996 struct string_list_item *item;
3997 const struct string_list *preferred_tips;
3998
3999 preferred_tips = bitmap_preferred_tips(the_repository);
4000 if (!preferred_tips)
4001 return;
4002
4003 for_each_string_list_item(item, preferred_tips) {
4004 for_each_ref_in(item->string, mark_bitmap_preferred_tip, NULL);
4005 }
4006}
4007
Derrick Stolee80f6de42022-03-22 17:28:36 +00004008static void get_object_list(struct rev_info *revs, int ac, const char **av)
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004009{
Matthew DeVorebbcde412018-12-03 14:10:19 -08004010 struct setup_revision_opt s_r_opt = {
4011 .allow_exclude_promisor_objects = 1,
4012 };
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004013 char line[1000];
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004014 int flags = 0;
Derrick Stoleea4544b32018-11-06 12:34:47 -08004015 int save_warning;
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004016
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004017 save_commit_buffer = 0;
Derrick Stolee80f6de42022-03-22 17:28:36 +00004018 setup_revisions(ac, av, revs, &s_r_opt);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004019
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +07004020 /* make sure shallows are read */
Stefan Bellerc8813482018-05-17 15:51:46 -07004021 is_repository_shallow(the_repository);
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +07004022
Derrick Stoleea4544b32018-11-06 12:34:47 -08004023 save_warning = warn_on_object_refname_ambiguity;
4024 warn_on_object_refname_ambiguity = 0;
4025
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004026 while (fgets(line, sizeof(line), stdin) != NULL) {
4027 int len = strlen(line);
Jim Meyering872c9302008-01-04 18:37:41 +01004028 if (len && line[len - 1] == '\n')
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004029 line[--len] = 0;
4030 if (!len)
4031 break;
4032 if (*line == '-') {
4033 if (!strcmp(line, "--not")) {
4034 flags ^= UNINTERESTING;
Vicent Marti7cc8f972013-12-21 09:00:16 -05004035 write_bitmap_index = 0;
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004036 continue;
4037 }
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +07004038 if (starts_with(line, "--shallow ")) {
brian m. carlsone92b8482017-05-06 22:10:06 +00004039 struct object_id oid;
4040 if (get_oid_hex(line + 10, &oid))
Junio C Hamano42790002020-08-13 18:07:12 -07004041 die("not an object name '%s'", line + 10);
Stefan Beller19143f12018-05-17 15:51:44 -07004042 register_shallow(the_repository, &oid);
Jeff Kingf7f91082014-08-12 00:34:53 -04004043 use_bitmap_index = 0;
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +07004044 continue;
4045 }
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004046 die(_("not a rev '%s'"), line);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004047 }
Derrick Stolee80f6de42022-03-22 17:28:36 +00004048 if (handle_revision_arg(line, revs, flags, REVARG_CANNOT_BE_FILENAME))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004049 die(_("bad revision '%s'"), line);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004050 }
4051
Derrick Stoleea4544b32018-11-06 12:34:47 -08004052 warn_on_object_refname_ambiguity = save_warning;
4053
Derrick Stolee80f6de42022-03-22 17:28:36 +00004054 if (use_bitmap_index && !get_object_list_from_bitmap(revs))
Vicent Marti6b8fda22013-12-21 09:00:09 -05004055 return;
4056
Jeff King28b8a732018-08-16 08:13:09 +02004057 if (use_delta_islands)
Jeff Kingbdbdf422019-06-20 04:58:32 -04004058 load_delta_islands(the_repository, progress);
Jeff King28b8a732018-08-16 08:13:09 +02004059
Taylor Blau3f267a12021-03-31 21:32:14 -04004060 if (write_bitmap_index)
4061 mark_bitmap_preferred_tips();
4062
Derrick Stolee80f6de42022-03-22 17:28:36 +00004063 if (prepare_revision_walk(revs))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004064 die(_("revision walk setup failed"));
Derrick Stolee80f6de42022-03-22 17:28:36 +00004065 mark_edges_uninteresting(revs, show_edge, sparse);
Jeff Hostetler9535ce72017-11-21 20:58:52 +00004066
4067 if (!fn_show_object)
4068 fn_show_object = show_object;
Derrick Stolee80f6de42022-03-22 17:28:36 +00004069 traverse_commit_list(revs,
Derrick Stolee3e0370a2022-03-09 16:01:36 +00004070 show_commit, fn_show_object,
4071 NULL);
Junio C Hamano08cdfb12007-09-16 23:20:07 -07004072
Jeff Kingabcb8652014-10-15 18:42:09 -04004073 if (unpack_unreachable_expiration) {
Derrick Stolee80f6de42022-03-22 17:28:36 +00004074 revs->ignore_missing_links = 1;
4075 if (add_unseen_recent_objects_to_traversal(revs,
Taylor Blau2fb90402022-05-20 19:17:54 -04004076 unpack_unreachable_expiration, NULL, 0))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004077 die(_("unable to add recent objects"));
Derrick Stolee80f6de42022-03-22 17:28:36 +00004078 if (prepare_revision_walk(revs))
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004079 die(_("revision walk setup failed"));
Derrick Stolee80f6de42022-03-22 17:28:36 +00004080 traverse_commit_list(revs, record_recent_commit,
Jeff Kingabcb8652014-10-15 18:42:09 -04004081 record_recent_object, NULL);
4082 }
4083
Junio C Hamano08cdfb12007-09-16 23:20:07 -07004084 if (keep_unreachable)
Jeff King7a2a7212019-05-09 17:31:16 -04004085 add_objects_in_unpacked_packs();
Jeff Kinge26a8c42016-06-13 00:38:04 -04004086 if (pack_loose_unreachable)
4087 add_unreachable_loose_objects();
Nicolas Pitreca11b212008-05-14 01:33:53 -04004088 if (unpack_unreachable)
Jeff King7a2a7212019-05-09 17:31:16 -04004089 loosen_unused_packed_objects();
Jeff Kingabcb8652014-10-15 18:42:09 -04004090
brian m. carlson910650d2017-03-31 01:40:00 +00004091 oid_array_clear(&recent_objects);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004092}
4093
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004094static void add_extra_kept_packs(const struct string_list *names)
4095{
4096 struct packed_git *p;
4097
4098 if (!names->nr)
4099 return;
4100
Derrick Stolee454ea2e2018-08-20 16:52:04 +00004101 for (p = get_all_packs(the_repository); p; p = p->next) {
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004102 const char *name = basename(p->pack_name);
4103 int i;
4104
4105 if (!p->pack_local)
4106 continue;
4107
4108 for (i = 0; i < names->nr; i++)
4109 if (!fspathcmp(name, names->items[i].string))
4110 break;
4111
4112 if (i < names->nr) {
4113 p->pack_keep_in_core = 1;
4114 ignore_packed_keep_in_core = 1;
4115 continue;
4116 }
4117 }
4118}
4119
René Scharfe36f76d22023-07-21 14:41:53 +02004120static int option_parse_quiet(const struct option *opt, const char *arg,
4121 int unset)
4122{
4123 BUG_ON_OPT_ARG(arg);
4124
4125 if (!unset)
4126 progress = 0;
4127 else if (!progress)
4128 progress = 1;
4129 return 0;
4130}
4131
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004132static int option_parse_index_version(const struct option *opt,
4133 const char *arg, int unset)
4134{
4135 char *c;
4136 const char *val = arg;
Jeff King517fe802018-11-05 01:45:42 -05004137
4138 BUG_ON_OPT_NEG(unset);
4139
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004140 pack_idx_opts.version = strtoul(val, &c, 10);
4141 if (pack_idx_opts.version > 2)
4142 die(_("unsupported index version %s"), val);
4143 if (*c == ',' && c[1])
4144 pack_idx_opts.off32_limit = strtoul(c+1, &c, 0);
4145 if (*c || pack_idx_opts.off32_limit & 0x80000000)
4146 die(_("bad index version '%s'"), val);
4147 return 0;
4148}
4149
Jeff King7e52f562012-04-07 06:30:09 -04004150static int option_parse_unpack_unreachable(const struct option *opt,
4151 const char *arg, int unset)
4152{
4153 if (unset) {
4154 unpack_unreachable = 0;
4155 unpack_unreachable_expiration = 0;
4156 }
4157 else {
4158 unpack_unreachable = 1;
4159 if (arg)
4160 unpack_unreachable_expiration = approxidate(arg);
4161 }
4162 return 0;
4163}
4164
Taylor Blaub7573532022-05-20 19:17:52 -04004165static int option_parse_cruft_expiration(const struct option *opt,
4166 const char *arg, int unset)
4167{
4168 if (unset) {
4169 cruft = 0;
4170 cruft_expiration = 0;
4171 } else {
4172 cruft = 1;
4173 if (arg)
4174 cruft_expiration = approxidate(arg);
4175 }
4176 return 0;
4177}
4178
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004179int cmd_pack_objects(int argc, const char **argv, const char *prefix)
4180{
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004181 int use_internal_rev_list = 0;
brian m. carlson2dacf262014-12-24 23:05:40 +00004182 int shallow = 0;
Nicolas Pitre4f366272009-11-23 12:43:50 -05004183 int all_progress_implied = 0;
Jeff King22f9b7f2020-07-28 16:24:27 -04004184 struct strvec rp = STRVEC_INIT;
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004185 int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0;
Jeff Kingc90f9e12014-10-16 20:44:49 -04004186 int rev_list_index = 0;
Taylor Blau339bce22021-02-22 21:25:10 -05004187 int stdin_packs = 0;
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004188 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
René Scharfe0d5448a2022-11-29 13:25:05 +01004189 struct list_objects_filter_options filter_options =
4190 LIST_OBJECTS_FILTER_INIT;
Derrick Stolee80f6de42022-03-22 17:28:36 +00004191
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004192 struct option pack_objects_options[] = {
René Scharfe36f76d22023-07-21 14:41:53 +02004193 OPT_CALLBACK_F('q', "quiet", NULL, NULL,
4194 N_("do not show progress meter"),
4195 PARSE_OPT_NOARG, option_parse_quiet),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004196 OPT_SET_INT(0, "progress", &progress,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004197 N_("show progress meter"), 1),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004198 OPT_SET_INT(0, "all-progress", &progress,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004199 N_("show progress meter during object writing phase"), 2),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004200 OPT_BOOL(0, "all-progress-implied",
4201 &all_progress_implied,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004202 N_("similar to --all-progress when progress meter is shown")),
Denton Liu203c8532020-04-28 04:36:28 -04004203 OPT_CALLBACK_F(0, "index-version", NULL, N_("<version>[,<offset>]"),
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004204 N_("write the pack index file in the specified idx format version"),
Denton Liu203c8532020-04-28 04:36:28 -04004205 PARSE_OPT_NONEG, option_parse_index_version),
Charles Bailey2a514ed2015-06-21 19:25:44 +01004206 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit,
4207 N_("maximum size of each output pack file")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004208 OPT_BOOL(0, "local", &local,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004209 N_("ignore borrowed objects from alternate object store")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004210 OPT_BOOL(0, "incremental", &incremental,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004211 N_("ignore packed objects")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004212 OPT_INTEGER(0, "window", &window,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004213 N_("limit pack window by objects")),
Charles Bailey2a514ed2015-06-21 19:25:44 +01004214 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit,
4215 N_("limit pack window by memory in addition to object limit")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004216 OPT_INTEGER(0, "depth", &depth,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004217 N_("maximum length of delta chain allowed in the resulting pack")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004218 OPT_BOOL(0, "reuse-delta", &reuse_delta,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004219 N_("reuse existing deltas")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004220 OPT_BOOL(0, "reuse-object", &reuse_object,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004221 N_("reuse existing objects")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004222 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004223 N_("use OFS_DELTA objects")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004224 OPT_INTEGER(0, "threads", &delta_search_threads,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004225 N_("use threads when searching for best delta matches")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004226 OPT_BOOL(0, "non-empty", &non_empty,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004227 N_("do not create an empty pack output")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004228 OPT_BOOL(0, "revs", &use_internal_rev_list,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004229 N_("read revision arguments from standard input")),
Nguyễn Thái Ngọc Duy3e4a67b2018-05-20 17:42:58 +02004230 OPT_SET_INT_F(0, "unpacked", &rev_list_unpacked,
4231 N_("limit the objects to those that are not yet packed"),
4232 1, PARSE_OPT_NONEG),
4233 OPT_SET_INT_F(0, "all", &rev_list_all,
4234 N_("include objects reachable from any reference"),
4235 1, PARSE_OPT_NONEG),
4236 OPT_SET_INT_F(0, "reflog", &rev_list_reflog,
4237 N_("include objects referred by reflog entries"),
4238 1, PARSE_OPT_NONEG),
4239 OPT_SET_INT_F(0, "indexed-objects", &rev_list_index,
4240 N_("include objects referred to by the index"),
4241 1, PARSE_OPT_NONEG),
Taylor Blau339bce22021-02-22 21:25:10 -05004242 OPT_BOOL(0, "stdin-packs", &stdin_packs,
4243 N_("read packs from stdin")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004244 OPT_BOOL(0, "stdout", &pack_to_stdout,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004245 N_("output pack to stdout")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004246 OPT_BOOL(0, "include-tag", &include_tag,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004247 N_("include tag objects that refer to objects to be packed")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004248 OPT_BOOL(0, "keep-unreachable", &keep_unreachable,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004249 N_("keep unreachable objects")),
Jeff Kinge26a8c42016-06-13 00:38:04 -04004250 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable,
4251 N_("pack loose unreachable objects")),
Denton Liu203c8532020-04-28 04:36:28 -04004252 OPT_CALLBACK_F(0, "unpack-unreachable", NULL, N_("time"),
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004253 N_("unpack unreachable objects newer than <time>"),
Denton Liu203c8532020-04-28 04:36:28 -04004254 PARSE_OPT_OPTARG, option_parse_unpack_unreachable),
Taylor Blaub7573532022-05-20 19:17:52 -04004255 OPT_BOOL(0, "cruft", &cruft, N_("create a cruft pack")),
4256 OPT_CALLBACK_F(0, "cruft-expiration", NULL, N_("time"),
4257 N_("expire cruft objects older than <time>"),
4258 PARSE_OPT_OPTARG, option_parse_cruft_expiration),
Derrick Stolee4f6d26b2019-01-16 10:25:58 -08004259 OPT_BOOL(0, "sparse", &sparse,
4260 N_("use the sparse reachability algorithm")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004261 OPT_BOOL(0, "thin", &thin,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004262 N_("create thin packs")),
brian m. carlson2dacf262014-12-24 23:05:40 +00004263 OPT_BOOL(0, "shallow", &shallow,
4264 N_("create packs suitable for shallow fetches")),
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004265 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004266 N_("ignore packs that have companion .keep file")),
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004267 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
4268 N_("ignore this pack")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004269 OPT_INTEGER(0, "compression", &pack_compression_level,
Nguyễn Thái Ngọc Duy4c688122012-08-20 19:32:29 +07004270 N_("pack compression level")),
René Scharfe3a5f3082023-07-21 14:41:56 +02004271 OPT_BOOL(0, "keep-true-parents", &grafts_keep_true_parents,
4272 N_("do not hide commits by grafts")),
Vicent Marti6b8fda22013-12-21 09:00:09 -05004273 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index,
4274 N_("use a bitmap index if available to speed up counting objects")),
Jeff King25575012019-07-31 01:39:27 -04004275 OPT_SET_INT(0, "write-bitmap-index", &write_bitmap_index,
4276 N_("write a bitmap index together with the pack index"),
4277 WRITE_BITMAP_TRUE),
4278 OPT_SET_INT_F(0, "write-bitmap-index-quiet",
4279 &write_bitmap_index,
4280 N_("write a bitmap index if possible"),
4281 WRITE_BITMAP_QUIET, PARSE_OPT_HIDDEN),
René Scharfe0d5448a2022-11-29 13:25:05 +01004282 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
Denton Liu203c8532020-04-28 04:36:28 -04004283 OPT_CALLBACK_F(0, "missing", NULL, N_("action"),
Jeff Hostetler9535ce72017-11-21 20:58:52 +00004284 N_("handling for missing objects"), PARSE_OPT_NONEG,
Denton Liu203c8532020-04-28 04:36:28 -04004285 option_parse_missing_action),
Jonathan Tan0c16cd42017-12-08 15:27:16 +00004286 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
4287 N_("do not pack objects in promisor packfiles")),
Jeff King28b8a732018-08-16 08:13:09 +02004288 OPT_BOOL(0, "delta-islands", &use_delta_islands,
4289 N_("respect islands during delta compression")),
Jonathan Tandd4b7322020-06-10 13:57:23 -07004290 OPT_STRING_LIST(0, "uri-protocol", &uri_protocols,
4291 N_("protocol"),
4292 N_("exclude any configured uploadpack.blobpackfileuri with this protocol")),
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004293 OPT_END(),
4294 };
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07004295
Nguyễn Thái Ngọc Duy0c6804a2018-04-14 17:35:02 +02004296 if (DFS_NUM_STATES > (1 << OE_DFS_STATE_BITS))
4297 BUG("too many dfs states, increase OE_DFS_STATE_BITS");
4298
Derrick Stoleed24eda42023-06-06 13:24:35 +00004299 disable_replace_refs();
Christian Couderdae556b2009-01-23 10:07:46 +01004300
Derrick Stolee2d657ab2020-03-20 12:38:10 +00004301 sparse = git_env_bool("GIT_TEST_PACK_SPARSE", -1);
Johannes Schindelin059fda12022-02-08 11:21:53 +00004302 if (the_repository->gitdir) {
4303 prepare_repo_settings(the_repository);
4304 if (sparse < 0)
4305 sparse = the_repository->settings.pack_use_sparse;
4306 }
Derrick Stolee7211b9e2019-08-13 11:37:43 -07004307
Junio C Hamanoebcfb372011-02-25 15:43:25 -08004308 reset_pack_idx_option(&pack_idx_opts);
Taylor Blaua8dd7e02023-04-12 18:20:33 -04004309 pack_idx_opts.flags |= WRITE_REV;
Johannes Schindelinef90d6d2008-05-14 18:46:53 +01004310 git_config(git_pack_config, NULL);
Taylor Blau9f7f10a2023-04-12 18:20:36 -04004311 if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX, 0))
4312 pack_idx_opts.flags &= ~WRITE_REV;
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004313
4314 progress = isatty(2);
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004315 argc = parse_options(argc, argv, prefix, pack_objects_options,
4316 pack_usage, 0);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004317
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004318 if (argc) {
4319 base_name = argv[0];
4320 argc--;
4321 }
4322 if (pack_to_stdout != !base_name || argc)
4323 usage_with_options(pack_usage, pack_objects_options);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004324
Jeff King6d52b6a2021-05-01 10:04:34 -04004325 if (depth < 0)
4326 depth = 0;
Nguyễn Thái Ngọc Duyb5c0cbd2018-04-14 17:35:03 +02004327 if (depth >= (1 << OE_DEPTH_BITS)) {
4328 warning(_("delta chain depth %d is too deep, forcing %d"),
4329 depth, (1 << OE_DEPTH_BITS) - 1);
4330 depth = (1 << OE_DEPTH_BITS) - 1;
4331 }
Nguyễn Thái Ngọc Duy0cb3c142018-04-14 17:35:07 +02004332 if (cache_max_small_delta_size >= (1U << OE_Z_DELTA_BITS)) {
4333 warning(_("pack.deltaCacheLimit is too high, forcing %d"),
4334 (1U << OE_Z_DELTA_BITS) - 1);
4335 cache_max_small_delta_size = (1U << OE_Z_DELTA_BITS) - 1;
4336 }
Jeff King953aa542021-05-01 10:03:37 -04004337 if (window < 0)
4338 window = 0;
Nguyễn Thái Ngọc Duyb5c0cbd2018-04-14 17:35:03 +02004339
Jeff King22f9b7f2020-07-28 16:24:27 -04004340 strvec_push(&rp, "pack-objects");
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004341 if (thin) {
4342 use_internal_rev_list = 1;
Jeff King22f9b7f2020-07-28 16:24:27 -04004343 strvec_push(&rp, shallow
brian m. carlson2dacf262014-12-24 23:05:40 +00004344 ? "--objects-edge-aggressive"
4345 : "--objects-edge");
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004346 } else
Jeff King22f9b7f2020-07-28 16:24:27 -04004347 strvec_push(&rp, "--objects");
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004348
4349 if (rev_list_all) {
4350 use_internal_rev_list = 1;
Jeff King22f9b7f2020-07-28 16:24:27 -04004351 strvec_push(&rp, "--all");
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004352 }
4353 if (rev_list_reflog) {
4354 use_internal_rev_list = 1;
Jeff King22f9b7f2020-07-28 16:24:27 -04004355 strvec_push(&rp, "--reflog");
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004356 }
Jeff Kingc90f9e12014-10-16 20:44:49 -04004357 if (rev_list_index) {
4358 use_internal_rev_list = 1;
Jeff King22f9b7f2020-07-28 16:24:27 -04004359 strvec_push(&rp, "--indexed-objects");
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004360 }
Taylor Blau339bce22021-02-22 21:25:10 -05004361 if (rev_list_unpacked && !stdin_packs) {
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004362 use_internal_rev_list = 1;
Jeff King22f9b7f2020-07-28 16:24:27 -04004363 strvec_push(&rp, "--unpacked");
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004364 }
4365
Jonathan Tan0c16cd42017-12-08 15:27:16 +00004366 if (exclude_promisor_objects) {
4367 use_internal_rev_list = 1;
4368 fetch_if_missing = 0;
Jeff King22f9b7f2020-07-28 16:24:27 -04004369 strvec_push(&rp, "--exclude-promisor-objects");
Jonathan Tan0c16cd42017-12-08 15:27:16 +00004370 }
Nguyễn Thái Ngọc Duy58bd77b2018-05-05 10:47:16 +02004371 if (unpack_unreachable || keep_unreachable || pack_loose_unreachable)
4372 use_internal_rev_list = 1;
Jonathan Tan0c16cd42017-12-08 15:27:16 +00004373
Nguyễn Thái Ngọc Duy99fb6e02012-02-01 22:17:20 +07004374 if (!reuse_object)
4375 reuse_delta = 0;
4376 if (pack_compression_level == -1)
4377 pack_compression_level = Z_DEFAULT_COMPRESSION;
4378 else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004379 die(_("bad pack compression level %d"), pack_compression_level);
Junio C Hamano0c45d252014-10-13 12:46:14 -07004380
4381 if (!delta_search_threads) /* --threads=0 means autodetect */
4382 delta_search_threads = online_cpus();
4383
Nguyễn Thái Ngọc Duy9c897c52018-11-03 09:48:46 +01004384 if (!HAVE_THREADS && delta_search_threads != 1)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004385 warning(_("no threads support, ignoring --threads"));
Taylor Blaub7573532022-05-20 19:17:52 -04004386 if (!pack_to_stdout && !pack_size_limit && !cruft)
Johannes Schindelin2b84b5a2008-02-05 14:25:04 +00004387 pack_size_limit = pack_size_limit_cfg;
Dana How01c12a22007-05-23 10:11:33 -07004388 if (pack_to_stdout && pack_size_limit)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004389 die(_("--max-pack-size cannot be used to build a pack for transfer"));
Nicolas Pitre07cf0f22010-02-03 22:48:28 -05004390 if (pack_size_limit && pack_size_limit < 1024*1024) {
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004391 warning(_("minimum pack size limit is 1 MiB"));
Nicolas Pitre07cf0f22010-02-03 22:48:28 -05004392 pack_size_limit = 1024*1024;
4393 }
Dana How01c12a22007-05-23 10:11:33 -07004394
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07004395 if (!pack_to_stdout && thin)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004396 die(_("--thin cannot be used to build an indexable pack"));
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004397
Nicolas Pitreca11b212008-05-14 01:33:53 -04004398 if (keep_unreachable && unpack_unreachable)
Jean-Noël Avila12909b62022-01-05 20:02:16 +00004399 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "--unpack-unreachable");
Jeff Kingb1e757f2014-10-16 20:44:54 -04004400 if (!rev_list_all || !rev_list_reflog || !rev_list_index)
4401 unpack_unreachable_expiration = 0;
Nicolas Pitreca11b212008-05-14 01:33:53 -04004402
René Scharfe0d5448a2022-11-29 13:25:05 +01004403 if (filter_options.choice) {
Jeff Hostetler9535ce72017-11-21 20:58:52 +00004404 if (!pack_to_stdout)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004405 die(_("cannot use --filter without --stdout"));
Taylor Blau339bce22021-02-22 21:25:10 -05004406 if (stdin_packs)
4407 die(_("cannot use --filter with --stdin-packs"));
Jeff Hostetler9535ce72017-11-21 20:58:52 +00004408 }
4409
Taylor Blau339bce22021-02-22 21:25:10 -05004410 if (stdin_packs && use_internal_rev_list)
4411 die(_("cannot use internal rev list with --stdin-packs"));
4412
Taylor Blaub7573532022-05-20 19:17:52 -04004413 if (cruft) {
4414 if (use_internal_rev_list)
4415 die(_("cannot use internal rev list with --cruft"));
4416 if (stdin_packs)
4417 die(_("cannot use --stdin-packs with --cruft"));
4418 if (pack_size_limit)
4419 die(_("cannot use --max-pack-size with --cruft"));
4420 }
4421
Kirill Smelkov645c4322016-09-10 18:01:44 +03004422 /*
4423 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
4424 *
4425 * - to produce good pack (with bitmap index not-yet-packed objects are
4426 * packed in suboptimal order).
4427 *
4428 * - to use more robust pack-generation codepath (avoiding possible
4429 * bugs in bitmap code and possible bitmap index corruption).
4430 */
4431 if (!pack_to_stdout)
4432 use_bitmap_index_default = 0;
4433
4434 if (use_bitmap_index < 0)
4435 use_bitmap_index = use_bitmap_index_default;
4436
4437 /* "hard" reasons not to use bitmaps; these just won't work at all */
Stefan Bellerc8813482018-05-17 15:51:46 -07004438 if (!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) || is_repository_shallow(the_repository))
Vicent Marti6b8fda22013-12-21 09:00:09 -05004439 use_bitmap_index = 0;
4440
Vicent Marti7cc8f972013-12-21 09:00:16 -05004441 if (pack_to_stdout || !rev_list_all)
4442 write_bitmap_index = 0;
4443
Jeff King28b8a732018-08-16 08:13:09 +02004444 if (use_delta_islands)
Jeff King22f9b7f2020-07-28 16:24:27 -04004445 strvec_push(&rp, "--topo-order");
Jeff King28b8a732018-08-16 08:13:09 +02004446
Nicolas Pitre4f366272009-11-23 12:43:50 -05004447 if (progress && all_progress_implied)
4448 progress = 2;
4449
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004450 add_extra_kept_packs(&keep_pack_list);
4451 if (ignore_packed_keep_on_disk) {
Jeff King56dfeb62016-07-29 00:11:31 -04004452 struct packed_git *p;
Derrick Stolee454ea2e2018-08-20 16:52:04 +00004453 for (p = get_all_packs(the_repository); p; p = p->next)
Jeff King56dfeb62016-07-29 00:11:31 -04004454 if (p->pack_local && p->pack_keep)
4455 break;
4456 if (!p) /* no keep-able packs found */
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004457 ignore_packed_keep_on_disk = 0;
Jeff King56dfeb62016-07-29 00:11:31 -04004458 }
4459 if (local) {
4460 /*
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +02004461 * unlike ignore_packed_keep_on_disk above, we do not
4462 * want to unset "local" based on looking at packs, as
4463 * it also covers non-local objects
Jeff King56dfeb62016-07-29 00:11:31 -04004464 */
4465 struct packed_git *p;
Derrick Stolee454ea2e2018-08-20 16:52:04 +00004466 for (p = get_all_packs(the_repository); p; p = p->next) {
Jeff King56dfeb62016-07-29 00:11:31 -04004467 if (!p->pack_local) {
4468 have_non_local_packs = 1;
4469 break;
4470 }
4471 }
4472 }
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004473
Derrick Stoleeae417802019-02-22 14:25:07 -08004474 trace2_region_enter("pack-objects", "enumerate-objects",
4475 the_repository);
Nguyễn Thái Ngọc Duy7c141122018-11-10 06:49:08 +01004476 prepare_packing_data(the_repository, &to_pack);
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +02004477
Taylor Blaub7573532022-05-20 19:17:52 -04004478 if (progress && !cruft)
Nguyễn Thái Ngọc Duy5af05042018-04-15 17:36:18 +02004479 progress_state = start_progress(_("Enumerating objects"), 0);
Taylor Blau339bce22021-02-22 21:25:10 -05004480 if (stdin_packs) {
4481 /* avoids adding objects in excluded packs */
4482 ignore_packed_keep_in_core = 1;
4483 read_packs_list_from_stdin();
4484 if (rev_list_unpacked)
4485 add_unreachable_loose_objects();
Taylor Blaub7573532022-05-20 19:17:52 -04004486 } else if (cruft) {
4487 read_cruft_objects();
Taylor Blau9e39acc2021-10-26 17:01:13 -04004488 } else if (!use_internal_rev_list) {
Junio C Hamanob5d97e62006-09-04 23:47:39 -07004489 read_object_list_from_stdin();
Taylor Blau9e39acc2021-10-26 17:01:13 -04004490 } else {
Ævar Arnfjörð Bjarmason5cb28272022-03-28 17:43:18 +02004491 struct rev_info revs;
4492
4493 repo_init_revisions(the_repository, &revs, NULL);
René Scharfe0d5448a2022-11-29 13:25:05 +01004494 list_objects_filter_copy(&revs.filter, &filter_options);
Derrick Stolee80f6de42022-03-22 17:28:36 +00004495 get_object_list(&revs, rp.nr, rp.v);
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02004496 release_revisions(&revs);
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07004497 }
Nicolas Pitre0ef95f72009-09-03 21:54:03 -04004498 cleanup_preferred_base();
Michael Haggertyd1552542015-05-25 18:38:38 +00004499 if (include_tag && nr_result)
Jacob Vosmaerbe181532021-01-20 13:45:14 +01004500 for_each_tag_ref(add_ref_tag, NULL);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -04004501 stop_progress(&progress_state);
Derrick Stoleeae417802019-02-22 14:25:07 -08004502 trace2_region_leave("pack-objects", "enumerate-objects",
4503 the_repository);
Nicolas Pitre96a02f82007-04-18 14:27:45 -04004504
Junio C Hamanof0b0af12006-02-24 21:55:23 -08004505 if (non_empty && !nr_result)
Taylor Blau9e39acc2021-10-26 17:01:13 -04004506 goto cleanup;
Derrick Stoleeae417802019-02-22 14:25:07 -08004507 if (nr_result) {
4508 trace2_region_enter("pack-objects", "prepare-pack",
4509 the_repository);
Nicolas Pitref7ae6a92007-04-16 12:30:15 -04004510 prepare_pack(window, depth);
Derrick Stoleeae417802019-02-22 14:25:07 -08004511 trace2_region_leave("pack-objects", "prepare-pack",
4512 the_repository);
4513 }
4514
4515 trace2_region_enter("pack-objects", "write-pack-file", the_repository);
Jonathan Tandd4b7322020-06-10 13:57:23 -07004516 write_excluded_by_configs();
Dana L. Howd01fb922007-05-13 11:34:56 -07004517 write_pack_file();
Derrick Stoleeae417802019-02-22 14:25:07 -08004518 trace2_region_leave("pack-objects", "write-pack-file", the_repository);
4519
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08004520 if (progress)
Nguyễn Thái Ngọc Duyf616db62018-07-21 09:49:24 +02004521 fprintf_ln(stderr,
4522 _("Total %"PRIu32" (delta %"PRIu32"),"
Jeff Kingbab28d92019-09-13 15:02:17 +02004523 " reused %"PRIu32" (delta %"PRIu32"),"
4524 " pack-reused %"PRIu32),
4525 written, written_delta, reused, reused_delta,
4526 reuse_packfile_objects);
Taylor Blau9e39acc2021-10-26 17:01:13 -04004527
4528cleanup:
René Scharfe0d5448a2022-11-29 13:25:05 +01004529 list_objects_filter_release(&filter_options);
Taylor Blau9e39acc2021-10-26 17:01:13 -04004530 strvec_clear(&rp);
4531
Linus Torvaldsc323ac72005-06-25 14:42:43 -07004532 return 0;
4533}