Skip to content

Commit 0afd556

Browse files
avargitster
authored andcommitted
worktree: define subcommand -h in terms of command -h
Avoid repeating the "-h" output for the "git worktree" command, and instead define the usage of each subcommand with macros, so that the "-h" output for the command itself can re-use those definitions. See [1], [2] and [3] for prior art using the same pattern. 1. b25b727 (builtin/multi-pack-index.c: define common usage with a macro, 2021-03-30) 2. 8757b35 (commit-graph: define common usage with a macro, 2021-08-23) 3. 1e91d3f (reflog: move "usage" variables and use macros, 2022-03-17) Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4618d2c commit 0afd556

File tree

1 file changed

+84
-25
lines changed

1 file changed

+84
-25
lines changed

builtin/worktree.c

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,72 @@
1515
#include "worktree.h"
1616
#include "quote.h"
1717

18-
static const char * const worktree_usage[] = {
19-
N_("git worktree add [<options>] <path> [<commit-ish>]"),
20-
N_("git worktree list [<options>]"),
21-
N_("git worktree lock [<options>] <path>"),
22-
N_("git worktree move <worktree> <new-path>"),
23-
N_("git worktree prune [<options>]"),
24-
N_("git worktree remove [<options>] <worktree>"),
25-
N_("git worktree repair [<path>...]"),
26-
N_("git worktree unlock <worktree>"),
18+
#define BUILTIN_WORKTREE_ADD_USAGE \
19+
N_("git worktree add [<options>] <path> [<commit-ish>]")
20+
#define BUILTIN_WORKTREE_LIST_USAGE \
21+
N_("git worktree list [<options>]")
22+
#define BUILTIN_WORKTREE_LOCK_USAGE \
23+
N_("git worktree lock [<options>] <path>")
24+
#define BUILTIN_WORKTREE_MOVE_USAGE \
25+
N_("git worktree move <worktree> <new-path>")
26+
#define BUILTIN_WORKTREE_PRUNE_USAGE \
27+
N_("git worktree prune [<options>]")
28+
#define BUILTIN_WORKTREE_REMOVE_USAGE \
29+
N_("git worktree remove [<options>] <worktree>")
30+
#define BUILTIN_WORKTREE_REPAIR_USAGE \
31+
N_("git worktree repair [<path>...]")
32+
#define BUILTIN_WORKTREE_UNLOCK_USAGE \
33+
N_("git worktree unlock <worktree>")
34+
35+
static const char * const git_worktree_usage[] = {
36+
BUILTIN_WORKTREE_ADD_USAGE,
37+
BUILTIN_WORKTREE_LIST_USAGE,
38+
BUILTIN_WORKTREE_LOCK_USAGE,
39+
BUILTIN_WORKTREE_MOVE_USAGE,
40+
BUILTIN_WORKTREE_PRUNE_USAGE,
41+
BUILTIN_WORKTREE_REMOVE_USAGE,
42+
BUILTIN_WORKTREE_REPAIR_USAGE,
43+
BUILTIN_WORKTREE_UNLOCK_USAGE,
44+
NULL
45+
};
46+
47+
static const char * const git_worktree_add_usage[] = {
48+
BUILTIN_WORKTREE_ADD_USAGE,
49+
NULL,
50+
};
51+
52+
static const char * const git_worktree_list_usage[] = {
53+
BUILTIN_WORKTREE_LIST_USAGE,
54+
NULL
55+
};
56+
57+
static const char * const git_worktree_lock_usage[] = {
58+
BUILTIN_WORKTREE_LOCK_USAGE,
59+
NULL
60+
};
61+
62+
static const char * const git_worktree_move_usage[] = {
63+
BUILTIN_WORKTREE_MOVE_USAGE,
64+
NULL
65+
};
66+
67+
static const char * const git_worktree_prune_usage[] = {
68+
BUILTIN_WORKTREE_PRUNE_USAGE,
69+
NULL
70+
};
71+
72+
static const char * const git_worktree_remove_usage[] = {
73+
BUILTIN_WORKTREE_REMOVE_USAGE,
74+
NULL
75+
};
76+
77+
static const char * const git_worktree_repair_usage[] = {
78+
BUILTIN_WORKTREE_REPAIR_USAGE,
79+
NULL
80+
};
81+
82+
static const char * const git_worktree_unlock_usage[] = {
83+
BUILTIN_WORKTREE_UNLOCK_USAGE,
2784
NULL
2885
};
2986

@@ -153,9 +210,10 @@ static int prune(int ac, const char **av, const char *prefix)
153210
};
154211

