Merge branch 'ao/merge-verbosity-getenv-just-once'
authorJunio C Hamano <gitster@pobox.com>
Wed, 15 Nov 2017 03:14:28 +0000 (12:14 +0900)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 Nov 2017 03:14:28 +0000 (12:14 +0900)
Code cleanup.

* ao/merge-verbosity-getenv-just-once:
  merge-recursive: check GIT_MERGE_VERBOSITY only once

1  2 
merge-recursive.c

diff --combined merge-recursive.c
@@@ -4,7 -4,6 +4,7 @@@
   * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
   */
  #include "cache.h"
 +#include "config.h"
  #include "advice.h"
  #include "lockfile.h"
  #include "cache-tree.h"
  #include "dir.h"
  #include "submodule.h"
  
 +struct path_hashmap_entry {
 +      struct hashmap_entry e;
 +      char path[FLEX_ARRAY];
 +};
 +
 +static int path_hashmap_cmp(const void *cmp_data,
 +                          const void *entry,
 +                          const void *entry_or_key,
 +                          const void *keydata)
 +{
 +      const struct path_hashmap_entry *a = entry;
 +      const struct path_hashmap_entry *b = entry_or_key;
 +      const char *key = keydata;
 +
 +      if (ignore_case)
 +              return strcasecmp(a->path, key ? key : b->path);
 +      else
 +              return strcmp(a->path, key ? key : b->path);
 +}
 +
 +static unsigned int path_hash(const char *path)
 +{
 +      return ignore_case ? strihash(path) : strhash(path);
 +}
 +
  static void flush_output(struct merge_options *o)
  {
        if (o->buffer_output < 2 && o->obuf.len) {
@@@ -93,7 -67,7 +93,7 @@@ static struct tree *shift_tree_object(s
        }
        if (!oidcmp(&two->object.oid, &shifted))
                return two;
 -      return lookup_tree(shifted.hash);
 +      return lookup_tree(&shifted);
  }
  
  static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
@@@ -330,7 -304,7 +330,7 @@@ struct tree *write_tree_from_memory(str
                return NULL;
        }
  
 -      result = lookup_tree(active_cache_tree->sha1);
 +      result = lookup_tree(&active_cache_tree->oid);
  
        return result;
  }
