Merge branch 'rs/copy-array'
authorJunio C Hamano <gitster@pobox.com>
Mon, 3 Oct 2016 20:30:33 +0000 (13:30 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 3 Oct 2016 20:30:33 +0000 (13:30 -0700)
Code cleanup.

* rs/copy-array:
use COPY_ARRAY
add COPY_ARRAY

1  2 
builtin/mv.c
commit.c
git-compat-util.h
diff --combined builtin/mv.c
index 446a316738e0e739b6a89992879c6e4838f23c8f,c13e49bf47ecfbb89af7fd1b9c2e9d090334e797..2f43877bc9a17c5bef2906a383cc8cdd6f4f5b82
@@@ -26,7 -26,7 +26,7 @@@ static const char **internal_copy_paths
        int i;
        const char **result;
        ALLOC_ARRAY(result, count + 1);
-       memcpy(result, pathspec, count * sizeof(const char *));
+       COPY_ARRAY(result, pathspec, count);
        result[count] = NULL;
        for (i = 0; i < count; i++) {
                int length = strlen(result[i]);
@@@ -104,7 -104,7 +104,7 @@@ static int index_range_of_same_dir(cons
  
  int cmd_mv(int argc, const char **argv, const char *prefix)
  {
 -      int i, gitmodules_modified = 0;
 +      int i, flags, gitmodules_modified = 0;
        int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
        struct option builtin_mv_options[] = {
                OPT__VERBOSE(&verbose, N_("be verbose")),
        modes = xcalloc(argc, sizeof(enum update_mode));
        /*
         * Keep trailing slash, needed to let
 -       * "git mv file no-such-dir/" error out.
 +       * "git mv file no-such-dir/" error out, except in the case
 +       * "git mv directory no-such-dir/".
         */
 -      dest_path = internal_copy_pathspec(prefix, argv + argc, 1,
 -                                         KEEP_TRAILING_SLASH);
 +      flags = KEEP_TRAILING_SLASH;
 +      if (argc == 1 && is_directory(argv[0]) && !is_directory(argv[1]))
 +              flags = 0;
 +      dest_path = internal_copy_pathspec(prefix, argv + argc, 1, flags);
        submodule_gitfile = xcalloc(argc, sizeof(char *));
  
        if (dest_path[0][0] == '\0')
diff --combined commit.c
index 8eb17075f7866db21b4a2e047bed80565a37be72,9d3d7119e065af27aac5516251d05018a8393f67..856fd4aeeff654c45ba0a6149f9db2c22806e0cb
+++ b/commit.c
@@@ -931,7 -931,7 +931,7 @@@ static int remove_redundant(struct comm
        }
  
        /* Now collect the result */
-       memcpy(work, array, sizeof(*array) * cnt);
+       COPY_ARRAY(work, array, cnt);
        for (i = filled = 0; i < cnt; i++)
                if (!redundant[i])
                        array[filled++] = work[i];
@@@ -1511,9 -1511,9 +1511,9 @@@ static int verify_utf8(struct strbuf *b
  }
  
  static const char commit_utf8_warn[] =
 -"Warning: commit message did not conform to UTF-8.\n"
 -"You may want to amend it after fixing the message, or set the config\n"
 -"variable i18n.commitencoding to the encoding your project uses.\n";
 +N_("Warning: commit message did not conform to UTF-8.\n"
 +   "You may want to amend it after fixing the message, or set the config\n"
 +   "variable i18n.commitencoding to the encoding your project uses.\n");
  
  int commit_tree_extended(const char *msg, size_t msg_len,
                         const unsigned char *tree,
  
        /* And check the encoding */
        if (encoding_is_utf8 && !verify_utf8(&buffer))
 -              fprintf(stderr, commit_utf8_warn);
 +              fprintf(stderr, _(commit_utf8_warn));
  
        if (sign_commit && do_sign_commit(&buffer, sign_commit))
                return -1;
        return result;
  }
  
 +void set_merge_remote_desc(struct commit *commit,
 +                         const char *name, struct object *obj)
 +{
 +      struct merge_remote_desc *desc;
 +      FLEX_ALLOC_STR(desc, name, name);
 +      desc->obj = obj;
 +      commit->util = desc;
 +}
 +
  struct commit *get_merge_parent(const char *name)
  {
        struct object *obj;
                return NULL;
        obj = parse_object(oid.hash);
        commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
 -      if (commit && !commit->util) {
 -              struct merge_remote_desc *desc;
 -              desc = xmalloc(sizeof(*desc));
 -              desc->obj = obj;
 -              desc->name = strdup(name);
 -              commit->util = desc;
 -      }
 +      if (commit && !commit->util)
 +              set_merge_remote_desc(commit, name, obj);
        return commit;
  }
  
@@@ -1626,6 -1622,16 +1626,6 @@@ struct commit_list **commit_list_append
        return &new->next;
  }
  
 -void print_commit_list(struct commit_list *list,
 -                     const char *format_cur,
 -                     const char *format_last)
 -{
 -      for ( ; list; list = list->next) {
 -              const char *format = list->next ? format_cur : format_last;
 -              printf(format, oid_to_hex(&list->item->object.oid));
 -      }
 -}
 -
  const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
  {
        int key_len = strlen(key);
diff --combined git-compat-util.h
index 8aab0c30474764b3e71d4e6c0bc66c52218b3d5e,4662d0d3c460826d505cfbfaa7242df02eeaf601..0ce2cdfb98d54e35881a94e9037e7380ec5b821c
@@@ -436,14 -436,10 +436,14 @@@ static inline int const_error(void
        return -1;
  }
  #define error(...) (error(__VA_ARGS__), const_error())
 +#define error_errno(...) (error_errno(__VA_ARGS__), const_error())
  #endif
  
  extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
  extern void set_error_routine(void (*routine)(const char *err, va_list params));
 +extern void (*get_error_routine(void))(const char *err, va_list params);
 +extern void set_warn_routine(void (*routine)(const char *warn, va_list params));
 +extern void (*get_warn_routine(void))(const char *warn, va_list params);
  extern void set_die_is_recursing_routine(int (*routine)(void));
  extern void set_error_handle(FILE *);
  
@@@ -667,22 -663,10 +667,22 @@@ void *gitmemmem(const void *haystack, s
                  const void *needle, size_t needlelen);
  #endif
  
 +#ifdef OVERRIDE_STRDUP
 +#ifdef strdup
 +#undef strdup
 +#endif
 +#define strdup gitstrdup
 +char *gitstrdup(const char *s);
 +#endif
 +
  #ifdef NO_GETPAGESIZE
  #define getpagesize() sysconf(_SC_PAGESIZE)
  #endif
  
 +#ifndef O_CLOEXEC
 +#define O_CLOEXEC 0
 +#endif
 +
  #ifdef FREAD_READS_DIRECTORIES
  #ifdef fopen
  #undef fopen
@@@ -801,6 -785,14 +801,14 @@@ extern FILE *fopen_for_writing(const ch
  #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
  #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
  
+ #define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
+       BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src))))
+ static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
+ {
+       if (n)
+               memcpy(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:
   * you can do:
   *
   *   struct foo *f;
 - *   FLEX_ALLOC_STR(f, name, src);
 + *   FLEXPTR_ALLOC_STR(f, name, src);
   *
   * and "name" will point to a block of memory after the struct, which will be
   * freed along with the struct (but the pointer can be repointed anywhere).
@@@ -977,19 -969,6 +985,19 @@@ void git_qsort(void *base, size_t nmemb
  #define qsort git_qsort
  #endif
  
 +#ifndef REG_STARTEND
 +#error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd"
 +#endif
 +
 +static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size,
 +                            size_t nmatch, regmatch_t pmatch[], int eflags)
 +{
 +      assert(nmatch > 0 && pmatch);
 +      pmatch[0].rm_so = 0;
 +      pmatch[0].rm_eo = size;
 +      return regexec(preg, buf, nmatch, pmatch, eflags | REG_STARTEND);
 +}
 +
  #ifndef DIR_HAS_BSD_GROUP_SEMANTICS
  # define FORCE_DIR_SET_GID S_ISGID
  #else
@@@ -1091,5 -1070,3 +1099,5 @@@ struct tm *git_gmtime_r(const time_t *
  #endif
  
  #endif
 +
 +extern int cmd_main(int, const char **);