Merge branch 'rs/swap'
authorJunio C Hamano <gitster@pobox.com>
Wed, 15 Feb 2017 20:54:19 +0000 (12:54 -0800)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 Feb 2017 20:54:19 +0000 (12:54 -0800)
Code clean-up.

* rs/swap:
graph: use SWAP macro
diff: use SWAP macro
use SWAP macro
apply: use SWAP macro
add SWAP macro

1  2 
diff.c
git-compat-util.h
graph.c
diff --combined diff.c
index a79f3408da44a085f97d5cc1b29c200a2e1defbf,6c4f3f6b72d0d5517f679b2eac8472d7020cfc8b..051761be405ece65423d43830f08ed7b7dec9756
--- 1/diff.c
--- 2/diff.c
+++ b/diff.c
@@@ -3015,7 -3015,7 +3015,7 @@@ static struct diff_tempfile *prepare_te
                        if (!one->oid_valid)
                                sha1_to_hex_r(temp->hex, null_sha1);
                        else
 -                              sha1_to_hex_r(temp->hex, one->oid.hash);
 +                              oid_to_hex_r(temp->hex, &one->oid);
                        /* Even though we may sometimes borrow the
                         * contents from the work tree, we always want
                         * one->mode.  mode is trustworthy even when
@@@ -4450,7 -4450,6 +4450,7 @@@ static void flush_one_pair(struct diff_
                name_a = p->two->path;
                name_b = NULL;
                strip_prefix(opt->prefix_length, &name_a, &name_b);
 +              fprintf(opt->file, "%s", diff_line_prefix(opt));
                write_name_quoted(name_a, opt->file, opt->line_termination);
        }
  }
@@@ -5118,14 -5117,10 +5118,10 @@@ void diff_change(struct diff_options *o
                return;
  
        if (DIFF_OPT_TST(options, REVERSE_DIFF)) {
-               unsigned tmp;
-               const unsigned char *tmp_c;
-               tmp = old_mode; old_mode = new_mode; new_mode = tmp;
-               tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
-               tmp = old_sha1_valid; old_sha1_valid = new_sha1_valid;
-                       new_sha1_valid = tmp;
-               tmp = old_dirty_submodule; old_dirty_submodule = new_dirty_submodule;
-                       new_dirty_submodule = tmp;
+               SWAP(old_mode, new_mode);
+               SWAP(old_sha1, new_sha1);
+               SWAP(old_sha1_valid, new_sha1_valid);
+               SWAP(old_dirty_submodule, new_dirty_submodule);
        }
  
        if (options->prefix &&
diff --combined git-compat-util.h
index f46d40e4a475c9bd35ff02373101e8431e722b7d,66cd466eeaca10eb4ebd8cb00becbe5afa9117c8..ef6d560e156c0adefd897e04377b09d5c32edf08
@@@ -527,6 -527,16 +527,16 @@@ static inline int ends_with(const char 
        return strip_suffix(str, suffix, &len);
  }
  
+ #define SWAP(a, b) do {                                               \
+       void *_swap_a_ptr = &(a);                               \
+       void *_swap_b_ptr = &(b);                               \
+       unsigned char _swap_buffer[sizeof(a)];                  \
+       memcpy(_swap_buffer, _swap_a_ptr, sizeof(a));           \
+       memcpy(_swap_a_ptr, _swap_b_ptr, sizeof(a) +            \
+              BUILD_ASSERT_OR_ZERO(sizeof(a) == sizeof(b)));   \
+       memcpy(_swap_b_ptr, _swap_buffer, sizeof(a));           \
+ } while (0)
  #if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
  
  #ifndef PROT_READ
@@@ -988,17 -998,6 +998,17 @@@ static inline void sane_qsort(void *bas
                qsort(base, nmemb, size, compar);
  }
  
 +#ifndef HAVE_ISO_QSORT_S
 +int git_qsort_s(void *base, size_t nmemb, size_t size,
 +              int (*compar)(const void *, const void *, void *), void *ctx);
 +#define qsort_s git_qsort_s
 +#endif
 +
 +#define QSORT_S(base, n, compar, ctx) do {                    \
 +      if (qsort_s((base), (n), sizeof(*(base)), compar, ctx)) \
 +              die("BUG: qsort_s() failed");                   \
 +} while (0)
 +
  #ifndef REG_STARTEND
  #error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd"
  #endif
diff --combined graph.c
index 0649007704ac635af163a4e1016121744a951d1c,29b0f51dc507caa91fe6a748ad9599b97b47e242..8b9049dd2c725f58314042deebfb69030e4321d3
+++ b/graph.c
@@@ -3,7 -3,6 +3,7 @@@
  #include "color.h"
  #include "graph.h"
  #include "revision.h"
 +#include "argv-array.h"
  
  /* Internal API */
  
