path.c: refactor and add worktree_git_path()
[gitweb.git] / branch.c
index c50ea42172ceadd2a76d12833631301223607067..1f1fbf528c638fc3a0dc9ae1b0f711870cd39af6 100644 (file)
--- a/branch.c
+++ b/branch.c
@@ -336,11 +336,36 @@ void remove_branch_state(void)
 
 void die_if_checked_out(const char *branch)
 {
-       char *existing;
+       const struct worktree *wt;
 
-       existing = find_shared_symref("HEAD", branch);
-       if (existing) {
-               skip_prefix(branch, "refs/heads/", &branch);
-               die(_("'%s' is already checked out at '%s'"), branch, existing);
+       wt = find_shared_symref("HEAD", branch);
+       if (!wt)
+               return;
+       skip_prefix(branch, "refs/heads/", &branch);
+       die(_("'%s' is already checked out at '%s'"),
+           branch, wt->path);
+}
+
+int replace_each_worktree_head_symref(const char *oldref, const char *newref)
+{
+       int ret = 0;
+       struct worktree **worktrees = get_worktrees();
+       int i;
+
+       for (i = 0; worktrees[i]; i++) {
+               if (worktrees[i]->is_detached)
+                       continue;
+               if (strcmp(oldref, worktrees[i]->head_ref))
+                       continue;
+
+               if (set_worktree_head_symref(get_worktree_git_dir(worktrees[i]),
+                                            newref)) {
+                       ret = -1;
+                       error(_("HEAD of working tree %s is not updated"),
+                             worktrees[i]->path);
+               }
        }
+
+       free_worktrees(worktrees);
+       return ret;
 }