]>
Commit | Line | Data |
---|---|---|
e7da9385 PS |
1 | #define USE_THE_REPOSITORY_VARIABLE |
2 | ||
85023577 | 3 | #include "builtin.h" |
b2141fc1 | 4 | #include "config.h" |
32a8f510 | 5 | #include "environment.h" |
d807c4a0 | 6 | #include "exec-cmd.h" |
f394e093 | 7 | #include "gettext.h" |
fd5c363d | 8 | #include "help.h" |
c784b0a5 | 9 | #include "object-file.h" |
ca4eed70 | 10 | #include "pager.h" |
08c46a49 | 11 | #include "read-cache-ll.h" |
d8e96fd8 | 12 | #include "run-command.h" |
65b5f948 | 13 | #include "alias.h" |
cbeab747 | 14 | #include "replace-object.h" |
e38da487 | 15 | #include "setup.h" |
44451a2e | 16 | #include "attr.h" |
120ad2b0 | 17 | #include "shallow.h" |
74ea5c95 EN |
18 | #include "trace.h" |
19 | #include "trace2.h" | |
8e49d503 | 20 | |
007aa8d8 NTND |
21 | #define RUN_SETUP (1<<0) |
22 | #define RUN_SETUP_GENTLY (1<<1) | |
23 | #define USE_PAGER (1<<2) | |
24 | /* | |
25 | * require working tree to be present -- anything uses this needs | |
26 | * RUN_SETUP for reading from the configuration file. | |
27 | */ | |
28 | #define NEED_WORK_TREE (1<<3) | |
4002ec3d ÆAB |
29 | #define DELAY_PAGER_CONFIG (1<<4) |
30 | #define NO_PARSEOPT (1<<5) /* parse-options is not used */ | |
007aa8d8 NTND |
31 | |
32 | struct cmd_struct { | |
33 | const char *cmd; | |
9b1cb507 | 34 | int (*fn)(int, const char **, const char *, struct repository *); |
e9547947 | 35 | unsigned int option; |
007aa8d8 NTND |
36 | }; |
37 | ||
822a7d50 | 38 | const char git_usage_string[] = |
6b52f48b | 39 | N_("git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]\n" |
fc045fe7 | 40 | " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n" |
9b715ad9 | 41 | " [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--no-lazy-fetch]\n" |
b79deeb5 JL |
42 | " [--no-optional-locks] [--no-advice] [--bare] [--git-dir=<path>]\n" |
43 | " [--work-tree=<path>] [--namespace=<name>] [--config-env=<name>=<envvar>]\n" | |
44 | " <command> [<args>]"); | |
822a7d50 | 45 | |
b7d96819 | 46 | const char git_more_info_string[] = |
ad5fe377 | 47 | N_("'git help -a' and 'git help -g' list available subcommands and some\n" |
73903d0b | 48 | "concept guides. See 'git help <command>' or 'git help <concept>'\n" |
e55f36b8 PO |
49 | "to read about a specific subcommand or concept.\n" |
50 | "See 'git help git' for an overview of the system."); | |
b7d96819 | 51 | |
4e10738a | 52 | static int use_pager = -1; |
c0562611 | 53 | |
e5d7a619 | 54 | static void list_builtins(struct string_list *list, unsigned int exclude_option); |
8893fd95 | 55 | |
e11dca10 NTND |
56 | static void exclude_helpers_from_list(struct string_list *list) |
57 | { | |
80c9e70e | 58 | size_t i = 0; |
e11dca10 NTND |
59 | |
60 | while (i < list->nr) { | |
61 | if (strstr(list->items[i].string, "--")) | |
62 | unsorted_string_list_delete_item(list, i, 0); | |
63 | else | |
64 | i++; | |
65 | } | |
66 | } | |
67 | ||
0089521c NTND |
68 | static int match_token(const char *spec, int len, const char *token) |
69 | { | |
70 | int token_len = strlen(token); | |
71 | ||
72 | return len == token_len && !strncmp(spec, token, token_len); | |
73 | } | |
74 | ||
75 | static int list_cmds(const char *spec) | |
76 | { | |
e5d7a619 | 77 | struct string_list list = STRING_LIST_INIT_DUP; |
83b0ecf3 JK |
78 | int nongit; |
79 | ||
80 | /* | |
81 | * Set up the repository so we can pick up any repo-level config (like | |
82 | * completion.commands). | |
83 | */ | |
84 | setup_git_directory_gently(&nongit); | |
e5d7a619 | 85 | |
0089521c NTND |
86 | while (*spec) { |
87 | const char *sep = strchrnul(spec, ','); | |
88 | int len = sep - spec; | |
89 | ||
90 | if (match_token(spec, len, "builtins")) | |
e5d7a619 | 91 | list_builtins(&list, 0); |
6bb2dc0b NTND |
92 | else if (match_token(spec, len, "main")) |
93 | list_all_main_cmds(&list); | |
94 | else if (match_token(spec, len, "others")) | |
95 | list_all_other_cmds(&list); | |
e11dca10 NTND |
96 | else if (match_token(spec, len, "nohelpers")) |
97 | exclude_helpers_from_list(&list); | |
3301d36b NTND |
98 | else if (match_token(spec, len, "alias")) |
99 | list_aliases(&list); | |
6532f374 NTND |
100 | else if (match_token(spec, len, "config")) |
101 | list_cmds_by_config(&list); | |
3c777767 NTND |
102 | else if (len > 5 && !strncmp(spec, "list-", 5)) { |
103 | struct strbuf sb = STRBUF_INIT; | |
104 | ||
105 | strbuf_add(&sb, spec + 5, len - 5); | |
106 | list_cmds_by_category(&list, sb.buf); | |
107 | strbuf_release(&sb); | |
108 | } | |
0089521c NTND |
109 | else |
110 | die(_("unsupported command listing type '%s'"), spec); | |
111 | spec += len; | |
112 | if (*spec == ',') | |
113 | spec++; | |
114 | } | |
80c9e70e | 115 | for (size_t i = 0; i < list.nr; i++) |
e5d7a619 NTND |
116 | puts(list.items[i].string); |
117 | string_list_clear(&list, 0); | |
0089521c NTND |
118 | return 0; |
119 | } | |
8893fd95 | 120 | |
3b335762 NTND |
121 | static void commit_pager_choice(void) |
122 | { | |
4e10738a JK |
123 | switch (use_pager) { |
124 | case 0: | |
125 | setenv("GIT_PAGER", "cat", 1); | |
126 | break; | |
127 | case 1: | |
59b6131a | 128 | setup_pager(the_repository); |
4e10738a JK |
129 | break; |
130 | default: | |
131 | break; | |
132 | } | |
133 | } | |
134 | ||
033fe3d9 MÅ |
135 | void setup_auto_pager(const char *cmd, int def) |
136 | { | |
137 | if (use_pager != -1 || pager_in_use()) | |
138 | return; | |
59b6131a | 139 | use_pager = check_pager_config(the_repository, cmd); |
033fe3d9 MÅ |
140 | if (use_pager == -1) |
141 | use_pager = def; | |
142 | commit_pager_choice(); | |
143 | } | |
144 | ||
ce15f9eb PS |
145 | static void print_system_path(const char *path) |
146 | { | |
147 | char *s_path = system_path(path); | |
148 | puts(s_path); | |
149 | free(s_path); | |
150 | } | |
151 | ||
4b25d091 | 152 | static int handle_options(const char ***argv, int *argc, int *envchanged) |
4ab243a9 | 153 | { |
73546c08 | 154 | const char **orig_argv = *argv; |
4ab243a9 JS |
155 | |
156 | while (*argc > 0) { | |
157 | const char *cmd = (*argv)[0]; | |
158 | if (cmd[0] != '-') | |
159 | break; | |
160 | ||
6acbcb92 JS |
161 | /* |
162 | * For legacy reasons, the "version" and "help" | |
163 | * commands can be written with "--" prepended | |
164 | * to make them look like flags. | |
165 | */ | |
6b52f48b GF |
166 | if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h") || |
167 | !strcmp(cmd, "--version") || !strcmp(cmd, "-v")) | |
6acbcb92 JS |
168 | break; |
169 | ||
170 | /* | |
171 | * Check remaining flags. | |
172 | */ | |
ae021d87 | 173 | if (skip_prefix(cmd, "--exec-path", &cmd)) { |
6acbcb92 | 174 | if (*cmd == '=') |
226c0ddd | 175 | git_set_exec_path(cmd + 1); |
6acbcb92 JS |
176 | else { |
177 | puts(git_exec_path()); | |
ee4512ed | 178 | trace2_cmd_name("_query_"); |
6acbcb92 JS |
179 | exit(0); |
180 | } | |
89a56bfb | 181 | } else if (!strcmp(cmd, "--html-path")) { |
ce15f9eb | 182 | print_system_path(GIT_HTML_PATH); |
ee4512ed | 183 | trace2_cmd_name("_query_"); |
89a56bfb | 184 | exit(0); |
f2dd8c37 | 185 | } else if (!strcmp(cmd, "--man-path")) { |
ce15f9eb | 186 | print_system_path(GIT_MAN_PATH); |
ee4512ed | 187 | trace2_cmd_name("_query_"); |
f2dd8c37 JS |
188 | exit(0); |
189 | } else if (!strcmp(cmd, "--info-path")) { | |
ce15f9eb | 190 | print_system_path(GIT_INFO_PATH); |
ee4512ed | 191 | trace2_cmd_name("_query_"); |
f2dd8c37 | 192 | exit(0); |
6acbcb92 | 193 | } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { |
4e10738a | 194 | use_pager = 1; |
7213c288 | 195 | } else if (!strcmp(cmd, "-P") || !strcmp(cmd, "--no-pager")) { |
4e10738a | 196 | use_pager = 0; |
463a849d MM |
197 | if (envchanged) |
198 | *envchanged = 1; | |
c784b0a5 JH |
199 | } else if (!strcmp(cmd, "--no-lazy-fetch")) { |
200 | fetch_if_missing = 0; | |
e6d5479e JH |
201 | setenv(NO_LAZY_FETCH_ENVIRONMENT, "1", 1); |
202 | if (envchanged) | |
203 | *envchanged = 1; | |
b0fa7ab5 | 204 | } else if (!strcmp(cmd, "--no-replace-objects")) { |
d24eda4e | 205 | disable_replace_refs(); |
6476b38b CC |
206 | setenv(NO_REPLACE_OBJECTS_ENVIRONMENT, "1", 1); |
207 | if (envchanged) | |
208 | *envchanged = 1; | |
6acbcb92 | 209 | } else if (!strcmp(cmd, "--git-dir")) { |
c321f00d | 210 | if (*argc < 2) { |
986cd655 | 211 | fprintf(stderr, _("no directory given for '%s' option\n" ), "--git-dir"); |
c321f00d BG |
212 | usage(git_usage_string); |
213 | } | |
45b09794 | 214 | setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1); |
4394efec ML |
215 | if (envchanged) |
216 | *envchanged = 1; | |
6acbcb92 JS |
217 | (*argv)++; |
218 | (*argc)--; | |
ae021d87 JK |
219 | } else if (skip_prefix(cmd, "--git-dir=", &cmd)) { |
220 | setenv(GIT_DIR_ENVIRONMENT, cmd, 1); | |
4394efec ML |
221 | if (envchanged) |
222 | *envchanged = 1; | |
a1bea2c1 JT |
223 | } else if (!strcmp(cmd, "--namespace")) { |
224 | if (*argc < 2) { | |
fc045fe7 | 225 | fprintf(stderr, _("no namespace given for --namespace\n" )); |
a1bea2c1 JT |
226 | usage(git_usage_string); |
227 | } | |
228 | setenv(GIT_NAMESPACE_ENVIRONMENT, (*argv)[1], 1); | |
229 | if (envchanged) | |
230 | *envchanged = 1; | |
231 | (*argv)++; | |
232 | (*argc)--; | |
ae021d87 JK |
233 | } else if (skip_prefix(cmd, "--namespace=", &cmd)) { |
234 | setenv(GIT_NAMESPACE_ENVIRONMENT, cmd, 1); | |
a1bea2c1 JT |
235 | if (envchanged) |
236 | *envchanged = 1; | |
892c41b9 ML |
237 | } else if (!strcmp(cmd, "--work-tree")) { |
238 | if (*argc < 2) { | |
986cd655 | 239 | fprintf(stderr, _("no directory given for '%s' option\n" ), "--work-tree"); |
892c41b9 ML |
240 | usage(git_usage_string); |
241 | } | |
242 | setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1); | |
4394efec ML |
243 | if (envchanged) |
244 | *envchanged = 1; | |
892c41b9 ML |
245 | (*argv)++; |
246 | (*argc)--; | |
ae021d87 JK |
247 | } else if (skip_prefix(cmd, "--work-tree=", &cmd)) { |
248 | setenv(GIT_WORK_TREE_ENVIRONMENT, cmd, 1); | |
4394efec ML |
249 | if (envchanged) |
250 | *envchanged = 1; | |
6acbcb92 | 251 | } else if (!strcmp(cmd, "--bare")) { |
4d3ab44d | 252 | char *cwd = xgetcwd(); |
6adcca3f | 253 | is_bare_repository_cfg = 1; |
4d3ab44d RS |
254 | setenv(GIT_DIR_ENVIRONMENT, cwd, 0); |
255 | free(cwd); | |
2cd83d10 | 256 | setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1); |
4394efec ML |
257 | if (envchanged) |
258 | *envchanged = 1; | |
8b1fa778 AR |
259 | } else if (!strcmp(cmd, "-c")) { |
260 | if (*argc < 2) { | |
fc045fe7 | 261 | fprintf(stderr, _("-c expects a configuration string\n" )); |
8b1fa778 AR |
262 | usage(git_usage_string); |
263 | } | |
2b64fc89 | 264 | git_config_push_parameter((*argv)[1]); |
8b1fa778 AR |
265 | (*argv)++; |
266 | (*argc)--; | |
c331551c PS |
267 | } else if (!strcmp(cmd, "--config-env")) { |
268 | if (*argc < 2) { | |
269 | fprintf(stderr, _("no config key given for --config-env\n" )); | |
270 | usage(git_usage_string); | |
271 | } | |
272 | git_config_push_env((*argv)[1]); | |
273 | (*argv)++; | |
274 | (*argc)--; | |
ce81b1da PS |
275 | } else if (skip_prefix(cmd, "--config-env=", &cmd)) { |
276 | git_config_push_env(cmd); | |
823ab40f JK |
277 | } else if (!strcmp(cmd, "--literal-pathspecs")) { |
278 | setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "1", 1); | |
279 | if (envchanged) | |
280 | *envchanged = 1; | |
281 | } else if (!strcmp(cmd, "--no-literal-pathspecs")) { | |
282 | setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "0", 1); | |
283 | if (envchanged) | |
284 | *envchanged = 1; | |
bd30c2e4 NTND |
285 | } else if (!strcmp(cmd, "--glob-pathspecs")) { |
286 | setenv(GIT_GLOB_PATHSPECS_ENVIRONMENT, "1", 1); | |
287 | if (envchanged) | |
288 | *envchanged = 1; | |
289 | } else if (!strcmp(cmd, "--noglob-pathspecs")) { | |
290 | setenv(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, "1", 1); | |
291 | if (envchanged) | |
292 | *envchanged = 1; | |
93d93537 NTND |
293 | } else if (!strcmp(cmd, "--icase-pathspecs")) { |
294 | setenv(GIT_ICASE_PATHSPECS_ENVIRONMENT, "1", 1); | |
295 | if (envchanged) | |
296 | *envchanged = 1; | |
27344d6a JK |
297 | } else if (!strcmp(cmd, "--no-optional-locks")) { |
298 | setenv(GIT_OPTIONAL_LOCKS_ENVIRONMENT, "0", 1); | |
299 | if (envchanged) | |
300 | *envchanged = 1; | |
6035d6aa NTND |
301 | } else if (!strcmp(cmd, "--shallow-file")) { |
302 | (*argv)++; | |
303 | (*argc)--; | |
6a2df51c | 304 | set_alternate_shallow_file(the_repository, (*argv)[0], 1); |
6035d6aa NTND |
305 | if (envchanged) |
306 | *envchanged = 1; | |
44e1e4d6 NR |
307 | } else if (!strcmp(cmd, "-C")) { |
308 | if (*argc < 2) { | |
986cd655 | 309 | fprintf(stderr, _("no directory given for '%s' option\n" ), "-C"); |
44e1e4d6 NR |
310 | usage(git_usage_string); |
311 | } | |
6a536e20 KN |
312 | if ((*argv)[1][0]) { |
313 | if (chdir((*argv)[1])) | |
fc045fe7 | 314 | die_errno("cannot change to '%s'", (*argv)[1]); |
6a536e20 KN |
315 | if (envchanged) |
316 | *envchanged = 1; | |
317 | } | |
44e1e4d6 NR |
318 | (*argv)++; |
319 | (*argc)--; | |
0089521c | 320 | } else if (skip_prefix(cmd, "--list-cmds=", &cmd)) { |
ee4512ed | 321 | trace2_cmd_name("_query_"); |
0089521c | 322 | if (!strcmp(cmd, "parseopt")) { |
e5d7a619 | 323 | struct string_list list = STRING_LIST_INIT_DUP; |
e5d7a619 NTND |
324 | |
325 | list_builtins(&list, NO_PARSEOPT); | |
80c9e70e | 326 | for (size_t i = 0; i < list.nr; i++) |
e5d7a619 NTND |
327 | printf("%s ", list.items[i].string); |
328 | string_list_clear(&list, 0); | |
0089521c NTND |
329 | exit(0); |
330 | } else { | |
331 | exit(list_cmds(cmd)); | |
332 | } | |
44451a2e JC |
333 | } else if (!strcmp(cmd, "--attr-source")) { |
334 | if (*argc < 2) { | |
335 | fprintf(stderr, _("no attribute source given for --attr-source\n" )); | |
336 | usage(git_usage_string); | |
337 | } | |
338 | setenv(GIT_ATTR_SOURCE_ENVIRONMENT, (*argv)[1], 1); | |
339 | if (envchanged) | |
340 | *envchanged = 1; | |
341 | (*argv)++; | |
342 | (*argc)--; | |
343 | } else if (skip_prefix(cmd, "--attr-source=", &cmd)) { | |
344 | set_git_attr_source(cmd); | |
345 | setenv(GIT_ATTR_SOURCE_ENVIRONMENT, cmd, 1); | |
346 | if (envchanged) | |
347 | *envchanged = 1; | |
b79deeb5 JL |
348 | } else if (!strcmp(cmd, "--no-advice")) { |
349 | setenv(GIT_ADVICE_ENVIRONMENT, "0", 1); | |
350 | if (envchanged) | |
351 | *envchanged = 1; | |
6acbcb92 | 352 | } else { |
fc045fe7 | 353 | fprintf(stderr, _("unknown option: %s\n"), cmd); |
822a7d50 | 354 | usage(git_usage_string); |
6acbcb92 | 355 | } |
4ab243a9 JS |
356 | |
357 | (*argv)++; | |
358 | (*argc)--; | |
4ab243a9 | 359 | } |
73546c08 | 360 | return (*argv) - orig_argv; |
4ab243a9 JS |
361 | } |
362 | ||
ffc5c046 | 363 | static int handle_alias(struct strvec *args) |
2b11e317 | 364 | { |
af05d679 | 365 | int envchanged = 0, ret = 0, saved_errno = errno; |
0347a8c7 | 366 | int count, option_count; |
4b25d091 | 367 | const char **new_argv; |
94351118 JK |
368 | const char *alias_command; |
369 | char *alias_string; | |
0347a8c7 | 370 | |
ffc5c046 | 371 | alias_command = args->v[0]; |
94351118 | 372 | alias_string = alias_lookup(alias_command); |
0347a8c7 | 373 | if (alias_string) { |
ffc5c046 | 374 | if (args->nr > 1 && !strcmp(args->v[1], "-h")) |
a9a60b94 RV |
375 | fprintf_ln(stderr, _("'%s' is aliased to '%s'"), |
376 | alias_command, alias_string); | |
dfd42a3c | 377 | if (alias_string[0] == '!') { |
850d2fec | 378 | struct child_process child = CHILD_PROCESS_INIT; |
a9bcf658 JS |
379 | int nongit_ok; |
380 | ||
381 | /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */ | |
382 | setup_git_directory_gently(&nongit_ok); | |
7f51f8bc | 383 | |
030149a4 | 384 | commit_pager_choice(); |
7f51f8bc | 385 | |
850d2fec | 386 | child.use_shell = 1; |
c0d73a59 TA |
387 | child.clean_on_exit = 1; |
388 | child.wait_after_clean = 1; | |
ee4512ed | 389 | child.trace2_child_class = "shell_alias"; |
ef8d7ac4 | 390 | strvec_push(&child.args, alias_string + 1); |
ffc5c046 | 391 | strvec_pushv(&child.args, args->v + 1); |
7f51f8bc | 392 | |
d70a9eb6 | 393 | trace2_cmd_alias(alias_command, child.args.v); |
ee4512ed JH |
394 | trace2_cmd_name("_run_shell_alias_"); |
395 | ||
850d2fec | 396 | ret = run_command(&child); |
7f51f8bc EFL |
397 | if (ret >= 0) /* normal exit */ |
398 | exit(ret); | |
399 | ||
8aa8c140 NTND |
400 | die_errno(_("while expanding alias '%s': '%s'"), |
401 | alias_command, alias_string + 1); | |
dfd42a3c | 402 | } |
0347a8c7 | 403 | count = split_cmdline(alias_string, &new_argv); |
dc4179f9 | 404 | if (count < 0) |
8aa8c140 | 405 | die(_("bad alias.%s string: %s"), alias_command, |
a7412ae1 | 406 | _(split_cmdline_strerror(count))); |
4394efec ML |
407 | option_count = handle_options(&new_argv, &count, &envchanged); |
408 | if (envchanged) | |
8aa8c140 NTND |
409 | die(_("alias '%s' changes environment variables.\n" |
410 | "You can use '!git' in the alias to do this"), | |
411 | alias_command); | |
7bd97d6d | 412 | MOVE_ARRAY(new_argv - option_count, new_argv, count); |
0347a8c7 ML |
413 | new_argv -= option_count; |
414 | ||
415 | if (count < 1) | |
8aa8c140 | 416 | die(_("empty alias for %s"), alias_command); |
0347a8c7 ML |
417 | |
418 | if (!strcmp(alias_command, new_argv[0])) | |
8aa8c140 | 419 | die(_("recursive alias: %s"), alias_command); |
0347a8c7 | 420 | |
b319ce4c | 421 | trace_argv_printf(new_argv, |
6ce4e61f CC |
422 | "trace: alias expansion: %s =>", |
423 | alias_command); | |
ee4512ed | 424 | trace2_cmd_alias(alias_command, new_argv); |
ee4512ed | 425 | |
ffc5c046 PS |
426 | /* Replace the alias with the new arguments. */ |
427 | strvec_splice(args, 0, 1, new_argv, count); | |
428 | ||
429 | free(alias_string); | |
430 | free(new_argv); | |
2b11e317 | 431 | |
0347a8c7 | 432 | ret = 1; |
2b11e317 JS |
433 | } |
434 | ||
47e5c0ca JS |
435 | errno = saved_errno; |
436 | ||
2b11e317 JS |
437 | return ret; |
438 | } | |
439 | ||
9b1cb507 | 440 | static int run_builtin(struct cmd_struct *p, int argc, const char **argv, struct repository *repo) |
47d0b4ff | 441 | { |
99caeed0 | 442 | int status, help; |
5db948d4 | 443 | int no_repo = 1; |
0f157315 | 444 | struct stat st; |
47d0b4ff | 445 | const char *prefix; |
e5b17bda | 446 | int run_setup = (p->option & (RUN_SETUP | RUN_SETUP_GENTLY)); |
47d0b4ff | 447 | |
99caeed0 | 448 | help = argc == 2 && !strcmp(argv[1], "-h"); |
e5b17bda LD |
449 | if (help && (run_setup & RUN_SETUP)) |
450 | /* demote to GENTLY to allow 'git cmd -h' outside repo */ | |
451 | run_setup = RUN_SETUP_GENTLY; | |
452 | ||
453 | if (run_setup & RUN_SETUP) { | |
454 | prefix = setup_git_directory(); | |
5db948d4 | 455 | no_repo = 0; |
e5b17bda | 456 | } else if (run_setup & RUN_SETUP_GENTLY) { |
5db948d4 | 457 | prefix = setup_git_directory_gently(&no_repo); |
e5b17bda LD |
458 | } else { |
459 | prefix = NULL; | |
99caeed0 | 460 | } |
9725c8dd | 461 | assert(!prefix || *prefix); |
e5b17bda LD |
462 | precompose_argv_prefix(argc, argv, NULL); |
463 | if (use_pager == -1 && run_setup && | |
464 | !(p->option & DELAY_PAGER_CONFIG)) | |
29394942 | 465 | use_pager = check_pager_config(repo, p->cmd); |
e5b17bda LD |
466 | if (use_pager == -1 && p->option & USE_PAGER) |
467 | use_pager = 1; | |
468 | if (run_setup && startup_info->have_repository) | |
469 | /* get_git_dir() may set up repo, avoid that */ | |
29394942 | 470 | trace_repo_setup(repo); |
4e10738a JK |
471 | commit_pager_choice(); |
472 | ||
99caeed0 | 473 | if (!help && p->option & NEED_WORK_TREE) |
59f0f2f3 MH |
474 | setup_work_tree(); |
475 | ||
b319ce4c | 476 | trace_argv_printf(argv, "trace: built-in: git"); |
ee4512ed | 477 | trace2_cmd_name(p->cmd); |
47d0b4ff | 478 | |
9b1cb507 | 479 | validate_cache_entries(repo->index); |
5db948d4 | 480 | status = p->fn(argc, argv, prefix, no_repo ? NULL : repo); |
9b1cb507 | 481 | validate_cache_entries(repo->index); |
8616a2d0 | 482 | |
0f157315 | 483 | if (status) |
47e3de0e | 484 | return status; |
0f157315 LT |
485 | |
486 | /* Somebody closed stdout? */ | |
487 | if (fstat(fileno(stdout), &st)) | |
488 | return 0; | |
489 | /* Ignore write errors for pipes and sockets.. */ | |
490 | if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) | |
491 | return 0; | |
492 | ||
493 | /* Check for ENOSPC and EIO errors.. */ | |
0227f988 | 494 | if (fflush(stdout)) |
8aa8c140 | 495 | die_errno(_("write failure on standard output")); |
0227f988 | 496 | if (ferror(stdout)) |
8aa8c140 | 497 | die(_("unknown write failure on standard output")); |
0227f988 | 498 | if (fclose(stdout)) |
8aa8c140 | 499 | die_errno(_("close failed on standard output")); |
0f157315 | 500 | return 0; |
47d0b4ff LT |
501 | } |
502 | ||
c6127fa3 SS |
503 | static struct cmd_struct commands[] = { |
504 | { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE }, | |
783d7e86 | 505 | { "am", cmd_am, RUN_SETUP | NEED_WORK_TREE }, |
66fa6e8e | 506 | { "annotate", cmd_annotate, RUN_SETUP }, |
c6127fa3 | 507 | { "apply", cmd_apply, RUN_SETUP_GENTLY }, |
eb0224c6 | 508 | { "archive", cmd_archive, RUN_SETUP_GENTLY }, |
a3f79e9a | 509 | { "backfill", cmd_backfill, RUN_SETUP }, |
73fce294 | 510 | { "bisect", cmd_bisect, RUN_SETUP }, |
c6127fa3 | 511 | { "blame", cmd_blame, RUN_SETUP }, |
d74b541e | 512 | { "branch", cmd_branch, RUN_SETUP | DELAY_PAGER_CONFIG }, |
d7a5649c | 513 | { "bugreport", cmd_bugreport, RUN_SETUP_GENTLY }, |
66fa6e8e | 514 | { "bundle", cmd_bundle, RUN_SETUP_GENTLY }, |
c6127fa3 SS |
515 | { "cat-file", cmd_cat_file, RUN_SETUP }, |
516 | { "check-attr", cmd_check_attr, RUN_SETUP }, | |
517 | { "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE }, | |
518 | { "check-mailmap", cmd_check_mailmap, RUN_SETUP }, | |
e9547947 | 519 | { "check-ref-format", cmd_check_ref_format, NO_PARSEOPT }, |
0ca560cb | 520 | { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE }, |
e9e8adf1 | 521 | { "checkout--worker", cmd_checkout__worker, |
4002ec3d | 522 | RUN_SETUP | NEED_WORK_TREE }, |
c6127fa3 SS |
523 | { "checkout-index", cmd_checkout_index, |
524 | RUN_SETUP | NEED_WORK_TREE}, | |
525 | { "cherry", cmd_cherry, RUN_SETUP }, | |
526 | { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE }, | |
527 | { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE }, | |
86d26f24 | 528 | { "clone", cmd_clone }, |
c6127fa3 SS |
529 | { "column", cmd_column, RUN_SETUP_GENTLY }, |
530 | { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE }, | |
4ce58ee3 | 531 | { "commit-graph", cmd_commit_graph, RUN_SETUP }, |
66fa6e8e | 532 | { "commit-tree", cmd_commit_tree, RUN_SETUP }, |
32888b8f | 533 | { "config", cmd_config, RUN_SETUP_GENTLY | DELAY_PAGER_CONFIG }, |
c6127fa3 | 534 | { "count-objects", cmd_count_objects, RUN_SETUP }, |
e9547947 | 535 | { "credential", cmd_credential, RUN_SETUP_GENTLY | NO_PARSEOPT }, |
b5dd96b7 JK |
536 | { "credential-cache", cmd_credential_cache }, |
537 | { "credential-cache--daemon", cmd_credential_cache_daemon }, | |
538 | { "credential-store", cmd_credential_store }, | |
c6127fa3 | 539 | { "describe", cmd_describe, RUN_SETUP }, |
6783fd3c | 540 | { "diagnose", cmd_diagnose, RUN_SETUP_GENTLY }, |
e9547947 NTND |
541 | { "diff", cmd_diff, NO_PARSEOPT }, |
542 | { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, | |
543 | { "diff-index", cmd_diff_index, RUN_SETUP | NO_PARSEOPT }, | |
5bd10b2a | 544 | { "diff-pairs", cmd_diff_pairs, RUN_SETUP | NO_PARSEOPT }, |
e9547947 | 545 | { "diff-tree", cmd_diff_tree, RUN_SETUP | NO_PARSEOPT }, |
20de316e | 546 | { "difftool", cmd_difftool, RUN_SETUP_GENTLY }, |
c6127fa3 | 547 | { "fast-export", cmd_fast_export, RUN_SETUP }, |
a006f875 | 548 | { "fast-import", cmd_fast_import, RUN_SETUP | NO_PARSEOPT }, |
c6127fa3 | 549 | { "fetch", cmd_fetch, RUN_SETUP }, |
e9547947 | 550 | { "fetch-pack", cmd_fetch_pack, RUN_SETUP | NO_PARSEOPT }, |
c6127fa3 SS |
551 | { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP }, |
552 | { "for-each-ref", cmd_for_each_ref, RUN_SETUP }, | |
4950b2a2 | 553 | { "for-each-repo", cmd_for_each_repo, RUN_SETUP_GENTLY }, |
c6127fa3 SS |
554 | { "format-patch", cmd_format_patch, RUN_SETUP }, |
555 | { "fsck", cmd_fsck, RUN_SETUP }, | |
556 | { "fsck-objects", cmd_fsck, RUN_SETUP }, | |
bb61a962 | 557 | { "fsmonitor--daemon", cmd_fsmonitor__daemon, RUN_SETUP }, |
c6127fa3 | 558 | { "gc", cmd_gc, RUN_SETUP }, |
e9547947 | 559 | { "get-tar-commit-id", cmd_get_tar_commit_id, NO_PARSEOPT }, |
f9ee2fcd | 560 | { "grep", cmd_grep, RUN_SETUP_GENTLY }, |
c6127fa3 SS |
561 | { "hash-object", cmd_hash_object }, |
562 | { "help", cmd_help }, | |
96e7225b | 563 | { "hook", cmd_hook, RUN_SETUP }, |
e9547947 | 564 | { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY | NO_PARSEOPT }, |
86d26f24 NTND |
565 | { "init", cmd_init_db }, |
566 | { "init-db", cmd_init_db }, | |
cbd9fc23 | 567 | { "interpret-trailers", cmd_interpret_trailers, RUN_SETUP_GENTLY }, |
c6127fa3 | 568 | { "log", cmd_log, RUN_SETUP }, |
188dce13 | 569 | { "ls-files", cmd_ls_files, RUN_SETUP }, |
c6127fa3 SS |
570 | { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY }, |
571 | { "ls-tree", cmd_ls_tree, RUN_SETUP }, | |
66fa6e8e | 572 | { "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY }, |
e9547947 | 573 | { "mailsplit", cmd_mailsplit, NO_PARSEOPT }, |
03509544 | 574 | { "maintenance", cmd_maintenance, RUN_SETUP }, |
c6127fa3 SS |
575 | { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE }, |
576 | { "merge-base", cmd_merge_base, RUN_SETUP }, | |
577 | { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY }, | |
e9547947 NTND |
578 | { "merge-index", cmd_merge_index, RUN_SETUP | NO_PARSEOPT }, |
579 | { "merge-ours", cmd_merge_ours, RUN_SETUP | NO_PARSEOPT }, | |
580 | { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, | |
581 | { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, | |
582 | { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, | |
583 | { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, | |
6ec755a0 | 584 | { "merge-tree", cmd_merge_tree, RUN_SETUP }, |
66fa6e8e | 585 | { "mktag", cmd_mktag, RUN_SETUP }, |
c6127fa3 | 586 | { "mktree", cmd_mktree, RUN_SETUP }, |
73ff4ad0 | 587 | { "multi-pack-index", cmd_multi_pack_index, RUN_SETUP }, |
c6127fa3 SS |
588 | { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE }, |
589 | { "name-rev", cmd_name_rev, RUN_SETUP }, | |
590 | { "notes", cmd_notes, RUN_SETUP }, | |
591 | { "pack-objects", cmd_pack_objects, RUN_SETUP }, | |
68f51871 | 592 | #ifndef WITH_BREAKING_CHANGES |
e9547947 | 593 | { "pack-redundant", cmd_pack_redundant, RUN_SETUP | NO_PARSEOPT }, |
68f51871 | 594 | #endif |
c6127fa3 | 595 | { "pack-refs", cmd_pack_refs, RUN_SETUP }, |
e9547947 | 596 | { "patch-id", cmd_patch_id, RUN_SETUP_GENTLY | NO_PARSEOPT }, |
c6127fa3 SS |
597 | { "pickaxe", cmd_blame, RUN_SETUP }, |
598 | { "prune", cmd_prune, RUN_SETUP }, | |
599 | { "prune-packed", cmd_prune_packed, RUN_SETUP }, | |
1e1ea69f | 600 | { "pull", cmd_pull, RUN_SETUP | NEED_WORK_TREE }, |
c6127fa3 | 601 | { "push", cmd_push, RUN_SETUP }, |
348ae56c | 602 | { "range-diff", cmd_range_diff, RUN_SETUP | USE_PAGER }, |
4002ec3d | 603 | { "read-tree", cmd_read_tree, RUN_SETUP }, |
80dfc924 | 604 | { "rebase", cmd_rebase, RUN_SETUP | NEED_WORK_TREE }, |
c6127fa3 SS |
605 | { "receive-pack", cmd_receive_pack }, |
606 | { "reflog", cmd_reflog, RUN_SETUP }, | |
25a0023f | 607 | { "refs", cmd_refs, RUN_SETUP }, |
c6127fa3 | 608 | { "remote", cmd_remote, RUN_SETUP }, |
e9547947 NTND |
609 | { "remote-ext", cmd_remote_ext, NO_PARSEOPT }, |
610 | { "remote-fd", cmd_remote_fd, NO_PARSEOPT }, | |
c6127fa3 SS |
611 | { "repack", cmd_repack, RUN_SETUP }, |
612 | { "replace", cmd_replace, RUN_SETUP }, | |
f920b028 | 613 | { "replay", cmd_replay, RUN_SETUP }, |
c6127fa3 SS |
614 | { "rerere", cmd_rerere, RUN_SETUP }, |
615 | { "reset", cmd_reset, RUN_SETUP }, | |
46e91b66 | 616 | { "restore", cmd_restore, RUN_SETUP | NEED_WORK_TREE }, |
e9547947 NTND |
617 | { "rev-list", cmd_rev_list, RUN_SETUP | NO_PARSEOPT }, |
618 | { "rev-parse", cmd_rev_parse, NO_PARSEOPT }, | |
c6127fa3 SS |
619 | { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE }, |
620 | { "rm", cmd_rm, RUN_SETUP }, | |
621 | { "send-pack", cmd_send_pack, RUN_SETUP }, | |
622 | { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER }, | |
623 | { "show", cmd_show, RUN_SETUP }, | |
624 | { "show-branch", cmd_show_branch, RUN_SETUP }, | |
88a09a55 | 625 | { "show-index", cmd_show_index, RUN_SETUP_GENTLY }, |
c6127fa3 | 626 | { "show-ref", cmd_show_ref, RUN_SETUP }, |
24fc2cde | 627 | { "sparse-checkout", cmd_sparse_checkout, RUN_SETUP }, |
c6127fa3 | 628 | { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE }, |
8a2cd3f5 | 629 | { "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE }, |
c6127fa3 SS |
630 | { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE }, |
631 | { "stripspace", cmd_stripspace }, | |
f5a6be9d | 632 | { "submodule--helper", cmd_submodule__helper, RUN_SETUP }, |
d787d311 | 633 | { "switch", cmd_switch, RUN_SETUP | NEED_WORK_TREE }, |
c6127fa3 | 634 | { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP }, |
de121ffe | 635 | { "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG }, |
e9547947 NTND |
636 | { "unpack-file", cmd_unpack_file, RUN_SETUP | NO_PARSEOPT }, |
637 | { "unpack-objects", cmd_unpack_objects, RUN_SETUP | NO_PARSEOPT }, | |
c6127fa3 SS |
638 | { "update-index", cmd_update_index, RUN_SETUP }, |
639 | { "update-ref", cmd_update_ref, RUN_SETUP }, | |
640 | { "update-server-info", cmd_update_server_info, RUN_SETUP }, | |
e9547947 NTND |
641 | { "upload-archive", cmd_upload_archive, NO_PARSEOPT }, |
642 | { "upload-archive--writer", cmd_upload_archive_writer, NO_PARSEOPT }, | |
a3d6b53e | 643 | { "upload-pack", cmd_upload_pack }, |
e9547947 | 644 | { "var", cmd_var, RUN_SETUP_GENTLY | NO_PARSEOPT }, |
d07b00b7 | 645 | { "verify-commit", cmd_verify_commit, RUN_SETUP }, |
c6127fa3 SS |
646 | { "verify-pack", cmd_verify_pack }, |
647 | { "verify-tag", cmd_verify_tag, RUN_SETUP }, | |
648 | { "version", cmd_version }, | |
07572f22 | 649 | #ifndef WITH_BREAKING_CHANGES |
c6127fa3 | 650 | { "whatchanged", cmd_whatchanged, RUN_SETUP }, |
07572f22 | 651 | #endif |
398c4ff5 | 652 | { "worktree", cmd_worktree, RUN_SETUP }, |
c6127fa3 SS |
653 | { "write-tree", cmd_write_tree, RUN_SETUP }, |
654 | }; | |
655 | ||
c4f901d1 | 656 | static struct cmd_struct *get_builtin(const char *s) |
c6127fa3 | 657 | { |
80c9e70e | 658 | for (size_t i = 0; i < ARRAY_SIZE(commands); i++) { |
c4f901d1 | 659 | struct cmd_struct *p = commands + i; |
c6127fa3 | 660 | if (!strcmp(s, p->cmd)) |
c4f901d1 | 661 | return p; |
c6127fa3 | 662 | } |
c4f901d1 SV |
663 | return NULL; |
664 | } | |
665 | ||
666 | int is_builtin(const char *s) | |
667 | { | |
668 | return !!get_builtin(s); | |
c6127fa3 SS |
669 | } |
670 | ||
e5d7a619 | 671 | static void list_builtins(struct string_list *out, unsigned int exclude_option) |
8893fd95 | 672 | { |
80c9e70e | 673 | for (size_t i = 0; i < ARRAY_SIZE(commands); i++) { |
e9547947 NTND |
674 | if (exclude_option && |
675 | (commands[i].option & exclude_option)) | |
676 | continue; | |
e5d7a619 | 677 | string_list_append(out, commands[i].cmd); |
e9547947 | 678 | } |
8893fd95 JK |
679 | } |
680 | ||
722fc374 JS |
681 | void load_builtin_commands(const char *prefix, struct cmdnames *cmds) |
682 | { | |
683 | const char *name; | |
722fc374 JS |
684 | |
685 | /* | |
686 | * Callers can ask for a subset of the commands based on a certain | |
687 | * prefix, which is then dropped from the added names. The names in | |
688 | * the `commands[]` array do not have the `git-` prefix, though, | |
689 | * therefore we must expect the `prefix` to at least start with `git-`. | |
690 | */ | |
691 | if (!skip_prefix(prefix, "git-", &prefix)) | |
692 | BUG("prefix '%s' must start with 'git-'", prefix); | |
693 | ||
80c9e70e | 694 | for (size_t i = 0; i < ARRAY_SIZE(commands); i++) |
722fc374 JS |
695 | if (skip_prefix(commands[i].cmd, prefix, &name)) |
696 | add_cmdname(cmds, name, strlen(name)); | |
697 | } | |
698 | ||
63ca1c09 | 699 | #ifdef STRIP_EXTENSION |
1dd7c32d | 700 | static void strip_extension(struct strvec *args) |
63ca1c09 AK |
701 | { |
702 | size_t len; | |
703 | ||
1dd7c32d PS |
704 | if (strip_suffix(args->v[0], STRIP_EXTENSION, &len)) { |
705 | char *stripped = xmemdupz(args->v[0], len); | |
706 | strvec_replace(args, 0, stripped); | |
707 | free(stripped); | |
708 | } | |
63ca1c09 AK |
709 | } |
710 | #else | |
711 | #define strip_extension(cmd) | |
712 | #endif | |
713 | ||
1dd7c32d | 714 | static void handle_builtin(struct strvec *args) |
231af832 | 715 | { |
63ca1c09 | 716 | const char *cmd; |
c4f901d1 | 717 | struct cmd_struct *builtin; |
23326d14 | 718 | |
1dd7c32d PS |
719 | strip_extension(args); |
720 | cmd = args->v[0]; | |
231af832 | 721 | |
2c6b6d9f | 722 | /* Turn "git cmd --help" into "git help --exclude-guides cmd" */ |
1dd7c32d PS |
723 | if (args->nr > 1 && !strcmp(args->v[1], "--help")) { |
724 | const char *exclude_guides_arg[] = { "--exclude-guides" }; | |
725 | ||
726 | strvec_replace(args, 1, args->v[0]); | |
727 | strvec_replace(args, 0, "help"); | |
728 | cmd = "help"; | |
729 | strvec_splice(args, 2, 0, exclude_guides_arg, | |
730 | ARRAY_SIZE(exclude_guides_arg)); | |
731 | } | |
2c6b6d9f | 732 | |
1dd7c32d PS |
733 | builtin = get_builtin(cmd); |
734 | if (builtin) { | |
735 | const char **argv_copy = NULL; | |
736 | int ret; | |
3aef7a05 PS |
737 | |
738 | /* | |
739 | * `run_builtin()` will modify the argv array, so we need to | |
740 | * create a shallow copy such that we can free all of its | |
741 | * strings. | |
742 | */ | |
1dd7c32d PS |
743 | if (args->nr) |
744 | DUP_ARRAY(argv_copy, args->v, args->nr + 1); | |
1cd95087 | 745 | |
1dd7c32d PS |
746 | ret = run_builtin(builtin, args->nr, argv_copy, the_repository); |
747 | strvec_clear(args); | |
3aef7a05 PS |
748 | free(argv_copy); |
749 | exit(ret); | |
750 | } | |
231af832 LT |
751 | } |
752 | ||
7550be0a JH |
753 | static void execv_dashed_external(const char **argv) |
754 | { | |
2b296c93 | 755 | struct child_process cmd = CHILD_PROCESS_INIT; |
d8e96fd8 | 756 | int status; |
7550be0a | 757 | |
595d59e2 | 758 | if (use_pager == -1 && !is_builtin(argv[0])) |
59b6131a | 759 | use_pager = check_pager_config(the_repository, argv[0]); |
030149a4 JN |
760 | commit_pager_choice(); |
761 | ||
ef8d7ac4 JK |
762 | strvec_pushf(&cmd.args, "git-%s", argv[0]); |
763 | strvec_pushv(&cmd.args, argv + 1); | |
2b296c93 | 764 | cmd.clean_on_exit = 1; |
46df6906 | 765 | cmd.wait_after_clean = 1; |
2b296c93 | 766 | cmd.silent_exec_failure = 1; |
ee4512ed | 767 | cmd.trace2_child_class = "dashed"; |
7550be0a | 768 | |
ee4512ed JH |
769 | trace2_cmd_name("_run_dashed_"); |
770 | ||
771 | /* | |
772 | * The code in run_command() logs trace2 child_start/child_exit | |
773 | * events, so we do not need to report exec/exec_result events here. | |
774 | */ | |
d70a9eb6 | 775 | trace_argv_printf(cmd.args.v, "trace: exec:"); |
7550be0a | 776 | |
d8e96fd8 | 777 | /* |
246f0ede JK |
778 | * If we fail because the command is not found, it is |
779 | * OK to return. Otherwise, we just pass along the status code, | |
780 | * or our usual generic code if we were not even able to exec | |
781 | * the program. | |
d8e96fd8 | 782 | */ |
2b296c93 | 783 | status = run_command(&cmd); |
ee4512ed JH |
784 | |
785 | /* | |
786 | * If the child process ran and we are now going to exit, emit a | |
787 | * generic string as our trace2 command verb to indicate that we | |
788 | * launched a dashed command. | |
789 | */ | |
246f0ede | 790 | if (status >= 0) |
5709e036 | 791 | exit(status); |
246f0ede JK |
792 | else if (errno != ENOENT) |
793 | exit(128); | |
7550be0a JH |
794 | } |
795 | ||
ffc5c046 | 796 | static int run_argv(struct strvec *args) |
a907e1b6 AS |
797 | { |
798 | int done_alias = 0; | |
ffc5c046 | 799 | struct string_list cmd_list = STRING_LIST_INIT_DUP; |
82f71d9a | 800 | struct string_list_item *seen; |
a907e1b6 AS |
801 | |
802 | while (1) { | |
441981bc JH |
803 | /* |
804 | * If we tried alias and futzed with our environment, | |
805 | * it no longer is safe to invoke builtins directly in | |
806 | * general. We have to spawn them as dashed externals. | |
807 | * | |
808 | * NEEDSWORK: if we can figure out cases | |
809 | * where it is safe to do, we can avoid spawning a new | |
810 | * process. | |
811 | */ | |
812 | if (!done_alias) | |
1dd7c32d | 813 | handle_builtin(args); |
ffc5c046 | 814 | else if (get_builtin(args->v[0])) { |
0e906739 | 815 | struct child_process cmd = CHILD_PROCESS_INIT; |
80c9e70e | 816 | int err; |
ee4512ed JH |
817 | |
818 | /* | |
819 | * The current process is committed to launching a | |
820 | * child process to run the command named in (**argv) | |
821 | * and exiting. Log a generic string as the trace2 | |
822 | * command verb to indicate this. Note that the child | |
823 | * process will log the actual verb when it runs. | |
824 | */ | |
825 | trace2_cmd_name("_run_git_alias_"); | |
826 | ||
ee4512ed JH |
827 | commit_pager_choice(); |
828 | ||
0e906739 | 829 | strvec_push(&cmd.args, "git"); |
80c9e70e | 830 | for (size_t i = 0; i < args->nr; i++) |
ffc5c046 | 831 | strvec_push(&cmd.args, args->v[i]); |
ee4512ed | 832 | |
0e906739 | 833 | trace_argv_printf(cmd.args.v, "trace: exec:"); |
ee4512ed JH |
834 | |
835 | /* | |
836 | * if we fail because the command is not found, it is | |
837 | * OK to return. Otherwise, we just pass along the status code. | |
838 | */ | |
0e906739 RS |
839 | cmd.silent_exec_failure = 1; |
840 | cmd.clean_on_exit = 1; | |
841 | cmd.wait_after_clean = 1; | |
842 | cmd.trace2_child_class = "git_alias"; | |
80c9e70e PS |
843 | err = run_command(&cmd); |
844 | if (err >= 0 || errno != ENOENT) | |
845 | exit(err); | |
ffc5c046 | 846 | die("could not execute builtin %s", args->v[0]); |
ee4512ed | 847 | } |
ee4512ed | 848 | |
a907e1b6 | 849 | /* .. then try the external ones */ |
ffc5c046 | 850 | execv_dashed_external(args->v); |
a907e1b6 | 851 | |
ffc5c046 | 852 | seen = unsorted_string_list_lookup(&cmd_list, args->v[0]); |
82f71d9a | 853 | if (seen) { |
82f71d9a | 854 | struct strbuf sb = STRBUF_INIT; |
80c9e70e | 855 | for (size_t i = 0; i < cmd_list.nr; i++) { |
82f71d9a TS |
856 | struct string_list_item *item = &cmd_list.items[i]; |
857 | ||
858 | strbuf_addf(&sb, "\n %s", item->string); | |
859 | if (item == seen) | |
860 | strbuf_addstr(&sb, " <=="); | |
861 | else if (i == cmd_list.nr - 1) | |
862 | strbuf_addstr(&sb, " ==>"); | |
863 | } | |
c6d75bc1 | 864 | die(_("alias loop detected: expansion of '%s' does" |
82f71d9a | 865 | " not terminate:%s"), cmd_list.items[0].string, sb.buf); |
c6d75bc1 TS |
866 | } |
867 | ||
ffc5c046 | 868 | string_list_append(&cmd_list, args->v[0]); |
c6d75bc1 TS |
869 | |
870 | /* | |
871 | * It could be an alias -- this works around the insanity | |
a907e1b6 AS |
872 | * of overriding "git log" with "git show" by having |
873 | * alias.log = show | |
874 | */ | |
ffc5c046 | 875 | if (!handle_alias(args)) |
a907e1b6 AS |
876 | break; |
877 | done_alias = 1; | |
878 | } | |
879 | ||
c6d75bc1 TS |
880 | string_list_clear(&cmd_list, 0); |
881 | ||
a907e1b6 AS |
882 | return done_alias; |
883 | } | |
884 | ||
3f2e2297 | 885 | int cmd_main(int argc, const char **argv) |
8e49d503 | 886 | { |
ffc5c046 | 887 | struct strvec args = STRVEC_INIT; |
4dd47c3b | 888 | const char *cmd; |
8fa7975b | 889 | int done_help = 0; |
231af832 | 890 | |
650c4492 | 891 | cmd = argv[0]; |
2cd72b0b | 892 | if (!cmd) |
4dd47c3b | 893 | cmd = "git-help"; |
6854a8f5 JK |
894 | else { |
895 | const char *slash = find_last_dir_sep(cmd); | |
896 | if (slash) | |
897 | cmd = slash + 1; | |
898 | } | |
8e49d503 | 899 | |
578da039 KB |
900 | trace_command_performance(argv); |
901 | ||
231af832 LT |
902 | /* |
903 | * "git-xxxx" is the same as "git xxxx", but we obviously: | |
904 | * | |
905 | * - cannot take flags in between the "git" and the "xxxx". | |
906 | * - cannot execute it externally (since it would just do | |
907 | * the same thing over again) | |
908 | * | |
3f784a4d SS |
909 | * So we just directly call the builtin handler, and die if |
910 | * that one cannot handle it. | |
231af832 | 911 | */ |
ae021d87 | 912 | if (skip_prefix(cmd, "git-", &cmd)) { |
1dd7c32d PS |
913 | strvec_push(&args, cmd); |
914 | strvec_pushv(&args, argv + 1); | |
915 | handle_builtin(&args); | |
916 | strvec_clear(&args); | |
8aa8c140 | 917 | die(_("cannot handle %s as a builtin"), cmd); |
231af832 | 918 | } |
8e49d503 | 919 | |
231af832 | 920 | /* Look for flags.. */ |
6acbcb92 JS |
921 | argv++; |
922 | argc--; | |
4394efec | 923 | handle_options(&argv, &argc, NULL); |
413bc6d2 DS |
924 | |
925 | if (!argc) { | |
3d7e2d85 | 926 | /* The user didn't specify a command; give them help */ |
73e25e7c | 927 | commit_pager_choice(); |
8aa8c140 | 928 | printf(_("usage: %s\n\n"), git_usage_string); |
3d7e2d85 | 929 | list_common_cmds_help(); |
421a9769 | 930 | printf("\n%s\n", _(git_more_info_string)); |
3d7e2d85 | 931 | exit(1); |
97fc6c5f | 932 | } |
413bc6d2 DS |
933 | |
934 | if (!strcmp("--version", argv[0]) || !strcmp("-v", argv[0])) | |
935 | argv[0] = "version"; | |
936 | else if (!strcmp("--help", argv[0]) || !strcmp("-h", argv[0])) | |
937 | argv[0] = "help"; | |
938 | ||
6acbcb92 | 939 | cmd = argv[0]; |
231af832 LT |
940 | |
941 | /* | |
511707d4 | 942 | * We use PATH to find git commands, but we prepend some higher |
3ea3c215 | 943 | * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH |
511707d4 SP |
944 | * environment, and the $(gitexecdir) from the Makefile at build |
945 | * time. | |
231af832 | 946 | */ |
e1464ca7 | 947 | setup_path(); |
8e49d503 | 948 | |
80c9e70e | 949 | for (int i = 0; i < argc; i++) |
ffc5c046 PS |
950 | strvec_push(&args, argv[i]); |
951 | ||
a025463b | 952 | while (1) { |
ffc5c046 | 953 | int was_alias = run_argv(&args); |
a907e1b6 | 954 | if (errno != ENOENT) |
a025463b | 955 | break; |
a907e1b6 | 956 | if (was_alias) { |
fc045fe7 AS |
957 | fprintf(stderr, _("expansion of alias '%s' failed; " |
958 | "'%s' is not a git command\n"), | |
ffc5c046 PS |
959 | cmd, args.v[0]); |
960 | strvec_clear(&args); | |
f3dd015c TT |
961 | exit(1); |
962 | } | |
a907e1b6 | 963 | if (!done_help) { |
7720dbe9 PS |
964 | char *assumed = help_unknown_cmd(cmd); |
965 | strvec_replace(&args, 0, assumed); | |
966 | free(assumed); | |
ffc5c046 | 967 | cmd = args.v[0]; |
a907e1b6 | 968 | done_help = 1; |
ffc5c046 | 969 | } else { |
a907e1b6 | 970 | break; |
ffc5c046 | 971 | } |
f3dd015c | 972 | } |
10b15b86 | 973 | |
fc045fe7 | 974 | fprintf(stderr, _("failed to run command '%s': %s\n"), |
33ebb871 | 975 | cmd, strerror(errno)); |
ffc5c046 | 976 | strvec_clear(&args); |
8e49d503 AE |
977 | |
978 | return 1; | |
979 | } |