Merge branch 'rs/move-array'
authorJunio C Hamano <gitster@pobox.com>
Fri, 11 Aug 2017 20:26:57 +0000 (13:26 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 11 Aug 2017 20:26:57 +0000 (13:26 -0700)
Code clean-up.

* rs/move-array:
ls-files: don't try to prune an empty index
apply: use COPY_ARRAY and MOVE_ARRAY in update_image()
use MOVE_ARRAY
add MOVE_ARRAY

1  2 
apply.c
commit.c
git-compat-util.h
diff --combined apply.c
index 9889b47d6a51c42f00a4da712d99683d904405e6,40707ca50c50331c392ab51375a83f4fb97edee1..41ee63e1db5f702509e51b2e6dd37b8617a40559
+++ b/apply.c
@@@ -2809,13 -2809,10 +2809,10 @@@ static void update_image(struct apply_s
                img->line_allocated = img->line;
        }
        if (preimage_limit != postimage->nr)
-               memmove(img->line + applied_pos + postimage->nr,
-                       img->line + applied_pos + preimage_limit,
-                       (img->nr - (applied_pos + preimage_limit)) *
-                       sizeof(*img->line));
-       memcpy(img->line + applied_pos,
-              postimage->line,
-              postimage->nr * sizeof(*img->line));
+               MOVE_ARRAY(img->line + applied_pos + postimage->nr,
+                          img->line + applied_pos + preimage_limit,
+                          img->nr - (applied_pos + preimage_limit));
+       COPY_ARRAY(img->line + applied_pos, postimage->line, postimage->nr);
        if (!state->allow_overlap)
                for (i = 0; i < postimage->nr; i++)
                        img->line[applied_pos + i].flag |= LINE_PATCHED;
@@@ -3551,7 -3548,7 +3548,7 @@@ static int try_threeway(struct apply_st
        /* Preimage the patch was prepared for */
        if (patch->is_new)
                write_sha1_file("", 0, blob_type, pre_oid.hash);
 -      else if (get_sha1(patch->old_sha1_prefix, pre_oid.hash) ||
 +      else if (get_oid(patch->old_sha1_prefix, &pre_oid) ||
                 read_blob_object(&buf, &pre_oid, patch->old_mode))
                return error(_("repository lacks the necessary blob to fall back on 3-way merge."));
  
@@@ -4075,7 -4072,7 +4072,7 @@@ static int build_fake_ancestor(struct a
                        else
                                return error(_("sha1 information is lacking or "
                                               "useless for submodule %s"), name);
 -              } else if (!get_sha1_blob(patch->old_sha1_prefix, oid.hash)) {
 +              } else if (!get_oid_blob(patch->old_sha1_prefix, &oid)) {
                        ; /* ok */
                } else if (!patch->lines_added && !patch->lines_deleted) {
                        /* mode-only change: update the current */
diff --combined commit.c
index b0bb356731d71f494a2ea65c742097965fb088fd,d3150d627071316accafb7d9c69b5988050f4a8e..8b28415939035e2b40153013e6f8805422094d84
+++ b/commit.c
@@@ -59,7 -59,7 +59,7 @@@ struct commit *lookup_commit_reference_
        struct object_id oid;
        struct commit *commit;
  
 -      if (get_sha1_committish(name, oid.hash))
 +      if (get_oid_committish(name, &oid))
                return NULL;
        commit = lookup_commit_reference(&oid);
        if (parse_commit(commit))
@@@ -199,11 -199,11 +199,11 @@@ static void prepare_commit_graft(void
        commit_graft_prepared = 1;
  }
  
 -struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
 +struct commit_graft *lookup_commit_graft(const struct object_id *oid)
  {
        int pos;
        prepare_commit_graft();
 -      pos = commit_graft_pos(sha1);
 +      pos = commit_graft_pos(oid->hash);
        if (pos < 0)
                return NULL;
        return commit_graft[pos];
@@@ -223,9 -223,8 +223,8 @@@ int unregister_shallow(const struct obj
        if (pos < 0)
                return -1;
        if (pos + 1 < commit_graft_nr)
-               memmove(commit_graft + pos, commit_graft + pos + 1,
-                               sizeof(struct commit_graft *)
-                               * (commit_graft_nr - pos - 1));
+               MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1,
+                          commit_graft_nr - pos - 1);
        commit_graft_nr--;
        return 0;
  }
@@@ -335,7 -334,7 +334,7 @@@ int parse_commit_buffer(struct commit *
        bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
        pptr = &item->parents;
  
 -      graft = lookup_commit_graft(item->object.oid.hash);
 +      graft = lookup_commit_graft(&item->object.oid);
        while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
                struct commit *new_parent;
  
@@@ -1587,7 -1586,7 +1586,7 @@@ struct commit *get_merge_parent(const c
        struct object *obj;
        struct commit *commit;
        struct object_id oid;
 -      if (get_sha1(name, oid.hash))
 +      if (get_oid(name, &oid))
                return NULL;
        obj = parse_object(&oid);
        commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
diff --combined git-compat-util.h
index db9c22de7693af4e905fdeb0a1766ba4469eda94,159f82154a8a4d24433d0767e83413d8e1dd105b..7d2c0ca75922ea1ca8c67ec798ff8ce1249c925f
  #include <sys/sysctl.h>
  #endif
  
 +#if defined(__CYGWIN__)
 +#include "compat/cygwin.h"
 +#endif
  #if defined(__MINGW32__)
  /* pull in Windows compatibility stuff */
  #include "compat/mingw.h"
@@@ -828,6 -825,14 +828,14 @@@ static inline void copy_array(void *dst
                memcpy(dst, src, st_mult(size, n));
  }
  
+ #define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \
+       BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src))))
+ static inline void move_array(void *dst, const void *src, size_t n, size_t size)
+ {
+       if (n)
+               memmove(dst, src, st_mult(size, n));
+ }
  /*
   * These functions help you allocate structs with flex arrays, and copy
   * the data directly into the array. For example, if you had: