Merge branch 'dt/commit-preserve-base-index-upon-opportunistic-cache-tree-update'
authorJunio C Hamano <gitster@pobox.com>
Tue, 1 Sep 2015 23:31:29 +0000 (16:31 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 1 Sep 2015 23:31:29 +0000 (16:31 -0700)
When re-priming the cache-tree opportunistically while committing
the in-core index as-is, we mistakenly invalidated the in-core
index too aggressively, causing the experimental split-index code
to unnecessarily rewrite the on-disk index file(s).

* dt/commit-preserve-base-index-upon-opportunistic-cache-tree-update:
commit: don't rewrite shared index unnecessarily

1  2 
builtin/commit.c
t/t0090-cache-tree.sh
diff --combined builtin/commit.c
index b37cb6c8b7966161ed156d15d9a910824b645dda,1692620e25fe4fd7fe658c7f00a2bdea0ae90138..63772d016a23905d2e58bff0ad117673ed6651e7
@@@ -166,9 -166,9 +166,9 @@@ static int opt_parse_m(const struct opt
  
  static void determine_whence(struct wt_status *s)
  {
 -      if (file_exists(git_path("MERGE_HEAD")))
 +      if (file_exists(git_path_merge_head()))
                whence = FROM_MERGE;
 -      else if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
 +      else if (file_exists(git_path_cherry_pick_head())) {
                whence = FROM_CHERRY_PICK;
                if (file_exists(git_path(SEQ_DIR)))
                        sequencer_in_use = 1;
@@@ -324,7 -324,6 +324,7 @@@ static const char *prepare_index(int ar
        struct string_list partial;
        struct pathspec pathspec;
        int refresh_flags = REFRESH_QUIET;
 +      const char *ret;
  
        if (is_status)
                refresh_flags |= REFRESH_UNMERGED;
                        die(_("unable to create temporary index"));
  
                old_index_env = getenv(INDEX_ENVIRONMENT);
 -              setenv(INDEX_ENVIRONMENT, index_lock.filename.buf, 1);
 +              setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);
  
                if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
                        die(_("interactive add failed"));
                        unsetenv(INDEX_ENVIRONMENT);
  
                discard_cache();
 -              read_cache_from(index_lock.filename.buf);
 +              read_cache_from(get_lock_file_path(&index_lock));
                if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
                        if (reopen_lock_file(&index_lock) < 0)
                                die(_("unable to write index file"));
                        warning(_("Failed to update main cache tree"));
  
                commit_style = COMMIT_NORMAL;
 -              return index_lock.filename.buf;
 +              return get_lock_file_path(&index_lock);
        }
  
        /*
                if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
                        die(_("unable to write new_index file"));
                commit_style = COMMIT_NORMAL;
 -              return index_lock.filename.buf;
 +              return get_lock_file_path(&index_lock);
        }
  
        /*
                hold_locked_index(&index_lock, 1);
                refresh_cache_or_die(refresh_flags);
                if (active_cache_changed
-                   || !cache_tree_fully_valid(active_cache_tree)) {
+                   || !cache_tree_fully_valid(active_cache_tree))
                        update_main_cache_tree(WRITE_TREE_SILENT);
-                       active_cache_changed = 1;
-               }
                if (active_cache_changed) {
                        if (write_locked_index(&the_index, &index_lock,
                                               COMMIT_LOCK))
                die(_("unable to write temporary index file"));
  
        discard_cache();
 -      read_cache_from(false_lock.filename.buf);
 -
 -      return false_lock.filename.buf;
 +      ret = get_lock_file_path(&false_lock);
 +      read_cache_from(ret);
 +      return ret;
  }
  
  static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
@@@ -726,12 -723,12 +724,12 @@@ static int prepare_to_commit(const cha
                format_commit_message(commit, "fixup! %s\n\n",
                                      &sb, &ctx);
                hook_arg1 = "message";
 -      } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
 -              if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
 +      } else if (!stat(git_path_merge_msg(), &statbuf)) {
 +              if (strbuf_read_file(&sb, git_path_merge_msg(), 0) < 0)
                        die_errno(_("could not read MERGE_MSG"));
                hook_arg1 = "merge";
 -      } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
 -              if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
 +      } else if (!stat(git_path_squash_msg(), &statbuf)) {
 +              if (strbuf_read_file(&sb, git_path_squash_msg(), 0) < 0)
                        die_errno(_("could not read SQUASH_MSG"));
                hook_arg1 = "squash";
        } else if (template_file) {
                                _("%s"
                                "Date:      %s"),
                                ident_shown++ ? "" : "\n",
 -                              show_ident_date(&ai, DATE_NORMAL));
 +                              show_ident_date(&ai, DATE_MODE(NORMAL)));
  
                if (!committer_ident_sufficiently_given())
                        status_printf_ln(s, GIT_COLOR_NORMAL,
@@@ -1047,7 -1044,7 +1045,7 @@@ static const char *find_author_by_nickn
        commit = get_revision(&revs);
        if (commit) {
                struct pretty_print_context ctx = {0};
 -              ctx.date_mode = DATE_NORMAL;
 +              ctx.date_mode.type = DATE_NORMAL;
                strbuf_release(&buf);
                format_commit_message(commit, "%aN <%aE>", &buf, &ctx);
                clear_mailmap(&mailmap);
@@@ -1685,10 -1682,10 +1683,10 @@@ int cmd_commit(int argc, const char **a
                if (!reflog_msg)
                        reflog_msg = "commit (merge)";
                pptr = &commit_list_insert(current_head, pptr)->next;
 -              fp = fopen(git_path("MERGE_HEAD"), "r");
 +              fp = fopen(git_path_merge_head(), "r");
                if (fp == NULL)
                        die_errno(_("could not open '%s' for reading"),
 -                                git_path("MERGE_HEAD"));
 +                                git_path_merge_head());
                while (strbuf_getline(&m, fp, '\n') != EOF) {
                        struct commit *parent;
  
                }
                fclose(fp);
                strbuf_release(&m);
 -              if (!stat(git_path("MERGE_MODE"), &statbuf)) {
 -                      if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
 +              if (!stat(git_path_merge_mode(), &statbuf)) {
 +                      if (strbuf_read_file(&sb, git_path_merge_mode(), 0) < 0)
                                die_errno(_("could not read MERGE_MODE"));
                        if (!strcmp(sb.buf, "no-ff"))
                                allow_fast_forward = 0;
        }
        ref_transaction_free(transaction);
  
 -      unlink(git_path("CHERRY_PICK_HEAD"));
 -      unlink(git_path("REVERT_HEAD"));
 -      unlink(git_path("MERGE_HEAD"));
 -      unlink(git_path("MERGE_MSG"));
 -      unlink(git_path("MERGE_MODE"));
 -      unlink(git_path("SQUASH_MSG"));
 +      unlink(git_path_cherry_pick_head());
 +      unlink(git_path_revert_head());
 +      unlink(git_path_merge_head());
 +      unlink(git_path_merge_msg());
 +      unlink(git_path_merge_mode());
 +      unlink(git_path_squash_msg());
  
        if (commit_index_files())
                die (_("Repository has been updated, but unable to write\n"
diff --combined t/t0090-cache-tree.sh
index 055cc19000784bdb820e7ae53ba48987ead0367a,f92dd1f1dccad0f87bf6dd32761caf2c8919fc5c..adfd4f0b5eea1c0c438647d3d6567479acc5d495
@@@ -199,30 -199,6 +199,30 @@@ test_expect_success 'checkout -B gives 
        test_cache_tree
  '
  
 +test_expect_success 'merge --ff-only maintains cache-tree' '
 +      git checkout current &&
 +      git checkout -b changes &&
 +      test_commit llamas &&
 +      test_commit pachyderm &&
 +      test_cache_tree &&
 +      git checkout current &&
 +      test_cache_tree &&
 +      git merge --ff-only changes &&
 +      test_cache_tree
 +'
 +
 +test_expect_success 'merge maintains cache-tree' '
 +      git checkout current &&
 +      git checkout -b changes2 &&
 +      test_commit alpacas &&
 +      test_cache_tree &&
 +      git checkout current &&
 +      test_commit struthio &&
 +      test_cache_tree &&
 +      git merge changes2 &&
 +      test_cache_tree
 +'
 +
  test_expect_success 'partial commit gives cache-tree' '
        git checkout -b partial no-children &&
        test_commit one &&
@@@ -242,4 -218,14 +242,14 @@@ test_expect_success 'no phantom error w
        ! test -s errors
  '
  
+ test_expect_success 'switching trees does not invalidate shared index' '
+       git update-index --split-index &&
+       >split &&
+       git add split &&
+       test-dump-split-index .git/index | grep -v ^own >before &&
+       git commit -m "as-is" &&
+       test-dump-split-index .git/index | grep -v ^own >after &&
+       test_cmp before after
+ '
  test_done