Skip to content

Commit

Permalink
pack-objects: proper pack time stamping with --max-pack-size
Browse files Browse the repository at this point in the history
Runtime pack access is done in the pack file mtime order since recent
packs are more likely to contain frequently used objects than old packs.
However the --max-pack-size option can produce multiple packs with mtime
in the reversed order as newer objects are always written first.

Let's modify mtime of later pack files (when any) so they appear older
than preceding ones when a repack creates multiple packs.

Signed-off-by: Nicolas Pitre <nico@cam.org>
  • Loading branch information
Nicolas Pitre authored and Junio C Hamano committed Mar 14, 2008
1 parent b75aaa5 commit f746bae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions builtin-pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ static void write_pack_file(void)
struct pack_header hdr;
int do_progress = progress >> pack_to_stdout;
uint32_t nr_remaining = nr_result;
time_t last_mtime = 0;

if (do_progress)
progress_state = start_progress("Writing objects", nr_result);
Expand Down Expand Up @@ -504,13 +505,15 @@ static void write_pack_file(void)

if (!pack_to_stdout) {
mode_t mode = umask(0);
struct stat st;
char *idx_tmp_name, tmpname[PATH_MAX];

umask(mode);
mode = 0444 & ~mode;

idx_tmp_name = write_idx_file(NULL, written_list,
nr_written, sha1);

snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
base_name, sha1_to_hex(sha1));
if (adjust_perm(pack_tmp_name, mode))
Expand All @@ -519,6 +522,28 @@ static void write_pack_file(void)
if (rename(pack_tmp_name, tmpname))
die("unable to rename temporary pack file: %s",
strerror(errno));

/*
* Packs are runtime accessed in their mtime
* order since newer packs are more likely to contain
* younger objects. So if we are creating multiple
* packs then we should modify the mtime of later ones
* to preserve this property.
*/
if (stat(tmpname, &st) < 0) {
warning("failed to stat %s: %s",
tmpname, strerror(errno));
} else if (!last_mtime) {
last_mtime = st.st_mtime;
} else {
struct utimbuf utb;
utb.actime = st.st_atime;
utb.modtime = --last_mtime;
if (utime(tmpname, &utb) < 0)
warning("failed utime() on %s: %s",
tmpname, strerror(errno));
}

snprintf(tmpname, sizeof(tmpname), "%s-%s.idx",
base_name, sha1_to_hex(sha1));
if (adjust_perm(idx_tmp_name, mode))
Expand All @@ -527,6 +552,7 @@ static void write_pack_file(void)
if (rename(idx_tmp_name, tmpname))
die("unable to rename temporary index file: %s",
strerror(errno));

free(idx_tmp_name);
free(pack_tmp_name);
puts(sha1_to_hex(sha1));
Expand Down
1 change: 1 addition & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <utime.h>
#ifndef NO_SYS_SELECT_H
#include <sys/select.h>
#endif
Expand Down

0 comments on commit f746bae

Please sign in to comment.