get_worktrees() must return main worktree as first item even on error

This is required by git-worktree.txt, stating that the main worktree is the first line (especially in --porcelain mode when we can't just change behavior at will). There's only one case when get_worktrees() may skip main worktree, when parse_ref() fails. Update the code so that we keep first item as main worktree and return something sensible in this case: - In user-friendly mode, since we're not constraint by anything, returning "(error)" should do the job (we already show "(detached HEAD)" which is not machine-friendly). Actually errors should be printed on stderr by parse_ref() (*) - In plumbing mode, we do not show neither 'bare', 'detached' or 'branch ...', which is possible by the format description if I read it right. Careful readers may realize that when the local variable "head_ref" in get_main_worktree() is emptied, add_head_info() will do nothing to wt->head_sha1. But that's ok because head_sha1 is zero-ized in the previous patch. (*) Well, it does not. But it's supposed to be a stop gap implementation until we can reuse refs code to parse "ref: " stuff in HEAD, from resolve_refs_unsafe(). Now may be the time since refs refactoring is mostly done. 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 Nov 28, 2016 at 16:36 UTC a234563a3b329c8513fabbe9d8f9435987f10eb6
3 files changed +28 -9
builtin/worktree.c
+4 -2
@@ -388,7 +388,7 @@ static void show_worktree_porcelain(struct worktree *wt)
388 printf("HEAD %s\n", sha1_to_hex(wt->head_sha1));
389 if (wt->is_detached)
390 printf("detached\n");
391 - else
391 + else if (wt->head_ref)
392 printf("branch %s\n", wt->head_ref);
393 }
394 printf("\n");
@@ -408,8 +408,10 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
408 find_unique_abbrev(wt->head_sha1, DEFAULT_ABBREV));
409 if (wt->is_detached)
410 strbuf_addstr(&sb, "(detached HEAD)");
411 - else
411 + else if (wt->head_ref)
412 strbuf_addf(&sb, "[%s]", shorten_unambiguous_ref(wt->head_ref, 0));
413 + else
414 + strbuf_addstr(&sb, "(error)");
415 }
416 printf("%s\n", sb.buf);
417
t/t2027-worktree-list.sh
+21
@@ -96,4 +96,25 @@ test_expect_success 'bare repo cleanup' '
96 rm -rf bare1
97 '
98
99 +test_expect_success 'broken main worktree still at the top' '
100 + git init broken-main &&
101 + (
102 + cd broken-main &&
103 + test_commit new &&
104 + git worktree add linked &&
105 + cat >expected <<-EOF &&
106 + worktree $(pwd)
107 + HEAD $_z40
108 +
109 + EOF
110 + cd linked &&
111 + echo "worktree $(pwd)" >expected &&
112 + echo "ref: .broken" >../.git/HEAD &&
113 + git worktree list --porcelain | head -n 3 >actual &&
114 + test_cmp ../expected actual &&
115 + git worktree list | head -n 1 >actual.2 &&
116 + grep -F "(error)" actual.2
117 + )
118 +'
119 +
120 test_done
worktree.c
+3 -7
@@ -88,16 +88,13 @@ static struct worktree *get_main_worktree(void)
88
89 strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
90
91 - if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
92 - goto done;
93 -
91 worktree = xcalloc(1, sizeof(*worktree));
92 worktree->path = strbuf_detach(&worktree_path, NULL);
93 worktree->is_bare = is_bare;
94 worktree->is_detached = is_detached;
98 - add_head_info(&head_ref, worktree);
95 + if (!parse_ref(path.buf, &head_ref, &is_detached))
96 + add_head_info(&head_ref, worktree);
97
100 -done:
98 strbuf_release(&path);
99 strbuf_release(&worktree_path);
100 strbuf_release(&head_ref);
@@ -173,8 +170,7 @@ struct worktree **get_worktrees(void)
170
171 list = xmalloc(alloc * sizeof(struct worktree *));
172
176 - if ((list[counter] = get_main_worktree()))
177 - counter++;
173 + list[counter++] = get_main_worktree();
174
175 strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
176 dir = opendir(path.buf);