@@@ -80,26 -79,6 +80,26 @@@ static void graph_show_line_prefix(cons
  static const char **column_colors;
  static unsigned short column_colors_max;
  
 +static void parse_graph_colors_config(struct argv_array *colors, const char *string)
 +{
 +      const char *end, *start;
 +
 +      start = string;
 +      end = string + strlen(string);
 +      while (start < end) {
 +              const char *comma = strchrnul(start, ',');
 +              char color[COLOR_MAXLEN];
 +
 +              if (!color_parse_mem(start, comma - start, color))
 +                      argv_array_push(colors, color);
 +              else
 +                      warning(_("ignore invalid color '%.*s' in log.graphColors"),
 +                              (int)(comma - start), start);
 +              start = comma + 1;
 +      }
 +      argv_array_push(colors, GIT_COLOR_RESET);
 +}
 +
  void graph_set_column_colors(const char **colors, unsigned short colors_max)
  {
        column_colors = colors;
@@@ -259,22 -238,9 +259,22 @@@ struct git_graph *graph_init(struct rev
  {
        struct git_graph *graph = xmalloc(sizeof(struct git_graph));
  
 -      if (!column_colors)
 -              graph_set_column_colors(column_colors_ansi,
 -                                      column_colors_ansi_max);
 +      if (!column_colors) {
 +              char *string;
 +              if (git_config_get_string("log.graphcolors", &string)) {
 +                      /* not configured -- use default */
 +                      graph_set_column_colors(column_colors_ansi,
 +                                              column_colors_ansi_max);
 +              } else {
 +                      static struct argv_array custom_colors = ARGV_ARRAY_INIT;
 +                      argv_array_clear(&custom_colors);
 +                      parse_graph_colors_config(&custom_colors, string);
 +                      free(string);
 +                      /* graph_set_column_colors takes a max-index, not a count */
 +                      graph_set_column_colors(custom_colors.argv,
 +                                              custom_colors.argc - 1);
 +              }
 +      }
  
        graph->commit = NULL;
        graph->revs = opt;
@@@ -497,7 -463,6 +497,6 @@@ static void graph_update_width(struct g
  static void graph_update_columns(struct git_graph *graph)
  {
        struct commit_list *parent;
-       struct column *tmp_columns;
        int max_new_columns;
        int mapping_idx;
        int i, seen_this, is_commit_in_columns;
         * We'll re-use the old columns array as storage to compute the new
         * columns list for the commit after this one.
         */
-       tmp_columns = graph->columns;
-       graph->columns = graph->new_columns;
+       SWAP(graph->columns, graph->new_columns);
        graph->num_columns = graph->num_new_columns;
-       graph->new_columns = tmp_columns;
        graph->num_new_columns = 0;
  
        /*
@@@ -1031,7 -993,6 +1027,6 @@@ static void graph_output_post_merge_lin
  static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
  {
        int i;
-       int *tmp_mapping;
        short used_horizontal = 0;
        int horizontal_edge = -1;
        int horizontal_edge_target = -1;
        /*
         * Swap mapping and new_mapping
         */
-       tmp_mapping = graph->mapping;
-       graph->mapping = graph->new_mapping;
-       graph->new_mapping = tmp_mapping;
+       SWAP(graph->mapping, graph->new_mapping);
  
        /*
         * If graph->mapping indicates that all of the branch lines