Subject: [PATCH v5 0/6] worktree lock/unlock

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Mon, 13 Jun 2016 19:18:20 +0700

v5 fixes some error messages mentioning "working directory" instead of
"working tree" and split the double use of "lock_reason" field in
"struct worktree". This series depends on
nd/worktree-cleanup-post-head-protection.

Diff from v4

-- 8< --
diff --git a/builtin/worktree.c b/builtin/worktree.c
index cb5026d..4877421 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -478,9 +478,9 @@ static int lock_worktree(int ac, const char **av, const char *prefix)
 	worktrees = get_worktrees();
 	wt = find_worktree(worktrees, prefix, av[0]);
 	if (!wt)
-		die(_("'%s' is not a working directory"), av[0]);
+		die(_("'%s' is not a working tree"), av[0]);
 	if (is_main_worktree(wt))
-		die(_("'%s' is a main working directory"), av[0]);
+		die(_("The main working tree cannot be locked or unlocked"));
 
 	old_reason = is_worktree_locked(wt);
 	if (old_reason) {
@@ -511,9 +511,9 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
 	worktrees = get_worktrees();
 	wt = find_worktree(worktrees, prefix, av[0]);
 	if (!wt)
-		die(_("'%s' is not a working directory"), av[0]);
+		die(_("'%s' is not a working tree"), av[0]);
 	if (is_main_worktree(wt))
-		die(_("'%s' is a main working directory"), av[0]);
+		die(_("The main working tree cannot be locked or unlocked"));
 	if (!is_worktree_locked(wt))
 		die(_("'%s' is not locked"), av[0]);
 	ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id));
diff --git a/worktree.c b/worktree.c
index b16262b..2107c06 100644
--- a/worktree.c
+++ b/worktree.c
@@ -5,8 +5,6 @@
 #include "dir.h"
 #include "wt-status.h"
 
-static const char *lock_field_uninitialized = "value is not important";
-
 void free_worktrees(struct worktree **worktrees)
 {
 	int i = 0;
@@ -15,8 +13,7 @@ void free_worktrees(struct worktree **worktrees)
 		free(worktrees[i]->path);
 		free(worktrees[i]->id);
 		free(worktrees[i]->head_ref);
-		if (worktrees[i]->lock_reason != lock_field_uninitialized)
-			free(worktrees[i]->lock_reason);
+		free(worktrees[i]->lock_reason);
 		free(worktrees[i]);
 	}
 	free (worktrees);
@@ -102,7 +99,8 @@ static struct worktree *get_main_worktree(void)
 	worktree->is_detached = is_detached;
 	worktree->is_current = 0;
 	add_head_info(&head_ref, worktree);
-	worktree->lock_reason = (char *)lock_field_uninitialized;
+	worktree->lock_reason = NULL;
+	worktree->lock_reason_valid = 0;
 
 done:
 	strbuf_release(&path);
@@ -148,7 +146,8 @@ static struct worktree *get_linked_worktree(const char *id)
 	worktree->is_detached = is_detached;
 	worktree->is_current = 0;
 	add_head_info(&head_ref, worktree);
-	worktree->lock_reason = (char *)lock_field_uninitialized;
+	worktree->lock_reason = NULL;
+	worktree->lock_reason_valid = 0;
 
 done:
 	strbuf_release(&path);
@@ -271,7 +270,9 @@ int is_main_worktree(const struct worktree *wt)
 
 const char *is_worktree_locked(struct worktree *wt)
 {
-	if (wt->lock_reason == lock_field_uninitialized) {
+	assert(!is_main_worktree(wt));
+
+	if (!wt->lock_reason_valid) {
 		struct strbuf path = STRBUF_INIT;
 
 		strbuf_addstr(&path, worktree_git_path(wt, "locked"));
@@ -283,6 +284,7 @@ const char *is_worktree_locked(struct worktree *wt)
 			wt->lock_reason = strbuf_detach(&lock_reason, NULL);
 		} else
 			wt->lock_reason = NULL;
+		wt->lock_reason_valid = 1;
 		strbuf_release(&path);
 	}
 
diff --git a/worktree.h b/worktree.h
index 263b61d..90e1311 100644
--- a/worktree.h
+++ b/worktree.h
@@ -10,6 +10,7 @@ struct worktree {
 	int is_detached;
 	int is_bare;
 	int is_current;
+	int lock_reason_valid;
 };
 
 /* Functions for acting on the information about worktrees. */
-- >8 --

Nguyễn Thái Ngọc Duy (6):
  worktree.c: add find_worktree()
  worktree.c: add is_main_worktree()
  worktree.c: add is_worktree_locked()
  worktree: add "lock" command
  worktree: add "unlock" command
  worktree.c: find_worktree() search by path suffix

 Documentation/git-worktree.txt         | 36 +++++++++++++---
 builtin/worktree.c                     | 66 +++++++++++++++++++++++++++++
 contrib/completion/git-completion.bash |  5 ++-
 t/t2028-worktree-move.sh (new +x)      | 62 +++++++++++++++++++++++++++
 worktree.c                             | 77 ++++++++++++++++++++++++++++++++++
 worktree.h                             | 21 ++++++++++
 6 files changed, 260 insertions(+), 7 deletions(-)
 create mode 100755 t/t2028-worktree-move.sh

-- 
2.8.2.524.g6ff3d78