fetch-pack: refactor writing promisor file

Let's replace the 2 different pieces of code that write a promisor file in 'builtin/repack.c' and 'fetch-pack.c' with a new function called 'write_promisor_file()' in 'pack-write.c' and 'pack.h'. This might also help us in the future, if we want to put back the ref names and associated hashes that were in the promisor files we are repacking in 'builtin/repack.c' as suggested by a NEEDSWORK comment just above the code we are refactoring. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Jan 12, 2021 at 09:21 UTC 33add2ad7d6921489aa0cafd4a865504c3709512
4 files changed +20 -12
builtin/repack.c
+3 -5
@@ -14,6 +14,7 @@
14 #include "object-store.h"
15 #include "promisor-remote.h"
16 #include "shallow.h"
17 +#include "pack.h"
18
19 static int delta_base_offset = 1;
20 static int pack_kept_objects = -1;
@@ -263,7 +264,7 @@ static void repack_promisor_objects(const struct pack_objects_args *args,
264 while (strbuf_getline_lf(&line, out) != EOF) {
265 struct string_list_item *item;
266 char *promisor_name;
266 - int fd;
267 +
268 if (line.len != the_hash_algo->hexsz)
269 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
270 item = string_list_append(names, line.buf);
@@ -281,10 +282,7 @@ static void repack_promisor_objects(const struct pack_objects_args *args,
282 */
283 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
284 line.buf);
284 - fd = open(promisor_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
285 - if (fd < 0)
286 - die_errno(_("unable to create '%s'"), promisor_name);
287 - close(fd);
285 + write_promisor_file(promisor_name, NULL, 0);
286
287 item->util = (void *)(uintptr_t)populate_pack_exts(item->string);
288
fetch-pack.c
+1 -7
@@ -777,8 +777,6 @@ static void create_promisor_file(const char *keep_name,
777 {
778 struct strbuf promisor_name = STRBUF_INIT;
779 int suffix_stripped;
780 - FILE *output;
781 - int i;
780
781 strbuf_addstr(&promisor_name, keep_name);
782 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
@@ -787,11 +785,7 @@ static void create_promisor_file(const char *keep_name,
785 keep_name);
786 strbuf_addstr(&promisor_name, ".promisor");
787
790 - output = xfopen(promisor_name.buf, "w");
791 - for (i = 0; i < nr_sought; i++)
792 - fprintf(output, "%s %s\n", oid_to_hex(&sought[i]->old_oid),
793 - sought[i]->name);
794 - fclose(output);
788 + write_promisor_file(promisor_name.buf, sought, nr_sought);
789
790 strbuf_release(&promisor_name);
791 }
pack-write.c
+12
@@ -1,6 +1,7 @@
1 #include "cache.h"
2 #include "pack.h"
3 #include "csum-file.h"
4 +#include "remote.h"
5
6 void reset_pack_idx_option(struct pack_idx_option *opts)
7 {
@@ -367,3 +368,14 @@ void finish_tmp_packfile(struct strbuf *name_buffer,
368
369 free((void *)idx_tmp_name);
370 }
371 +
372 +void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought)
373 +{
374 + int i;
375 + FILE *output = xfopen(promisor_name, "w");
376 +
377 + for (i = 0; i < nr_sought; i++)
378 + fprintf(output, "%s %s\n", oid_to_hex(&sought[i]->old_oid),
379 + sought[i]->name);
380 + fclose(output);
381 +}
pack.h
+4
@@ -87,6 +87,10 @@ off_t write_pack_header(struct hashfile *f, uint32_t);
87 void fixup_pack_header_footer(int, unsigned char *, const char *, uint32_t, unsigned char *, off_t);
88 char *index_pack_lockfile(int fd);
89
90 +struct ref;
91 +
92 +void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought);
93 +
94 /*
95 * The "hdr" output buffer should be at least this big, which will handle sizes
96 * up to 2^67.