write_pack_header(): a helper function

Factor out a small logic out of the private write_pack_file() function in builtin/pack-objects.c Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 28, 2011 at 11:40 UTC c0ad465725302b9411e53d248871c36880b6f8fd
3 files changed +17 -6
builtin/pack-objects.c
+3 -6
@@ -571,7 +571,6 @@ static void write_pack_file(void)
571 uint32_t i = 0, j;
572 struct sha1file *f;
573 off_t offset;
574 - struct pack_header hdr;
574 uint32_t nr_remaining = nr_result;
575 time_t last_mtime = 0;
576 struct object_entry **write_order;
@@ -596,11 +595,9 @@ static void write_pack_file(void)
595 f = sha1fd(fd, pack_tmp_name);
596 }
597
599 - hdr.hdr_signature = htonl(PACK_SIGNATURE);
600 - hdr.hdr_version = htonl(PACK_VERSION);
601 - hdr.hdr_entries = htonl(nr_remaining);
602 - sha1write(f, &hdr, sizeof(hdr));
603 - offset = sizeof(hdr);
598 + offset = write_pack_header(f, nr_remaining);
599 + if (!offset)
600 + die_errno("unable to write pack header");
601 nr_written = 0;
602 for (; i < nr_objects; i++) {
603 struct object_entry *e = write_order[i];
pack-write.c
+12
@@ -178,6 +178,18 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
178 return index_name;
179 }
180
181 +off_t write_pack_header(struct sha1file *f, uint32_t nr_entries)
182 +{
183 + struct pack_header hdr;
184 +
185 + hdr.hdr_signature = htonl(PACK_SIGNATURE);
186 + hdr.hdr_version = htonl(PACK_VERSION);
187 + hdr.hdr_entries = htonl(nr_entries);
188 + if (sha1write(f, &hdr, sizeof(hdr)))
189 + return 0;
190 + return sizeof(hdr);
191 +}
192 +
193 /*
194 * Update pack header with object_count and compute new SHA1 for pack data
195 * associated to pack_fd, and write that SHA1 at the end. That new SHA1
pack.h
+2
@@ -2,6 +2,7 @@
2 #define PACK_H
3
4 #include "object.h"
5 +#include "csum-file.h"
6
7 /*
8 * Packed object header
@@ -74,6 +75,7 @@ extern const char *write_idx_file(const char *index_name, struct pack_idx_entry
75 extern int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, off_t offset, off_t len, unsigned int nr);
76 extern int verify_pack_index(struct packed_git *);
77 extern int verify_pack(struct packed_git *);
78 +extern off_t write_pack_header(struct sha1file *f, uint32_t);
79 extern void fixup_pack_header_footer(int, unsigned char *, const char *, uint32_t, unsigned char *, off_t);
80 extern char *index_pack_lockfile(int fd);
81 extern int encode_in_pack_object_header(enum object_type, uintmax_t, unsigned char *);