@@@ -339,25 -313,29 +339,25 @@@ static int save_files_dirs(const unsign
                struct strbuf *base, const char *path,
                unsigned int mode, int stage, void *context)
  {
 +      struct path_hashmap_entry *entry;
        int baselen = base->len;
        struct merge_options *o = context;
  
        strbuf_addstr(base, path);
  
 -      if (S_ISDIR(mode))
 -              string_list_insert(&o->current_directory_set, base->buf);
 -      else
 -              string_list_insert(&o->current_file_set, base->buf);
 +      FLEX_ALLOC_MEM(entry, path, base->buf, base->len);
 +      hashmap_entry_init(entry, path_hash(entry->path));
 +      hashmap_add(&o->current_file_dir_set, entry);
  
        strbuf_setlen(base, baselen);
        return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
  }
  
 -static int get_files_dirs(struct merge_options *o, struct tree *tree)
 +static void get_files_dirs(struct merge_options *o, struct tree *tree)
  {
 -      int n;
        struct pathspec match_all;
        memset(&match_all, 0, sizeof(match_all));
 -      if (read_tree_recursive(tree, "", 0, 0, &match_all, save_files_dirs, o))
 -              return 0;
 -      n = o->current_file_set.nr + o->current_directory_set.nr;
 -      return n;
 +      read_tree_recursive(tree, "", 0, 0, &match_all, save_files_dirs, o);
  }
  
  /*
@@@ -540,8 -518,8 +540,8 @@@ static struct string_list *get_renames(
                return renames;
  
        diff_setup(&opts);
 -      DIFF_OPT_SET(&opts, RECURSIVE);
 -      DIFF_OPT_CLR(&opts, RENAME_EMPTY);
 +      opts.flags.recursive = 1;
 +      opts.flags.rename_empty = 0;
        opts.detect_rename = DIFF_DETECT_RENAME;
        opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
                            o->diff_rename_limit >= 0 ? o->diff_rename_limit :
        opts.show_rename_progress = o->show_rename_progress;
        opts.output_format = DIFF_FORMAT_NO_OUTPUT;
        diff_setup_done(&opts);
 -      diff_tree_sha1(o_tree->object.oid.hash, tree->object.oid.hash, "", &opts);
 +      diff_tree_oid(&o_tree->object.oid, &tree->object.oid, "", &opts);
        diffcore_std(&opts);
        if (opts.needed_rename_limit > o->needed_rename_limit)
                o->needed_rename_limit = opts.needed_rename_limit;
@@@ -667,7 -645,6 +667,7 @@@ static void add_flattened_path(struct s
  
  static char *unique_path(struct merge_options *o, const char *path, const char *branch)
  {
 +      struct path_hashmap_entry *entry;
        struct strbuf newpath = STRBUF_INIT;
        int suffix = 0;
        size_t base_len;
        add_flattened_path(&newpath, branch);
  
        base_len = newpath.len;
 -      while (string_list_has_string(&o->current_file_set, newpath.buf) ||
 -             string_list_has_string(&o->current_directory_set, newpath.buf) ||
 +      while (hashmap_get_from_hash(&o->current_file_dir_set,
 +                                   path_hash(newpath.buf), newpath.buf) ||
               (!o->call_depth && file_exists(newpath.buf))) {
                strbuf_setlen(&newpath, base_len);
                strbuf_addf(&newpath, "_%d", suffix++);
        }
  
 -      string_list_insert(&o->current_file_set, newpath.buf);
 +      FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len);
 +      hashmap_entry_init(entry, path_hash(entry->path));
 +      hashmap_add(&o->current_file_dir_set, entry);
        return strbuf_detach(&newpath, NULL);
  }
  
@@@ -1019,11 -994,11 +1019,11 @@@ static int merge_file_1(struct merge_op
                                return ret;
                        result->clean = (merge_status == 0);
                } else if (S_ISGITLINK(a->mode)) {
 -                      result->clean = merge_submodule(result->oid.hash,
 +                      result->clean = merge_submodule(&result->oid,
                                                       one->path,
 -                                                     one->oid.hash,
 -                                                     a->oid.hash,
 -                                                     b->oid.hash,
 +                                                     &one->oid,
 +                                                     &a->oid,
 +                                                     &b->oid,
                                                       !o->call_depth);
                } else if (S_ISLNK(a->mode)) {
                        oidcpy(&result->oid, &a->oid);
@@@ -1664,8 -1639,8 +1664,8 @@@ static int blob_unchanged(struct merge_
         * performed.  Comparison can be skipped if both files are
         * unchanged since their sha1s have already been compared.
         */
 -      if (renormalize_buffer(path, o.buf, o.len, &o) |
 -          renormalize_buffer(path, a.buf, a.len, &a))
 +      if (renormalize_buffer(&the_index, path, o.buf, o.len, &o) |
 +          renormalize_buffer(&the_index, path, a.buf, a.len, &a))
                ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));
  
  error_return:
