Merge branch 'sb/sha1-loose-object-info-check-existence' into maint
authorJunio C Hamano <gitster@pobox.com>
Tue, 17 Dec 2013 19:31:18 +0000 (11:31 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 17 Dec 2013 19:31:18 +0000 (11:31 -0800)
"git cat-file --batch-check=ok" did not check the existence of the
named object.

* sb/sha1-loose-object-info-check-existence:
sha1_loose_object_info(): do not return success on missing object

1  2 
sha1_file.c
t/t1006-cat-file.sh
diff --combined sha1_file.c
index 7dadd04cb75a9f681cab17f616600cf437fe82ba,d17315d72be7b6a7b230949ab152c6ce2fd832e6..9ae98c852c862679a064af34aa4d43141db7cc0c
@@@ -614,7 -614,7 +614,7 @@@ static void scan_windows(struct packed_
        }
  }
  
 -static int unuse_one_window(struct packed_git *current, int keep_fd)
 +static int unuse_one_window(struct packed_git *current)
  {
        struct packed_git *p, *lru_p = NULL;
        struct pack_window *lru_w = NULL, *lru_l = NULL;
                pack_mapped -= lru_w->len;
                if (lru_l)
                        lru_l->next = lru_w->next;
 -              else {
 +              else
                        lru_p->windows = lru_w->next;
 -                      if (!lru_p->windows && lru_p->pack_fd != -1
 -                              && lru_p->pack_fd != keep_fd) {
 -                              close(lru_p->pack_fd);
 -                              pack_open_fds--;
 -                              lru_p->pack_fd = -1;
 -                      }
 -              }
                free(lru_w);
                pack_open_windows--;
                return 1;
        return 0;
  }
  
 -void release_pack_memory(size_t need, int fd)
 +void release_pack_memory(size_t need)
  {
        size_t cur = pack_mapped;
 -      while (need >= (cur - pack_mapped) && unuse_one_window(NULL, fd))
 +      while (need >= (cur - pack_mapped) && unuse_one_window(NULL))
                ; /* nothing */
  }
  
