index-pack: refactor renaming in final()

Refactor the renaming in final() into a helper function, this is similar in spirit to a preceding refactoring of finish_tmp_packfile() in pack-write.c. Before e37d0b8730b (builtin/index-pack.c: write reverse indexes, 2021-01-25) it probably wasn't worth it to have this sort of helper, due to the differing "else if" case for "pack" files v.s. "idx" files. But since we've got "rev" as well now, let's do the renaming via a helper, this is both a net decrease in lines, and improves the readability, since we can easily see at a glance that the logic for writing these three types of files is exactly the same, aside from the obviously differing cases of "*final_name" being NULL, and "make_read_only_if_same" being different. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Sep 9, 2021 at 19:24 UTC 8737dab3463b40120c249f8f64854326f32618ec
1 file changed +23 -25
builtin/index-pack.c
+23 -25
@@ -1481,6 +1481,22 @@ static void write_special_file(const char *suffix, const char *msg,
1481 strbuf_release(&name_buf);
1482 }
1483
1484 +static void rename_tmp_packfile(const char **final_name,
1485 + const char *curr_name,
1486 + struct strbuf *name, unsigned char *hash,
1487 + const char *ext, int make_read_only_if_same)
1488 +{
1489 + if (*final_name != curr_name) {
1490 + if (!*final_name)
1491 + *final_name = odb_pack_name(name, hash, ext);
1492 + if (finalize_object_file(curr_name, *final_name))
1493 + die(_("unable to rename temporary '*.%s' file to '%s"),
1494 + ext, *final_name);
1495 + } else if (make_read_only_if_same) {
1496 + chmod(*final_name, 0444);
1497 + }
1498 +}
1499 +
1500 static void final(const char *final_pack_name, const char *curr_pack_name,
1501 const char *final_index_name, const char *curr_index_name,
1502 const char *final_rev_index_name, const char *curr_rev_index_name,
@@ -1509,31 +1525,13 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
1525 write_special_file("promisor", promisor_msg, final_pack_name,
1526 hash, NULL);
1527
1512 - if (final_pack_name != curr_pack_name) {
1513 - if (!final_pack_name)
1514 - final_pack_name = odb_pack_name(&pack_name, hash, "pack");
1515 - if (finalize_object_file(curr_pack_name, final_pack_name))
1516 - die(_("cannot store pack file"));
1517 - } else if (from_stdin)
1518 - chmod(final_pack_name, 0444);
1519 -
1520 - if (final_index_name != curr_index_name) {
1521 - if (!final_index_name)
1522 - final_index_name = odb_pack_name(&index_name, hash, "idx");
1523 - if (finalize_object_file(curr_index_name, final_index_name))
1524 - die(_("cannot store index file"));
1525 - } else
1526 - chmod(final_index_name, 0444);
1527 -
1528 - if (curr_rev_index_name) {
1529 - if (final_rev_index_name != curr_rev_index_name) {
1530 - if (!final_rev_index_name)
1531 - final_rev_index_name = odb_pack_name(&rev_index_name, hash, "rev");
1532 - if (finalize_object_file(curr_rev_index_name, final_rev_index_name))
1533 - die(_("cannot store reverse index file"));
1534 - } else
1535 - chmod(final_rev_index_name, 0444);
1536 - }
1528 + rename_tmp_packfile(&final_pack_name, curr_pack_name, &pack_name,
1529 + hash, "pack", from_stdin);
1530 + rename_tmp_packfile(&final_index_name, curr_index_name, &index_name,
1531 + hash, "idx", 1);
1532 + if (curr_rev_index_name)
1533 + rename_tmp_packfile(&final_rev_index_name, curr_rev_index_name,
1534 + &rev_index_name, hash, "rev", 1);
1535
1536 if (do_fsck_object) {
1537 struct packed_git *p;