From: Ben Peart <ben.pe...@microsoft.com>

If the new core.optimizecheckout config setting is set to true, speed up
"git checkout -b foo" by avoiding the work to merge the working tree.  This
is valid because no merge needs to occur - only creating the new branch/
updating the refs. Any other options force it through the old code path.

This change in behavior is off by default and behind the config setting so
that users have to opt-in to the optimized behavior.

We've been running with this patch internally for a long time but it was
rejected when I submitted it to the mailing list before because it
implicitly changes the behavior of checkout -b. Trying it again configured
behind a config setting as a potential solution for other optimizations to
checkout that could change the behavior as well.

https://public-inbox.org/git/20180724042740.gb13...@sigill.intra.peff.net/T/#m75afe3ab318d23f36334cf3a6e3d058839592469

Signed-off-by: Ben Peart <ben.pe...@microsoft.com>
---

Notes:
    Base Ref: master
    Web-Diff: https://github.com/benpeart/git/commit/f43d934ce7
    Checkout: git fetch https://github.com/benpeart/git checkout-b-v1 && git 
checkout f43d934ce7

 Documentation/config.txt |  6 +++
 builtin/checkout.c       | 94 ++++++++++++++++++++++++++++++++++++++++
 cache.h                  |  1 +
 config.c                 |  5 +++
 environment.c            |  1 +
 5 files changed, 107 insertions(+)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index a32172a43c..2c4f513bf1 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -911,6 +911,12 @@ core.commitGraph::
        Enable git commit graph feature. Allows reading from the
        commit-graph file.
 
+core.optimizedCheckout
+       Speed up "git checkout -b foo" by skipping much of the work of a
+       full checkout command.  This changs the behavior as it will skip
+       merging the trees and updating the index and instead only create
+       and switch to the new ref.
+
 core.sparseCheckout::
        Enable "sparse checkout" feature. See section "Sparse checkout" in
        linkgit:git-read-tree[1] for more information.
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 28627650cd..b186a3201e 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -41,6 +41,10 @@ struct checkout_opts {
        int ignore_skipworktree;
        int ignore_other_worktrees;
        int show_progress;
+       /*
+        * If new checkout options are added, needs_working_tree_merge
+        * should be updated accordingly.
+        */
 
        const char *new_branch;
        const char *new_branch_force;
@@ -471,6 +475,88 @@ static void setup_branch_path(struct branch_info *branch)
        branch->path = strbuf_detach(&buf, NULL);
 }
 
+static int needs_working_tree_merge(const struct checkout_opts *opts,
+       const struct branch_info *old_branch_info,
+       const struct branch_info *new_branch_info)
+{
+       /*
+        * We must do the merge if we are actually moving to a new
+        * commit tree.
+        */
+       if (!old_branch_info->commit || !new_branch_info->commit ||
+               oidcmp(&old_branch_info->commit->object.oid, 
&new_branch_info->commit->object.oid))
+               return 1;
+
+       /*
+        * opts->patch_mode cannot be used with switching branches so is
+        * not tested here
+        */
+
+       /*
+        * opts->quiet only impacts output so doesn't require a merge
+        */
+
+       /*
+        * Honor the explicit request for a three-way merge or to throw away
+        * local changes
+        */
+       if (opts->merge || opts->force)
+               return 1;
+
+       /*
+        * --detach is documented as "updating the index and the files in the
+        * working tree" but this optimization skips those steps so fall through
+        * to the regular code path.
+        */
+       if (opts->force_detach)
+               return 1;
+
+       /*
+        * opts->writeout_stage cannot be used with switching branches so is
+        * not tested here
+        */
+
+       /*
+        * Honor the explicit ignore requests
+        */
+       if (!opts->overwrite_ignore || opts->ignore_skipworktree ||
+               opts->ignore_other_worktrees)
+               return 1;
+
+       /*
+        * opts->show_progress only impacts output so doesn't require a merge
+        */
+
+       /*
+        * If we aren't creating a new branch any changes or updates will
+        * happen in the existing branch.  Since that could only be updating
+        * the index and working directory, we don't want to skip those steps
+        * or we've defeated any purpose in running the command.
+        */
+       if (!opts->new_branch)
+               return 1;
+
+       /*
+        * new_branch_force is defined to "create/reset and checkout a branch"
+        * so needs to go through the merge to do the reset
+        */
+       if (opts->new_branch_force)
+               return 1;
+
+       /*
+        * A new orphaned branch requrires the index and the working tree to be
+        * adjusted to <start_point>
+        */
+       if (opts->new_orphan_branch)
+               return 1;
+
+       /*
+        * Remaining variables are not checkout options but used to track state
+        */
+
+       return 0;
+}
+
 static int merge_working_tree(const struct checkout_opts *opts,
                              struct branch_info *old_branch_info,
                              struct branch_info *new_branch_info,
@@ -479,6 +565,14 @@ static int merge_working_tree(const struct checkout_opts 
*opts,
        int ret;
        struct lock_file lock_file = LOCK_INIT;
 
+       /*
+        * Skip merging the trees, updating the index, and work tree only if we
+        * are simply creating a new branch via "git checkout -b foo."  Any
+        * other options or usage will continue to do all these steps.
+        */
+       if (core_optimize_checkout && !needs_working_tree_merge(opts, 
old_branch_info, new_branch_info))
+               return 0;
+
        hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
        if (read_cache_preload(NULL) < 0)
                return error(_("index file corrupt"));
diff --git a/cache.h b/cache.h
index 8b447652a7..7f2b386c67 100644
--- a/cache.h
+++ b/cache.h
@@ -815,6 +815,7 @@ extern int fsync_object_files;
 extern int core_preload_index;
 extern int core_commit_graph;
 extern int core_apply_sparse_checkout;
+extern int core_optimize_checkout;
 extern int precomposed_unicode;
 extern int protect_hfs;
 extern int protect_ntfs;
diff --git a/config.c b/config.c
index 7968ef7566..b59a303e52 100644
--- a/config.c
+++ b/config.c
@@ -1319,6 +1319,11 @@ static int git_default_core_config(const char *var, 
const char *value)
                return 0;
        }
 
+       if (!strcmp(var, "core.optimizecheckout")) {
+               core_optimize_checkout = git_config_bool(var, value);
+               return 0;
+       }
+
        if (!strcmp(var, "core.precomposeunicode")) {
                precomposed_unicode = git_config_bool(var, value);
                return 0;
diff --git a/environment.c b/environment.c
index 013e845235..1991fb4855 100644
--- a/environment.c
+++ b/environment.c
@@ -68,6 +68,7 @@ char *notes_ref_name;
 int grafts_replace_parents = 1;
 int core_commit_graph;
 int core_apply_sparse_checkout;
+int core_optimize_checkout;
 int merge_log_config = -1;
 int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
 unsigned long pack_size_limit_cfg;

base-commit: b7bd9486b055c3f967a870311e704e3bb0654e4f
-- 
2.17.0.gvfs.1.123.g449c066

Reply via email to