Merge branch 'nd/misc-cleanups'
authorJunio C Hamano <gitster@pobox.com>
Mon, 5 Dec 2011 23:10:20 +0000 (15:10 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 5 Dec 2011 23:10:20 +0000 (15:10 -0800)
* nd/misc-cleanups:
unpack_object_header_buffer(): clear the size field upon error
tree_entry_interesting: make use of local pointer "item"
tree_entry_interesting(): give meaningful names to return values
read_directory_recursive: reduce one indentation level
get_tree_entry(): do not call find_tree_entry() on an empty tree
tree-walk.c: do not leak internal structure in tree_entry_len()

1  2 
builtin/grep.c
builtin/pack-objects.c
sha1_file.c
diff --combined builtin/grep.c
index 3d7329d78c6e3ec31ed8ce03b8928c9ed24afb9e,2fc51fa2dc31a6fac8ba79d394205b8ed4e80646..988ea1d3324d6e32fcfd3b97da0fad5ae41b592c
@@@ -74,32 -74,13 +74,32 @@@ static int all_work_added
  /* This lock protects all the variables above. */
  static pthread_mutex_t grep_mutex;
  
 +static inline void grep_lock(void)
 +{
 +      if (use_threads)
 +              pthread_mutex_lock(&grep_mutex);
 +}
 +
 +static inline void grep_unlock(void)
 +{
 +      if (use_threads)
 +              pthread_mutex_unlock(&grep_mutex);
 +}
 +
  /* Used to serialize calls to read_sha1_file. */
  static pthread_mutex_t read_sha1_mutex;
  
 -#define grep_lock() pthread_mutex_lock(&grep_mutex)
 -#define grep_unlock() pthread_mutex_unlock(&grep_mutex)
 -#define read_sha1_lock() pthread_mutex_lock(&read_sha1_mutex)
 -#define read_sha1_unlock() pthread_mutex_unlock(&read_sha1_mutex)
 +static inline void read_sha1_lock(void)
 +{
 +      if (use_threads)
 +              pthread_mutex_lock(&read_sha1_mutex);
 +}
 +
 +static inline void read_sha1_unlock(void)
 +{
 +      if (use_threads)
 +              pthread_mutex_unlock(&read_sha1_mutex);
 +}
  
  /* Signalled when a new work_item is added to todo. */
  static pthread_cond_t cond_add;
@@@ -373,9 -354,13 +373,9 @@@ static void *lock_and_read_sha1_file(co
  {
        void *data;
  
 -      if (use_threads) {
 -              read_sha1_lock();
 -              data = read_sha1_file(sha1, type, size);
 -              read_sha1_unlock();
 -      } else {
 -              data = read_sha1_file(sha1, type, size);
 -      }
 +      read_sha1_lock();
 +      data = read_sha1_file(sha1, type, size);
 +      read_sha1_unlock();
        return data;
  }
  
@@@ -557,18 -542,19 +557,19 @@@ static int grep_cache(struct grep_opt *
  static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
                     struct tree_desc *tree, struct strbuf *base, int tn_len)
  {
-       int hit = 0, match = 0;
+       int hit = 0;
+       enum interesting match = entry_not_interesting;
        struct name_entry entry;
        int old_baselen = base->len;
  
        while (tree_entry(tree, &entry)) {
-               int te_len = tree_entry_len(entry.path, entry.sha1);
+               int te_len = tree_entry_len(&entry);
  
-               if (match != 2) {
+               if (match != all_entries_interesting) {
                        match = tree_entry_interesting(&entry, base, tn_len, pathspec);
-                       if (match < 0)
+                       if (match == all_entries_not_interesting)
                                break;
-                       if (match == 0)
+                       if (match == entry_not_interesting)
                                continue;
                }
  
diff --combined builtin/pack-objects.c
index 824ecee20b94c471083ad8c7f697b39fbb7cb2b8,b4f78555507b984609ce5ef71c4a3b63dd73e4e5..558cd34bccf66b1ef4cf9d66bfefe43fae76ceb3
@@@ -454,8 -454,8 +454,8 @@@ static int mark_tagged(const char *path
        return 0;
  }
  
 -static void add_to_write_order(struct object_entry **wo,
 -                             int *endp,
 +static inline void add_to_write_order(struct object_entry **wo,
 +                             unsigned int *endp,
                               struct object_entry *e)
  {
        if (e->filled)
  }
  
  static void add_descendants_to_write_order(struct object_entry **wo,
 -                                         int *endp,
 +                                         unsigned int *endp,
                                           struct object_entry *e)
  {
 -      struct object_entry *child;
 -
 -      for (child = e->delta_child; child; child = child->delta_sibling)
 -              add_to_write_order(wo, endp, child);
 -      for (child = e->delta_child; child; child = child->delta_sibling)
 -              add_descendants_to_write_order(wo, endp, child);
 +      int add_to_order = 1;
 +      while (e) {
 +              if (add_to_order) {
 +                      struct object_entry *s;
 +                      /* add this node... */
 +                      add_to_write_order(wo, endp, e);
 +                      /* all its siblings... */
 +                      for (s = e->delta_sibling; s; s = s->delta_sibling) {
 +                              add_to_write_order(wo, endp, s);
 +                      }
 +              }
 +              /* drop down a level to add left subtree nodes if possible */
 +              if (e->delta_child) {
 +                      add_to_order = 1;
 +                      e = e->delta_child;
 +              } else {
 +                      add_to_order = 0;
 +                      /* our sibling might have some children, it is next */
 +                      if (e->delta_sibling) {
 +                              e = e->delta_sibling;
 +                              continue;
 +                      }
 +                      /* go back to our parent node */
 +                      e = e->delta;
 +                      while (e && !e->delta_sibling) {
 +                              /* we're on the right side of a subtree, keep
 +                               * going up until we can go right again */
 +                              e = e->delta;
 +                      }
 +                      if (!e) {
 +                              /* done- we hit our original root node */
 +                              return;
 +                      }
 +                      /* pass it off to sibling at this level */
 +                      e = e->delta_sibling;
 +              }
 +      };
  }
  
  static void add_family_to_write_order(struct object_entry **wo,
 -                                    int *endp,
 +                                    unsigned int *endp,
                                      struct object_entry *e)
  {
        struct object_entry *root;
  
        for (root = e; root->delta; root = root->delta)
                ; /* nothing */
 -      add_to_write_order(wo, endp, root);
        add_descendants_to_write_order(wo, endp, root);
  }
  
  static struct object_entry **compute_write_order(void)
  {
 -      int i, wo_end;
 +      unsigned int i, wo_end, last_untagged;
  
        struct object_entry **wo = xmalloc(nr_objects * sizeof(*wo));
  
         * Make sure delta_sibling is sorted in the original
         * recency order.
         */
 -      for (i = nr_objects - 1; 0 <= i; i--) {
 -              struct object_entry *e = &objects[i];
 +      for (i = nr_objects; i > 0;) {
 +              struct object_entry *e = &objects[--i];
                if (!e->delta)
                        continue;
                /* Mark me as the first child */
        for_each_tag_ref(mark_tagged, NULL);
  
        /*
 -       * Give the commits in the original recency order until
 +       * Give the objects in the original recency order until
         * we see a tagged tip.
         */
        for (i = wo_end = 0; i < nr_objects; i++) {
                        break;
                add_to_write_order(wo, &wo_end, &objects[i]);
        }
 +      last_untagged = i;
  
        /*
         * Then fill all the tagged tips.
        /*
         * And then all remaining commits and tags.
         */
 -      for (i = 0; i < nr_objects; i++) {
 +      for (i = last_untagged; i < nr_objects; i++) {
                if (objects[i].type != OBJ_COMMIT &&
                    objects[i].type != OBJ_TAG)
                        continue;
        /*
         * And then all the trees.
         */
 -      for (i = 0; i < nr_objects; i++) {
 +      for (i = last_untagged; i < nr_objects; i++) {
                if (objects[i].type != OBJ_TREE)
                        continue;
                add_to_write_order(wo, &wo_end, &objects[i]);
        /*
         * Finally all the rest in really tight order
         */
 -      for (i = 0; i < nr_objects; i++)
 -              add_family_to_write_order(wo, &wo_end, &objects[i]);
 +      for (i = last_untagged; i < nr_objects; i++) {
 +              if (!objects[i].filled)
 +                      add_family_to_write_order(wo, &wo_end, &objects[i]);
 +      }
 +
 +      if (wo_end != nr_objects)
 +              die("ordered %u objects, expected %"PRIu32, wo_end, nr_objects);
  
        return wo;
  }
@@@ -1015,7 -979,7 +1015,7 @@@ static void add_pbase_object(struct tre
        while (tree_entry(tree,&entry)) {
                if (S_ISGITLINK(entry.mode))
                        continue;
-               cmp = tree_entry_len(entry.path, entry.sha1) != cmplen ? 1 :
+               cmp = tree_entry_len(&entry) != cmplen ? 1 :
                      memcmp(name, entry.path, cmplen);
                if (cmp > 0)
                        continue;
diff --combined sha1_file.c
index 6dcae3882bf969cce9ae0a973a80abf76e3bb814,833f5b9b5820ce60f9fb99f6c9bcdb313ae8fca9..956422ba4a5df5f46caaf85f10e4c85321439272
@@@ -1267,7 -1267,8 +1267,8 @@@ unsigned long unpack_object_header_buff
        while (c & 0x80) {
                if (len <= used || bitsizeof(long) <= shift) {
                        error("bad object header");
-                       return 0;
+                       size = used = 0;
+                       break;
                }
                c = buf[used++];
                size += (c & 0x7f) << shift;
@@@ -2616,7 -2617,7 +2617,7 @@@ static int index_mem(unsigned char *sha
        if ((type == OBJ_BLOB) && path) {
                struct strbuf nbuf = STRBUF_INIT;
                if (convert_to_git(path, buf, size, &nbuf,
 -                                 write_object ? safe_crlf : 0)) {
 +                                 write_object ? safe_crlf : SAFE_CRLF_FALSE)) {
                        buf = strbuf_detach(&nbuf, &size);
                        re_allocated = 1;
                }