pack-write: split up finish_tmp_packfile() function
Split up the finish_tmp_packfile() function and use the split-up version in pack-objects.c in preparation for moving the step of renaming the *.idx file later as part of a function change. Since the only other caller of finish_tmp_packfile() was in bulk-checkin.c, and it won't be needing a change to its *.idx renaming, provide a thin wrapper for the old function as a static function in that file. If other callers end up needing the simpler version it could be moved back to "pack-write.c" and "pack.h". 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
2ec02dd5a8261bc837b961ef36788081ded5c2bc
4 files changed
+39
-13
builtin/pack-objects.c
+5
-2
@@ -1217,6 +1217,7 @@ static void write_pack_file(void)
1217
if (!pack_to_stdout) {
1218
struct stat st;
1219
struct strbuf tmpname = STRBUF_INIT;
1220
+ char *idx_tmp_name = NULL;
1221
1222
/*
1223
* Packs are runtime accessed in their mtime
@@ -1246,9 +1247,10 @@ static void write_pack_file(void)
1247
&to_pack, written_list, nr_written);
1248
}
1249
1249
- finish_tmp_packfile(&tmpname, pack_tmp_name,
1250
+ stage_tmp_packfiles(&tmpname, pack_tmp_name,
1251
written_list, nr_written,
1251
- &pack_idx_opts, hash);
1252
+ &pack_idx_opts, hash, &idx_tmp_name);
1253
+ rename_tmp_packfile_idx(&tmpname, &idx_tmp_name);
1254
1255
if (write_bitmap_index) {
1256
size_t tmpname_len = tmpname.len;
@@ -1265,6 +1267,7 @@ static void write_pack_file(void)
1267
strbuf_setlen(&tmpname, tmpname_len);
1268
}
1269
1270
+ free(idx_tmp_name);
1271
strbuf_release(&tmpname);
1272
free(pack_tmp_name);
1273
puts(hash_to_hex(hash));
bulk-checkin.c
+16
@@ -23,6 +23,22 @@ static struct bulk_checkin_state {
23
uint32_t nr_written;
24
} state;
25
26
+static void finish_tmp_packfile(struct strbuf *basename,
27
+ const char *pack_tmp_name,
28
+ struct pack_idx_entry **written_list,
29
+ uint32_t nr_written,
30
+ struct pack_idx_option *pack_idx_opts,
31
+ unsigned char hash[])
32
+{
33
+ char *idx_tmp_name = NULL;
34
+
35
+ stage_tmp_packfiles(basename, pack_tmp_name, written_list, nr_written,
36
+ pack_idx_opts, hash, &idx_tmp_name);
37
+ rename_tmp_packfile_idx(basename, &idx_tmp_name);
38
+
39
+ free(idx_tmp_name);
40
+}
41
+
42
static void finish_bulk_checkin(struct bulk_checkin_state *state)
43
{
44
unsigned char hash[GIT_MAX_RAWSZ];
pack-write.c
+13
-9
@@ -474,21 +474,28 @@ static void rename_tmp_packfile(struct strbuf *name_prefix, const char *source,
474
strbuf_setlen(name_prefix, name_prefix_len);
475
}
476
477
-void finish_tmp_packfile(struct strbuf *name_buffer,
477
+void rename_tmp_packfile_idx(struct strbuf *name_buffer,
478
+ char **idx_tmp_name)
479
+{
480
+ rename_tmp_packfile(name_buffer, *idx_tmp_name, "idx");
481
+}
482
+
483
+void stage_tmp_packfiles(struct strbuf *name_buffer,
484
const char *pack_tmp_name,
485
struct pack_idx_entry **written_list,
486
uint32_t nr_written,
487
struct pack_idx_option *pack_idx_opts,
482
- unsigned char hash[])
488
+ unsigned char hash[],
489
+ char **idx_tmp_name)
490
{
484
- const char *idx_tmp_name, *rev_tmp_name = NULL;
491
+ const char *rev_tmp_name = NULL;
492
493
if (adjust_shared_perm(pack_tmp_name))
494
die_errno("unable to make temporary pack file readable");
495
489
- idx_tmp_name = write_idx_file(NULL, written_list, nr_written,
490
- pack_idx_opts, hash);
491
- if (adjust_shared_perm(idx_tmp_name))
496
+ *idx_tmp_name = (char *)write_idx_file(NULL, written_list, nr_written,
497
+ pack_idx_opts, hash);
498
+ if (adjust_shared_perm(*idx_tmp_name))
499
die_errno("unable to make temporary index file readable");
500
501
rev_tmp_name = write_rev_file(NULL, written_list, nr_written, hash,
@@ -497,9 +504,6 @@ void finish_tmp_packfile(struct strbuf *name_buffer,
504
rename_tmp_packfile(name_buffer, pack_tmp_name, "pack");
505
if (rev_tmp_name)
506
rename_tmp_packfile(name_buffer, rev_tmp_name, "rev");
500
- rename_tmp_packfile(name_buffer, idx_tmp_name, "idx");
501
-
502
- free((void *)idx_tmp_name);
507
}
508
509
void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought)
pack.h
+5
-2
@@ -110,11 +110,14 @@ int encode_in_pack_object_header(unsigned char *hdr, int hdr_len,
110
int read_pack_header(int fd, struct pack_header *);
111
112
struct hashfile *create_tmp_packfile(char **pack_tmp_name);
113
-void finish_tmp_packfile(struct strbuf *name_buffer,
113
+void stage_tmp_packfiles(struct strbuf *name_buffer,
114
const char *pack_tmp_name,
115
struct pack_idx_entry **written_list,
116
uint32_t nr_written,
117
struct pack_idx_option *pack_idx_opts,
118
- unsigned char sha1[]);
118
+ unsigned char hash[],
119
+ char **idx_tmp_name);
120
+void rename_tmp_packfile_idx(struct strbuf *basename,
121
+ char **idx_tmp_name);
122
123
#endif