diff mbox series

[04/11] worktree: extract checkout_worktree()

Message ID 1e62e4e4fa1a543ad134d70d97740cac5826e587.1645379667.git.gitgitgadget@gmail.com (mailing list archive)
State Superseded
Headers show
Series Updates to worktree code and docs | expand

Commit Message

Derrick Stolee Feb. 20, 2022, 5:54 p.m. UTC
From: Derrick Stolee <derrickstolee@github.com>

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>
---
 builtin/worktree.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

Comments

Taylor Blau Feb. 20, 2022, 9:59 p.m. UTC | #1
On Sun, Feb 20, 2022 at 05:54:20PM +0000, Derrick Stolee via GitGitGadget wrote:
> From: Derrick Stolee <derrickstolee@github.com>
>
> 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.

Thanks; all of these "worktree: extract ..." patches look fine to me. I
reviewed them locally with:

    $ git show <the-patch> --color-moved-ws=ignore-all-space --color-moved

and it was clear that all of the existing functionality was preserved.

Thanks,
Taylor
diff mbox series

Patch

diff --git a/builtin/worktree.c b/builtin/worktree.c
index c806aa2b261..25807e63a25 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -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);