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. b25b727494f (builtin/multi-pack-index.c: define common usage with a macro, 2021-03-30) 2. 8757b35d443 (commit-graph: define common usage with a macro, 2021-08-23) 3. 1e91d3faf6c (reflog: move "usage" variables and use macros, 2022-03-17) Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Oct 13, 2022 at 17:39 UTC 0afd556b2e1a8b5028d27cdede92bbab74a714da
1 file changed +84 -25
builtin/worktree.c
+84 -25
@@ -15,15 +15,72 @@
15 #include "worktree.h"
16 #include "quote.h"
17
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,
84 NULL
85 };
86
@@ -153,9 +210,10 @@ static int prune(int ac, const char **av, const char *prefix)
210 };
211
212 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);
215 if (ac)
158 - usage_with_options(worktree_usage, options);
216 + usage_with_options(git_worktree_prune_usage, options);
217 prune_worktrees();
218 return 0;
219 }
@@ -573,7 +631,7 @@ static int add(int ac, const char **av, const char *prefix)
631
632 memset(&opts, 0, sizeof(opts));
633 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);
635 if (!!opts.detach + !!new_branch + !!new_branch_force > 1)
636 die(_("options '%s', '%s', and '%s' cannot be used together"), "-b", "-B", "--detach");
637 if (lock_reason && !keep_locked)
@@ -584,7 +642,7 @@ static int add(int ac, const char **av, const char *prefix)
642 opts.keep_locked = _("added with --lock");
643
644 if (ac < 1 || ac > 2)
587 - usage_with_options(worktree_usage, options);
645 + usage_with_options(git_worktree_add_usage, options);
646
647 path = prefix_filename(prefix, av[0]);
648 branch = ac < 2 ? "HEAD" : av[1];
@@ -772,9 +830,9 @@ static int list(int ac, const char **av, const char *prefix)
830 };
831
832 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);
834 if (ac)
777 - usage_with_options(worktree_usage, options);
835 + usage_with_options(git_worktree_list_usage, options);
836 else if (verbose && porcelain)
837 die(_("options '%s' and '%s' cannot be used together"), "--verbose", "--porcelain");
838 else if (!line_terminator && !porcelain)
@@ -811,9 +869,9 @@ static int lock_worktree(int ac, const char **av, const char *prefix)
869 };
870 struct worktree **worktrees, *wt;
871
814 - ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
872 + ac = parse_options(ac, av, prefix, options, git_worktree_lock_usage, 0);
873 if (ac != 1)
816 - usage_with_options(worktree_usage, options);
874 + usage_with_options(git_worktree_lock_usage, options);
875
876 worktrees = get_worktrees();
877 wt = find_worktree(worktrees, prefix, av[0]);
@@ -844,9 +902,9 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
902 struct worktree **worktrees, *wt;
903 int ret;
904
847 - ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
905 + ac = parse_options(ac, av, prefix, options, git_worktree_unlock_usage, 0);
906 if (ac != 1)
849 - usage_with_options(worktree_usage, options);
907 + usage_with_options(git_worktree_unlock_usage, options);
908
909 worktrees = get_worktrees();
910 wt = find_worktree(worktrees, prefix, av[0]);
@@ -914,9 +972,10 @@ static int move_worktree(int ac, const char **av, const char *prefix)
972 const char *reason = NULL;
973 char *path;
974
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);
977 if (ac != 2)
919 - usage_with_options(worktree_usage, options);
978 + usage_with_options(git_worktree_move_usage, options);
979
980 path = prefix_filename(prefix, av[1]);
981 strbuf_addstr(&dst, path);
@@ -1042,9 +1101,9 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
1101 const char *reason = NULL;
1102 int ret = 0;
1103
1045 - ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
1104 + ac = parse_options(ac, av, prefix, options, git_worktree_remove_usage, 0);
1105 if (ac != 1)
1047 - usage_with_options(worktree_usage, options);
1106 + usage_with_options(git_worktree_remove_usage, options);
1107
1108 worktrees = get_worktrees();
1109 wt = find_worktree(worktrees, prefix, av[0]);
@@ -1102,7 +1161,7 @@ static int repair(int ac, const char **av, const char *prefix)
1161 };
1162 int rc = 0;
1163
1105 - ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
1164 + ac = parse_options(ac, av, prefix, options, git_worktree_repair_usage, 0);
1165 p = ac > 0 ? av : self;
1166 for (; *p; p++)
1167 repair_worktree_at_path(*p, report_repair, &rc);
@@ -1130,6 +1189,6 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
1189 if (!prefix)
1190 prefix = "";
1191
1133 - ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
1192 + ac = parse_options(ac, av, prefix, options, git_worktree_usage, 0);
1193 return fn(ac, av, prefix);
1194 }