Merge branch 'jc/maint-1.6.0-keep-pack'
authorJunio C Hamano <gitster@pobox.com>
Thu, 2 Apr 2009 05:34:19 +0000 (22:34 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 2 Apr 2009 05:34:19 +0000 (22:34 -0700)
* jc/maint-1.6.0-keep-pack:
pack-objects: don't loosen objects available in alternate or kept packs
t7700: demonstrate repack flaw which may loosen objects unnecessarily
Remove --kept-pack-only option and associated infrastructure
pack-objects: only repack or loosen objects residing in "local" packs
git-repack.sh: don't use --kept-pack-only option to pack-objects
t7700-repack: add two new tests demonstrating repacking flaws

Conflicts:
t/t7700-repack.sh

1  2 
builtin-pack-objects.c
cache.h
git-repack.sh
revision.c
revision.h
sha1_file.c
t/t7700-repack.sh
diff --combined builtin-pack-objects.c
index 2000d97ec406a439db81542684f77ebbec9d0172,ad3f8e7751a8de2697288966a39ce54abd7b3242..9fc3b355470466bd5663e1fca1fe759e18869ee2
@@@ -78,7 -78,7 +78,7 @@@ static int progress = 1
  static int window = 10;
  static uint32_t pack_size_limit, pack_size_limit_cfg;
  static int depth = 50;
 -static int delta_search_threads = 1;
 +static int delta_search_threads;
  static int pack_to_stdout;
  static int num_preferred_base;
  static struct progress *progress_state;
@@@ -195,16 -195,16 +195,16 @@@ static int check_pack_inflate(struct pa
        int st;
  
        memset(&stream, 0, sizeof(stream));
 -      inflateInit(&stream);
 +      git_inflate_init(&stream);
        do {
                in = use_pack(p, w_curs, offset, &stream.avail_in);
                stream.next_in = in;
                stream.next_out = fakebuf;
                stream.avail_out = sizeof(fakebuf);
 -              st = inflate(&stream, Z_FINISH);
 +              st = git_inflate(&stream, Z_FINISH);
                offset += stream.next_in - in;
        } while (st == Z_OK || st == Z_BUF_ERROR);
 -      inflateEnd(&stream);
 +      git_inflate_end(&stream);
        return (st == Z_STREAM_END &&
                stream.total_out == expect &&
                stream.total_in == len) ? 0 : -1;
@@@ -286,7 -286,6 +286,7 @@@ static unsigned long write_object(struc
                                 */
  
        if (!to_reuse) {
 +              no_reuse:
                if (!usable_delta) {
                        buf = read_sha1_file(entry->idx.sha1, &type, &size);
                        if (!buf)
                struct revindex_entry *revidx;
                off_t offset;
  
 -              if (entry->delta) {
 +              if (entry->delta)
                        type = (allow_ofs_delta && entry->delta->idx.offset) ?
                                OBJ_OFS_DELTA : OBJ_REF_DELTA;
 -                      reused_delta++;
 -              }
                hdrlen = encode_header(type, entry->size, header);
 +
                offset = entry->in_pack_offset;
                revidx = find_pack_revindex(p, offset);
                datalen = revidx[1].offset - offset;
                if (!pack_to_stdout && p->index_version > 1 &&
 -                  check_pack_crc(p, &w_curs, offset, datalen, revidx->nr))
 -                      die("bad packed object CRC for %s", sha1_to_hex(entry->idx.sha1));
 +                  check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) {
 +                      error("bad packed object CRC for %s", sha1_to_hex(entry->idx.sha1));
 +                      unuse_pack(&w_curs);
 +                      goto no_reuse;
 +              }
 +
                offset += entry->in_pack_header_size;
                datalen -= entry->in_pack_header_size;
 +              if (!pack_to_stdout && p->index_version == 1 &&
 +                  check_pack_inflate(p, &w_curs, offset, datalen, entry->size)) {
 +                      error("corrupt packed object for %s", sha1_to_hex(entry->idx.sha1));
 +                      unuse_pack(&w_curs);
 +                      goto no_reuse;
 +              }
 +
                if (type == OBJ_OFS_DELTA) {
                        off_t ofs = entry->idx.offset - entry->delta->idx.offset;
                        unsigned pos = sizeof(dheader) - 1;
                        dheader[pos] = ofs & 127;
                        while (ofs >>= 7)
                                dheader[--pos] = 128 | (--ofs & 127);
 -                      if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit)
 +                      if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
 +                              unuse_pack(&w_curs);
                                return 0;
 +                      }
                        sha1write(f, header, hdrlen);
                        sha1write(f, dheader + pos, sizeof(dheader) - pos);
                        hdrlen += sizeof(dheader) - pos;
 +                      reused_delta++;
                } else if (type == OBJ_REF_DELTA) {
 -                      if (limit && hdrlen + 20 + datalen + 20 >= limit)
 +                      if (limit && hdrlen + 20 + datalen + 20 >= limit) {
 +                              unuse_pack(&w_curs);
                                return 0;
 +                      }
                        sha1write(f, header, hdrlen);
                        sha1write(f, entry->delta->idx.sha1, 20);
                        hdrlen += 20;
 +                      reused_delta++;
                } else {
 -                      if (limit && hdrlen + datalen + 20 >= limit)
 +                      if (limit && hdrlen + datalen + 20 >= limit) {
 +                              unuse_pack(&w_curs);
                                return 0;
 +                      }
                        sha1write(f, header, hdrlen);
                }
 -
 -              if (!pack_to_stdout && p->index_version == 1 &&
 -                  check_pack_inflate(p, &w_curs, offset, datalen, entry->size))
 -                      die("corrupt packed object for %s", sha1_to_hex(entry->idx.sha1));
                copy_pack_data(f, p, &w_curs, offset, datalen);
                unuse_pack(&w_curs);
                reused++;
@@@ -488,8 -473,9 +488,8 @@@ static void write_pack_file(void
                } else {
                        char tmpname[PATH_MAX];
                        int fd;
 -                      snprintf(tmpname, sizeof(tmpname),
 -                               "%s/pack/tmp_pack_XXXXXX", get_object_directory());
 -                      fd = xmkstemp(tmpname);
 +                      fd = odb_mkstemp(tmpname, sizeof(tmpname),
 +                                       "pack/tmp_pack_XXXXXX");
                        pack_tmp_name = xstrdup(tmpname);
                        f = sha1fd(fd, pack_tmp_name);
                }
@@@ -1031,11 -1017,9 +1031,11 @@@ static void check_object(struct object_
                 * We want in_pack_type even if we do not reuse delta
                 * since non-delta representations could still be reused.
                 */
 -              used = unpack_object_header_gently(buf, avail,
 +              used = unpack_object_header_buffer(buf, avail,
                                                   &entry->in_pack_type,
                                                   &entry->size);
 +              if (used == 0)
 +                      goto give_up;
  
                /*
                 * Determine if this is a delta and if so whether we can
                        /* Not a delta hence we've already got all we need. */
                        entry->type = entry->in_pack_type;
                        entry->in_pack_header_size = used;
 +                      if (entry->type < OBJ_COMMIT || entry->type > OBJ_BLOB)
 +                              goto give_up;
                        unuse_pack(&w_curs);
                        return;
                case OBJ_REF_DELTA:
                        ofs = c & 127;
                        while (c & 128) {
                                ofs += 1;
 -                              if (!ofs || MSB(ofs, 7))
 -                                      die("delta base offset overflow in pack for %s",
 -                                          sha1_to_hex(entry->idx.sha1));
 +                              if (!ofs || MSB(ofs, 7)) {
 +                                      error("delta base offset overflow in pack for %s",
 +                                            sha1_to_hex(entry->idx.sha1));
 +                                      goto give_up;
 +                              }
                                c = buf[used_0++];
                                ofs = (ofs << 7) + (c & 127);
                        }
 -                      if (ofs >= entry->in_pack_offset)
 -                              die("delta base offset out of bound for %s",
 -                                  sha1_to_hex(entry->idx.sha1));
                        ofs = entry->in_pack_offset - ofs;
 +                      if (ofs <= 0 || ofs >= entry->in_pack_offset) {
 +                              error("delta base offset out of bound for %s",
 +                                    sha1_to_hex(entry->idx.sha1));
 +                              goto give_up;
 +                      }
                        if (reuse_delta && !entry->preferred_base) {
                                struct revindex_entry *revidx;
                                revidx = find_pack_revindex(p, ofs);
 +                              if (!revidx)
 +                                      goto give_up;
                                base_ref = nth_packed_object_sha1(p, revidx->nr);
                        }
                        entry->in_pack_header_size = used + used_0;
                         */
                        entry->type = entry->in_pack_type;
                        entry->delta = base_entry;
 +                      entry->delta_size = entry->size;
                        entry->delta_sibling = base_entry->delta_child;
                        base_entry->delta_child = entry;
                        unuse_pack(&w_curs);
                         */
                        entry->size = get_size_from_delta(p, &w_curs,
                                        entry->in_pack_offset + entry->in_pack_header_size);
 +                      if (entry->size == 0)
 +                              goto give_up;
                        unuse_pack(&w_curs);
                        return;
                }
                 * with sha1_object_info() to find about the object type
                 * at this point...
                 */
 +              give_up:
                unuse_pack(&w_curs);
        }
  
@@@ -1293,7 -1265,7 +1293,7 @@@ static int try_delta(struct unpacked *t
                max_size = trg_entry->delta_size;
                ref_depth = trg->depth;
        }
 -      max_size = max_size * (max_depth - src->depth) /
 +      max_size = (uint64_t)max_size * (max_depth - src->depth) /
                                                (max_depth - ref_depth + 1);
        if (max_size == 0)
                return 0;
@@@ -1412,10 -1384,12 +1412,10 @@@ static void find_deltas(struct object_e
                        int window, int depth, unsigned *processed)
  {
        uint32_t i, idx = 0, count = 0;
 -      unsigned int array_size = window * sizeof(struct unpacked);
        struct unpacked *array;
        unsigned long mem_usage = 0;
  
 -      array = xmalloc(array_size);
 -      memset(array, 0, array_size);
 +      array = xcalloc(window, sizeof(struct unpacked));
  
        for (;;) {
                struct object_entry *entry;
@@@ -1611,18 -1585,11 +1611,18 @@@ static void ll_find_deltas(struct objec
                find_deltas(list, &list_size, window, depth, processed);
                return;
        }
 +      if (progress > pack_to_stdout)
 +              fprintf(stderr, "Delta compression using %d threads.\n",
 +                              delta_search_threads);
  
        /* Partition the work amongst work threads. */
        for (i = 0; i < delta_search_threads; i++) {
                unsigned sub_size = list_size / (delta_search_threads - i);
  
 +              /* don't use too small segments or no deltas will be found */
 +              if (sub_size < 2*window && i+1 < delta_search_threads)
 +                      sub_size = 0;
 +
                p[i].window = window;
                p[i].depth = depth;
                p[i].processed = processed;
@@@ -1748,16 -1715,6 +1748,16 @@@ static void prepare_pack(int window, in
  
        get_object_details();
  
 +      /*
 +       * If we're locally repacking then we need to be doubly careful
 +       * from now on in order to make sure no stealth corruption gets
 +       * propagated to the new pack.  Clients receiving streamed packs
 +       * should validate everything they get anyway so no need to incur
 +       * the additional cost here in that case.
 +       */
 +      if (!pack_to_stdout)
 +              do_check_packed_object_crc = 1;
 +
        if (!nr_objects || !window || !depth)
                return;
  
                        if (entry->type < 0)
                                die("unable to get type of object %s",
                                    sha1_to_hex(entry->idx.sha1));
 +              } else {
 +                      if (entry->type < 0) {
 +                              /*
 +                               * This object is not found, but we
 +                               * don't have to include it anyway.
 +                               */
 +                              continue;
 +                      }
                }
  
                delta_list[n++] = entry;
@@@ -1966,7 -1915,7 +1966,7 @@@ static void add_objects_in_unpacked_pac
                const unsigned char *sha1;
                struct object *o;
  
-               if (p->pack_keep)
+               if (!p->pack_local || p->pack_keep)
                        continue;
                if (open_pack_index(p))
                        die("cannot open pack index");
        free(in_pack.array);
  }
  
+ static int has_sha1_pack_kept_or_nonlocal(const unsigned char *sha1)
+ {
+       static struct packed_git *last_found = (void *)1;
+       struct packed_git *p;
+       p = (last_found != (void *)1) ? last_found : packed_git;
+       while (p) {
+               if ((!p->pack_local || p->pack_keep) &&
+                       find_pack_entry_one(sha1, p)) {
+                       last_found = p;
+                       return 1;
+               }
+               if (p == last_found)
+                       p = packed_git;
+               else
+                       p = p->next;
+               if (p == last_found)
+                       p = p->next;
+       }
+       return 0;
+ }
  static void loosen_unused_packed_objects(struct rev_info *revs)
  {
        struct packed_git *p;
        const unsigned char *sha1;
  
        for (p = packed_git; p; p = p->next) {
-               if (p->pack_keep)
+               if (!p->pack_local || p->pack_keep)
                        continue;
  
                if (open_pack_index(p))
  
                for (i = 0; i < p->num_objects; i++) {
                        sha1 = nth_packed_object_sha1(p, i);
-                       if (!locate_object_entry(sha1))
+                       if (!locate_object_entry(sha1) &&
+                               !has_sha1_pack_kept_or_nonlocal(sha1))
                                if (force_object_loose(sha1, p->mtime))
                                        die("unable to force loose object");
                }
@@@ -2200,7 -2173,6 +2224,6 @@@ int cmd_pack_objects(int argc, const ch
                        continue;
                }
                if (!strcmp("--unpacked", arg) ||
-                   !strcmp("--kept-pack-only", arg) ||
                    !strcmp("--reflog", arg) ||
                    !strcmp("--all", arg)) {
                        use_internal_rev_list = 1;
diff --combined cache.h
index f48e80bc057fb5970200cd12a791af56fa4eef8a,23c16d0d99777cc9a6f23c6b23fb16d837b1742c..9cf5a13c2cbec321aa728f5024a48e3291fb697d
+++ b/cache.h
@@@ -6,22 -6,12 +6,22 @@@
  #include "hash.h"
  
  #include SHA1_HEADER
 -#include <zlib.h>
 +#ifndef git_SHA_CTX
 +#define git_SHA_CTX   SHA_CTX
 +#define git_SHA1_Init SHA1_Init
 +#define git_SHA1_Update       SHA1_Update
 +#define git_SHA1_Final        SHA1_Final
 +#endif
  
 +#include <zlib.h>
  #if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200
  #define deflateBound(c,s)  ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
  #endif
  
 +void git_inflate_init(z_streamp strm);
 +void git_inflate_end(z_streamp strm);
 +int git_inflate(z_streamp strm, int flush);
 +
  #if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
  #define DTYPE(de)     ((de)->d_type)
  #else
@@@ -119,29 -109,9 +119,29 @@@ struct ondisk_cache_entry 
        char name[FLEX_ARRAY]; /* more */
  };
  
 +/*
 + * This struct is used when CE_EXTENDED bit is 1
 + * The struct must match ondisk_cache_entry exactly from
 + * ctime till flags
 + */
 +struct ondisk_cache_entry_extended {
 +      struct cache_time ctime;
 +      struct cache_time mtime;
 +      unsigned int dev;
 +      unsigned int ino;
 +      unsigned int mode;
 +      unsigned int uid;
 +      unsigned int gid;
 +      unsigned int size;
 +      unsigned char sha1[20];
 +      unsigned short flags;
 +      unsigned short flags2;
 +      char name[FLEX_ARRAY]; /* more */
 +};
 +
  struct cache_entry {
 -      unsigned int ce_ctime;
 -      unsigned int ce_mtime;
 +      struct cache_time ce_ctime;
 +      struct cache_time ce_mtime;
        unsigned int ce_dev;
        unsigned int ce_ino;
        unsigned int ce_mode;
  
  #define CE_NAMEMASK  (0x0fff)
  #define CE_STAGEMASK (0x3000)
 +#define CE_EXTENDED  (0x4000)
  #define CE_VALID     (0x8000)
  #define CE_STAGESHIFT 12
  
 -/* In-memory only */
 +/*
 + * Range 0xFFFF0000 in ce_flags is divided into
 + * two parts: in-memory flags and on-disk ones.
 + * Flags in CE_EXTENDED_FLAGS will get saved on-disk
 + * if you want to save a new flag, add it in
 + * CE_EXTENDED_FLAGS
 + *
 + * In-memory only flags
 + */
  #define CE_UPDATE    (0x10000)
  #define CE_REMOVE    (0x20000)
  #define CE_UPTODATE  (0x40000)
  #define CE_HASHED    (0x100000)
  #define CE_UNHASHED  (0x200000)
  
 +/*
 + * Extended on-disk flags
 + */
 +#define CE_INTENT_TO_ADD 0x20000000
 +/* CE_EXTENDED2 is for future extension */
 +#define CE_EXTENDED2 0x80000000
 +
 +#define CE_EXTENDED_FLAGS (CE_INTENT_TO_ADD)
 +
 +/*
 + * Safeguard to avoid saving wrong flags:
 + *  - CE_EXTENDED2 won't get saved until its semantic is known
 + *  - Bits in 0x0000FFFF have been saved in ce_flags already
 + *  - Bits in 0x003F0000 are currently in-memory flags
 + */
 +#if CE_EXTENDED_FLAGS & 0x803FFFFF
 +#error "CE_EXTENDED_FLAGS out of range"
 +#endif
 +
  /*
   * Copy the sha1 and stat state of a cache entry from one to
   * another. But we never change the name, or the hash state!
@@@ -228,9 -170,7 +228,9 @@@ static inline size_t ce_namelen(const s
  }
  
  #define ce_size(ce) cache_entry_size(ce_namelen(ce))
 -#define ondisk_ce_size(ce) ondisk_cache_entry_size(ce_namelen(ce))
 +#define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \
 +                          ondisk_cache_entry_extended_size(ce_namelen(ce)) : \
 +                          ondisk_cache_entry_size(ce_namelen(ce)))
  #define ce_stage(ce) ((CE_STAGEMASK & (ce)->ce_flags) >> CE_STAGESHIFT)
  #define ce_uptodate(ce) ((ce)->ce_flags & CE_UPTODATE)
  #define ce_mark_uptodate(ce) ((ce)->ce_flags |= CE_UPTODATE)
@@@ -273,16 -213,14 +273,16 @@@ static inline int ce_to_dtype(const str
        (S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
        S_ISLNK(mode) ? S_IFLNK : S_ISDIR(mode) ? S_IFDIR : S_IFGITLINK)
  
 -#define cache_entry_size(len) ((offsetof(struct cache_entry,name) + (len) + 8) & ~7)
 -#define ondisk_cache_entry_size(len) ((offsetof(struct ondisk_cache_entry,name) + (len) + 8) & ~7)
 +#define flexible_size(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)
 +#define cache_entry_size(len) flexible_size(cache_entry,len)
 +#define ondisk_cache_entry_size(len) flexible_size(ondisk_cache_entry,len)
 +#define ondisk_cache_entry_extended_size(len) flexible_size(ondisk_cache_entry_extended,len)
  
  struct index_state {
        struct cache_entry **cache;
        unsigned int cache_nr, cache_alloc, cache_changed;
        struct cache_tree *cache_tree;
 -      time_t timestamp;
 +      struct cache_time timestamp;
        void *alloc;
        unsigned name_hash_initialized : 1,
                 initialized : 1;
@@@ -317,7 -255,6 +317,7 @@@ static inline void remove_name_hash(str
  
  #define read_cache() read_index(&the_index)
  #define read_cache_from(path) read_index_from(&the_index, (path))
 +#define read_cache_preload(pathspec) read_index_preload(&the_index, (pathspec))
  #define is_cache_unborn() is_index_unborn(&the_index)
  #define read_cache_unmerged() read_index_unmerged(&the_index)
  #define write_cache(newfd, cache, entries) write_index(&the_index, (newfd))
@@@ -377,7 -314,6 +377,7 @@@ extern int is_bare_repository(void)
  extern int is_inside_git_dir(void);
  extern char *git_work_tree_cfg;
  extern int is_inside_work_tree(void);
 +extern int have_git_dir(void);
  extern const char *get_git_dir(void);
  extern char *get_object_directory(void);
  extern char *get_index_file(void);
@@@ -424,11 -360,10 +424,11 @@@ extern int init_db(const char *template
  
  /* Initialize and use the cache information */
  extern int read_index(struct index_state *);
 +extern int read_index_preload(struct index_state *, const char **pathspec);
  extern int read_index_from(struct index_state *, const char *path);
  extern int is_index_unborn(struct index_state *);
  extern int read_index_unmerged(struct index_state *);
 -extern int write_index(const struct index_state *, int newfd);
 +extern int write_index(struct index_state *, int newfd);
  extern int discard_index(struct index_state *);
  extern int unmerged_index(const struct index_state *);
  extern int verify_path(const char *path);
@@@ -438,18 -373,14 +438,18 @@@ extern int index_name_pos(const struct 
  #define ADD_CACHE_OK_TO_REPLACE 2     /* Ok to replace file/directory */
  #define ADD_CACHE_SKIP_DFCHECK 4      /* Ok to skip DF conflict checks */
  #define ADD_CACHE_JUST_APPEND 8               /* Append only; tree.c::read_tree() */
 +#define ADD_CACHE_NEW_ONLY 16         /* Do not replace existing ones */
  extern int add_index_entry(struct index_state *, struct cache_entry *ce, int option);
  extern struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really);
  extern void rename_index_entry_at(struct index_state *, int pos, const char *new_name);
  extern int remove_index_entry_at(struct index_state *, int pos);
 +extern void remove_marked_cache_entries(struct index_state *istate);
  extern int remove_file_from_index(struct index_state *, const char *path);
  #define ADD_CACHE_VERBOSE 1
  #define ADD_CACHE_PRETEND 2
  #define ADD_CACHE_IGNORE_ERRORS       4
 +#define ADD_CACHE_IGNORE_REMOVAL 8
 +#define ADD_CACHE_INTENT 16
  extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
  extern int add_file_to_index(struct index_state *, const char *path, int flags);
  extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh);
@@@ -465,6 -396,7 +465,6 @@@ extern int ie_modified(const struct ind
  
  extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
  extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path);
 -extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object);
  extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
  extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
  
@@@ -485,7 -417,6 +485,7 @@@ struct lock_file 
  };
  #define LOCK_DIE_ON_ERROR 1
  #define LOCK_NODEREF 2
 +extern NORETURN void unable_to_lock_index_die(const char *path, int err);
  extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
  extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
  extern int commit_lock_file(struct lock_file *);
@@@ -517,7 -448,6 +517,7 @@@ extern size_t packed_git_limit
  extern size_t delta_base_cache_limit;
  extern int auto_crlf;
  extern int fsync_object_files;
 +extern int core_preload_index;
  
  enum safe_crlf {
        SAFE_CRLF_FALSE = 0,
  extern enum safe_crlf safe_crlf;
  
  enum branch_track {
 +      BRANCH_TRACK_UNSPECIFIED = -1,
        BRANCH_TRACK_NEVER = 0,
        BRANCH_TRACK_REMOTE,
        BRANCH_TRACK_ALWAYS,
@@@ -542,17 -471,8 +542,17 @@@ enum rebase_setup_type 
        AUTOREBASE_ALWAYS,
  };
  
 +enum push_default_type {
 +      PUSH_DEFAULT_UNSPECIFIED = -1,
 +      PUSH_DEFAULT_NOTHING = 0,
 +      PUSH_DEFAULT_MATCHING,
 +      PUSH_DEFAULT_TRACKING,
 +      PUSH_DEFAULT_CURRENT,
 +};
 +
  extern enum branch_track git_branch_track;
  extern enum rebase_setup_type autorebase;
 +extern enum push_default_type push_default;
  
  #define GIT_REPO_VERSION 0
  extern int repository_format_version;
@@@ -597,13 -517,6 +597,13 @@@ static inline void hashclr(unsigned cha
  {
        memset(hash, 0, 20);
  }
 +extern int is_empty_blob_sha1(const unsigned char *sha1);
 +
 +#define EMPTY_TREE_SHA1_HEX \
 +      "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
 +#define EMPTY_TREE_SHA1_BIN \
 +       "\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60" \
 +       "\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04"
  
  int git_mkstemp(char *path, size_t n, const char *template);
  
@@@ -631,13 -544,11 +631,13 @@@ static inline int is_absolute_path(cons
  {
        return path[0] == '/' || has_dos_drive_prefix(path);
  }
 +int is_directory(const char *);
  const char *make_absolute_path(const char *path);
  const char *make_nonrelative_path(const char *path);
  const char *make_relative_path(const char *abs, const char *base);
 -int normalize_absolute_path(char *buf, const char *path);
 +int normalize_path_copy(char *dst, const char *src);
  int longest_ancestor_length(const char *path, const char *prefix_list);
 +char *strip_path_suffix(const char *path, const char *suffix);
  
  /* Read and unpack a sha1 file into memory, write memory to a sha1 file */
  extern int sha1_object_info(const unsigned char *, unsigned long *);
@@@ -647,15 -558,14 +647,14 @@@ extern int write_sha1_file(void *buf, u
  extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);
  extern int force_object_loose(const unsigned char *sha1, time_t mtime);
  
 -/* just like read_sha1_file(), but non fatal in presence of bad objects */
 -extern void *read_object(const unsigned char *sha1, enum object_type *type, unsigned long *size);
 +/* global flag to enable extra checks when accessing packed objects */
 +extern int do_check_packed_object_crc;
  
  extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type);
  
  extern int move_temp_to_file(const char *tmpfile, const char *filename);
  
  extern int has_sha1_pack(const unsigned char *sha1);
- extern int has_sha1_kept_pack(const unsigned char *sha1);
  extern int has_sha1_file(const unsigned char *sha1);
  extern int has_loose_object_nonlocal(const unsigned char *sha1);
  
@@@ -680,7 -590,6 +679,7 @@@ extern int read_ref(const char *filenam
  extern const char *resolve_ref(const char *path, unsigned char *sha1, int, int *);
  extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
  extern int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);
 +extern int interpret_nth_last_branch(const char *str, struct strbuf *);
  
  extern int refname_match(const char *abbrev_name, const char *full_name, const char **rules);
  extern const char *ref_rev_parse_rules[];
@@@ -707,8 -616,7 +706,8 @@@ enum date_mode 
        DATE_SHORT,
        DATE_LOCAL,
        DATE_ISO8601,
 -      DATE_RFC2822
 +      DATE_RFC2822,
 +      DATE_RAW
  };
  
  const char *show_date(unsigned long time, int timezone, enum date_mode mode);
@@@ -735,13 -643,7 +734,13 @@@ struct checkout 
  };
  
  extern int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath);
 -extern int has_symlink_leading_path(int len, const char *name);
 +extern int has_symlink_leading_path(const char *name, int len);
 +extern int has_symlink_or_noent_leading_path(const char *name, int len);
 +extern int has_dirs_only_path(const char *name, int len, int prefix_len);
 +extern void invalidate_lstat_cache(const char *name, int len);
 +extern void clear_lstat_cache(void);
 +extern void schedule_dir_for_removal(const char *name, int len);
 +extern void remove_scheduled_dirs(void);
  
  extern struct alternate_object_database {
        struct alternate_object_database *next;
  } *alt_odb_list;
  extern void prepare_alt_odb(void);
  extern void add_to_alternates_file(const char *reference);
 +typedef int alt_odb_fn(struct alternate_object_database *, void *);
 +extern void foreach_alt_odb(alt_odb_fn, void*);
  
  struct pack_window {
        struct pack_window *next;
@@@ -814,18 -714,14 +813,18 @@@ struct ref 
  #define REF_HEADS     (1u << 1)
  #define REF_TAGS      (1u << 2)
  
 -extern struct ref *find_ref_by_name(struct ref *list, const char *name);
 +extern struct ref *find_ref_by_name(const struct ref *list, const char *name);
  
  #define CONNECT_VERBOSE       (1u << 0)
  extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags);
  extern int finish_connect(struct child_process *conn);
  extern int path_match(const char *path, int nr, char **match);
  extern int get_ack(int fd, unsigned char *result_sha1);
 -extern struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match, unsigned int flags);
 +struct extra_have_objects {
 +      int nr, alloc;
 +      unsigned char (*array)[20];
 +};
 +extern struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match, unsigned int flags, struct extra_have_objects *);
  extern int server_supports(const char *feature);
  
  extern struct packed_git *parse_pack_index(unsigned char *sha1);
@@@ -843,13 -739,12 +842,13 @@@ extern unsigned char* use_pack(struct p
  extern void close_pack_windows(struct packed_git *);
  extern void unuse_pack(struct pack_window **);
  extern void free_pack_by_name(const char *);
 +extern void clear_delta_base_cache(void);
  extern struct packed_git *add_packed_git(const char *, int, int);
  extern const unsigned char *nth_packed_object_sha1(struct packed_git *, uint32_t);
  extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t);
  extern off_t find_pack_entry_one(const unsigned char *, struct packed_git *);
  extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);
 -extern unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
 +extern unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
  extern unsigned long get_size_from_delta(struct packed_git *, struct pack_window **, off_t);
  extern const char *packed_object_info_detail(struct packed_git *, off_t, unsigned long *, unsigned long *, unsigned int *, unsigned char *);
  
