Skip to content

Commit c0ad465

Browse files
committed
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 <[email protected]>
1 parent e8e1c29 commit c0ad465

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

builtin/pack-objects.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@ static void write_pack_file(void)
571571
uint32_t i = 0, j;
572572
struct sha1file *f;
573573
off_t offset;
574-
struct pack_header hdr;
575574
uint32_t nr_remaining = nr_result;
576575
time_t last_mtime = 0;
577576
struct object_entry **write_order;
@@ -596,11 +595,9 @@ static void write_pack_file(void)
596595
f = sha1fd(fd, pack_tmp_name);
597596
}
598597

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");
604601
nr_written = 0;
605602
for (; i < nr_objects; i++) {
606603
struct object_entry *e = write_order[i];

pack-write.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
178178
return index_name;
179179
}
180180

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+
181193
/*
182194
* Update pack header with object_count and compute new SHA1 for pack data
183195
* associated to pack_fd, and write that SHA1 at the end. That new SHA1

pack.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define PACK_H
33

44
#include "object.h"
5+
#include "csum-file.h"
56

67
/*
78
* Packed object header
@@ -74,6 +75,7 @@ extern const char *write_idx_file(const char *index_name, struct pack_idx_entry
7475
extern int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, off_t offset, off_t len, unsigned int nr);
7576
extern int verify_pack_index(struct packed_git *);
7677
extern int verify_pack(struct packed_git *);
78+
extern off_t write_pack_header(struct sha1file *f, uint32_t);
7779
extern void fixup_pack_header_footer(int, unsigned char *, const char *, uint32_t, unsigned char *, off_t);
7880
extern char *index_pack_lockfile(int fd);
7981
extern int encode_in_pack_object_header(enum object_type, uintmax_t, unsigned char *);

0 commit comments

Comments
 (0)