From 33add2ad7d6921489aa0cafd4a865504c3709512 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Tue, 12 Jan 2021 09:21:59 +0100 Subject: 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 Signed-off-by: Junio C Hamano --- pack-write.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'pack-write.c') diff --git a/pack-write.c b/pack-write.c index 3513665e1e..db3ff9980f 100644 --- a/pack-write.c +++ b/pack-write.c @@ -1,6 +1,7 @@ #include "cache.h" #include "pack.h" #include "csum-file.h" +#include "remote.h" void reset_pack_idx_option(struct pack_idx_option *opts) { @@ -367,3 +368,14 @@ void finish_tmp_packfile(struct strbuf *name_buffer, free((void *)idx_tmp_name); } + +void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought) +{ + int i; + FILE *output = xfopen(promisor_name, "w"); + + for (i = 0; i < nr_sought; i++) + fprintf(output, "%s %s\n", oid_to_hex(&sought[i]->old_oid), + sought[i]->name); + fclose(output); +} -- cgit v1.3-5-g9baa From 7c99bc23fcd69eeebf8a92263b5d90673c510a6a Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Thu, 14 Jan 2021 16:50:16 +0100 Subject: pack-write: die on error in write_promisor_file() write_promisor_file() already uses xfopen(), so it would die if the file cannot be opened for writing. To be consistent with this behavior and not overlook issues, let's also die if there are errors when we are actually writing to the file. Suggested-by: Jeff King Suggested-by: Taylor Blau Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- pack-write.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'pack-write.c') diff --git a/pack-write.c b/pack-write.c index db3ff9980f..e9bb3fd949 100644 --- a/pack-write.c +++ b/pack-write.c @@ -371,11 +371,15 @@ void finish_tmp_packfile(struct strbuf *name_buffer, void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought) { - int i; + int i, err; FILE *output = xfopen(promisor_name, "w"); for (i = 0; i < nr_sought; i++) fprintf(output, "%s %s\n", oid_to_hex(&sought[i]->old_oid), sought[i]->name); - fclose(output); + + err = ferror(output); + err |= fclose(output); + if (err) + die(_("could not write '%s' promisor file"), promisor_name); } -- cgit v1.3-5-g9baa