@@@ -1951,7 -1926,7 +1951,7 @@@ int merge_trees(struct merge_options *o
        }
  
        if (oid_eq(&common->object.oid, &merge->object.oid)) {
 -              output(o, 0, _("Already up-to-date!"));
 +              output(o, 0, _("Already up to date!"));
                *result = head;
                return 1;
        }
        if (unmerged_cache()) {
                struct string_list *entries, *re_head, *re_merge;
                int i;
 -              string_list_clear(&o->current_file_set, 1);
 -              string_list_clear(&o->current_directory_set, 1);
 +              /*
 +               * Only need the hashmap while processing entries, so
 +               * initialize it here and free it when we are done running
 +               * through the entries. Keeping it in the merge_options as
 +               * opposed to decaring a local hashmap is for convenience
 +               * so that we don't have to pass it to around.
 +               */
 +              hashmap_init(&o->current_file_dir_set, path_hashmap_cmp, NULL, 512);
                get_files_dirs(o, head);
                get_files_dirs(o, merge);
  
                re_merge = get_renames(o, merge, common, head, merge, entries);
                clean = process_renames(o, re_head, re_merge);
                if (clean < 0)
 -                      return clean;
 +                      goto cleanup;
                for (i = entries->nr-1; 0 <= i; i--) {
                        const char *path = entries->items[i].string;
                        struct stage_data *e = entries->items[i].util;
                                int ret = process_entry(o, path, e);
                                if (!ret)
                                        clean = 0;
 -                              else if (ret < 0)
 -                                      return ret;
 +                              else if (ret < 0) {
 +                                      clean = ret;
 +                                      goto cleanup;
 +                              }
                        }
                }
                for (i = 0; i < entries->nr; i++) {
                                    entries->items[i].string);
                }
  
 +cleanup:
                string_list_clear(re_merge, 0);
                string_list_clear(re_head, 0);
                string_list_clear(entries, 1);
  
 +              hashmap_free(&o->current_file_dir_set, 1);
 +
                free(re_merge);
                free(re_head);
                free(entries);
 +
 +              if (clean < 0)
 +                      return clean;
        }
        else
                clean = 1;
@@@ -2081,7 -2042,7 +2081,7 @@@ int merge_recursive(struct merge_option
                /* if there is no common ancestor, use an empty tree */
                struct tree *tree;
  
 -              tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
 +              tree = lookup_tree(&empty_tree_oid);
                merged_common_ancestors = make_virtual_commit(tree, "ancestor");
        }
  
@@@ -2142,7 -2103,7 +2142,7 @@@ static struct commit *get_ref(const str
  {
        struct object *object;
  
 -      object = deref_tag(parse_object(oid->hash), name, strlen(name));
 +      object = deref_tag(parse_object(oid), name, strlen(name));
        if (!object)
                return NULL;
        if (object->type == OBJ_TREE)
@@@ -2162,7 -2123,7 +2162,7 @@@ int merge_recursive_generic(struct merg
                            struct commit **result)
  {
        int clean;
 -      struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
 +      struct lock_file lock = LOCK_INIT;
        struct commit *head_commit = get_ref(head, o->branch1);
        struct commit *next_commit = get_ref(merge, o->branch2);
        struct commit_list *ca = NULL;
                }
        }
  
 -      hold_locked_index(lock, LOCK_DIE_ON_ERROR);
 +      hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
        clean = merge_recursive(o, head_commit, next_commit, ca,
                        result);
        if (clean < 0)
                return clean;
  
        if (active_cache_changed &&
 -          write_locked_index(&the_index, lock, COMMIT_LOCK))
 +          write_locked_index(&the_index, &lock, COMMIT_LOCK))
                return err(o, _("Unable to write index."));
  
        return clean ? 0 : 1;
@@@ -2201,6 -2162,7 +2201,7 @@@ static void merge_recursive_config(stru
  
  void init_merge_options(struct merge_options *o)
  {
+       const char *merge_verbosity;
        memset(o, 0, sizeof(struct merge_options));
        o->verbosity = 2;
        o->buffer_output = 1;
        o->renormalize = 0;
        o->detect_rename = 1;
        merge_recursive_config(o);
-       if (getenv("GIT_MERGE_VERBOSITY"))
-               o->verbosity =
-                       strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
+       merge_verbosity = getenv("GIT_MERGE_VERBOSITY");
+       if (merge_verbosity)
+               o->verbosity = strtol(merge_verbosity, NULL, 10);
        if (o->verbosity >= 5)
                o->buffer_output = 0;
        strbuf_init(&o->obuf, 0);
 -      string_list_init(&o->current_file_set, 1);
 -      string_list_init(&o->current_directory_set, 1);
        string_list_init(&o->df_conflict_file_set, 1);
  }