@@@ -651,7 -658,7 +651,7 @@@ void *xmmap(void *start, size_t length
        if (ret == MAP_FAILED) {
                if (!length)
                        return NULL;
 -              release_pack_memory(length, fd);
 +              release_pack_memory(length);
                ret = mmap(start, length, prot, flags, fd, offset);
                if (ret == MAP_FAILED)
                        die_errno("Out of memory? mmap failed");
@@@ -675,83 -682,6 +675,83 @@@ void close_pack_windows(struct packed_g
        }
  }
  
 +/*
 + * The LRU pack is the one with the oldest MRU window, preferring packs
 + * with no used windows, or the oldest mtime if it has no windows allocated.
 + */
 +static void find_lru_pack(struct packed_git *p, struct packed_git **lru_p, struct pack_window **mru_w, int *accept_windows_inuse)
 +{
 +      struct pack_window *w, *this_mru_w;
 +      int has_windows_inuse = 0;
 +
 +      /*
 +       * Reject this pack if it has windows and the previously selected
 +       * one does not.  If this pack does not have windows, reject
 +       * it if the pack file is newer than the previously selected one.
 +       */
 +      if (*lru_p && !*mru_w && (p->windows || p->mtime > (*lru_p)->mtime))
 +              return;
 +
 +      for (w = this_mru_w = p->windows; w; w = w->next) {
 +              /*
 +               * Reject this pack if any of its windows are in use,
 +               * but the previously selected pack did not have any
 +               * inuse windows.  Otherwise, record that this pack
 +               * has windows in use.
 +               */
 +              if (w->inuse_cnt) {
 +                      if (*accept_windows_inuse)
 +                              has_windows_inuse = 1;
 +                      else
 +                              return;
 +              }
 +
 +              if (w->last_used > this_mru_w->last_used)
 +                      this_mru_w = w;
 +
 +              /*
 +               * Reject this pack if it has windows that have been
 +               * used more recently than the previously selected pack.
 +               * If the previously selected pack had windows inuse and
 +               * we have not encountered a window in this pack that is
 +               * inuse, skip this check since we prefer a pack with no
 +               * inuse windows to one that has inuse windows.
 +               */
 +              if (*mru_w && *accept_windows_inuse == has_windows_inuse &&
 +                  this_mru_w->last_used > (*mru_w)->last_used)
 +                      return;
 +      }
 +
 +      /*
 +       * Select this pack.
 +       */
 +      *mru_w = this_mru_w;
 +      *lru_p = p;
 +      *accept_windows_inuse = has_windows_inuse;
 +}
 +
 +static int close_one_pack(void)
 +{
 +      struct packed_git *p, *lru_p = NULL;
 +      struct pack_window *mru_w = NULL;
 +      int accept_windows_inuse = 1;
 +
 +      for (p = packed_git; p; p = p->next) {
 +              if (p->pack_fd == -1)
 +                      continue;
 +              find_lru_pack(p, &lru_p, &mru_w, &accept_windows_inuse);
 +      }
 +
 +      if (lru_p) {
 +              close(lru_p->pack_fd);
 +              pack_open_fds--;
 +              lru_p->pack_fd = -1;
 +              return 1;
 +      }
 +
 +      return 0;
 +}
 +
  void unuse_pack(struct pack_window **w_cursor)
  {
        struct pack_window *w = *w_cursor;
@@@ -847,7 -777,7 +847,7 @@@ static int open_packed_git_1(struct pac
                        pack_max_fds = 1;
        }
  
 -      while (pack_max_fds <= pack_open_fds && unuse_one_window(NULL, -1))
 +      while (pack_max_fds <= pack_open_fds && close_one_pack())
                ; /* nothing */
  
        p->pack_fd = git_open_noatime(p->pack_name);
@@@ -963,7 -893,7 +963,7 @@@ unsigned char *use_pack(struct packed_g
                        win->len = (size_t)len;
                        pack_mapped += win->len;
                        while (packed_git_limit < pack_mapped
 -                              && unuse_one_window(p, p->pack_fd))
 +                              && unuse_one_window(p))
                                ; /* nothing */
                        win->base = xmmap(NULL, win->len,
                                PROT_READ, MAP_PRIVATE,
@@@ -1009,7 -939,7 +1009,7 @@@ static struct packed_git *alloc_packed_
  
  static void try_to_free_pack_memory(size_t size)
  {
 -      release_pack_memory(size, -1);
 +      release_pack_memory(size);
  }
  
  struct packed_git *add_packed_git(const char *path, int path_len, int local)
@@@ -2126,16 -2056,6 +2126,16 @@@ void *unpack_entry(struct packed_git *p
                int i;
                struct delta_base_cache_entry *ent;
  
 +              ent = get_delta_base_cache_entry(p, curpos);
 +              if (eq_delta_base_cache_entry(ent, p, curpos)) {
 +                      type = ent->type;
 +                      data = ent->data;
 +                      size = ent->size;
 +                      clear_delta_base_cache_entry(ent);
 +                      base_from_cache = 1;
 +                      break;
 +              }
 +
                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;
                        }
                }
  
 -              ent = get_delta_base_cache_entry(p, curpos);
 -              if (eq_delta_base_cache_entry(ent, p, curpos)) {
 -                      type = ent->type;
 -                      data = ent->data;
 -                      size = ent->size;
 -                      clear_delta_base_cache_entry(ent);
 -                      base_from_cache = 1;
 -                      break;
 -              }
 -
                type = unpack_object_header(p, &w_curs, &curpos, &size);
                if (type != OBJ_OFS_DELTA && type != OBJ_REF_DELTA)
                        break;
@@@ -2483,15 -2413,18 +2483,18 @@@ static int sha1_loose_object_info(cons
  
        /*
         * If we don't care about type or size, then we don't
-        * need to look inside the object at all.
+        * need to look inside the object at all. Note that we
+        * do not optimize out the stat call, even if the
+        * caller doesn't care about the disk-size, since our
+        * return value implicitly indicates whether the
+        * object even exists.
         */
        if (!oi->typep && !oi->sizep) {
-               if (oi->disk_sizep) {
-                       struct stat st;
-                       if (stat_sha1_file(sha1, &st) < 0)
-                               return -1;
+               struct stat st;
+               if (stat_sha1_file(sha1, &st) < 0)
+                       return -1;
+               if (oi->disk_sizep)
                        *oi->disk_sizep = st.st_size;
-               }
                return 0;
        }
  
diff --combined t/t1006-cat-file.sh
index a420742494024e127d4e4233153c7054e077e471,558f0f5292325296c1a2fc2882b8f03b39d9bb3e..8a1bc5c53281eb6f44c15d68dd0ae737bc0125f3
@@@ -78,13 -78,6 +78,13 @@@ $content
        echo $sha1 | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
        test_cmp expect actual
      '
 +
 +    test_expect_success '--batch-check with %(rest)' '
 +      echo "$type this is some extra content" >expect &&
 +      echo "$sha1    this is some extra content" |
 +              git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
 +      test_cmp expect actual
 +    '
  }
  
  hello_content="Hello World"
@@@ -98,14 -91,6 +98,14 @@@ test_expect_success "setup" 
  
  run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
  
 +test_expect_success '--batch-check without %(rest) considers whole line' '
 +      echo "$hello_sha1 blob $hello_size" >expect &&
 +      git update-index --add --cacheinfo 100644 $hello_sha1 "white space" &&
 +      test_when_finished "git update-index --remove \"white space\"" &&
 +      echo ":white space" | git cat-file --batch-check >actual &&
 +      test_cmp expect actual
 +'
 +
  tree_sha1=$(git write-tree)
  tree_size=33
  tree_pretty_content="100644 blob $hello_sha1  hello"
@@@ -194,6 -179,12 +194,12 @@@ test_expect_success "--batch-check for 
      test " missing" = "$(echo | git cat-file --batch-check)"
  '
  
+ test_expect_success 'empty --batch-check notices missing object' '
+       echo "$_z40 missing" >expect &&
+       echo "$_z40" | git cat-file --batch-check="" >actual &&
+       test_cmp expect actual
+ '
  batch_input="$hello_sha1
  $commit_sha1
  $tag_sha1