Skip to content

Commit 645f631

Browse files
committed
Merge branch 'es/get-worktrees-unsort'
API cleanup for get_worktrees() * es/get-worktrees-unsort: worktree: drop get_worktrees() unused 'flags' argument worktree: drop get_worktrees() special-purpose sorting option
2 parents e7e113a + 03f2465 commit 645f631

File tree

11 files changed

+41
-40
lines changed

11 files changed

+41
-40
lines changed

branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ int replace_each_worktree_head_symref(const char *oldref, const char *newref,
370370
const char *logmsg)
371371
{
372372
int ret = 0;
373-
struct worktree **worktrees = get_worktrees(0);
373+
struct worktree **worktrees = get_worktrees();
374374
int i;
375375

376376
for (i = 0; worktrees[i]; i++) {

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ static void print_current_branch_name(void)
468468

469469
static void reject_rebase_or_bisect_branch(const char *target)
470470
{
471-
struct worktree **worktrees = get_worktrees(0);
471+
struct worktree **worktrees = get_worktrees();
472472
int i;
473473

474474
for (i = 0; worktrees[i]; i++) {

builtin/config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
672672
given_config_source.file = git_pathdup("config");
673673
given_config_source.scope = CONFIG_SCOPE_LOCAL;
674674
} else if (use_worktree_config) {
675-
struct worktree **worktrees = get_worktrees(0);
675+
struct worktree **worktrees = get_worktrees();
676676
if (repository_format_worktree_config)
677677
given_config_source.file = git_pathdup("config.worktree");
678678
else if (worktrees[0] && worktrees[1])

builtin/fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ static void get_default_heads(void)
577577

578578
for_each_rawref(fsck_handle_ref, NULL);
579579

580-
worktrees = get_worktrees(0);
580+
worktrees = get_worktrees();
581581
for (p = worktrees; *p; p++) {
582582
struct worktree *wt = *p;
583583
struct strbuf ref = STRBUF_INIT;

builtin/reflog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
615615
int i;
616616

617617
memset(&collected, 0, sizeof(collected));
618-
worktrees = get_worktrees(0);
618+
worktrees = get_worktrees();
619619
for (p = worktrees; *p; p++) {
620620
if (!all_worktrees && !(*p)->is_current)
621621
continue;

builtin/worktree.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ static int add_worktree(const char *path, const char *refname,
325325
struct strbuf sb_name = STRBUF_INIT;
326326
struct worktree **worktrees;
327327

328-
worktrees = get_worktrees(0);
328+
worktrees = get_worktrees();
329329
check_candidate_path(path, opts->force, worktrees, "add");
330330
free_worktrees(worktrees);
331331
worktrees = NULL;
@@ -697,6 +697,23 @@ static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
697697
}
698698
}
699699

700+
static int pathcmp(const void *a_, const void *b_)
701+
{
702+
const struct worktree *const *a = a_;
703+
const struct worktree *const *b = b_;
704+
return fspathcmp((*a)->path, (*b)->path);
705+
}
706+
707+
static void pathsort(struct worktree **wt)
708+
{
709+
int n = 0;
710+
struct worktree **p = wt;
711+
712+
while (*p++)
713+
n++;
714+
QSORT(wt, n, pathcmp);
715+
}
716+
700717
static int list(int ac, const char **av, const char *prefix)
701718
{
702719
int porcelain = 0;
@@ -710,9 +727,12 @@ static int list(int ac, const char **av, const char *prefix)
710727
if (ac)
711728
usage_with_options(worktree_usage, options);
712729
else {
713-
struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
730+
struct worktree **worktrees = get_worktrees();
714731
int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
715732

733+
/* sort worktrees by path but keep main worktree at top */
734+
pathsort(worktrees + 1);
735+
716736
if (!porcelain)
717737
measure_widths(worktrees, &abbrev, &path_maxlen);
718738

@@ -741,7 +761,7 @@ static int lock_worktree(int ac, const char **av, const char *prefix)
741761
if (ac != 1)
742762
usage_with_options(worktree_usage, options);
743763

744-
worktrees = get_worktrees(0);
764+
worktrees = get_worktrees();
745765
wt = find_worktree(worktrees, prefix, av[0]);
746766
if (!wt)
747767
die(_("'%s' is not a working tree"), av[0]);
@@ -774,7 +794,7 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
774794
if (ac != 1)
775795
usage_with_options(worktree_usage, options);
776796

777-
worktrees = get_worktrees(0);
797+
worktrees = get_worktrees();
778798
wt = find_worktree(worktrees, prefix, av[0]);
779799
if (!wt)
780800
die(_("'%s' is not a working tree"), av[0]);
@@ -848,7 +868,7 @@ static int move_worktree(int ac, const char **av, const char *prefix)
848868
strbuf_addstr(&dst, path);
849869
free(path);
850870

851-
worktrees = get_worktrees(0);
871+
worktrees = get_worktrees();
852872
wt = find_worktree(worktrees, prefix, av[0]);
853873
if (!wt)
854874
die(_("'%s' is not a working tree"), av[0]);
@@ -974,7 +994,7 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
974994
if (ac != 1)
975995
usage_with_options(worktree_usage, options);
976996

977-
worktrees = get_worktrees(0);
997+
worktrees = get_worktrees();
978998
wt = find_worktree(worktrees, prefix, av[0]);
979999
if (!wt)
9801000
die(_("'%s' is not a working tree"), av[0]);

ref-filter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,7 @@ static void lazy_init_worktree_map(void)
15791579
if (ref_to_worktree_map.worktrees)
15801580
return;
15811581

1582-
ref_to_worktree_map.worktrees = get_worktrees(0);
1582+
ref_to_worktree_map.worktrees = get_worktrees();
15831583
hashmap_init(&(ref_to_worktree_map.map), ref_to_worktree_map_cmpfnc, NULL, 0);
15841584
populate_worktree_map(&(ref_to_worktree_map.map), ref_to_worktree_map.worktrees);
15851585
}

revision.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,7 +1609,7 @@ static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
16091609
{
16101610
struct worktree **worktrees, **p;
16111611

1612-
worktrees = get_worktrees(0);
1612+
worktrees = get_worktrees();
16131613
for (p = worktrees; *p; p++) {
16141614
struct worktree *wt = *p;
16151615

@@ -1697,7 +1697,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
16971697
if (revs->single_worktree)
16981698
return;
16991699

1700-
worktrees = get_worktrees(0);
1700+
worktrees = get_worktrees();
17011701
for (p = worktrees; *p; p++) {
17021702
struct worktree *wt = *p;
17031703
struct index_state istate = { NULL };

t/helper/test-ref-store.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static const char **get_store(const char **argv, struct ref_store **refs)
3737

3838
*refs = get_submodule_ref_store(gitdir);
3939
} else if (skip_prefix(argv[0], "worktree:", &gitdir)) {
40-
struct worktree **p, **worktrees = get_worktrees(0);
40+
struct worktree **p, **worktrees = get_worktrees();
4141

4242
for (p = worktrees; *p; p++) {
4343
struct worktree *wt = *p;

worktree.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,7 @@ static void mark_current_worktree(struct worktree **worktrees)
123123
free(git_dir);
124124
}
125125

126-
static int compare_worktree(const void *a_, const void *b_)
127-
{
128-
const struct worktree *const *a = a_;
129-
const struct worktree *const *b = b_;
130-
return fspathcmp((*a)->path, (*b)->path);
131-
}
132-
133-
struct worktree **get_worktrees(unsigned flags)
126+
struct worktree **get_worktrees(void)
134127
{
135128
struct worktree **list = NULL;
136129
struct strbuf path = STRBUF_INIT;
@@ -161,13 +154,6 @@ struct worktree **get_worktrees(unsigned flags)
161154
ALLOC_GROW(list, counter + 1, alloc);
162155
list[counter] = NULL;
163156

164-
if (flags & GWT_SORT_LINKED)
165-
/*
166-
* don't sort the first item (main worktree), which will
167-
* always be the first
168-
*/
169-
QSORT(list + 1, counter - 1, compare_worktree);
170-
171157
mark_current_worktree(list);
172158
return list;
173159
}
@@ -418,7 +404,7 @@ const struct worktree *find_shared_symref(const char *symref,
418404

419405
if (worktrees)
420406
free_worktrees(worktrees);
421-
worktrees = get_worktrees(0);
407+
worktrees = get_worktrees();
422408

423409
for (i = 0; worktrees[i]; i++) {
424410
struct worktree *wt = worktrees[i];
@@ -577,7 +563,7 @@ int other_head_refs(each_ref_fn fn, void *cb_data)
577563
struct worktree **worktrees, **p;
578564
int ret = 0;
579565

580-
worktrees = get_worktrees(0);
566+
worktrees = get_worktrees();
581567
for (p = worktrees; *p; p++) {
582568
struct worktree *wt = *p;
583569
struct object_id oid;

0 commit comments

Comments
 (0)