@@@ -860,6 -755,7 +859,6 @@@ typedef int (*config_fn_t)(const char *
  extern int git_default_config(const char *, const char *, void *);
  extern int git_config_from_file(config_fn_t fn, const char *, void *);
  extern int git_config(config_fn_t fn, void *);
 -extern int git_parse_long(const char *, long *);
  extern int git_parse_ulong(const char *, unsigned long *);
  extern int git_config_int(const char *, const char *);
  extern unsigned long git_config_ulong(const char *, const char *);
@@@ -883,7 -779,6 +882,7 @@@ extern int user_ident_explicitly_given
  
  extern const char *git_commit_encoding;
  extern const char *git_log_output_encoding;
 +extern const char *git_mailmap_file;
  
  /* IO helper functions */
  extern void maybe_flush_or_die(FILE *, const char *);
@@@ -959,6 -854,7 +958,6 @@@ extern int ws_fix_copy(char *, const ch
  extern int ws_blank_line(const char *line, int len, unsigned ws_rule);
  
  /* ls-files */
 -int pathspec_match(const char **spec, char *matched, const char *filename, int skiplen);
  int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset);
  void overlay_tree_on_cache(const char *tree_name, const char *prefix);
  
diff --combined git-repack.sh
index 0144c2d7b95d5393b7993c95e6aff46f18f01e8b,e02bf27aa6a7ee9552f5d0a937e38e6689704438..1782a23b26106545dde94a63a42340b3427cb04b
@@@ -71,11 -71,7 +71,7 @@@ case ",$all_into_one," i
                                existing="$existing $e"
                        fi
                done
-               if test -n "$existing"
-               then
-                       args="--kept-pack-only"
-               fi
-               if test -n "$args" -a -n "$unpack_unreachable" -a \
+               if test -n "$existing" -a -n "$unpack_unreachable" -a \
                        -n "$remove_redundant"
                then
                        args="$args $unpack_unreachable"
@@@ -92,79 -88,32 +88,79 @@@ if [ -z "$names" ]; the
                echo Nothing new to pack.
        fi
  fi
 -for name in $names ; do
 -      fullbases="$fullbases pack-$name"
 -      chmod a-w "$PACKTMP-$name.pack"
 -      chmod a-w "$PACKTMP-$name.idx"
 -      mkdir -p "$PACKDIR" || exit
  
 +# Ok we have prepared all new packfiles.
 +mkdir -p "$PACKDIR" || exit
 +
 +# First see if there are packs of the same name and if so
 +# if we can move them out of the way (this can happen if we
 +# repacked immediately after packing fully.
 +rollback=
 +failed=
 +for name in $names
 +do
        for sfx in pack idx
        do
 -              if test -f "$PACKDIR/pack-$name.$sfx"
 -              then
 -                      mv -f "$PACKDIR/pack-$name.$sfx" \
 -                              "$PACKDIR/old-pack-$name.$sfx"
 -              fi
 -      done &&
 +              file=pack-$name.$sfx
 +              test -f "$PACKDIR/$file" || continue
 +              rm -f "$PACKDIR/old-$file" &&
 +              mv "$PACKDIR/$file" "$PACKDIR/old-$file" || {
 +                      failed=t
 +                      break
 +              }
 +              rollback="$rollback $file"
 +      done
 +      test -z "$failed" || break
 +done
 +
 +# If renaming failed for any of them, roll the ones we have
 +# already renamed back to their original names.
 +if test -n "$failed"
 +then
 +      rollback_failure=
 +      for file in $rollback
 +      do
 +              mv "$PACKDIR/old-$file" "$PACKDIR/$file" ||
 +              rollback_failure="$rollback_failure $file"
 +      done
 +      if test -n "$rollback_failure"
 +      then
 +              echo >&2 "WARNING: Some packs in use have been renamed by"
 +              echo >&2 "WARNING: prefixing old- to their name, in order to"
 +              echo >&2 "WARNING: replace them with the new version of the"
 +              echo >&2 "WARNING: file.  But the operation failed, and"
 +              echo >&2 "WARNING: attempt to rename them back to their"
 +              echo >&2 "WARNING: original names also failed."
 +              echo >&2 "WARNING: Please rename them in $PACKDIR manually:"
 +              for file in $rollback_failure
 +              do
 +                      echo >&2 "WARNING:   old-$file -> $file"
 +              done
 +      fi
 +      exit 1
 +fi
 +
 +# Now the ones with the same name are out of the way...
 +fullbases=
 +for name in $names
 +do
 +      fullbases="$fullbases pack-$name"
 +      chmod a-w "$PACKTMP-$name.pack"
 +      chmod a-w "$PACKTMP-$name.idx"
        mv -f "$PACKTMP-$name.pack" "$PACKDIR/pack-$name.pack" &&
 -      mv -f "$PACKTMP-$name.idx"  "$PACKDIR/pack-$name.idx" &&
 -      test -f "$PACKDIR/pack-$name.pack" &&
 -      test -f "$PACKDIR/pack-$name.idx" || {
 -              echo >&2 "Couldn't replace the existing pack with updated one."
 -              echo >&2 "The original set of packs have been saved as"
 -              echo >&2 "old-pack-$name.{pack,idx} in $PACKDIR."
 -              exit 1
 -      }
 -      rm -f "$PACKDIR/old-pack-$name.pack" "$PACKDIR/old-pack-$name.idx"
 +      mv -f "$PACKTMP-$name.idx"  "$PACKDIR/pack-$name.idx" ||
 +      exit
 +done
 +
 +# Remove the "old-" files
 +for name in $names
 +do
 +      rm -f "$PACKDIR/old-pack-$name.idx"
 +      rm -f "$PACKDIR/old-pack-$name.pack"
  done
  
 +# End of pack replacement.
 +
  if test "$remove_redundant" = t
  then
        # We know $existing are all redundant.
diff --combined revision.c
index f5771c7898a939e34effb8c757c5a237830075b9,50a5b5f3943a89dab65dbfaa1657b9792aca51d5..b6215cc72c77baa3f72d652e8457d3ace6e5f439
@@@ -11,7 -11,6 +11,7 @@@
  #include "reflog-walk.h"
  #include "patch-ids.h"
  #include "decorate.h"
 +#include "log-tree.h"
  
  volatile show_early_output_fn_t show_early_output;
  
@@@ -183,11 -182,8 +183,11 @@@ static struct commit *handle_commit(str
                if (!tag->tagged)
                        die("bad tag");
                object = parse_object(tag->tagged->sha1);
 -              if (!object)
 +              if (!object) {
 +                      if (flags & UNINTERESTING)
 +                              return NULL;
                        die("bad object %s", sha1_to_hex(tag->tagged->sha1));
 +              }
        }
  
        /*
                        mark_parents_uninteresting(commit);
                        revs->limited = 1;
                }
 +              if (revs->show_source && !commit->util)
 +                      commit->util = (void *) name;
                return commit;
        }
  
@@@ -298,31 -292,10 +298,31 @@@ static void file_change(struct diff_opt
        DIFF_OPT_SET(options, HAS_CHANGES);
  }
  
 -static int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
 +static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
  {
 +      struct tree *t1 = parent->tree;
 +      struct tree *t2 = commit->tree;
 +
        if (!t1)
                return REV_TREE_NEW;
 +
 +      if (revs->simplify_by_decoration) {
 +              /*
 +               * If we are simplifying by decoration, then the commit
 +               * is worth showing if it has a tag pointing at it.
 +               */
 +              if (lookup_decoration(&name_decoration, &commit->object))
 +                      return REV_TREE_DIFFERENT;
 +              /*
 +               * A commit that is not pointed by a tag is uninteresting
 +               * if we are not limited by path.  This means that you will
 +               * see the usual "commits that touch the paths" plus any
 +               * tagged commit by specifying both --simplify-by-decoration
 +               * and pathspec.
 +               */
 +              if (!revs->prune_data)
 +                      return REV_TREE_SAME;
 +      }
        if (!t2)
                return REV_TREE_DIFFERENT;
        tree_difference = REV_TREE_SAME;
        return tree_difference;
  }
  
 -static int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
 +static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
  {
        int retval;
        void *tree;
        unsigned long size;
        struct tree_desc empty, real;
 +      struct tree *t1 = commit->tree;
  
        if (!t1)
                return 0;
@@@ -373,7 -345,7 +373,7 @@@ static void try_to_simplify_commit(stru
                return;
  
        if (!commit->parents) {
 -              if (rev_same_tree_as_empty(revs, commit->tree))
 +              if (rev_same_tree_as_empty(revs, commit))
                        commit->object.flags |= TREESAME;
                return;
        }
                        die("cannot simplify commit %s (because of %s)",
                            sha1_to_hex(commit->object.sha1),
                            sha1_to_hex(p->object.sha1));
 -              switch (rev_compare_tree(revs, p->tree, commit->tree)) {
 +              switch (rev_compare_tree(revs, p, commit)) {
                case REV_TREE_SAME:
                        tree_same = 1;
                        if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
  
                case REV_TREE_NEW:
                        if (revs->remove_empty_trees &&
 -                          rev_same_tree_as_empty(revs, p->tree)) {
 +                          rev_same_tree_as_empty(revs, p)) {
                                /* We are adding all the specified
                                 * paths from this parent, so the
                                 * history beyond this parent is not
@@@ -482,10 -454,9 +482,10 @@@ static int add_parents_to_list(struct r
                while (parent) {
                        struct commit *p = parent->item;
                        parent = parent->next;
 +                      if (p)
 +                              p->object.flags |= UNINTERESTING;
                        if (parse_commit(p) < 0)
 -                              return -1;
 -                      p->object.flags |= UNINTERESTING;
 +                              continue;
                        if (p->parents)
                                mark_parents_uninteresting(p);
                        if (p->object.flags & SEEN)
  
                if (parse_commit(p) < 0)
                        return -1;
 +              if (revs->show_source && !p->util)
 +                      p->util = commit->util;
                p->object.flags |= left_flag;
                if (!(p->object.flags & SEEN)) {
                        p->object.flags |= SEEN;
                        insert_by_date_cached(p, list, cached_base, cache_ptr);
                }
 -              if(revs->first_parent_only)
 +              if (revs->first_parent_only)
                        break;
        }
        return 0;
@@@ -1049,19 -1018,6 +1049,19 @@@ static int handle_revision_opt(struct r
        } else if (!strcmp(arg, "--topo-order")) {
                revs->lifo = 1;
                revs->topo_order = 1;
 +      } else if (!strcmp(arg, "--simplify-merges")) {
 +              revs->simplify_merges = 1;
 +              revs->rewrite_parents = 1;
 +              revs->simplify_history = 0;
 +              revs->limited = 1;
 +      } else if (!strcmp(arg, "--simplify-by-decoration")) {
 +              revs->simplify_merges = 1;
 +              revs->rewrite_parents = 1;
 +              revs->simplify_history = 0;
 +              revs->simplify_by_decoration = 1;
 +              revs->limited = 1;
 +              revs->prune = 1;
 +              load_ref_decorations();
        } else if (!strcmp(arg, "--date-order")) {
                revs->lifo = 0;
                revs->topo_order = 1;
                revs->edge_hint = 1;
        } else if (!strcmp(arg, "--unpacked")) {
                revs->unpacked = 1;
-               revs->kept_pack_only = 0;
-       } else if (!strcmp(arg, "--kept-pack-only")) {
-               revs->unpacked = 1;
-               revs->kept_pack_only = 1;
        } else if (!prefixcmp(arg, "--unpacked=")) {
                die("--unpacked=<packfile> no longer supported.");
        } else if (!strcmp(arg, "-r")) {
        } else if (!strcmp(arg, "--pretty")) {
                revs->verbose_header = 1;
                get_commit_format(arg+8, revs);
 -      } else if (!prefixcmp(arg, "--pretty=")) {
 +      } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
                revs->verbose_header = 1;
                get_commit_format(arg+9, revs);
 +      } else if (!strcmp(arg, "--oneline")) {
 +              revs->verbose_header = 1;
 +              get_commit_format("oneline", revs);
 +              revs->abbrev_commit = 1;
        } else if (!strcmp(arg, "--graph")) {
                revs->topo_order = 1;
                revs->rewrite_parents = 1;
@@@ -1261,7 -1209,6 +1257,7 @@@ int setup_revisions(int argc, const cha
  
                        if (!strcmp(arg, "--all")) {
                                handle_refs(revs, flags, for_each_ref);
 +                              handle_refs(revs, flags, head_ref);
                                continue;
                        }
                        if (!strcmp(arg, "--branches")) {
@@@ -1394,179 -1341,6 +1390,179 @@@ static void add_child(struct rev_info *
        l->next = add_decoration(&revs->children, &parent->object, l);
  }
  
 +static int remove_duplicate_parents(struct commit *commit)
 +{
 +      struct commit_list **pp, *p;
 +      int surviving_parents;
 +
 +      /* Examine existing parents while marking ones we have seen... */
 +      pp = &commit->parents;
 +      while ((p = *pp) != NULL) {
 +              struct commit *parent = p->item;
 +              if (parent->object.flags & TMP_MARK) {
 +                      *pp = p->next;
 +                      continue;
 +              }
 +              parent->object.flags |= TMP_MARK;
 +              pp = &p->next;
 +      }
 +      /* count them while clearing the temporary mark */
 +      surviving_parents = 0;
 +      for (p = commit->parents; p; p = p->next) {
 +              p->item->object.flags &= ~TMP_MARK;
 +              surviving_parents++;
 +      }
 +      return surviving_parents;
 +}
 +
 +struct merge_simplify_state {
 +      struct commit *simplified;
 +};
 +
 +static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
 +{
 +      struct merge_simplify_state *st;
 +
 +      st = lookup_decoration(&revs->merge_simplification, &commit->object);
 +      if (!st) {
 +              st = xcalloc(1, sizeof(*st));
 +              add_decoration(&revs->merge_simplification, &commit->object, st);
 +      }
 +      return st;
 +}
 +
 +static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
 +{
 +      struct commit_list *p;
 +      struct merge_simplify_state *st, *pst;
 +      int cnt;
 +
 +      st = locate_simplify_state(revs, commit);
 +
 +      /*
 +       * Have we handled this one?
 +       */
 +      if (st->simplified)
 +              return tail;
 +
 +      /*
 +       * An UNINTERESTING commit simplifies to itself, so does a
 +       * root commit.  We do not rewrite parents of such commit
 +       * anyway.
 +       */
 +      if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
 +              st->simplified = commit;
 +              return tail;
 +      }
 +
 +      /*
 +       * Do we know what commit all of our parents should be rewritten to?
 +       * Otherwise we are not ready to rewrite this one yet.
 +       */
 +      for (cnt = 0, p = commit->parents; p; p = p->next) {
 +              pst = locate_simplify_state(revs, p->item);
 +              if (!pst->simplified) {
 +                      tail = &commit_list_insert(p->item, tail)->next;
 +                      cnt++;
 +              }
 +      }
 +      if (cnt) {
 +              tail = &commit_list_insert(commit, tail)->next;
 +              return tail;
 +      }
 +
 +      /*
 +       * Rewrite our list of parents.
 +       */
 +      for (p = commit->parents; p; p = p->next) {
 +              pst = locate_simplify_state(revs, p->item);
 +              p->item = pst->simplified;
 +      }
 +      cnt = remove_duplicate_parents(commit);
 +
 +      /*
 +       * It is possible that we are a merge and one side branch
 +       * does not have any commit that touches the given paths;
 +       * in such a case, the immediate parents will be rewritten
 +       * to different commits.
 +       *
 +       *      o----X          X: the commit we are looking at;
 +       *     /    /           o: a commit that touches the paths;
 +       * ---o----'
 +       *
 +       * Further reduce the parents by removing redundant parents.
 +       */
 +      if (1 < cnt) {
 +              struct commit_list *h = reduce_heads(commit->parents);
 +              cnt = commit_list_count(h);
 +              free_commit_list(commit->parents);
 +              commit->parents = h;
 +      }
 +
 +      /*
 +       * A commit simplifies to itself if it is a root, if it is
 +       * UNINTERESTING, if it touches the given paths, or if it is a
 +       * merge and its parents simplifies to more than one commits
 +       * (the first two cases are already handled at the beginning of
 +       * this function).
 +       *
 +       * Otherwise, it simplifies to what its sole parent simplifies to.
 +       */
 +      if (!cnt ||
 +          (commit->object.flags & UNINTERESTING) ||
 +          !(commit->object.flags & TREESAME) ||
 +          (1 < cnt))
 +              st->simplified = commit;
 +      else {
 +              pst = locate_simplify_state(revs, commit->parents->item);
 +              st->simplified = pst->simplified;
 +      }
 +      return tail;
 +}
 +
 +static void simplify_merges(struct rev_info *revs)
 +{
 +      struct commit_list *list;
 +      struct commit_list *yet_to_do, **tail;
 +
 +      if (!revs->topo_order)
 +              sort_in_topological_order(&revs->commits, revs->lifo);
 +      if (!revs->prune)
 +              return;
 +
 +      /* feed the list reversed */
 +      yet_to_do = NULL;
 +      for (list = revs->commits; list; list = list->next)
 +              commit_list_insert(list->item, &yet_to_do);
 +      while (yet_to_do) {
 +              list = yet_to_do;
 +              yet_to_do = NULL;
 +              tail = &yet_to_do;
 +              while (list) {
 +                      struct commit *commit = list->item;
 +                      struct commit_list *next = list->next;
 +                      free(list);
 +                      list = next;
 +                      tail = simplify_one(revs, commit, tail);
 +              }
 +      }
 +
 +      /* clean up the result, removing the simplified ones */
 +      list = revs->commits;
 +      revs->commits = NULL;
 +      tail = &revs->commits;
 +      while (list) {
 +              struct commit *commit = list->item;
 +              struct commit_list *next = list->next;
 +              struct merge_simplify_state *st;
 +              free(list);
 +              list = next;
 +              st = locate_simplify_state(revs, commit);
 +              if (st->simplified == commit)
 +                      tail = &commit_list_insert(commit, tail)->next;
 +      }
 +}
 +
  static void set_children(struct rev_info *revs)
  {
        struct commit_list *l;
@@@ -1607,8 -1381,6 +1603,8 @@@ int prepare_revision_walk(struct rev_in
                        return -1;
        if (revs->topo_order)
                sort_in_topological_order(&revs->commits, revs->lifo);
 +      if (revs->simplify_merges)
 +              simplify_merges(revs);
        if (revs->children.name)
                set_children(revs);
        return 0;
@@@ -1641,6 -1413,26 +1637,6 @@@ static enum rewrite_result rewrite_one(
        }
  }
  
 -static void remove_duplicate_parents(struct commit *commit)
 -{
 -      struct commit_list **pp, *p;
 -
 -      /* Examine existing parents while marking ones we have seen... */
 -      pp = &commit->parents;
 -      while ((p = *pp) != NULL) {
 -              struct commit *parent = p->item;
 -              if (parent->object.flags & TMP_MARK) {
 -                      *pp = p->next;
 -                      continue;
 -              }
 -              parent->object.flags |= TMP_MARK;
 -              pp = &p->next;
 -      }
 -      /* ... and clear the temporary mark */
 -      for (p = commit->parents; p; p = p->next)
 -              p->item->object.flags &= ~TMP_MARK;
 -}
 -
  static int rewrite_parents(struct rev_info *revs, struct commit *commit)
  {
        struct commit_list **pp = &commit->parents;
@@@ -1679,10 -1471,7 +1675,7 @@@ enum commit_action simplify_commit(stru
  {
        if (commit->object.flags & SHOWN)
                return commit_ignore;
-       if (revs->unpacked &&
-           (revs->kept_pack_only
-            ? has_sha1_kept_pack(commit->object.sha1)
-            : has_sha1_pack(commit->object.sha1)))
+       if (revs->unpacked && has_sha1_pack(commit->object.sha1))
                return commit_ignore;
        if (revs->show_all)
                return commit_show;
@@@ -1735,16 -1524,14 +1728,16 @@@ static struct commit *get_revision_1(st
                            (commit->date < revs->max_age))
                                continue;
                        if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
 -                              return NULL;
 +                              die("Failed to traverse parents of commit %s",
 +                                  sha1_to_hex(commit->object.sha1));
                }
  
                switch (simplify_commit(revs, commit)) {
                case commit_ignore:
                        continue;
                case commit_error:
 -                      return NULL;
 +                      die("Failed to simplify parents of commit %s",
 +                          sha1_to_hex(commit->object.sha1));
                default:
                        return commit;
                }
@@@ -1832,6 -1619,26 +1825,6 @@@ static struct commit *get_revision_inte
                return c;
        }
  
 -      if (revs->reverse) {
 -              int limit = -1;
 -
 -              if (0 <= revs->max_count) {
 -                      limit = revs->max_count;
 -                      if (0 < revs->skip_count)
 -                              limit += revs->skip_count;
 -              }
 -              l = NULL;
 -              while ((c = get_revision_1(revs))) {
 -                      commit_list_insert(c, &l);
 -                      if ((0 < limit) && !--limit)
 -                              break;
 -              }
 -              revs->commits = l;
 -              revs->reverse = 0;
 -              revs->max_count = -1;
 -              c = NULL;
 -      }
 -
        /*
         * Now pick up what they want to give us
         */
  
  struct commit *get_revision(struct rev_info *revs)
  {
 -      struct commit *c = get_revision_internal(revs);
 +      struct commit *c;
 +      struct commit_list *reversed;
 +
 +      if (revs->reverse) {
 +              reversed = NULL;
 +              while ((c = get_revision_internal(revs))) {
 +                      commit_list_insert(c, &reversed);
 +              }
 +              revs->commits = reversed;
 +              revs->reverse = 0;
 +              revs->reverse_output_stage = 1;
 +      }
 +
 +      if (revs->reverse_output_stage)
 +              return pop_commit(&revs->commits);
 +
 +      c = get_revision_internal(revs);
        if (c && revs->graph)
                graph_update(revs->graph, c);
        return c;
diff --combined revision.h
index ad123d78c582e11e27748a613fae5b17bd87b0e9,1d322759aab49f608ca9f225689ec2d945813d2a..6e98b71b49a4db01761d45aa244424f2512c5a41
@@@ -42,23 -42,17 +42,22 @@@ struct rev_info 
                        simplify_history:1,
                        lifo:1,
                        topo_order:1,
 +                      simplify_merges:1,
 +                      simplify_by_decoration:1,
                        tag_objects:1,
                        tree_objects:1,
                        blob_objects:1,
                        edge_hint:1,
                        limited:1,
                        unpacked:1,
-                       kept_pack_only:1,
                        boundary:2,
                        left_right:1,
                        rewrite_parents:1,
                        print_parents:1,
 +                      show_source:1,
 +                      show_decorations:1,
                        reverse:1,
 +                      reverse_output_stage:1,
                        cherry_pick:1,
                        first_parent_only:1;
  
@@@ -87,7 -81,7 +86,7 @@@
        int             nr, total;
        const char      *mime_boundary;
        char            *message_id;
 -      const char      *ref_message_id;
 +      struct string_list *ref_message_ids;
        const char      *add_signoff;
        const char      *extra_headers;
        const char      *log_reencode;
  
        struct reflog_walk_info *reflog_info;
        struct decoration children;
 +      struct decoration merge_simplification;
  };
  
  #define REV_TREE_SAME         0
diff --combined sha1_file.c
index 54972f97e004e2794416baac2f79806a608dc88e,500fd93127246fad72edca165fd4673070813f82..37e833b77d1ee556d18256154073a706ab11b3a4
@@@ -99,11 -99,7 +99,11 @@@ int safe_create_leading_directories(cha
                pos = strchr(pos, '/');
                if (!pos)
                        break;
 -              *pos = 0;
 +              while (*++pos == '/')
 +                      ;
 +              if (!*pos)
 +                      break;
 +              *--pos = '\0';
                if (!stat(path, &st)) {
                        /* path exists */
                        if (!S_ISDIR(st.st_mode)) {
@@@ -254,6 -250,7 +254,6 @@@ static void read_info_alternates(const 
   */
  static int link_alt_odb_entry(const char * entry, int len, const char * relative_base, int depth)
  {
 -      struct stat st;
        const char *objdir = get_object_directory();
        struct alternate_object_database *ent;
        struct alternate_object_database *alt;
        ent->base[pfxlen] = ent->base[entlen-1] = 0;
  
        /* Detect cases where alternate disappeared */
 -      if (stat(ent->base, &st) || !S_ISDIR(st.st_mode)) {
 +      if (!is_directory(ent->base)) {
                error("object directory %s does not exist; "
                      "check .git/objects/info/alternates.",
                      ent->base);
@@@ -397,16 -394,6 +397,16 @@@ void add_to_alternates_file(const char 
                link_alt_odb_entries(alt, alt + strlen(alt), '\n', NULL, 0);
  }
  
 +void foreach_alt_odb(alt_odb_fn fn, void *cb)
 +{
 +      struct alternate_object_database *ent;
 +
 +      prepare_alt_odb();
 +      for (ent = alt_odb_list; ent; ent = ent->next)
 +              if (fn(ent, cb))
 +                      return;
 +}
 +
  void prepare_alt_odb(void)
  {
        const char *alt;
@@@ -689,7 -676,6 +689,7 @@@ void free_pack_by_name(const char *pack
        while (*pp) {
                p = *pp;
                if (strcmp(pack_name, p->pack_name) == 0) {
 +                      clear_delta_base_cache();
                        close_pack_windows(p);
                        if (p->pack_fd != -1)
                                close(p->pack_fd);
@@@ -801,7 -787,7 +801,7 @@@ unsigned char* use_pack(struct packed_g
        if (p->pack_fd == -1 && open_packed_git(p))
                die("packfile %s cannot be accessed", p->pack_name);
  
 -      /* Since packfiles end in a hash of their content and its
 +      /* Since packfiles end in a hash of their content and it's
         * pointless to ask for an offset into the middle of that
         * hash, and the in_window function above wouldn't match
         * don't allow an offset too close to the end of the file.
@@@ -1154,8 -1140,7 +1154,8 @@@ static int legacy_loose_object(unsigne
                return 0;
  }
  
 -unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep)
 +unsigned long unpack_object_header_buffer(const unsigned char *buf,
 +              unsigned long len, enum object_type *type, unsigned long *sizep)
  {
        unsigned shift;
        unsigned char c;
        size = c & 15;
        shift = 4;
        while (c & 0x80) {
 -              if (len <= used)
 -                      return 0;
 -              if (sizeof(long) * 8 <= shift)
 +              if (len <= used || sizeof(long) * 8 <= shift) {
 +                      error("bad object header");
                        return 0;
 +              }
                c = buf[used++];
                size += (c & 0x7f) << shift;
                shift += 7;
@@@ -1197,8 -1182,8 +1197,8 @@@ static int unpack_sha1_header(z_stream 
        stream->avail_out = bufsiz;
  
        if (legacy_loose_object(map)) {
 -              inflateInit(stream);
 -              return inflate(stream, 0);
 +              git_inflate_init(stream);
 +              return git_inflate(stream, 0);
        }
  
  
         * really worth it and we don't write it any longer.  But we
         * can still read it.
         */
 -      used = unpack_object_header_gently(map, mapsize, &type, &size);
 +      used = unpack_object_header_buffer(map, mapsize, &type, &size);
        if (!used || !valid_loose_object_type[type])
                return -1;
        map += used;
        /* Set up the stream for the rest.. */
        stream->next_in = map;
        stream->avail_in = mapsize;
 -      inflateInit(stream);
 +      git_inflate_init(stream);
  
        /* And generate the fake traditional header */
        stream->total_out = 1 + snprintf(buffer, bufsiz, "%s %lu",
@@@ -1255,11 -1240,11 +1255,11 @@@ static void *unpack_sha1_rest(z_stream 
                stream->next_out = buf + bytes;
                stream->avail_out = size - bytes;
                while (status == Z_OK)
 -                      status = inflate(stream, Z_FINISH);
 +                      status = git_inflate(stream, Z_FINISH);
        }
        buf[size] = 0;
        if (status == Z_STREAM_END && !stream->avail_in) {
 -              inflateEnd(stream);
 +              git_inflate_end(stream);
                return buf;
        }
  
@@@ -1349,19 -1334,17 +1349,19 @@@ unsigned long get_size_from_delta(struc
        stream.next_out = delta_head;
        stream.avail_out = sizeof(delta_head);
  
 -      inflateInit(&stream);
 +      git_inflate_init(&stream);
        do {
                in = use_pack(p, w_curs, curpos, &stream.avail_in);
                stream.next_in = in;
 -              st = inflate(&stream, Z_FINISH);
 +              st = git_inflate(&stream, Z_FINISH);
                curpos += stream.next_in - in;
        } while ((st == Z_OK || st == Z_BUF_ERROR) &&
                 stream.total_out < sizeof(delta_head));
 -      inflateEnd(&stream);
 -      if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head))
 -              die("delta data unpack-initial failed");
 +      git_inflate_end(&stream);
 +      if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head)) {
 +              error("delta data unpack-initial failed");
 +              return 0;
 +      }
  
        /* Examine the initial part of the delta to figure out
         * the result size.
@@@ -1402,7 -1385,7 +1402,7 @@@ static off_t get_delta_base(struct pack
                        base_offset = (base_offset << 7) + (c & 127);
                }
                base_offset = delta_obj_offset - base_offset;
 -              if (base_offset >= delta_obj_offset)
 +              if (base_offset <= 0 || base_offset >= delta_obj_offset)
                        return 0;  /* out of bound */
                *curpos += used;
        } else if (type == OBJ_REF_DELTA) {
@@@ -1428,32 -1411,15 +1428,32 @@@ static int packed_delta_info(struct pac
        off_t base_offset;
  
        base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset);
 +      if (!base_offset)
 +              return OBJ_BAD;
        type = packed_object_info(p, base_offset, NULL);
 +      if (type <= OBJ_NONE) {
 +              struct revindex_entry *revidx;
 +              const unsigned char *base_sha1;
 +              revidx = find_pack_revindex(p, base_offset);
 +              if (!revidx)
 +                      return OBJ_BAD;
 +              base_sha1 = nth_packed_object_sha1(p, revidx->nr);
 +              mark_bad_packed_object(p, base_sha1);
 +              type = sha1_object_info(base_sha1, NULL);
 +              if (type <= OBJ_NONE)
 +                      return OBJ_BAD;
 +      }
  
        /* We choose to only get the type of the base object and
         * ignore potentially corrupt pack file that expects the delta
         * based on a base with a wrong size.  This saves tons of
         * inflate() calls.
         */
 -      if (sizep)
 +      if (sizep) {
                *sizep = get_size_from_delta(p, w_curs, curpos);
 +              if (*sizep == 0)
 +                      type = OBJ_BAD;
 +      }
  
        return type;
  }
@@@ -1475,11 -1441,10 +1475,11 @@@ static int unpack_object_header(struct 
         * insane, so we know won't exceed what we have been given.
         */
        base = use_pack(p, w_curs, *curpos, &left);
 -      used = unpack_object_header_gently(base, left, &type, sizep);
 -      if (!used)
 -              die("object offset outside of pack file");
 -      *curpos += used;
 +      used = unpack_object_header_buffer(base, left, &type, sizep);
 +      if (!used) {
 +              type = OBJ_BAD;
 +      } else
 +              *curpos += used;
  
        return type;
  }
@@@ -1563,9 -1528,8 +1563,9 @@@ static int packed_object_info(struct pa
                        *sizep = size;
                break;
        default:
 -              die("pack %s contains unknown object type %d",
 -                  p->pack_name, type);
 +              error("unknown object type %i at offset %"PRIuMAX" in %s",
 +                    type, (uintmax_t)obj_offset, p->pack_name);
 +              type = OBJ_BAD;
        }
        unuse_pack(&w_curs);
        return type;
@@@ -1586,14 -1550,14 +1586,14 @@@ static void *unpack_compressed_entry(st
        stream.next_out = buffer;
        stream.avail_out = size;
  
 -      inflateInit(&stream);
 +      git_inflate_init(&stream);
        do {
                in = use_pack(p, w_curs, curpos, &stream.avail_in);
                stream.next_in = in;
 -              st = inflate(&stream, Z_FINISH);
 +              st = git_inflate(&stream, Z_FINISH);
                curpos += stream.next_in - in;
        } while (st == Z_OK || st == Z_BUF_ERROR);
 -      inflateEnd(&stream);
 +      git_inflate_end(&stream);
        if ((st != Z_STREAM_END) || stream.total_out != size) {
                free(buffer);
                return NULL;
@@@ -1637,9 -1601,11 +1637,9 @@@ static void *cache_or_unpack_entry(stru
        struct delta_base_cache_entry *ent = delta_base_cache + hash;
  
        ret = ent->data;
 -      if (ret && ent->p == p && ent->base_offset == base_offset)
 -              goto found_cache_entry;
 -      return unpack_entry(p, base_offset, type, base_size);
 +      if (!ret || ent->p != p || ent->base_offset != base_offset)
 +              return unpack_entry(p, base_offset, type, base_size);
  
 -found_cache_entry:
        if (!keep_cache) {
                ent->data = NULL;
                ent->lru.next->prev = ent->lru.prev;
@@@ -1664,13 -1630,6 +1664,13 @@@ static inline void release_delta_base_c
        }
  }
  
 +void clear_delta_base_cache(void)
 +{
 +      unsigned long p;
 +      for (p = 0; p < MAX_DELTA_CACHE; p++)
 +              release_delta_base_cache(&delta_base_cache[p]);
 +}
 +
  static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
        void *base, unsigned long base_size, enum object_type type)
  {
        delta_base_cache_lru.prev = &ent->lru;
  }
  
 +static void *read_object(const unsigned char *sha1, enum object_type *type,
 +                       unsigned long *size);
 +
  static void *unpack_delta_entry(struct packed_git *p,
                                struct pack_window **w_curs,
                                off_t curpos,
                 * This is costly but should happen only in the presence
                 * of a corrupted pack, and is better than failing outright.
                 */
 -              struct revindex_entry *revidx = find_pack_revindex(p, base_offset);
 -              const unsigned char *base_sha1 =
 -                                      nth_packed_object_sha1(p, revidx->nr);
 +              struct revindex_entry *revidx;
 +              const unsigned char *base_sha1;
 +              revidx = find_pack_revindex(p, base_offset);
 +              if (!revidx)
 +                      return NULL;
 +              base_sha1 = nth_packed_object_sha1(p, revidx->nr);
                error("failed to read delta base object %s"
                      " at offset %"PRIuMAX" from %s",
                      sha1_to_hex(base_sha1), (uintmax_t)base_offset,
        return result;
  }
  
 +int do_check_packed_object_crc;
 +
  void *unpack_entry(struct packed_git *p, off_t obj_offset,
                   enum object_type *type, unsigned long *sizep)
  {
        off_t curpos = obj_offset;
        void *data;
  
 +      if (do_check_packed_object_crc && p->index_version > 1) {
 +              struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
 +              unsigned long len = revidx[1].offset - obj_offset;
 +              if (check_pack_crc(p, &w_curs, obj_offset, len, revidx->nr)) {
 +                      const unsigned char *sha1 =
 +                              nth_packed_object_sha1(p, revidx->nr);
 +                      error("bad packed object CRC for %s",
 +                            sha1_to_hex(sha1));
 +                      mark_bad_packed_object(p, sha1);
 +                      unuse_pack(&w_curs);
 +                      return NULL;
 +              }
 +      }
 +
        *type = unpack_object_header(p, &w_curs, &curpos, sizep);
        switch (*type) {
        case OBJ_OFS_DELTA:
@@@ -1919,8 -1856,7 +1919,7 @@@ off_t find_pack_entry_one(const unsigne
        return 0;
  }
  
- static int find_pack_ent(const unsigned char *sha1, struct pack_entry *e,
-                        int kept_pack_only)
+ static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
  {
        static struct packed_git *last_found = (void *)1;
        struct packed_git *p;
        p = (last_found == (void *)1) ? packed_git : last_found;
  
        do {
-               if (kept_pack_only && !p->pack_keep)
-                       goto next;
                if (p->num_bad_objects) {
                        unsigned i;
                        for (i = 0; i < p->num_bad_objects; i++)
        return 0;
  }
  
- static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
- {
-       return find_pack_ent(sha1, e, 0);
- }
- static int find_kept_pack_entry(const unsigned char *sha1, struct pack_entry *e)
- {
-       return find_pack_ent(sha1, e, 1);
- }
  struct packed_git *find_sha1_pack(const unsigned char *sha1,
                                  struct packed_git *packs)
  {
@@@ -2014,7 -1938,7 +2001,7 @@@ static int sha1_loose_object_info(cons
                status = error("unable to parse %s header", sha1_to_hex(sha1));
        else if (sizep)
                *sizep = size;
 -      inflateEnd(&stream);
 +      git_inflate_end(&stream);
        munmap(map, mapsize);
        return status;
  }
@@@ -2035,14 -1959,7 +2022,14 @@@ int sha1_object_info(const unsigned cha
                if (!find_pack_entry(sha1, &e))
                        return status;
        }
 -      return packed_object_info(e.p, e.offset, sizep);
 +
 +      status = packed_object_info(e.p, e.offset, sizep);
 +      if (status < 0) {
 +              mark_bad_packed_object(e.p, sha1);
 +              status = sha1_object_info(sha1, sizep);
 +      }
 +
 +      return status;
  }
  
  static void *read_packed_sha1(const unsigned char *sha1,
@@@ -2084,7 -2001,9 +2071,7 @@@ static struct cached_object 
  static int cached_object_nr, cached_object_alloc;
  
  static struct cached_object empty_tree = {
 -      /* empty tree sha1: 4b825dc642cb6eb9a060e54bf8d69288fbee4904 */
 -      "\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60"
 -      "\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04",
 +      EMPTY_TREE_SHA1_BIN,
        OBJ_TREE,
        "",
        0
@@@ -2127,8 -2046,8 +2114,8 @@@ int pretend_sha1_file(void *buf, unsign
        return 0;
  }
  
 -void *read_object(const unsigned char *sha1, enum object_type *type,
 -                unsigned long *size)
 +static void *read_object(const unsigned char *sha1, enum object_type *type,
 +                       unsigned long *size)
  {
        unsigned long mapsize;
        void *map, *buf;
@@@ -2216,16 -2135,16 +2203,16 @@@ static void write_sha1_file_prepare(con
                                      const char *type, unsigned char *sha1,
                                      char *hdr, int *hdrlen)
  {
 -      SHA_CTX c;
 +      git_SHA_CTX c;
  
        /* Generate the header */
        *hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
  
        /* Sha1.. */
 -      SHA1_Init(&c);
 -      SHA1_Update(&c, hdr, *hdrlen);
 -      SHA1_Update(&c, buf, len);
 -      SHA1_Final(sha1, &c);
 +      git_SHA1_Init(&c);
 +      git_SHA1_Update(&c, hdr, *hdrlen);
 +      git_SHA1_Update(&c, buf, len);
 +      git_SHA1_Final(sha1, &c);
  }
  
  /*
@@@ -2287,7 -2206,7 +2274,7 @@@ static void close_sha1_file(int fd
                fsync_or_die(fd, "sha1 file");
        fchmod(fd, 0444);
        if (close(fd) != 0)
 -              die("unable to write sha1 file");
 +              die("error when closing sha1 file (%s)", strerror(errno));
  }
  
  /* Size of directory component, including the ending '/' */
@@@ -2334,8 -2253,7 +2321,8 @@@ static int create_tmpfile(char *buffer
  static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
                              void *buf, unsigned long len, time_t mtime)
  {
 -      int fd, size, ret;
 +      int fd, ret;
 +      size_t size;
        unsigned char *compressed;
        z_stream stream;
        char *filename;
@@@ -2456,12 -2374,6 +2443,6 @@@ int has_sha1_pack(const unsigned char *
        return find_pack_entry(sha1, &e);
  }
  
- int has_sha1_kept_pack(const unsigned char *sha1)
- {
-       struct pack_entry e;
-       return find_kept_pack_entry(sha1, &e);
- }
  int has_sha1_file(const unsigned char *sha1)
  {
        struct pack_entry e;
        return has_loose_object(sha1);
  }
  
 -int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
 -{
 -      struct strbuf buf;
 -      int ret;
 -
 -      strbuf_init(&buf, 0);
 -      if (strbuf_read(&buf, fd, 4096) < 0) {
 -              strbuf_release(&buf);
 -              return -1;
 -      }
 -
 -      if (!type)
 -              type = blob_type;
 -      if (write_object)
 -              ret = write_sha1_file(buf.buf, buf.len, type, sha1);
 -      else
 -              ret = hash_sha1_file(buf.buf, buf.len, type, sha1);
 -      strbuf_release(&buf);
 -
 -      return ret;
 -}
 -
 -int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
 -           enum object_type type, const char *path)
 +static int index_mem(unsigned char *sha1, void *buf, size_t size,
 +                   int write_object, enum object_type type, const char *path)
  {
 -      size_t size = xsize_t(st->st_size);
 -      void *buf = NULL;
        int ret, re_allocated = 0;
  
 -      if (size)
 -              buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
 -      close(fd);
 -
        if (!type)
                type = OBJ_BLOB;
  
        /*
         * Convert blobs to git internal format
         */
 -      if ((type == OBJ_BLOB) && S_ISREG(st->st_mode)) {
 -              struct strbuf nbuf;
 -              strbuf_init(&nbuf, 0);
 +      if ((type == OBJ_BLOB) && path) {
 +              struct strbuf nbuf = STRBUF_INIT;
                if (convert_to_git(path, buf, size, &nbuf,
                                   write_object ? safe_crlf : 0)) {
 -                      munmap(buf, size);
                        buf = strbuf_detach(&nbuf, &size);
                        re_allocated = 1;
                }
                ret = write_sha1_file(buf, size, typename(type), sha1);
        else
                ret = hash_sha1_file(buf, size, typename(type), sha1);
 -      if (re_allocated) {
 +      if (re_allocated)
                free(buf);
 -              return ret;
 -      }
 -      if (size)
 +      return ret;
 +}
 +
 +int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
 +           enum object_type type, const char *path)
 +{
 +      int ret;
 +      size_t size = xsize_t(st->st_size);
 +
 +      if (!S_ISREG(st->st_mode)) {
 +              struct strbuf sbuf = STRBUF_INIT;
 +              if (strbuf_read(&sbuf, fd, 4096) >= 0)
 +                      ret = index_mem(sha1, sbuf.buf, sbuf.len, write_object,
 +                                      type, path);
 +              else
 +                      ret = -1;
 +              strbuf_release(&sbuf);
 +      } else if (size) {
 +              void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
 +              ret = index_mem(sha1, buf, size, write_object, type, path);
                munmap(buf, size);
 +      } else
 +              ret = index_mem(sha1, NULL, size, write_object, type, path);
 +      close(fd);
        return ret;
  }
  
  int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object)
  {
        int fd;
 -      char *target;
 -      size_t len;
 +      struct strbuf sb = STRBUF_INIT;
  
        switch (st->st_mode & S_IFMT) {
        case S_IFREG:
                                     path);
                break;
        case S_IFLNK:
 -              len = xsize_t(st->st_size);
 -              target = xmalloc(len + 1);
 -              if (readlink(path, target, len + 1) != st->st_size) {
 +              if (strbuf_readlink(&sb, path, st->st_size)) {
                        char *errstr = strerror(errno);
 -                      free(target);
                        return error("readlink(\"%s\"): %s", path,
                                     errstr);
                }
                if (!write_object)
 -                      hash_sha1_file(target, len, blob_type, sha1);
 -              else if (write_sha1_file(target, len, blob_type, sha1))
 +                      hash_sha1_file(sb.buf, sb.len, blob_type, sha1);
 +              else if (write_sha1_file(sb.buf, sb.len, blob_type, sha1))
                        return error("%s: failed to insert into database",
                                     path);
 -              free(target);
 +              strbuf_release(&sb);
                break;
        case S_IFDIR:
                return resolve_gitlink_ref(path, "HEAD", sha1);
diff --combined t/t7700-repack.sh
index f5682d66db2832311774fb68b7264002dfeb091f,9ce546e3b225563279c54eb6ceafd87398a3e5cc..6b29bff782f5a46bb6970d70598fd3be82c679fa
@@@ -69,24 -69,66 +69,85 @@@ test_expect_success 'packed obs in alt 
        done
  '
  
 +test_expect_failure 'packed obs in alt ODB are repacked when local repo has packs' '
 +      rm -f .git/objects/pack/* &&
 +      echo new_content >> file1 &&
 +      git add file1 &&
 +      git commit -m more_content &&
 +      git repack &&
 +      git repack -a -d &&
 +      myidx=$(ls -1 .git/objects/pack/*.idx) &&
 +      test -f "$myidx" &&
 +      for p in alt_objects/pack/*.idx; do
 +              git verify-pack -v $p | sed -n -e "/^[0-9a-f]\{40\}/p"
 +      done | while read sha1 rest; do
 +              if ! ( git verify-pack -v $myidx | grep "^$sha1" ); then
 +                      echo "Missing object in local pack: $sha1"
 +                      return 1
 +              fi
 +      done
 +'
 +
+ test_expect_success 'packed obs in alternate ODB kept pack are repacked' '
+       # swap the .keep so the commit object is in the pack with .keep
+       for p in alt_objects/pack/*.pack
+       do
+               base_name=$(basename $p .pack)
+               if test -f alt_objects/pack/$base_name.keep
+               then
+                       rm alt_objects/pack/$base_name.keep
+               else
+                       touch alt_objects/pack/$base_name.keep
+               fi
+       done
+       git repack -a -d &&
+       myidx=$(ls -1 .git/objects/pack/*.idx) &&
+       test -f "$myidx" &&
+       for p in alt_objects/pack/*.idx; do
+               git verify-pack -v $p | sed -n -e "/^[0-9a-f]\{40\}/p"
+       done | while read sha1 rest; do
+               if ! ( git verify-pack -v $myidx | grep "^$sha1" ); then
+                       echo "Missing object in local pack: $sha1"
+                       return 1
+               fi
+       done
+ '
+ test_expect_success 'packed unreachable obs in alternate ODB are not loosened' '
+       rm -f alt_objects/pack/*.keep &&
+       mv .git/objects/pack/* alt_objects/pack/ &&
+       csha1=$(git rev-parse HEAD^{commit}) &&
+       git reset --hard HEAD^ &&
+       sleep 1 &&
+       git reflog expire --expire=now --expire-unreachable=now --all &&
+       # The pack-objects call on the next line is equivalent to
+       # git repack -A -d without the call to prune-packed
+       git pack-objects --honor-pack-keep --non-empty --all --reflog \
+           --unpack-unreachable </dev/null pack &&
+       rm -f .git/objects/pack/* &&
+       mv pack-* .git/objects/pack/ &&
+       test 0 = $(git verify-pack -v -- .git/objects/pack/*.idx |
+               egrep "^$csha1 " | sort | uniq | wc -l) &&
+       echo > .git/objects/info/alternates &&
+       test_must_fail git show $csha1
+ '
+ test_expect_success 'local packed unreachable obs that exist in alternate ODB are not loosened' '
+       echo `pwd`/alt_objects > .git/objects/info/alternates &&
+       echo "$csha1" | git pack-objects --non-empty --all --reflog pack &&
+       rm -f .git/objects/pack/* &&
+       mv pack-* .git/objects/pack/ &&
+       # The pack-objects call on the next line is equivalent to
+       # git repack -A -d without the call to prune-packed
+       git pack-objects --honor-pack-keep --non-empty --all --reflog \
+           --unpack-unreachable </dev/null pack &&
+       rm -f .git/objects/pack/* &&
+       mv pack-* .git/objects/pack/ &&
+       test 0 = $(git verify-pack -v -- .git/objects/pack/*.idx |
+               egrep "^$csha1 " | sort | uniq | wc -l) &&
+       echo > .git/objects/info/alternates &&
+       test_must_fail git show $csha1
+ '
  test_done