refs: kill set_worktree_head_symref()
70999e9cec (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 70999e9cec, where create_symref() was used for updating HEAD. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Apr 24, 2017 at 17:01 UTC
d026a25657cbe15ceb6bcb5d5047a36a0a70b33e
4 files changed
+18
-60
branch.c
+6
-6
@@ -353,18 +353,18 @@ int replace_each_worktree_head_symref(const char *oldref, const char *newref,
353
int i;
354
355
for (i = 0; worktrees[i]; i++) {
356
+ struct ref_store *refs;
357
+
358
if (worktrees[i]->is_detached)
359
continue;
360
if (worktrees[i]->head_ref &&
361
strcmp(oldref, worktrees[i]->head_ref))
362
continue;
363
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);
368
}
369
370
free_worktrees(worktrees);
refs.h
-10
@@ -402,16 +402,6 @@ int refs_create_symref(struct ref_store *refs, const char *refname,
402
const char *target, const char *logmsg);
403
int create_symref(const char *refname, const char *target, const char *logmsg);
404
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
-
405
enum action_on_err {
406
UPDATE_REFS_MSG_ON_ERR,
407
UPDATE_REFS_DIE_ON_ERR,
refs/files-backend.c
-44
@@ -3160,50 +3160,6 @@ static int files_create_symref(struct ref_store *ref_store,
3160
return ret;
3161
}
3162
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
-
3163
static int files_reflog_exists(struct ref_store *ref_store,
3164
const char *refname)
3165
{
t/t1407-worktree-ref-store.sh
+12
@@ -37,4 +37,16 @@ test_expect_success 'resolve_ref(<per-worktree-ref>)' '
37
test_cmp expected actual
38
'
39
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
+
52
test_done