]> git.ipfire.org Git - thirdparty/git.git/commitdiff
worktree: extract checkout_worktree()
authorDerrick Stolee <derrickstolee@github.com>
Wed, 23 Feb 2022 14:29:12 +0000 (14:29 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 23 Feb 2022 20:24:41 +0000 (12:24 -0800)
The ability to add the --no-checkout flag to 'git worktree' was added in
ef2a0ac9a0 (worktree: add: introduce --checkout option, 2016-03-29).
Recently, we noticed that add_worktree() is rather complicated, so
extract the logic for this checkout process to simplify the method.

Signed-off-by: Derrick Stolee <derrickstolee@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/worktree.c

index c806aa2b261efefd9f9fcca24e05f10a800b3bc4..25807e63a2500d3a06d3effee28034308acb85ed 100644 (file)
@@ -292,6 +292,18 @@ worktree_copy_cleanup:
        free(to_file);
 }
 
+static int checkout_worktree(const struct add_opts *opts,
+                            struct strvec *child_env)
+{
+       struct child_process cp = CHILD_PROCESS_INIT;
+       cp.git_cmd = 1;
+       strvec_pushl(&cp.args, "reset", "--hard", "--no-recurse-submodules", NULL);
+       if (opts->quiet)
+               strvec_push(&cp.args, "--quiet");
+       strvec_pushv(&cp.env_array, child_env->v);
+       return run_command(&cp);
+}
+
 static int add_worktree(const char *path, const char *refname,
                        const struct add_opts *opts)
 {
@@ -425,17 +437,9 @@ static int add_worktree(const char *path, const char *refname,
        if (ret)
                goto done;
 
-       if (opts->checkout) {
-               struct child_process cp = CHILD_PROCESS_INIT;
-               cp.git_cmd = 1;
-               strvec_pushl(&cp.args, "reset", "--hard", "--no-recurse-submodules", NULL);
-               if (opts->quiet)
-                       strvec_push(&cp.args, "--quiet");
-               strvec_pushv(&cp.env_array, child_env.v);
-               ret = run_command(&cp);
-               if (ret)
-                       goto done;
-       }
+       if (opts->checkout &&
+           (ret = checkout_worktree(opts, &child_env)))
+               goto done;
 
        is_junk = 0;
        FREE_AND_NULL(junk_work_tree);