Skip to content

Commit d026a25

Browse files
pcloudsgitster
authored andcommitted
refs: kill set_worktree_head_symref()
70999e9 (branch -m: update all per-worktree HEADs - 2016-03-27) added this function in order to update HEADs of all relevant worktrees, when a branch is renamed. It, as a public ref api, kind of breaks abstraction when it uses internal functions of files backend. With the introduction of refs_create_symref(), we can move back pretty close to the code before 70999e9, where create_symref() was used for updating HEAD. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fa099d2 commit d026a25

File tree

4 files changed

+18
-60
lines changed

4 files changed

+18
-60
lines changed

branch.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,18 +353,18 @@ int replace_each_worktree_head_symref(const char *oldref, const char *newref,
353353
int i;
354354

355355
for (i = 0; worktrees[i]; i++) {
356+
struct ref_store *refs;
357+
356358
if (worktrees[i]->is_detached)
357359
continue;
358360
if (worktrees[i]->head_ref &&
359361
strcmp(oldref, worktrees[i]->head_ref))
360362
continue;
361363

362-
if (set_worktree_head_symref(get_worktree_git_dir(worktrees[i]),
363-
newref, logmsg)) {
364-
ret = -1;
365-
error(_("HEAD of working tree %s is not updated"),
366-
worktrees[i]->path);
367-
}
364+
refs = get_worktree_ref_store(worktrees[i]);
365+
if (refs_create_symref(refs, "HEAD", newref, logmsg))
366+
ret = error(_("HEAD of working tree %s is not updated"),
367+
worktrees[i]->path);
368368
}
369369

370370
free_worktrees(worktrees);

refs.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -402,16 +402,6 @@ int refs_create_symref(struct ref_store *refs, const char *refname,
402402
const char *target, const char *logmsg);
403403
int create_symref(const char *refname, const char *target, const char *logmsg);
404404

405-
/*
406-
* Update HEAD of the specified gitdir.
407-
* Similar to create_symref("relative-git-dir/HEAD", target, NULL), but
408-
* this can update the main working tree's HEAD regardless of where
409-
* $GIT_DIR points to.
410-
* Return 0 if successful, non-zero otherwise.
411-
* */
412-
int set_worktree_head_symref(const char *gitdir, const char *target,
413-
const char *logmsg);
414-
415405
enum action_on_err {
416406
UPDATE_REFS_MSG_ON_ERR,
417407
UPDATE_REFS_DIE_ON_ERR,

refs/files-backend.c

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,50 +3160,6 @@ static int files_create_symref(struct ref_store *ref_store,
31603160
return ret;
31613161
}
31623162

3163-
int set_worktree_head_symref(const char *gitdir, const char *target, const char *logmsg)
3164-
{
3165-
/*
3166-
* FIXME: this obviously will not work well for future refs
3167-
* backends. This function needs to die.
3168-
*/
3169-
struct files_ref_store *refs =
3170-
files_downcast(get_main_ref_store(),
3171-
REF_STORE_WRITE,
3172-
"set_head_symref");
3173-
3174-
static struct lock_file head_lock;
3175-
struct ref_lock *lock;
3176-
struct strbuf head_path = STRBUF_INIT;
3177-
const char *head_rel;
3178-
int ret;
3179-
3180-
strbuf_addf(&head_path, "%s/HEAD", absolute_path(gitdir));
3181-
if (hold_lock_file_for_update(&head_lock, head_path.buf,
3182-
LOCK_NO_DEREF) < 0) {
3183-
struct strbuf err = STRBUF_INIT;
3184-
unable_to_lock_message(head_path.buf, errno, &err);
3185-
error("%s", err.buf);
3186-
strbuf_release(&err);
3187-
strbuf_release(&head_path);
3188-
return -1;
3189-
}
3190-
3191-
/* head_rel will be "HEAD" for the main tree, "worktrees/wt/HEAD" for
3192-
linked trees */
3193-
head_rel = remove_leading_path(head_path.buf,
3194-
absolute_path(get_git_common_dir()));
3195-
/* to make use of create_symref_locked(), initialize ref_lock */
3196-
lock = xcalloc(1, sizeof(struct ref_lock));
3197-
lock->lk = &head_lock;
3198-
lock->ref_name = xstrdup(head_rel);
3199-
3200-
ret = create_symref_locked(refs, lock, head_rel, target, logmsg);
3201-
3202-
unlock_ref(lock); /* will free lock */
3203-
strbuf_release(&head_path);
3204-
return ret;
3205-
}
3206-
32073163
static int files_reflog_exists(struct ref_store *ref_store,
32083164
const char *refname)
32093165
{

t/t1407-worktree-ref-store.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,16 @@ test_expect_success 'resolve_ref(<per-worktree-ref>)' '
3737
test_cmp expected actual
3838
'
3939

40+
test_expect_success 'create_symref(FOO, refs/heads/master)' '
41+
$RWT create-symref FOO refs/heads/master nothing &&
42+
echo refs/heads/master >expected &&
43+
git -C wt symbolic-ref FOO >actual &&
44+
test_cmp expected actual &&
45+
46+
$RMAIN create-symref FOO refs/heads/wt-master nothing &&
47+
echo refs/heads/wt-master >expected &&
48+
git symbolic-ref FOO >actual &&
49+
test_cmp expected actual
50+
'
51+
4052
test_done

0 commit comments

Comments
 (0)