diff options
author | Junio C Hamano <gitster@pobox.com> | 2016-05-02 14:24:05 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2016-05-02 14:24:05 -0700 |
commit | 3c383a30c854232b2a678110dd191b639a8b7989 (patch) | |
tree | 2cb164e470ca962978422638f86e42899c4fb23b /refs | |
parent | Merge branch 'ky/branch-d-worktree' into maint (diff) | |
parent | set_worktree_head_symref(): fix error message (diff) | |
download | tgif-3c383a30c854232b2a678110dd191b639a8b7989.tar.xz |
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
Diffstat (limited to 'refs')
-rw-r--r-- | refs/files-backend.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/refs/files-backend.c b/refs/files-backend.c index 81f68f846b..ea78ce9d90 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -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; |