155212
expire = TIME_MAX;
156-
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
213+
ac = parse_options(ac, av, prefix, options, git_worktree_prune_usage,
214+
0);
157215
if (ac)
158-
usage_with_options(worktree_usage, options);
216+
usage_with_options(git_worktree_prune_usage, options);
159217
prune_worktrees();
160218
return 0;
161219
}
@@ -573,7 +631,7 @@ static int add(int ac, const char **av, const char *prefix)
573631

574632
memset(&opts, 0, sizeof(opts));
575633
opts.checkout = 1;
576-
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
634+
ac = parse_options(ac, av, prefix, options, git_worktree_add_usage, 0);
577635
if (!!opts.detach + !!new_branch + !!new_branch_force > 1)
578636
die(_("options '%s', '%s', and '%s' cannot be used together"), "-b", "-B", "--detach");
579637
if (lock_reason && !keep_locked)
@@ -584,7 +642,7 @@ static int add(int ac, const char **av, const char *prefix)
584642
opts.keep_locked = _("added with --lock");
585643

586644
if (ac < 1 || ac > 2)
587-
usage_with_options(worktree_usage, options);
645+
usage_with_options(git_worktree_add_usage, options);
588646

589647
path = prefix_filename(prefix, av[0]);
590648
branch = ac < 2 ? "HEAD" : av[1];
@@ -772,9 +830,9 @@ static int list(int ac, const char **av, const char *prefix)
772830
};
773831

774832
expire = TIME_MAX;
775-
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
833+
ac = parse_options(ac, av, prefix, options, git_worktree_list_usage, 0);
776834
if (ac)
777-
usage_with_options(worktree_usage, options);
835+
usage_with_options(git_worktree_list_usage, options);
778836
else if (verbose && porcelain)
779837
die(_("options '%s' and '%s' cannot be used together"), "--verbose", "--porcelain");
780838
else if (!line_terminator && !porcelain)
@@ -811,9 +869,9 @@ static int lock_worktree(int ac, const char **av, const char *prefix)
811869
};
812870
struct worktree **worktrees, *wt;
813871

814-
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
872+
ac = parse_options(ac, av, prefix, options, git_worktree_lock_usage, 0);
815873
if (ac != 1)
816-
usage_with_options(worktree_usage, options);
874+
usage_with_options(git_worktree_lock_usage, options);
817875

818876
worktrees = get_worktrees();
819877
wt = find_worktree(worktrees, prefix, av[0]);
@@ -844,9 +902,9 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
844902
struct worktree **worktrees, *wt;
845903
int ret;
846904

847-
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
905+
ac = parse_options(ac, av, prefix, options, git_worktree_unlock_usage, 0);
848906
if (ac != 1)
849-
usage_with_options(worktree_usage, options);
907+
usage_with_options(git_worktree_unlock_usage, options);
850908

851909
worktrees = get_worktrees();
852910
wt = find_worktree(worktrees, prefix, av[0]);
@@ -914,9 +972,10 @@ static int move_worktree(int ac, const char **av, const char *prefix)
914972
const char *reason = NULL;
915973
char *path;
916974

917-
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
975+
ac = parse_options(ac, av, prefix, options, git_worktree_move_usage,
976+
0);
918977
if (ac != 2)
919-
usage_with_options(worktree_usage, options);
978+
usage_with_options(git_worktree_move_usage, options);
920979

921980
path = prefix_filename(prefix, av[1]);
922981
strbuf_addstr(&dst, path);
@@ -1042,9 +1101,9 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
10421101
const char *reason = NULL;
10431102
int ret = 0;
10441103

1045-
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
1104+
ac = parse_options(ac, av, prefix, options, git_worktree_remove_usage, 0);
10461105
if (ac != 1)
1047-
usage_with_options(worktree_usage, options);
1106+
usage_with_options(git_worktree_remove_usage, options);
10481107

10491108
worktrees = get_worktrees();
10501109
wt = find_worktree(worktrees, prefix, av[0]);
@@ -1102,7 +1161,7 @@ static int repair(int ac, const char **av, const char *prefix)
11021161
};
11031162
int rc = 0;
11041163

1105-
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
1164+
ac = parse_options(ac, av, prefix, options, git_worktree_repair_usage, 0);
11061165
p = ac > 0 ? av : self;
11071166
for (; *p; p++)
11081167
repair_worktree_at_path(*p, report_repair, &rc);
@@ -1130,6 +1189,6 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
11301189
if (!prefix)
11311190
prefix = "";
11321191

1133-
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
1192+
ac = parse_options(ac, av, prefix, options, git_worktree_usage, 0);
11341193
return fn(ac, av, prefix);
11351194
}

0 commit comments

Comments
 (0)