Merge branch 'ky/branch-m-worktree' into maint

When "git worktree" feature is in use, "git branch -m" renamed a
branch that is checked out in another worktree without adjusting
the HEAD symbolic ref for the worktree.

* ky/branch-m-worktree:
  set_worktree_head_symref(): fix error message
  branch -m: update all per-worktree HEADs
  refs: add a new function set_worktree_head_symref
This commit is contained in:
Junio C Hamano 2016-05-02 14:24:05 -07:00
Родитель a4127142c6 18eb3a9ce7
Коммит 3c383a30c8
6 изменённых файлов: 98 добавлений и 3 удалений

Просмотреть файл

@ -344,3 +344,26 @@ void die_if_checked_out(const char *branch)
die(_("'%s' is already checked out at '%s'"), branch, existing);
}
}
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(worktrees[i]->git_dir, newref)) {
ret = -1;
error(_("HEAD of working tree %s is not updated"),
worktrees[i]->path);
}
}
free_worktrees(worktrees);
return ret;
}

Просмотреть файл

@ -60,4 +60,11 @@ extern int read_branch_desc(struct strbuf *, const char *branch_name);
*/
extern void die_if_checked_out(const char *branch);
/*
* Update all per-worktree HEADs pointing at the old ref to point the new ref.
* This will be used when renaming a branch. Returns 0 if successful, non-zero
* otherwise.
*/
extern int replace_each_worktree_head_symref(const char *oldref, const char *newref);
#endif

Просмотреть файл

@ -558,8 +558,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 (replace_each_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);

9
refs.h
Просмотреть файл

@ -306,6 +306,15 @@ extern int rename_ref(const char *oldref, const char *newref, const char *logmsg
extern int create_symref(const char *refname, const char *target, const char *logmsg);
/*
* Update HEAD of the specified gitdir.
* Similar to create_symref("relative-git-dir/HEAD", target, NULL), but
* this can update the main working tree's HEAD regardless of where
* $GIT_DIR points to.
* Return 0 if successful, non-zero otherwise.
* */
extern int set_worktree_head_symref(const char *gitdir, const char *target);
enum action_on_err {
UPDATE_REFS_MSG_ON_ERR,
UPDATE_REFS_DIE_ON_ERR,

Просмотреть файл

@ -2894,6 +2894,42 @@ int create_symref(const char *refname, const char *target, const char *logmsg)
return ret;
}
int set_worktree_head_symref(const char *gitdir, const char *target)
{
static struct lock_file head_lock;
struct ref_lock *lock;
struct strbuf head_path = STRBUF_INIT;
const char *head_rel;
int ret;
strbuf_addf(&head_path, "%s/HEAD", absolute_path(gitdir));
if (hold_lock_file_for_update(&head_lock, head_path.buf,
LOCK_NO_DEREF) < 0) {
struct strbuf err = STRBUF_INIT;
unable_to_lock_message(head_path.buf, errno, &err);
error("%s", err.buf);
strbuf_release(&err);
strbuf_release(&head_path);
return -1;
}
/* head_rel will be "HEAD" for the main tree, "worktrees/wt/HEAD" for
linked trees */
head_rel = remove_leading_path(head_path.buf,
absolute_path(get_git_common_dir()));
/* to make use of create_symref_locked(), initialize ref_lock */
lock = xcalloc(1, sizeof(struct ref_lock));
lock->lk = &head_lock;
lock->ref_name = xstrdup(head_rel);
lock->orig_ref_name = xstrdup(head_rel);
ret = create_symref_locked(lock, head_rel, target, NULL);
unlock_ref(lock); /* will free lock */
strbuf_release(&head_path);
return ret;
}
int reflog_exists(const char *refname)
{
struct stat st;

Просмотреть файл

@ -126,7 +126,28 @@ test_expect_success 'git branch -M foo bar should fail when bar is checked out'
test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
git checkout -b baz &&
git branch bam &&
git branch -M baz bam
git branch -M baz bam &&
test $(git rev-parse --abbrev-ref HEAD) = bam
'
test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
git checkout master &&
git worktree add -b baz bazdir &&
git worktree add -f bazdir2 baz &&
git branch -M baz bam &&
test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam
'
test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
git checkout -b baz &&
git worktree add -f bazdir3 baz &&
(
cd bazdir3 &&
git branch -M baz bam &&
test $(git rev-parse --abbrev-ref HEAD) = bam
) &&
test $(git rev-parse --abbrev-ref HEAD) = bam
'
test_expect_success 'git branch -M master should work when master is checked out' '