Merge branch 'rs/qsort'
authorJunio C Hamano <gitster@pobox.com>
Mon, 10 Oct 2016 21:03:46 +0000 (14:03 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 10 Oct 2016 21:03:46 +0000 (14:03 -0700)
We call "qsort(array, nelem, sizeof(array[0]), fn)", and most of
the time third parameter is redundant. A new QSORT() macro lets us
omit it.

* rs/qsort:
show-branch: use QSORT
use QSORT, part 2
coccicheck: use --all-includes by default
remove unnecessary check before QSORT
use QSORT
add QSORT

1  2 
diff.c
fetch-pack.c
git-compat-util.h
pack-check.c
pathspec.c
ref-filter.c
sha1-array.c
diff --combined diff.c
index be11e4ef2b516aa5348f01a64af8a0d9da96027f,c2f09fb8dda42c37a141800c9f9d6127822b2612..1d304e0550766edf4679f089aafac4de14258482
--- 1/diff.c
--- 2/diff.c
+++ b/diff.c
@@@ -2019,7 -2019,7 +2019,7 @@@ found_damage
                return;
  
        /* Show all directories with more than x% of the changes */
-       qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare);
+       QSORT(dir.files, dir.nr, dirstat_compare);
        gather_dirstat(options, &dir, changed, "", 0);
  }
  
@@@ -2063,7 -2063,7 +2063,7 @@@ static void show_dirstat_by_line(struc
                return;
  
        /* Show all directories with more than x% of the changes */
-       qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare);
+       QSORT(dir.files, dir.nr, dirstat_compare);
        gather_dirstat(options, &dir, changed, "", 0);
  }
  
@@@ -3109,7 -3109,7 +3109,7 @@@ static void fill_metainfo(struct strbu
                }
                strbuf_addf(msg, "%s%sindex %s..", line_prefix, set,
                            find_unique_abbrev(one->oid.hash, abbrev));
 -              strbuf_addstr(msg, find_unique_abbrev(two->oid.hash, abbrev));
 +              strbuf_add_unique_abbrev(msg, two->oid.hash, abbrev);
                if (one->mode == two->mode)
                        strbuf_addf(msg, " %06o", one->mode);
                strbuf_addf(msg, "%s\n", reset);
