worktree.c: kill parse_ref() in favor of refs_resolve_ref_unsafe()
The manual parsing code is replaced with a call to refs_resolve_ref_unsafe(). The manual parsing code must die because only refs/files-backend.c should do that. 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
fa099d23227f88b5a1cd79c646551130d9b36e6d
5 files changed
+88
-77
branch.c
+2
-1
@@ -355,7 +355,8 @@ int replace_each_worktree_head_symref(const char *oldref, const char *newref,
355
for (i = 0; worktrees[i]; i++) {
356
if (worktrees[i]->is_detached)
357
continue;
358
- if (strcmp(oldref, worktrees[i]->head_ref))
358
+ if (worktrees[i]->head_ref &&
359
+ strcmp(oldref, worktrees[i]->head_ref))
360
continue;
361
362
if (set_worktree_head_symref(get_worktree_git_dir(worktrees[i]),
t/helper/test-ref-store.c
+18
@@ -1,5 +1,6 @@
1
#include "cache.h"
2
#include "refs.h"
3
+#include "worktree.h"
4
5
static const char *notnull(const char *arg, const char *name)
6
{
@@ -32,6 +33,23 @@ static const char **get_store(const char **argv, struct ref_store **refs)
33
strbuf_release(&sb);
34
35
*refs = get_submodule_ref_store(gitdir);
36
+ } else if (skip_prefix(argv[0], "worktree:", &gitdir)) {
37
+ struct worktree **p, **worktrees = get_worktrees(0);
38
+
39
+ for (p = worktrees; *p; p++) {
40
+ struct worktree *wt = *p;
41
+
42
+ if (!wt->id) {
43
+ /* special case for main worktree */
44
+ if (!strcmp(gitdir, "main"))
45
+ break;
46
+ } else if (!strcmp(gitdir, wt->id))
47
+ break;
48
+ }
49
+ if (!*p)
50
+ die("no such worktree: %s", gitdir);
51
+
52
+ *refs = get_worktree_ref_store(*p);
53
} else
54
die("unknown backend %s", argv[0]);
55
t/t1407-worktree-ref-store.sh
new
+40
@@ -0,0 +1,40 @@
1
+#!/bin/sh
2
+
3
+test_description='test worktree ref store api'
4
+
5
+. ./test-lib.sh
6
+
7
+RWT="test-ref-store worktree:wt"
8
+RMAIN="test-ref-store worktree:main"
9
+
10
+test_expect_success 'setup' '
11
+ test_commit first &&
12
+ git worktree add -b wt-master wt &&
13
+ (
14
+ cd wt &&
15
+ test_commit second
16
+ )
17
+'
18
+
19
+test_expect_success 'resolve_ref(<shared-ref>)' '
20
+ SHA1=`git rev-parse master` &&
21
+ echo "$SHA1 refs/heads/master 0x0" >expected &&
22
+ $RWT resolve-ref refs/heads/master 0 >actual &&
23
+ test_cmp expected actual &&
24
+ $RMAIN resolve-ref refs/heads/master 0 >actual &&
25
+ test_cmp expected actual
26
+'
27
+
28
+test_expect_success 'resolve_ref(<per-worktree-ref>)' '
29
+ SHA1=`git -C wt rev-parse HEAD` &&
30
+ echo "$SHA1 refs/heads/wt-master 0x1" >expected &&
31
+ $RWT resolve-ref HEAD 0 >actual &&
32
+ test_cmp expected actual &&
33
+
34
+ SHA1=`git rev-parse HEAD` &&
35
+ echo "$SHA1 refs/heads/master 0x1" >expected &&
36
+ $RMAIN resolve-ref HEAD 0 >actual &&
37
+ test_cmp expected actual
38
+'
39
+
40
+test_done
worktree.c
+27
-75
@@ -19,54 +19,25 @@ void free_worktrees(struct worktree **worktrees)
19
free (worktrees);
20
}
21
22
-/*
23
- * read 'path_to_ref' into 'ref'. Also if is_detached is not NULL,
24
- * set is_detached to 1 (0) if the ref is detached (is not detached).
25
- *
26
- * $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside $GIT_DIR so
27
- * for linked worktrees, `resolve_ref_unsafe()` won't work (it uses
28
- * git_path). Parse the ref ourselves.
29
- *
30
- * return -1 if the ref is not a proper ref, 0 otherwise (success)
31
- */
32
-static int parse_ref(char *path_to_ref, struct strbuf *ref, int *is_detached)
33
-{
34
- if (is_detached)
35
- *is_detached = 0;
36
- if (!strbuf_readlink(ref, path_to_ref, 0)) {
37
- /* HEAD is symbolic link */
38
- if (!starts_with(ref->buf, "refs/") ||
39
- check_refname_format(ref->buf, 0))
40
- return -1;
41
- } else if (strbuf_read_file(ref, path_to_ref, 0) >= 0) {
42
- /* textual symref or detached */
43
- if (!starts_with(ref->buf, "ref:")) {
44
- if (is_detached)
45
- *is_detached = 1;
46
- } else {
47
- strbuf_remove(ref, 0, strlen("ref:"));
48
- strbuf_trim(ref);
49
- if (check_refname_format(ref->buf, 0))
50
- return -1;
51
- }
52
- } else
53
- return -1;
54
- return 0;
55
-}
56
-
22
/**
58
- * Add the head_sha1 and head_ref (if not detached) to the given worktree
23
+ * Update head_sha1, head_ref and is_detached of the given worktree
24
*/
60
-static void add_head_info(struct strbuf *head_ref, struct worktree *worktree)
25
+static void add_head_info(struct worktree *wt)
26
{
62
- if (head_ref->len) {
63
- if (worktree->is_detached) {
64
- get_sha1_hex(head_ref->buf, worktree->head_sha1);
65
- } else {
66
- resolve_ref_unsafe(head_ref->buf, 0, worktree->head_sha1, NULL);
67
- worktree->head_ref = strbuf_detach(head_ref, NULL);
68
- }
69
- }
27
+ int flags;
28
+ const char *target;
29
+
30
+ target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
31
+ "HEAD",
32
+ RESOLVE_REF_READING,
33
+ wt->head_sha1, &flags);
34
+ if (!target)
35
+ return;
36
+
37
+ if (flags & REF_ISSYMREF)
38
+ wt->head_ref = xstrdup(target);
39
+ else
40
+ wt->is_detached = 1;
41
}
42
43
/**
@@ -77,9 +48,7 @@ static struct worktree *get_main_worktree(void)
48
struct worktree *worktree = NULL;
49
struct strbuf path = STRBUF_INIT;
50
struct strbuf worktree_path = STRBUF_INIT;
80
- struct strbuf head_ref = STRBUF_INIT;
51
int is_bare = 0;
82
- int is_detached = 0;
52
53
strbuf_add_absolute_path(&worktree_path, get_git_common_dir());
54
is_bare = !strbuf_strip_suffix(&worktree_path, "/.git");
@@ -91,13 +60,10 @@ static struct worktree *get_main_worktree(void)
60
worktree = xcalloc(1, sizeof(*worktree));
61
worktree->path = strbuf_detach(&worktree_path, NULL);
62
worktree->is_bare = is_bare;
94
- worktree->is_detached = is_detached;
95
- if (!parse_ref(path.buf, &head_ref, &is_detached))
96
- add_head_info(&head_ref, worktree);
63
+ add_head_info(worktree);
64
65
strbuf_release(&path);
66
strbuf_release(&worktree_path);
100
- strbuf_release(&head_ref);
67
return worktree;
68
}
69
@@ -106,8 +72,6 @@ static struct worktree *get_linked_worktree(const char *id)
72
struct worktree *worktree = NULL;
73
struct strbuf path = STRBUF_INIT;
74
struct strbuf worktree_path = STRBUF_INIT;
109
- struct strbuf head_ref = STRBUF_INIT;
110
- int is_detached = 0;
75
76
if (!id)
77
die("Missing linked worktree name");
@@ -127,19 +91,14 @@ static struct worktree *get_linked_worktree(const char *id)
91
strbuf_reset(&path);
92
strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id);
93
130
- if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
131
- goto done;
132
-
94
worktree = xcalloc(1, sizeof(*worktree));
95
worktree->path = strbuf_detach(&worktree_path, NULL);
96
worktree->id = xstrdup(id);
136
- worktree->is_detached = is_detached;
137
- add_head_info(&head_ref, worktree);
97
+ add_head_info(worktree);
98
99
done:
100
strbuf_release(&path);
101
strbuf_release(&worktree_path);
142
- strbuf_release(&head_ref);
102
return worktree;
103
}
104
@@ -334,8 +293,6 @@ const struct worktree *find_shared_symref(const char *symref,
293
const char *target)
294
{
295
const struct worktree *existing = NULL;
337
- struct strbuf path = STRBUF_INIT;
338
- struct strbuf sb = STRBUF_INIT;
296
static struct worktree **worktrees;
297
int i = 0;
298
@@ -345,6 +302,11 @@ const struct worktree *find_shared_symref(const char *symref,
302
303
for (i = 0; worktrees[i]; i++) {
304
struct worktree *wt = worktrees[i];
305
+ const char *symref_target;
306
+ unsigned char sha1[20];
307
+ struct ref_store *refs;
308
+ int flags;
309
+
310
if (wt->is_bare)
311
continue;
312
@@ -359,25 +321,15 @@ const struct worktree *find_shared_symref(const char *symref,
321
}
322
}
323
362
- strbuf_reset(&path);
363
- strbuf_reset(&sb);
364
- strbuf_addf(&path, "%s/%s",
365
- get_worktree_git_dir(wt),
366
- symref);
367
-
368
- if (parse_ref(path.buf, &sb, NULL)) {
369
- continue;
370
- }
371
-
372
- if (!strcmp(sb.buf, target)) {
324
+ refs = get_worktree_ref_store(wt);
325
+ symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
326
+ sha1, &flags);
327
+ if ((flags & REF_ISSYMREF) && !strcmp(symref_target, target)) {
328
existing = wt;
329
break;
330
}
331
}
332
378
- strbuf_release(&path);
379
- strbuf_release(&sb);
380
-
333
return existing;
334
}
335
worktree.h
+1
-1
@@ -4,7 +4,7 @@
4
struct worktree {
5
char *path;
6
char *id;
7
- char *head_ref;
7
+ char *head_ref; /* NULL if HEAD is broken or detached */
8
char *lock_reason; /* internal use */
9
unsigned char head_sha1[20];
10
int is_detached;