Skip to content

Instantly share code, notes, and snippets.

@rhenium
Created March 18, 2016 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhenium/0637f67eb91d224c31b2 to your computer and use it in GitHub Desktop.
Save rhenium/0637f67eb91d224c31b2 to your computer and use it in GitHub Desktop.
git branch -m が worktree の HEAD を更新するようになる diff
diff --git a/builtin/branch.c b/builtin/branch.c
index 7b45b6b..07b7afb 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -20,6 +20,7 @@
#include "utf8.h"
#include "wt-status.h"
#include "ref-filter.h"
+#include "worktree.h"
static const char * const builtin_branch_usage[] = {
N_("git branch [<options>] [-r | -a] [--merged | --no-merged]"),
@@ -552,8 +553,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
if (recovery)
warning(_("Renamed a misnamed branch '%s' away"), oldref.buf + 11);
- /* no need to pass logmsg here as HEAD didn't really move */
- if (!strcmp(oldname, head) && create_symref("HEAD", newref.buf, NULL))
+ if (update_all_worktree_head_symref(oldref.buf, newref.buf))
die(_("Branch renamed to %s, but HEAD is not updated!"), newname);
strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11);
diff --git a/worktree.c b/worktree.c
index 6181a66..e712150 100644
--- a/worktree.c
+++ b/worktree.c
@@ -217,3 +217,40 @@ char *find_shared_symref(const char *symref, const char *target)
return existing;
}
+
+int update_all_worktree_head_symref(const char *oldref, const char *newref)
+{
+ int error = 0;
+ struct strbuf path = STRBUF_INIT;
+ struct strbuf ref_target = STRBUF_INIT;
+ struct worktree **worktrees = get_worktrees();
+ int i;
+
+ for (i = 0; worktrees[i]; i++) {
+ if (worktrees[i]->is_detached)
+ continue;
+
+ strbuf_reset(&path);
+ strbuf_reset(&ref_target);
+ strbuf_addf(&path, "%s/HEAD", worktrees[i]->git_dir);
+
+ if (parse_ref(path.buf, &ref_target, NULL))
+ continue;
+
+ if (!strcmp(ref_target.buf, oldref)) {
+ const char *symref = path.buf + strlen(absolute_path(get_git_common_dir())) + 1;
+
+ /* no need to pass logmsg here as HEAD didn't really move */
+ if (create_symref(symref, newref, NULL)) {
+ error = -1;
+ break;
+ }
+ }
+ }
+
+ strbuf_release(&path);
+ strbuf_release(&ref_target);
+ free_worktrees(worktrees);
+
+ return error;
+}
diff --git a/worktree.h b/worktree.h
index b4b3dda..512855b 100644
--- a/worktree.h
+++ b/worktree.h
@@ -35,4 +35,9 @@ extern void free_worktrees(struct worktree **);
*/
extern char *find_shared_symref(const char *symref, const char *target);
+/*
+ * TODO
+ */
+extern int update_all_worktree_head_symref(const char *oldref, const char *newref);
+
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment