Commit

2154d88f3aef51dda4cd75216dc5749bdafc8852

odb/transaction: add transaction interface to write packfiles

In git-receive-pack(1), the incoming packfile is written to the ODB via
`unpack()`, which spawns git-index-pack(1) or git-unpack-objects(1)
directly. With pluggable object databases, an alternative backend may
need to handle writing packfile data differently though.

Introduce `odb_transaction_write_pack()` as a generic interface to
handle writing a packfile to a transaction and use the logic from
`unpack()` as the "files" backend implementation. Note that when storing
the objects as a packfile, git-index-pack(1) also writes a ".keep"
lockfile next to it to prevent a concurrent repack from removing the new
pack prior to reference updates being performed. The "files" transaction
backend is responsible for managing these ".keep" files and removes them
post-commit once the transaction is finalized.

Call sites in git-receive-pack(1) are updated accordingly.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Justin Tobler <jltobler@gmail.com> · 2026-08-20 23:49 UTC · committed by Junio C Hamano

Browse files
4 files changed, +250 −157
modified builtin/receive-pack.c
@@ -15,7 +15,6 @@
15 15 #include "gpg-interface.h"
16 16 #include "hex.h"
17 17 #include "hook.h"
18 - #include "lockfile.h"
19 18 #include "object.h"
20 19 #include "object-file.h"
21 20 #include "object-name.h"
@@ -23,7 +22,6 @@
23 22 #include "oid-array.h"
24 23 #include "oidset.h"
25 24 #include "pack.h"
26 - #include "packfile.h"
27 25 #include "parse-options.h"
28 26 #include "pkt-line.h"
29 27 #include "protocol.h"
@@ -2292,162 +2290,11 @@ static void read_push_options(struct packet_reader *reader,
2292 2290 }
2293 2291 }
2294 2292
2295 - static const char *parse_pack_header(struct pack_header *hdr, int pack_fd)
2296 - {
2297 - switch (read_pack_header(pack_fd, hdr)) {
2298 - case PH_ERROR_EOF:
2299 - return "eof before pack header was fully read";
2300 -
2301 - case PH_ERROR_PACK_SIGNATURE:
2302 - return "protocol error (pack signature mismatch detected)";
2303 -
2304 - case PH_ERROR_PROTOCOL:
2305 - return "protocol error (pack version unsupported)";
2306 -
2307 - default:
2308 - return "unknown error in parse_pack_header";
2309 -
2310 - case 0:
2311 - return NULL;
2312 - }
2313 - }
2314 -
2315 - static struct tempfile *pack_lockfile;
2316 -
2317 - static void push_header_arg(struct strvec *args, struct pack_header *hdr)
2318 - {
2319 - strvec_pushf(args, "--pack_header=%"PRIu32",%"PRIu32,
2320 - ntohl(hdr->hdr_version), ntohl(hdr->hdr_entries));
2321 - }
2322 -
2323 - static unsigned int get_unpack_limit(struct repository *repo)
2324 - {
2325 - unsigned int limit = 100;
2326 -
2327 - repo_config_get_uint(repo, "transfer.unpacklimit", &limit);
2328 - repo_config_get_uint(repo, "receive.unpacklimit", &limit);
2329 -
2330 - return limit;
2331 - }
2332 -
2333 - struct unpack_opts {
2334 - const char *fsck_msg_types;
2335 - const char *shallow_file;
2336 - off_t max_input_size;
2337 - int fsck_objects;
2338 - int reject_thin;
2339 - int err_fd;
2340 - int quiet;
2341 - };
2342 -
2343 - static int unpack(struct odb_transaction *transaction, int pack_fd,
2344 - struct strbuf *err_msg, const struct unpack_opts *opts)
2345 - {
2346 - struct pack_header hdr;
2347 - const char *hdr_err;
2348 - int status;
2349 - struct child_process child = CHILD_PROCESS_INIT;
2350 - int err_fd = opts->err_fd;
2351 -
2352 - hdr_err = parse_pack_header(&hdr, pack_fd);
2353 - if (hdr_err) {
2354 - if (err_fd > 0)
2355 - close(err_fd);
2356 - strbuf_addstr(err_msg, hdr_err);
2357 - return -1;
2358 - }
2359 -
2360 - if (opts->shallow_file) {
2361 - strvec_push(&child.args, "--shallow-file");
2362 - strvec_push(&child.args, opts->shallow_file);
2363 - }
2364 -
2365 - odb_transaction_env(transaction, &child.env);
2366 -
2367 - if (ntohl(hdr.hdr_entries) < get_unpack_limit(the_repository)) {
2368 - strvec_push(&child.args, "unpack-objects");
2369 - push_header_arg(&child.args, &hdr);
2370 - if (opts->quiet)
2371 - strvec_push(&child.args, "-q");
2372 - if (opts->fsck_objects)
2373 - strvec_pushf(&child.args, "--strict%s",
2374 - opts->fsck_msg_types);
2375 - if (opts->max_input_size)
2376 - strvec_pushf(&child.args, "--max-input-size=%"PRIuMAX,
2377 - (uintmax_t)opts->max_input_size);
2378 - child.no_stdout = 1;
2379 - child.in = pack_fd;
2380 - child.err = err_fd;
2381 - child.git_cmd = 1;
2382 - status = run_command(&child);
2383 - if (status) {
2384 - strbuf_addstr(err_msg, "unpack-objects abnormal exit");
2385 - return -1;
2386 - }
2387 - } else {
2388 - char hostname[HOST_NAME_MAX + 1];
2389 - char *lockfile;
2390 -
2391 - strvec_pushl(&child.args, "index-pack", "--stdin", NULL);
2392 - push_header_arg(&child.args, &hdr);
2393 -
2394 - if (xgethostname(hostname, sizeof(hostname)))
2395 - xsnprintf(hostname, sizeof(hostname), "localhost");
2396 - strvec_pushf(&child.args,
2397 - "--keep=receive-pack %"PRIuMAX" on %s",
2398 - (uintmax_t)getpid(),
2399 - hostname);
2400 -
2401 - if (!opts->quiet && err_fd)
2402 - strvec_push(&child.args, "--show-resolving-progress");
2403 - if (err_fd)
2404 - strvec_push(&child.args, "--report-end-of-input");
2405 - if (opts->fsck_objects)
2406 - strvec_pushf(&child.args, "--strict%s",
2407 - opts->fsck_msg_types);
2408 - if (!opts->reject_thin)
2409 - strvec_push(&child.args, "--fix-thin");
2410 - if (opts->max_input_size)
2411 - strvec_pushf(&child.args, "--max-input-size=%"PRIuMAX,
2412 - (uintmax_t)opts->max_input_size);
2413 - child.out = -1;
2414 - child.in = pack_fd;
2415 - child.err = err_fd;
2416 - child.git_cmd = 1;
2417 - status = start_command(&child);
2418 - if (status) {
2419 - strbuf_addstr(err_msg, "index-pack fork failed");
2420 - return -1;
2421 - }
2422 -
2423 - /*
2424 - * The lockfile filepath is expected to be the final location of
2425 - * the ".keep" file after being migrated to the main ODB source.
2426 - * This ensures the lockfile can be found and removed later
2427 - * after the ODB transaction has been committed.
2428 - */
2429 - lockfile = index_pack_lockfile(transaction->source, child.out, NULL);
2430 - if (lockfile) {
2431 - pack_lockfile = register_tempfile(lockfile);
2432 - free(lockfile);
2433 - }
2434 - close(child.out);
2435 -
2436 - status = finish_command(&child);
2437 - if (status) {
2438 - strbuf_addstr(err_msg, "index-pack abnormal exit");
2439 - return -1;
2440 - }
2441 - odb_reprepare(the_repository->objects);
2442 - }
2443 - return 0;
2444 - }
2445 -
2446 2293 static int unpack_with_sideband(struct odb_transaction *transaction,
2447 2294 const char *shallow_file,
2448 2295 struct strbuf *err_msg)
2449 2296 {
2450 - struct unpack_opts opts = {
2297 + struct odb_transaction_write_pack_opts opts = {
2451 2298 .fsck_objects = (receive_fsck_objects >= 0
2452 2299 ? receive_fsck_objects
2453 2300 : transfer_fsck_objects >= 0
@@ -2463,7 +2310,7 @@ static int unpack_with_sideband(struct odb_transaction *transaction,
2463 2310 int ret;
2464 2311
2465 2312 if (!use_sideband)
2466 - return unpack(transaction, 0, err_msg, &opts);
2313 + return odb_transaction_write_pack(transaction, 0, err_msg, &opts);
2467 2314
2468 2315 use_keepalive = KEEPALIVE_AFTER_NUL;
2469 2316 memset(&muxer, 0, sizeof(muxer));
@@ -2473,7 +2320,7 @@ static int unpack_with_sideband(struct odb_transaction *transaction,
2473 2320 return 0;
2474 2321
2475 2322 opts.err_fd = muxer.in;
2476 - ret = unpack(transaction, 0, err_msg, &opts);
2323 + ret = odb_transaction_write_pack(transaction, 0, err_msg, &opts);
2477 2324
2478 2325 finish_async(&muxer);
2479 2326 return ret;
@@ -2748,7 +2595,6 @@ int cmd_receive_pack(int argc,
2748 2595 execute_commands(commands, !!unpack_status.len, &si, transaction,
2749 2596 &push_options);
2750 2597 odb_transaction_finalize(transaction);
2751 - delete_tempfile(&pack_lockfile);
2752 2598 sigchain_push(SIGPIPE, SIG_IGN);
2753 2599 if (report_status_v2)
2754 2600 report_v2(commands, &unpack_status);
modified object-file.c
@@ -10,6 +10,7 @@
10 10 #define USE_THE_REPOSITORY_VARIABLE
11 11
12 12 #include "git-compat-util.h"
13 + #include "config.h"
13 14 #include "convert.h"
14 15 #include "dir.h"
15 16 #include "environment.h"
@@ -26,6 +27,7 @@
26 27 #include "packfile.h"
27 28 #include "path.h"
28 29 #include "read-cache-ll.h"
30 + #include "run-command.h"
29 31 #include "setup.h"
30 32 #include "strvec.h"
31 33 #include "tempfile.h"
@@ -483,11 +485,16 @@ struct transaction_packfile {
483 485
484 486 struct odb_transaction_files {
485 487 struct odb_transaction base;
488 + enum odb_transaction_flags flags;
486 489
487 490 struct tmp_objdir *objdir;
488 491 struct odb_source *quarantine;
489 492 struct transaction_packfile packfile;
490 493 const char *prefix;
494 +
495 + struct tempfile **pack_lockfiles;
496 + size_t pack_lockfiles_nr;
497 + size_t pack_lockfiles_alloc;
491 498 };
492 499
493 500 int odb_transaction_files_prepare(struct odb_transaction *base)
@@ -1291,6 +1298,174 @@ static int odb_transaction_files_commit(struct odb_transaction *base)
1291 1298 return 0;
1292 1299 }
1293 1300
1301 + static const char *parse_pack_header(struct pack_header *hdr, int pack_fd)
1302 + {
1303 + switch (read_pack_header(pack_fd, hdr)) {
1304 + case PH_ERROR_EOF:
1305 + return "eof before pack header was fully read";
1306 +
1307 + case PH_ERROR_PACK_SIGNATURE:
1308 + return "protocol error (pack signature mismatch detected)";
1309 +
1310 + case PH_ERROR_PROTOCOL:
1311 + return "protocol error (pack version unsupported)";
1312 +
1313 + default:
1314 + return "unknown error in parse_pack_header";
1315 +
1316 + case 0:
1317 + return NULL;
1318 + }
1319 + }
1320 +
1321 + static void push_header_arg(struct strvec *args, struct pack_header *hdr)
1322 + {
1323 + strvec_pushf(args, "--pack_header=%"PRIu32",%"PRIu32,
1324 + ntohl(hdr->hdr_version), ntohl(hdr->hdr_entries));
1325 + }
1326 +
1327 + static unsigned int get_unpack_limit(struct repository *repo,
1328 + enum odb_transaction_flags flags)
1329 + {
1330 + unsigned int limit = 0;
1331 +
1332 + if (flags & ODB_TRANSACTION_RECEIVE) {
1333 + limit = 100;
1334 + repo_config_get_uint(repo, "transfer.unpacklimit", &limit);
1335 + repo_config_get_uint(repo, "receive.unpacklimit", &limit);
1336 + }
1337 +
1338 + return limit;
1339 + }
1340 +
1341 + static int odb_transaction_files_write_pack(struct odb_transaction *base,
1342 + int pack_fd, struct strbuf *err_msg,
1343 + const struct odb_transaction_write_pack_opts *opts)
1344 + {
1345 + struct odb_transaction_files *transaction =
1346 + container_of(base, struct odb_transaction_files, base);
1347 + struct repository *repo = base->source->odb->repo;
1348 + struct child_process child = CHILD_PROCESS_INIT;
1349 + struct pack_header hdr;
1350 + const char *hdr_err;
1351 + int err_fd = opts->err_fd;
1352 + int status;
1353 +
1354 + hdr_err = parse_pack_header(&hdr, pack_fd);
1355 + if (hdr_err) {
1356 + if (err_fd > 0)
1357 + close(err_fd);
1358 + strbuf_addstr(err_msg, hdr_err);
1359 + return -1;
1360 + }
1361 +
1362 + if (opts->shallow_file) {
1363 + strvec_push(&child.args, "--shallow-file");
1364 + strvec_push(&child.args, opts->shallow_file);
1365 + }
1366 +
1367 + odb_transaction_env(base, &child.env);
1368 +
1369 + if (ntohl(hdr.hdr_entries) < get_unpack_limit(repo, transaction->flags)) {
1370 + strvec_push(&child.args, "unpack-objects");
1371 + push_header_arg(&child.args, &hdr);
1372 + if (opts->quiet)
1373 + strvec_push(&child.args, "-q");
1374 + if (opts->fsck_objects)
1375 + strvec_pushf(&child.args, "--strict%s",
1376 + opts->fsck_msg_types);
1377 + if (opts->max_input_size)
1378 + strvec_pushf(&child.args, "--max-input-size=%"PRIuMAX,
1379 + (uintmax_t)opts->max_input_size);
1380 + child.no_stdout = 1;
1381 + child.in = pack_fd;
1382 + child.err = err_fd;
1383 + child.git_cmd = 1;
1384 + status = run_command(&child);
1385 + if (status) {
1386 + strbuf_addstr(err_msg, "unpack-objects abnormal exit");
1387 + return -1;
1388 + }
1389 + } else {
1390 + char hostname[HOST_NAME_MAX + 1];
1391 + char *lockfile;
1392 +
1393 + strvec_pushl(&child.args, "index-pack", "--stdin", NULL);
1394 + push_header_arg(&child.args, &hdr);
1395 +
1396 + if (xgethostname(hostname, sizeof(hostname)))
1397 + xsnprintf(hostname, sizeof(hostname), "localhost");
1398 + strvec_pushf(&child.args,
1399 + "--keep=receive-pack %"PRIuMAX" on %s",
1400 + (uintmax_t)getpid(),
1401 + hostname);
1402 +
1403 + if (!opts->quiet && err_fd)
1404 + strvec_push(&child.args, "--show-resolving-progress");
1405 + if (err_fd)
1406 + strvec_push(&child.args, "--report-end-of-input");
1407 + if (opts->fsck_objects)
1408 + strvec_pushf(&child.args, "--strict%s",
1409 + opts->fsck_msg_types);
1410 + if (!opts->reject_thin)
1411 + strvec_push(&child.args, "--fix-thin");
1412 + if (opts->max_input_size)
1413 + strvec_pushf(&child.args, "--max-input-size=%"PRIuMAX,
1414 + (uintmax_t)opts->max_input_size);
1415 + child.out = -1;
1416 + child.in = pack_fd;
1417 + child.err = err_fd;
1418 + child.git_cmd = 1;
1419 + status = start_command(&child);
1420 + if (status) {
1421 + strbuf_addstr(err_msg, "index-pack fork failed");
1422 + return -1;
1423 + }
1424 +
1425 + /*
1426 + * The lockfile filepath is expected to be the final location of
1427 + * the ".keep" file after being migrated to the main ODB source.
1428 + * This ensures the lockfile can be found and removed later
1429 + * after the ODB transaction has been committed.
1430 + */
1431 + lockfile = index_pack_lockfile(base->source, child.out, NULL);
1432 + if (lockfile) {
1433 + ALLOC_GROW(transaction->pack_lockfiles,
1434 + transaction->pack_lockfiles_nr + 1,
1435 + transaction->pack_lockfiles_alloc);
1436 + transaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =
1437 + register_tempfile(lockfile);
1438 + free(lockfile);
1439 + }
1440 + close(child.out);
1441 +
1442 + status = finish_command(&child);
1443 + if (status) {
1444 + strbuf_addstr(err_msg, "index-pack abnormal exit");
1445 + return -1;
1446 + }
1447 +
1448 + odb_source_prepare(transaction->quarantine,
1449 + ODB_PREPARE_FLUSH_CACHES);
1450 + }
1451 +
1452 + return 0;
1453 + }
1454 +
1455 + static int odb_transaction_files_finalize(struct odb_transaction *base)
1456 + {
1457 + struct odb_transaction_files *transaction =
1458 + container_of(base, struct odb_transaction_files, base);
1459 + int ret = 0;
1460 +
1461 + for (size_t i = 0; i < transaction->pack_lockfiles_nr; i++)
1462 + ret |= delete_tempfile(&transaction->pack_lockfiles[i]);
1463 +
1464 + free(transaction->pack_lockfiles);
1465 +
1466 + return ret;
1467 + }
1468 +
1294 1469 static int odb_transaction_files_env(struct odb_transaction *base,
1295 1470 struct strvec *env)
1296 1471 {
@@ -1314,8 +1489,11 @@ int odb_transaction_files_begin(struct odb_source *source,
1314 1489 transaction = xcalloc(1, sizeof(*transaction));
1315 1490 transaction->base.source = source;
1316 1491 transaction->base.commit = odb_transaction_files_commit;
1492 + transaction->base.finalize = odb_transaction_files_finalize;
1317 1493 transaction->base.write_object_stream = odb_transaction_files_write_object_stream;
1494 + transaction->base.write_pack = odb_transaction_files_write_pack;
1318 1495 transaction->base.env = odb_transaction_files_env;
1496 + transaction->flags = flags;
1319 1497
1320 1498 transaction->prefix = "bulk-fsync";
1321 1499 if (flags & ODB_TRANSACTION_RECEIVE) {
modified odb/transaction.c
@@ -59,6 +59,13 @@ int odb_transaction_write_object_stream(struct odb_transaction *transaction,
59 59 return transaction->write_object_stream(transaction, stream, len, oid);
60 60 }
61 61
62 + int odb_transaction_write_pack(struct odb_transaction *transaction, int pack_fd,
63 + struct strbuf *err_msg,
64 + const struct odb_transaction_write_pack_opts *opts)
65 + {
66 + return transaction->write_pack(transaction, pack_fd, err_msg, opts);
67 + }
68 +
62 69 int odb_transaction_env(struct odb_transaction *transaction, struct strvec *env)
63 70 {
64 71 if (!transaction)
modified odb/transaction.h
@@ -4,6 +4,50 @@
4 4 #include "gettext.h"
5 5 #include "odb.h"
6 6
7 + /*
8 + * Options controlling how odb_transaction_write_pack() ingests a packfile.
9 + */
10 + struct odb_transaction_write_pack_opts {
11 + /*
12 + * Optional fsck severity configuration to apply when incoming objects
13 + * are verified.
14 + */
15 + const char *fsck_msg_types;
16 +
17 + /*
18 + * Path to an alternative shallow file describing the shallow boundaries
19 + * to honor while ingesting the pack.
20 + */
21 + const char *shallow_file;
22 +
23 + /*
24 + * The max size in bytes of the incoming packfile allowed. No limit is
25 + * enforced when set to 0.
26 + */
27 + off_t max_input_size;
28 +
29 + /*
30 + * Whether the validity of incoming objects should be verified.
31 + */
32 + int fsck_objects;
33 +
34 + /*
35 + * Whether to reject an incoming packfile if it is "thin".
36 + */
37 + int reject_thin;
38 +
39 + /*
40 + * Optional file descriptor for reporting progress and errors. Set to 0
41 + * for none.
42 + */
43 + int err_fd;
44 +
45 + /*
46 + * Suppresses progress reporting.
47 + */
48 + int quiet;
49 + };
50 +
7 51 /*
8 52 * A transaction may be started for an object database prior to writing new
9 53 * objects via odb_transaction_begin(). These objects are not committed until
@@ -40,6 +84,15 @@ struct odb_transaction {
40 84 int (*write_object_stream)(struct odb_transaction *transaction,
41 85 struct odb_write_stream *stream, size_t len,
42 86 struct object_id *oid);
87 + /*
88 + * This callback is expected to ingest the packfile readable via
89 + * `pack_fd` into the transaction. Returns 0 on success, a negative
90 + * error code otherwise. On failure, a human-readable description is
91 + * appended to `err_msg`.
92 + */
93 + int (*write_pack)(struct odb_transaction *transaction, int pack_fd,
94 + struct strbuf *err_msg,
95 + const struct odb_transaction_write_pack_opts *opts);
43 96
44 97 /*
45 98 * This callback is expected to populate the provided strvec with the
@@ -107,6 +160,15 @@ int odb_transaction_write_object_stream(struct odb_transaction *transaction,
107 160 struct odb_write_stream *stream,
108 161 size_t len, struct object_id *oid);
109 162
163 + /*
164 + * Ingests the packfile readable via `pack_fd` into the transaction. Returns 0
165 + * on success, a negative error code otherwise. On failure, a human-readable
166 + * description is appended to `err_msg`.
167 + */
168 + int odb_transaction_write_pack(struct odb_transaction *transaction, int pack_fd,
169 + struct strbuf *err_msg,
170 + const struct odb_transaction_write_pack_opts *opts);
171 +
110 172 /*
111 173 * Populates the provided strvec with the environment variables that a child
112 174 * process should inherit so that its object writes participate in the