Skip to content

Commit 8737dab

Browse files
avargitster
authored andcommitted
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 e37d0b8 (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 <[email protected]> Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4e58ced commit 8737dab

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

builtin/index-pack.c

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,22 @@ static void write_special_file(const char *suffix, const char *msg,
14811481
strbuf_release(&name_buf);
14821482
}
14831483

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+
14841500
static void final(const char *final_pack_name, const char *curr_pack_name,
14851501
const char *final_index_name, const char *curr_index_name,
14861502
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,
15091525
write_special_file("promisor", promisor_msg, final_pack_name,
15101526
hash, NULL);
15111527

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);
15371535

15381536
if (do_fsck_object) {
15391537
struct packed_git *p;

0 commit comments

Comments
 (0)