Merge branch 'nd/worktree-list-fixup' into maint

The output from "git worktree list" was made in readdir() order,
and was unstable.

* nd/worktree-list-fixup:
  worktree list: keep the list sorted
  worktree.c: get_worktrees() takes a new flag argument
  get_worktrees() must return main worktree as first item even on error
  worktree: reorder an if statement
  worktree.c: zero new 'struct worktree' on allocation
This commit is contained in:
Junio C Hamano 2017-01-17 14:49:27 -08:00
Родитель 3075e40c75 4df1d4d466
Коммит 430fd1cae5
6 изменённых файлов: 74 добавлений и 30 удалений

Просмотреть файл

@ -348,7 +348,7 @@ void die_if_checked_out(const char *branch, int ignore_current_worktree)
int replace_each_worktree_head_symref(const char *oldref, const char *newref)
{
int ret = 0;
struct worktree **worktrees = get_worktrees();
struct worktree **worktrees = get_worktrees(0);
int i;
for (i = 0; worktrees[i]; i++) {

Просмотреть файл

@ -531,7 +531,7 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
static void reject_rebase_or_bisect_branch(const char *target)
{
struct worktree **worktrees = get_worktrees();
struct worktree **worktrees = get_worktrees(0);
int i;
for (i = 0; worktrees[i]; i++) {

Просмотреть файл

@ -388,7 +388,7 @@ static void show_worktree_porcelain(struct worktree *wt)
printf("HEAD %s\n", sha1_to_hex(wt->head_sha1));
if (wt->is_detached)
printf("detached\n");
else
else if (wt->head_ref)
printf("branch %s\n", wt->head_ref);
}
printf("\n");
@ -406,10 +406,12 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
else {
strbuf_addf(&sb, "%-*s ", abbrev_len,
find_unique_abbrev(wt->head_sha1, DEFAULT_ABBREV));
if (!wt->is_detached)
if (wt->is_detached)
strbuf_addstr(&sb, "(detached HEAD)");
else if (wt->head_ref)
strbuf_addf(&sb, "[%s]", shorten_unambiguous_ref(wt->head_ref, 0));
else
strbuf_addstr(&sb, "(detached HEAD)");
strbuf_addstr(&sb, "(error)");
}
printf("%s\n", sb.buf);
@ -445,7 +447,7 @@ static int list(int ac, const char **av, const char *prefix)
if (ac)
usage_with_options(worktree_usage, options);
else {
struct worktree **worktrees = get_worktrees();
struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
if (!porcelain)
@ -476,7 +478,7 @@ static int lock_worktree(int ac, const char **av, const char *prefix)
if (ac != 1)
usage_with_options(worktree_usage, options);
worktrees = get_worktrees();
worktrees = get_worktrees(0);
wt = find_worktree(worktrees, prefix, av[0]);
if (!wt)
die(_("'%s' is not a working tree"), av[0]);
@ -509,7 +511,7 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
if (ac != 1)
usage_with_options(worktree_usage, options);
worktrees = get_worktrees();
worktrees = get_worktrees(0);
wt = find_worktree(worktrees, prefix, av[0]);
if (!wt)
die(_("'%s' is not a working tree"), av[0]);

Просмотреть файл

@ -96,4 +96,44 @@ test_expect_success 'bare repo cleanup' '
rm -rf bare1
'
test_expect_success 'broken main worktree still at the top' '
git init broken-main &&
(
cd broken-main &&
test_commit new &&
git worktree add linked &&
cat >expected <<-EOF &&
worktree $(pwd)
HEAD $_z40
EOF
cd linked &&
echo "worktree $(pwd)" >expected &&
echo "ref: .broken" >../.git/HEAD &&
git worktree list --porcelain | head -n 3 >actual &&
test_cmp ../expected actual &&
git worktree list | head -n 1 >actual.2 &&
grep -F "(error)" actual.2
)
'
test_expect_success 'linked worktrees are sorted' '
mkdir sorted &&
git init sorted/main &&
(
cd sorted/main &&
test_tick &&
test_commit new &&
git worktree add ../first &&
git worktree add ../second &&
git worktree list --porcelain | grep ^worktree >actual
) &&
cat >expected <<-EOF &&
worktree $(pwd)/sorted/main
worktree $(pwd)/sorted/first
worktree $(pwd)/sorted/second
EOF
test_cmp expected sorted/main/actual
'
test_done

Просмотреть файл

@ -88,21 +88,13 @@ static struct worktree *get_main_worktree(void)
strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
goto done;
worktree = xmalloc(sizeof(struct worktree));
worktree = xcalloc(1, sizeof(*worktree));
worktree->path = strbuf_detach(&worktree_path, NULL);
worktree->id = NULL;
worktree->is_bare = is_bare;
worktree->head_ref = NULL;
worktree->is_detached = is_detached;
worktree->is_current = 0;
add_head_info(&head_ref, worktree);
worktree->lock_reason = NULL;
worktree->lock_reason_valid = 0;
if (!parse_ref(path.buf, &head_ref, &is_detached))
add_head_info(&head_ref, worktree);
done:
strbuf_release(&path);
strbuf_release(&worktree_path);
strbuf_release(&head_ref);
@ -138,16 +130,11 @@ static struct worktree *get_linked_worktree(const char *id)
if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
goto done;
worktree = xmalloc(sizeof(struct worktree));
worktree = xcalloc(1, sizeof(*worktree));
worktree->path = strbuf_detach(&worktree_path, NULL);
worktree->id = xstrdup(id);
worktree->is_bare = 0;
worktree->head_ref = NULL;
worktree->is_detached = is_detached;
worktree->is_current = 0;
add_head_info(&head_ref, worktree);
worktree->lock_reason = NULL;
worktree->lock_reason_valid = 0;
done:
strbuf_release(&path);
@ -173,7 +160,14 @@ static void mark_current_worktree(struct worktree **worktrees)
free(git_dir);
}
struct worktree **get_worktrees(void)
static int compare_worktree(const void *a_, const void *b_)
{
const struct worktree *const *a = a_;
const struct worktree *const *b = b_;
return fspathcmp((*a)->path, (*b)->path);
}
struct worktree **get_worktrees(unsigned flags)
{
struct worktree **list = NULL;
struct strbuf path = STRBUF_INIT;
@ -183,8 +177,7 @@ struct worktree **get_worktrees(void)
list = xmalloc(alloc * sizeof(struct worktree *));
if ((list[counter] = get_main_worktree()))
counter++;
list[counter++] = get_main_worktree();
strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
dir = opendir(path.buf);
@ -205,6 +198,13 @@ struct worktree **get_worktrees(void)
ALLOC_GROW(list, counter + 1, alloc);
list[counter] = NULL;
if (flags & GWT_SORT_LINKED)
/*
* don't sort the first item (main worktree), which will
* always be the first
*/
QSORT(list + 1, counter - 1, compare_worktree);
mark_current_worktree(list);
return list;
}
@ -341,7 +341,7 @@ const struct worktree *find_shared_symref(const char *symref,
if (worktrees)
free_worktrees(worktrees);
worktrees = get_worktrees();
worktrees = get_worktrees(0);
for (i = 0; worktrees[i]; i++) {
struct worktree *wt = worktrees[i];

Просмотреть файл

@ -15,6 +15,8 @@ struct worktree {
/* Functions for acting on the information about worktrees. */
#define GWT_SORT_LINKED (1 << 0) /* keeps linked worktrees sorted */
/*
* Get the worktrees. The primary worktree will always be the first returned,
* and linked worktrees will be pointed to by 'next' in each subsequent
@ -23,7 +25,7 @@ struct worktree {
* The caller is responsible for freeing the memory from the returned
* worktree(s).
*/
extern struct worktree **get_worktrees(void);
extern struct worktree **get_worktrees(unsigned flags);
/*
* Return git dir of the worktree. Note that the path may be relative.