@@@ -4923,7 -4923,7 +4923,7 @@@ static int diffnamecmp(const void *a_, 
  void diffcore_fix_diff_index(struct diff_options *options)
  {
        struct diff_queue_struct *q = &diff_queued_diff;
-       qsort(q->queue, q->nr, sizeof(q->queue[0]), diffnamecmp);
+       QSORT(q->queue, q->nr, diffnamecmp);
  }
  
  void diffcore_std(struct diff_options *options)
diff --combined fetch-pack.c
index 413937e7404d163883d5d0456ebefc4e1504cd87,8a38d30707eb48ee7cb089f3be7fd500f63e6cea..300763fae1e5b602108ad15020d4f50152778b2b
@@@ -428,17 -428,10 +428,17 @@@ static int find_common(struct fetch_pac
                                                const char *hex = sha1_to_hex(result_sha1);
                                                packet_buf_write(&req_buf, "have %s\n", hex);
                                                state_len = req_buf.len;
 -                                      }
 +                                              /*
 +                                               * Reset in_vain because an ack
 +                                               * for this commit has not been
 +                                               * seen.
 +                                               */
 +                                              in_vain = 0;
 +                                      } else if (!args->stateless_rpc
 +                                                 || ack != ACK_common)
 +                                              in_vain = 0;
                                        mark_common(commit, 0, 1);
                                        retval = 0;
 -                                      in_vain = 0;
                                        got_continue = 1;
                                        if (ack == ACK_ready) {
                                                clear_prio_queue(&rev_list);
@@@ -819,7 -812,7 +819,7 @@@ static struct ref *do_fetch_pack(struc
        int agent_len;
  
        sort_ref_list(&ref, ref_compare_name);
-       qsort(sought, nr_sought, sizeof(*sought), cmp_ref_by_name);
+       QSORT(sought, nr_sought, cmp_ref_by_name);
  
        if ((args->depth > 0 || is_repository_shallow()) && !server_supports("shallow"))
                die("Server does not support shallow clients");
diff --combined git-compat-util.h
index 0ce2cdfb98d54e35881a94e9037e7380ec5b821c,544db2ae658b782ca983a57997a1e6f990751fd2..43718dabae88062b39d65f2d6c1f5e35a6eb4800
@@@ -801,14 -801,6 +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:
@@@ -985,6 -977,14 +985,14 @@@ void git_qsort(void *base, size_t nmemb
  #define qsort git_qsort
  #endif
  
+ #define QSORT(base, n, compar) sane_qsort((base), (n), sizeof(*(base)), compar)
+ static inline void sane_qsort(void *base, size_t nmemb, size_t size,
+                             int(*compar)(const void *, const void *))
+ {
+       if (nmemb > 1)
+               qsort(base, nmemb, size, compar);
+ }
  #ifndef REG_STARTEND
  #error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd"
  #endif
diff --combined pack-check.c
index c5c7763323156232748314723e39deb92b93fa4e,72440a8fce14043e73e4fc40e18fee5ddd3dbc54..27f70d345fbf1b339cb4bad1011253cece1bf619
@@@ -57,8 -57,11 +57,8 @@@ static int verify_packfile(struct packe
        int err = 0;
        struct idx_entry *entries;
  
 -      /* Note that the pack header checks are actually performed by
 -       * use_pack when it first opens the pack file.  If anything
 -       * goes wrong during those checks then the call will die out
 -       * immediately.
 -       */
 +      if (!is_pack_valid(p))
 +              return error("packfile %s cannot be accessed", p->pack_name);
  
        git_SHA1_Init(&ctx);
        do {
@@@ -96,7 -99,7 +96,7 @@@
                entries[i].offset = nth_packed_object_offset(p, i);
                entries[i].nr = i;
        }
-       qsort(entries, nr_objects, sizeof(*entries), compare_entries);
+       QSORT(entries, nr_objects, compare_entries);
  
        for (i = 0; i < nr_objects; i++) {
                void *data;
diff --combined pathspec.c
index 49a53607bb004e8ccc34e6a18eadc8dfc2e7879d,eda13b54c7dc5003049136cf50dd63a21db02fa1..86f2b449b1b43d7abb0917c07676304a666056a9
@@@ -446,8 -446,7 +446,7 @@@ void parse_pathspec(struct pathspec *pa
        if (pathspec->magic & PATHSPEC_MAXDEPTH) {
                if (flags & PATHSPEC_KEEP_ORDER)
                        die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
-               qsort(pathspec->items, pathspec->nr,
-                     sizeof(struct pathspec_item), pathspec_item_cmp);
+               QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
        }
  }
  
@@@ -485,7 -484,8 +484,7 @@@ void copy_pathspec(struct pathspec *dst
  {
        *dst = *src;
        ALLOC_ARRAY(dst->items, dst->nr);
 -      memcpy(dst->items, src->items,
 -             sizeof(struct pathspec_item) * dst->nr);
 +      COPY_ARRAY(dst->items, src->items, dst->nr);
  }
  
  void clear_pathspec(struct pathspec *pathspec)
diff --combined ref-filter.c
index 9a8f55e45a160717caed50f1ea86ff108c42d6d5,44029b0e3495758f29361ca6f4aa3adb1e9231e7..d4c2931f3aab14accaa5720c87b2fc58aed83faa
@@@ -235,7 -235,7 +235,7 @@@ int parse_ref_filter_atom(const char *a
  {
        const char *sp;
        const char *arg;
 -      int i, at;
 +      int i, at, atom_len;
  
        sp = atom;
        if (*sp == '*' && sp < ep)
                        return i;
        }
  
 +      /*
 +       * If the atom name has a colon, strip it and everything after
 +       * it off - it specifies the format for this entry, and
 +       * shouldn't be used for checking against the valid_atom
 +       * table.
 +       */
 +      arg = memchr(sp, ':', ep - sp);
 +      atom_len = (arg ? arg : ep) - sp;
 +
        /* Is the atom a valid one? */
        for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
                int len = strlen(valid_atom[i].name);
 -
 -              /*
 -               * If the atom name has a colon, strip it and everything after
 -               * it off - it specifies the format for this entry, and
 -               * shouldn't be used for checking against the valid_atom
 -               * table.
 -               */
 -              arg = memchr(sp, ':', ep - sp);
 -              if (len == (arg ? arg : ep) - sp &&
 -                  !memcmp(valid_atom[i].name, sp, len))
 +              if (len == atom_len && !memcmp(valid_atom[i].name, sp, len))
                        break;
        }
  
@@@ -1573,7 -1573,7 +1573,7 @@@ static int compare_refs(const void *a_
  void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
  {
        ref_sorting = sorting;
-       qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
+       QSORT(array->items, array->nr, compare_refs);
  }
  
  static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
diff --combined sha1-array.c
index af1d7d560d303f6a04ff1029909782c7969a655c,21188deb7a4c44b279ae9ca2b67a0a785f2950ef..c1cc25cd95da66aec1c695f4bba9680f5cb834f6
@@@ -16,7 -16,7 +16,7 @@@ static int void_hashcmp(const void *a, 
  
  static void sha1_array_sort(struct sha1_array *array)
  {
-       qsort(array->sha1, array->nr, sizeof(*array->sha1), void_hashcmp);
+       QSORT(array->sha1, array->nr, void_hashcmp);
        array->sorted = 1;
  }
  
@@@ -42,7 -42,7 +42,7 @@@ void sha1_array_clear(struct sha1_arra
        array->sorted = 0;
  }
  
 -void sha1_array_for_each_unique(struct sha1_array *array,
 +int sha1_array_for_each_unique(struct sha1_array *array,
                                for_each_sha1_fn fn,
                                void *data)
  {
                sha1_array_sort(array);
  
        for (i = 0; i < array->nr; i++) {
 +              int ret;
                if (i > 0 && !hashcmp(array->sha1[i], array->sha1[i-1]))
                        continue;
 -              fn(array->sha1[i], data);
 +              ret = fn(array->sha1[i], data);
 +              if (ret)
 +                      return ret;
        }
 +      return 0;
  }