worktree.c: rewrite mark_current_worktree() to avoid strbuf
[gitweb.git] / branch.c
index 713ceda36a900b88435528bff7d2d423c89bb00b..a5a8dcbd0ed929d09a73674361ee1dd81dd5b88c 100644 (file)
--- a/branch.c
+++ b/branch.c
@@ -70,18 +70,18 @@ int install_branch_config(int flag, const char *local, const char *origin, const
        }
 
        strbuf_addf(&key, "branch.%s.remote", local);
-       if (git_config_set(key.buf, origin ? origin : ".") < 0)
+       if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
                goto out_err;
 
        strbuf_reset(&key);
        strbuf_addf(&key, "branch.%s.merge", local);
-       if (git_config_set(key.buf, remote) < 0)
+       if (git_config_set_gently(key.buf, remote) < 0)
                goto out_err;
 
        if (rebasing) {
                strbuf_reset(&key);
                strbuf_addf(&key, "branch.%s.rebase", local);
-               if (git_config_set(key.buf, "true") < 0)
+               if (git_config_set_gently(key.buf, "true") < 0)
                        goto out_err;
        }
        strbuf_release(&key);
@@ -334,13 +334,38 @@ void remove_branch_state(void)
        unlink(git_path_squash_msg());
 }
 
-void die_if_checked_out(const char *branch)
+void die_if_checked_out(const char *branch, int ignore_current_worktree)
 {
-       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 || (ignore_current_worktree && wt->is_current))
+               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;
 }