Subject: [PATCHv6 5/7] worktree: add function to check if worktrees are in use

From: Stefan Beller <[email-masked]>
Date: Wed, 7 Dec 2016 17:46:21 -0800

Signed-off-by: Stefan Beller <[email-masked]>
---
 worktree.c | 24 ++++++++++++++++++++++++
 worktree.h |  7 +++++++
 2 files changed, 31 insertions(+)

diff --git a/worktree.c b/worktree.c
index 75db689672..2559f33846 100644
--- a/worktree.c
+++ b/worktree.c
@@ -406,3 +406,27 @@ const struct worktree *find_shared_symref(const char *symref,
 
 	return existing;
 }
+
+static int uses_worktree_internal(struct worktree **worktrees)
+{
+	int i;
+	for (i = 0; worktrees[i]; i++)
+		; /* nothing */
+
+	free_worktrees(worktrees);
+	return i > 1;
+}
+
+int uses_worktrees(void)
+{
+	return uses_worktree_internal(get_worktrees(0));
+}
+
+int submodule_uses_worktrees(const char *path)
+{
+	struct worktree **worktrees = get_submodule_worktrees(path, 0);
+	if (!worktrees)
+		return 0;
+
+	return uses_worktree_internal(worktrees);
+}
diff --git a/worktree.h b/worktree.h
index 157fbc4a66..76027b1fd2 100644
--- a/worktree.h
+++ b/worktree.h
@@ -33,6 +33,13 @@ extern struct worktree **get_worktrees(unsigned flags);
 extern struct worktree **get_submodule_worktrees(const char *path,
 						 unsigned flags);
 
+/*
+ * Returns 1 if more than one worktree exists.
+ * Returns 0 if only the main worktree exists.
+ */
+extern int uses_worktrees(void);
+extern int submodule_uses_worktrees(const char *path);
+
 /*
  * Return git dir of the worktree. Note that the path may be relative.
  * If wt is NULL, git dir of current worktree is returned.
-- 
2.11.0.rc2.30.gc512cbd.dirty