Browse Source

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
maint
Junio C Hamano 5 years ago
parent
commit
645f63111b
  1. 2
      branch.c
  2. 2
      builtin/branch.c
  3. 2
      builtin/config.c
  4. 2
      builtin/fsck.c
  5. 2
      builtin/reflog.c
  6. 32
      builtin/worktree.c
  7. 2
      ref-filter.c
  8. 4
      revision.c
  9. 2
      t/helper/test-ref-store.c
  10. 20
      worktree.c
  11. 11
      worktree.h

2
branch.c

@ -370,7 +370,7 @@ int replace_each_worktree_head_symref(const char *oldref, const char *newref, @@ -370,7 +370,7 @@ int replace_each_worktree_head_symref(const char *oldref, const char *newref,
const char *logmsg)
{
int ret = 0;
struct worktree **worktrees = get_worktrees(0);
struct worktree **worktrees = get_worktrees();
int i;

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

2
builtin/branch.c

@ -468,7 +468,7 @@ static void print_current_branch_name(void) @@ -468,7 +468,7 @@ static void print_current_branch_name(void)

static void reject_rebase_or_bisect_branch(const char *target)
{
struct worktree **worktrees = get_worktrees(0);
struct worktree **worktrees = get_worktrees();
int i;

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

2
builtin/config.c

@ -672,7 +672,7 @@ int cmd_config(int argc, const char **argv, const char *prefix) @@ -672,7 +672,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
given_config_source.file = git_pathdup("config");
given_config_source.scope = CONFIG_SCOPE_LOCAL;
} else if (use_worktree_config) {
struct worktree **worktrees = get_worktrees(0);
struct worktree **worktrees = get_worktrees();
if (repository_format_worktree_config)
given_config_source.file = git_pathdup("config.worktree");
else if (worktrees[0] && worktrees[1])

2
builtin/fsck.c

@ -577,7 +577,7 @@ static void get_default_heads(void) @@ -577,7 +577,7 @@ static void get_default_heads(void)

for_each_rawref(fsck_handle_ref, NULL);

worktrees = get_worktrees(0);
worktrees = get_worktrees();
for (p = worktrees; *p; p++) {
struct worktree *wt = *p;
struct strbuf ref = STRBUF_INIT;

2
builtin/reflog.c

@ -615,7 +615,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix) @@ -615,7 +615,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
int i;

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

32
builtin/worktree.c

@ -325,7 +325,7 @@ static int add_worktree(const char *path, const char *refname, @@ -325,7 +325,7 @@ static int add_worktree(const char *path, const char *refname,
struct strbuf sb_name = STRBUF_INIT;
struct worktree **worktrees;

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

static int pathcmp(const void *a_, const void *b_)
{
const struct worktree *const *a = a_;
const struct worktree *const *b = b_;
return fspathcmp((*a)->path, (*b)->path);
}

static void pathsort(struct worktree **wt)
{
int n = 0;
struct worktree **p = wt;

while (*p++)
n++;
QSORT(wt, n, pathcmp);
}

static int list(int ac, const char **av, const char *prefix)
{
int porcelain = 0;
@ -710,9 +727,12 @@ static int list(int ac, const char **av, const char *prefix) @@ -710,9 +727,12 @@ 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(GWT_SORT_LINKED);
struct worktree **worktrees = get_worktrees();
int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;

/* sort worktrees by path but keep main worktree at top */
pathsort(worktrees + 1);

if (!porcelain)
measure_widths(worktrees, &abbrev, &path_maxlen);

@ -741,7 +761,7 @@ static int lock_worktree(int ac, const char **av, const char *prefix) @@ -741,7 +761,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(0);
worktrees = get_worktrees();
wt = find_worktree(worktrees, prefix, av[0]);
if (!wt)
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) @@ -774,7 +794,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(0);
worktrees = get_worktrees();
wt = find_worktree(worktrees, prefix, av[0]);
if (!wt)
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) @@ -848,7 +868,7 @@ static int move_worktree(int ac, const char **av, const char *prefix)
strbuf_addstr(&dst, path);
free(path);

worktrees = get_worktrees(0);
worktrees = get_worktrees();
wt = find_worktree(worktrees, prefix, av[0]);
if (!wt)
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) @@ -974,7 +994,7 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
if (ac != 1)
usage_with_options(worktree_usage, options);

worktrees = get_worktrees(0);
worktrees = get_worktrees();
wt = find_worktree(worktrees, prefix, av[0]);
if (!wt)
die(_("'%s' is not a working tree"), av[0]);

2
ref-filter.c

@ -1579,7 +1579,7 @@ static void lazy_init_worktree_map(void) @@ -1579,7 +1579,7 @@ static void lazy_init_worktree_map(void)
if (ref_to_worktree_map.worktrees)
return;

ref_to_worktree_map.worktrees = get_worktrees(0);
ref_to_worktree_map.worktrees = get_worktrees();
hashmap_init(&(ref_to_worktree_map.map), ref_to_worktree_map_cmpfnc, NULL, 0);
populate_worktree_map(&(ref_to_worktree_map.map), ref_to_worktree_map.worktrees);
}

4
revision.c

@ -1609,7 +1609,7 @@ static void add_other_reflogs_to_pending(struct all_refs_cb *cb) @@ -1609,7 +1609,7 @@ static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
{
struct worktree **worktrees, **p;

worktrees = get_worktrees(0);
worktrees = get_worktrees();
for (p = worktrees; *p; p++) {
struct worktree *wt = *p;

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

worktrees = get_worktrees(0);
worktrees = get_worktrees();
for (p = worktrees; *p; p++) {
struct worktree *wt = *p;
struct index_state istate = { NULL };

2
t/helper/test-ref-store.c

@ -37,7 +37,7 @@ static const char **get_store(const char **argv, struct ref_store **refs) @@ -37,7 +37,7 @@ static const char **get_store(const char **argv, struct ref_store **refs)

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

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

20
worktree.c

@ -123,14 +123,7 @@ static void mark_current_worktree(struct worktree **worktrees) @@ -123,14 +123,7 @@ static void mark_current_worktree(struct worktree **worktrees)
free(git_dir);
}

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 **get_worktrees(void)
{
struct worktree **list = NULL;
struct strbuf path = STRBUF_INIT;
@ -161,13 +154,6 @@ struct worktree **get_worktrees(unsigned flags) @@ -161,13 +154,6 @@ struct worktree **get_worktrees(unsigned flags)
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;
}
@ -418,7 +404,7 @@ const struct worktree *find_shared_symref(const char *symref, @@ -418,7 +404,7 @@ const struct worktree *find_shared_symref(const char *symref,

if (worktrees)
free_worktrees(worktrees);
worktrees = get_worktrees(0);
worktrees = get_worktrees();

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

worktrees = get_worktrees(0);
worktrees = get_worktrees();
for (p = worktrees; *p; p++) {
struct worktree *wt = *p;
struct object_id oid;

11
worktree.h

@ -18,19 +18,14 @@ struct worktree { @@ -18,19 +18,14 @@ struct worktree {
int lock_reason_valid; /* private */
};

/* 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
* worktree. No specific ordering is done on the linked worktrees.
* and linked worktrees will follow in no particular order.
*
* The caller is responsible for freeing the memory from the returned
* worktree(s).
* worktrees by calling free_worktrees().
*/
struct worktree **get_worktrees(unsigned flags);
struct worktree **get_worktrees(void);

/*
* Returns 1 if linked worktrees exist, 0 otherwise.

Loading…
Cancel
Save