Merge branch 'rs/cocci' into maint
authorJunio C Hamano <gitster@pobox.com>
Fri, 28 Oct 2016 16:01:23 +0000 (09:01 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 28 Oct 2016 16:01:23 +0000 (09:01 -0700)
Code cleanup.

* rs/cocci:
use strbuf_add_unique_abbrev() for adding short hashes, part 3
remove unnecessary NULL check before free(3)
coccicheck: make transformation for strbuf_addf(sb, "...") more precise
use strbuf_add_unique_abbrev() for adding short hashes, part 2
use strbuf_addstr() instead of strbuf_addf() with "%s", part 2
gitignore: ignore output files of coccicheck make target
use strbuf_addstr() for adding constant strings to a strbuf, part 2
add coccicheck make target
contrib/coccinelle: fix semantic patch for oid_to_hex_r()

1  2 
Makefile
diff.c
pretty.c
submodule.c
diff --combined Makefile
index 7a36af86659b549fb32a18c9d364936437fccc9e,4b5b1a953d268619d20b98ef64ef1cf2e124ed65..ddd1bdfc2de8a383f9bd625de6e5c26cb6f62237
+++ b/Makefile
@@@ -296,13 -296,7 +296,13 @@@ all:
  # Define USE_NED_ALLOCATOR if you want to replace the platforms default
  # memory allocators with the nedmalloc allocator written by Niall Douglas.
  #
 -# Define NO_REGEX if you have no or inferior regex support in your C library.
 +# Define OVERRIDE_STRDUP to override the libc version of strdup(3).
 +# This is necessary when using a custom allocator in order to avoid
 +# crashes due to allocation and free working on different 'heaps'.
 +# It's defined automatically if USE_NED_ALLOCATOR is set.
 +#
 +# Define NO_REGEX if your C library lacks regex support with REG_STARTEND
 +# feature.
  #
  # Define HAVE_DEV_TTY if your system can open /dev/tty to interact with the
  # user.
@@@ -462,6 -456,7 +462,7 @@@ CURL_CONFIG = curl-confi
  PTHREAD_LIBS = -lpthread
  PTHREAD_CFLAGS =
  GCOV = gcov
+ SPATCH = spatch
  
  export TCL_PATH TCLTK_PATH
  
@@@ -1462,14 -1457,8 +1463,14 @@@ ifdef NATIVE_CRL
  endif
  
  ifdef USE_NED_ALLOCATOR
 -       COMPAT_CFLAGS += -Icompat/nedmalloc
 -       COMPAT_OBJS += compat/nedmalloc/nedmalloc.o
 +      COMPAT_CFLAGS += -Icompat/nedmalloc
 +      COMPAT_OBJS += compat/nedmalloc/nedmalloc.o
 +      OVERRIDE_STRDUP = YesPlease
 +endif
 +
 +ifdef OVERRIDE_STRDUP
 +      COMPAT_CFLAGS += -DOVERRIDE_STRDUP
 +      COMPAT_OBJS += compat/strdup.o
  endif
  
  ifdef GIT_TEST_CMP_USE_COPIED_CONTEXT
@@@ -2041,7 -2030,7 +2042,7 @@@ endi
  
  ifdef USE_NED_ALLOCATOR
  compat/nedmalloc/nedmalloc.sp compat/nedmalloc/nedmalloc.o: EXTRA_CPPFLAGS = \
 -      -DNDEBUG -DOVERRIDE_STRDUP -DREPLACE_SYSTEM_ALLOCATOR
 +      -DNDEBUG -DREPLACE_SYSTEM_ALLOCATOR
  compat/nedmalloc/nedmalloc.sp: SPARSE_FLAGS += -Wno-non-pointer-null
  endif
  
@@@ -2308,6 -2297,18 +2309,18 @@@ check: common-cmds.
                exit 1; \
        fi
  
+ C_SOURCES = $(patsubst %.o,%.c,$(C_OBJ))
+ %.cocci.patch: %.cocci $(C_SOURCES)
+       @echo '    ' SPATCH $<; \
+       for f in $(C_SOURCES); do \
+               $(SPATCH) --sp-file $< $$f; \
+       done >$@ 2>$@.log; \
+       if test -s $@; \
+       then \
+               echo '    ' SPATCH result: $@; \
+       fi
+ coccicheck: $(patsubst %.cocci,%.cocci.patch,$(wildcard contrib/coccinelle/*.cocci))
  ### Installation rules
  
  ifneq ($(filter /%,$(firstword $(template_dir))),)
@@@ -2499,6 -2500,7 +2512,7 @@@ clean: profile-clean coverage-clea
        $(RM) -r $(GIT_TARNAME) .doc-tmp-dir
        $(RM) $(GIT_TARNAME).tar.gz git-core_$(GIT_VERSION)-*.tar.gz
        $(RM) $(htmldocs).tar.gz $(manpages).tar.gz
+       $(RM) contrib/coccinelle/*.cocci.patch*
        $(MAKE) -C Documentation/ clean
  ifndef NO_PERL
        $(MAKE) -C gitweb clean
diff --combined diff.c
index 01e1507e74b02753aef74aac4e427a0b0804878b,ceaac826e21b7afe6c90b41f2338c29da2320d4d..fe6f59160fd70a6dc801d3a67c61484dda521fb1
--- 1/diff.c
--- 2/diff.c
+++ b/diff.c
@@@ -354,6 -354,7 +354,6 @@@ struct emit_callback 
        const char **label_path;
        struct diff_words_data *diff_words;
        struct diff_options *opt;
 -      int *found_changesp;
        struct strbuf *header;
  };
  
@@@ -721,6 -722,7 +721,6 @@@ static void emit_rewrite_diff(const cha
  
        memset(&ecbdata, 0, sizeof(ecbdata));
        ecbdata.color_diff = want_color(o->use_color);
 -      ecbdata.found_changesp = &o->found_changes;
        ecbdata.ws_rule = whitespace_rule(name_b);
        ecbdata.opt = o;
        if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
@@@ -949,8 -951,7 +949,8 @@@ static int find_word_boundaries(mmfile_
  {
        if (word_regex && *begin < buffer->size) {
                regmatch_t match[1];
 -              if (!regexec(word_regex, buffer->ptr + *begin, 1, match, 0)) {
 +              if (!regexec_buf(word_regex, buffer->ptr + *begin,
 +                               buffer->size - *begin, 1, match, 0)) {
                        char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
                                        '\n', match[0].rm_eo - match[0].rm_so);
                        *end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
@@@ -1215,13 -1216,12 +1215,13 @@@ static void fn_out_consume(void *priv, 
        struct diff_options *o = ecbdata->opt;
        const char *line_prefix = diff_line_prefix(o);
  
 +      o->found_changes = 1;
 +
        if (ecbdata->header) {
 -              fprintf(ecbdata->opt->file, "%s", ecbdata->header->buf);
 +              fprintf(o->file, "%s", ecbdata->header->buf);
                strbuf_reset(ecbdata->header);
                ecbdata->header = NULL;
        }
 -      *(ecbdata->found_changesp) = 1;
  
        if (ecbdata->label_path[0]) {
                const char *name_a_tab, *name_b_tab;
                name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
                name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
  
 -              fprintf(ecbdata->opt->file, "%s%s--- %s%s%s\n",
 +              fprintf(o->file, "%s%s--- %s%s%s\n",
                        line_prefix, meta, ecbdata->label_path[0], reset, name_a_tab);
 -              fprintf(ecbdata->opt->file, "%s%s+++ %s%s%s\n",
 +              fprintf(o->file, "%s%s+++ %s%s%s\n",
                        line_prefix, meta, ecbdata->label_path[1], reset, name_b_tab);
                ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
        }
                find_lno(line, ecbdata);
                emit_hunk_header(ecbdata, line, len);
                if (line[len-1] != '\n')
 -                      putc('\n', ecbdata->opt->file);
 -              return;
 -      }
 -
 -      if (len < 1) {
 -              emit_line(ecbdata->opt, reset, reset, line, len);
 -              if (ecbdata->diff_words
 -                  && ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN)
 -                      fputs("~\n", ecbdata->opt->file);
 +                      putc('\n', o->file);
                return;
        }
  
                }
                diff_words_flush(ecbdata);
                if (ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN) {
 -                      emit_line(ecbdata->opt, context, reset, line, len);
 -                      fputs("~\n", ecbdata->opt->file);
 +                      emit_line(o, context, reset, line, len);
 +                      fputs("~\n", o->file);
                } else {
                        /*
                         * Skip the prefix character, if any.  With
                              line++;
                              len--;
                        }
 -                      emit_line(ecbdata->opt, context, reset, line, len);
 +                      emit_line(o, context, reset, line, len);
                }
                return;
        }
        default:
                /* incomplete line at the end */
                ecbdata->lno_in_preimage++;
 -              emit_line(ecbdata->opt,
 -                        diff_get_color(ecbdata->color_diff, DIFF_CONTEXT),
 +              emit_line(o, diff_get_color(ecbdata->color_diff, DIFF_CONTEXT),
                          reset, line, len);
                break;
        }
@@@ -2429,6 -2438,7 +2429,6 @@@ static void builtin_diff(const char *na
                memset(&ecbdata, 0, sizeof(ecbdata));
                ecbdata.label_path = lbl;
                ecbdata.color_diff = want_color(o->use_color);
 -              ecbdata.found_changesp = &o->found_changes;
                ecbdata.ws_rule = whitespace_rule(name_b);
                if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
                        check_blank_at_eof(&mf1, &mf2, &ecbdata);
@@@ -3076,7 -3086,7 +3076,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);
@@@ -4089,8 -4099,7 +4089,8 @@@ void diff_free_filepair(struct diff_fil
        free(p);
  }
  
 -/* This is different from find_unique_abbrev() in that
 +/*
 + * This is different from find_unique_abbrev() in that
   * it stuffs the result with dots for alignment.
   */
  const char *diff_unique_abbrev(const unsigned char *sha1, int len)
  
        abbrev = find_unique_abbrev(sha1, len);
        abblen = strlen(abbrev);
 +
 +      /*
 +       * In well-behaved cases, where the abbbreviated result is the
 +       * same as the requested length, append three dots after the
 +       * abbreviation (hence the whole logic is limited to the case
 +       * where abblen < 37); when the actual abbreviated result is a
 +       * bit longer than the requested length, we reduce the number
 +       * of dots so that they match the well-behaved ones.  However,
 +       * if the actual abbreviation is longer than the requested
 +       * length by more than three, we give up on aligning, and add
 +       * three dots anyway, to indicate that the output is not the
 +       * full object name.  Yes, this may be suboptimal, but this
 +       * appears only in "diff --raw --abbrev" output and it is not
 +       * worth the effort to change it now.  Note that this would
 +       * likely to work fine when the automatic sizing of default
 +       * abbreviation length is used--we would be fed -1 in "len" in
 +       * that case, and will end up always appending three-dots, but
 +       * the automatic sizing is supposed to give abblen that ensures
 +       * uniqueness across all objects (statistically speaking).
 +       */
        if (abblen < 37) {
                static char hex[41];
                if (len < abblen && abblen <= len + 2)
diff --combined pretty.c
index 25efbcac9206e82343f954405176fbf477055903,19089b35a97c0657a312c5fffe20e56cff120406..0c3149524059bd46c3c6d70bd774df0aaa65f976
+++ b/pretty.c
@@@ -544,15 -544,13 +544,13 @@@ static void add_merge_info(const struc
        strbuf_addstr(sb, "Merge:");
  
        while (parent) {
-               struct commit *p = parent->item;
-               const char *hex = NULL;
+               struct object_id *oidp = &parent->item->object.oid;
+               strbuf_addch(sb, ' ');
                if (pp->abbrev)
-                       hex = find_unique_abbrev(p->object.oid.hash, pp->abbrev);
-               if (!hex)
-                       hex = oid_to_hex(&p->object.oid);
+                       strbuf_add_unique_abbrev(sb, oidp->hash, pp->abbrev);
+               else
+                       strbuf_addstr(sb, oid_to_hex(oidp));
                parent = parent->next;
-               strbuf_addf(sb, " %s", hex);
        }
        strbuf_addch(sb, '\n');
  }
@@@ -1065,15 -1063,13 +1063,15 @@@ static size_t format_commit_one(struct 
        const struct commit *commit = c->commit;
        const char *msg = c->message;
        struct commit_list *p;
 -      int h1, h2;
 +      int ch;
  
        /* these are independent of the commit */
        switch (placeholder[0]) {
        case 'C':
                if (starts_with(placeholder + 1, "(auto)")) {
                        c->auto_color = want_color(c->pretty_ctx->color);
 +                      if (c->auto_color && sb->len)
 +                              strbuf_addstr(sb, GIT_COLOR_RESET);
                        return 7; /* consumed 7 bytes, "C(auto)" */
                } else {
                        int ret = parse_color(sb, placeholder, c);
                return 1;
        case 'x':
                /* %x00 == NUL, %x0a == LF, etc. */
 -              if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
 -                  h1 <= 16 &&
 -                  0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
 -                  h2 <= 16) {
 -                      strbuf_addch(sb, (h1<<4)|h2);
 -                      return 3;
 -              } else
 +              ch = hex2chr(placeholder + 1);
 +              if (ch < 0)
                        return 0;
 +              strbuf_addch(sb, ch);
 +              return 3;
        case 'w':
                if (placeholder[1] == '(') {
                        unsigned long width = 0, indent1 = 0, indent2 = 0;
diff --combined submodule.c
index e8258f061ac5726d8001a5b207886b3191745eaa,f2ba4a2dc2a4894d76006d4ef8ba5a9bd64f153d..9d8d91c207e7a2c09bdf997ba05c67b1262d8091
@@@ -370,11 -370,10 +370,10 @@@ void show_submodule_summary(FILE *f, co
                return;
        }
  
-       strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path,
-                       find_unique_abbrev(one, DEFAULT_ABBREV));
-       if (!fast_backward && !fast_forward)
-               strbuf_addch(&sb, '.');
-       strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
+       strbuf_addf(&sb, "%s%sSubmodule %s ", line_prefix, meta, path);
+       strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV);
+       strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
+       strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV);
        if (message)
                strbuf_addf(&sb, " %s%s\n", message, reset);
        else
@@@ -1160,5 -1159,4 +1159,5 @@@ void prepare_submodule_repo_env(struct 
                if (strcmp(*var, CONFIG_DATA_ENVIRONMENT))
                        argv_array_push(out, *var);
        }
 +      argv_array_push(out, "GIT_DIR=.git");
  }