diff mbox series

[4/8] worktree: make high-level pruning re-usable

Message ID 20200608062356.40264-5-sunshine@sunshineco.com (mailing list archive)
State New, archived
Headers show
Series worktree: tighten duplicate path detection | expand

Commit Message

Eric Sunshine June 8, 2020, 6:23 a.m. UTC
The low-level logic for removing a worktree is well encapsulated in
delete_git_dir(). However, high-level details related to pruning a
worktree -- such as dealing with verbosity and dry-run mode -- are not
encapsulated. Factor out this high-level logic into its own function so
it can be re-used as new worktree corruption detectors are added.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 builtin/worktree.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

Comments

Junio C Hamano June 8, 2020, 9:29 p.m. UTC | #1
Eric Sunshine <sunshine@sunshineco.com> writes:

> The low-level logic for removing a worktree is well encapsulated in
> delete_git_dir(). However, high-level details related to pruning a
> worktree -- such as dealing with verbosity and dry-run mode -- are not
> encapsulated. Factor out this high-level logic into its own function so
> it can be re-used as new worktree corruption detectors are added.
>
> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
> ---
>  builtin/worktree.c | 15 +++++++++------
>  1 file changed, 9 insertions(+), 6 deletions(-)
>
> diff --git a/builtin/worktree.c b/builtin/worktree.c
> index 27681a1396..d0c046e885 100644
> --- a/builtin/worktree.c
> +++ b/builtin/worktree.c
> @@ -133,6 +133,14 @@ static int should_prune_worktree(const char *id, struct strbuf *reason)
>  	return 0;
>  }
>  
> +static void prune_worktree(const char *id, const char *reason)
> +{
> +	if (show_only || verbose)
> +		printf_ln(_("Removing %s/%s: %s"), "worktrees", id, reason);
> +	if (!show_only)
> +		delete_git_dir(id);
> +}

Makes sense, and this sensible name can be used only because we've
cleaned up the other one in the previous step.

Good so far (except that I still do not know why 2/8 is a good move
after reading the series up to this point).

>  static void prune_worktrees(void)
>  {
>  	struct strbuf reason = STRBUF_INIT;
> @@ -146,12 +154,7 @@ static void prune_worktrees(void)
>  		strbuf_reset(&reason);
>  		if (!should_prune_worktree(d->d_name, &reason))
>  			continue;
> -		if (show_only || verbose)
> -			printf_ln(_("Removing %s/%s: %s"),
> -				  "worktrees", d->d_name, reason.buf);
> -		if (show_only)
> -			continue;
> -		delete_git_dir(d->d_name);
> +		prune_worktree(d->d_name, reason.buf);
>  	}
>  	closedir(dir);
>  	if (!show_only)
diff mbox series

Patch

diff --git a/builtin/worktree.c b/builtin/worktree.c
index 27681a1396..d0c046e885 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -133,6 +133,14 @@  static int should_prune_worktree(const char *id, struct strbuf *reason)
 	return 0;
 }
 
+static void prune_worktree(const char *id, const char *reason)
+{
+	if (show_only || verbose)
+		printf_ln(_("Removing %s/%s: %s"), "worktrees", id, reason);
+	if (!show_only)
+		delete_git_dir(id);
+}
+
 static void prune_worktrees(void)
 {
 	struct strbuf reason = STRBUF_INIT;
@@ -146,12 +154,7 @@  static void prune_worktrees(void)
 		strbuf_reset(&reason);
 		if (!should_prune_worktree(d->d_name, &reason))
 			continue;
-		if (show_only || verbose)
-			printf_ln(_("Removing %s/%s: %s"),
-				  "worktrees", d->d_name, reason.buf);
-		if (show_only)
-			continue;
-		delete_git_dir(d->d_name);
+		prune_worktree(d->d_name, reason.buf);
 	}
 	closedir(dir);
 	if (!show_only)