]>
Commit | Line | Data |
---|---|---|
85023577 | 1 | #include "builtin.h" |
b2141fc1 | 2 | #include "config.h" |
fd5c363d TF |
3 | #include "exec_cmd.h" |
4 | #include "help.h" | |
d8e96fd8 | 5 | #include "run-command.h" |
8e49d503 | 6 | |
007aa8d8 NTND |
7 | #define RUN_SETUP (1<<0) |
8 | #define RUN_SETUP_GENTLY (1<<1) | |
9 | #define USE_PAGER (1<<2) | |
10 | /* | |
11 | * require working tree to be present -- anything uses this needs | |
12 | * RUN_SETUP for reading from the configuration file. | |
13 | */ | |
14 | #define NEED_WORK_TREE (1<<3) | |
15 | #define SUPPORT_SUPER_PREFIX (1<<4) | |
16 | #define DELAY_PAGER_CONFIG (1<<5) | |
e9547947 | 17 | #define NO_PARSEOPT (1<<6) /* parse-options is not used */ |
007aa8d8 NTND |
18 | |
19 | struct cmd_struct { | |
20 | const char *cmd; | |
21 | int (*fn)(int, const char **, const char *); | |
e9547947 | 22 | unsigned int option; |
007aa8d8 NTND |
23 | }; |
24 | ||
822a7d50 | 25 | const char git_usage_string[] = |
fc045fe7 AS |
26 | N_("git [--version] [--help] [-C <path>] [-c <name>=<value>]\n" |
27 | " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n" | |
28 | " [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]\n" | |
29 | " [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n" | |
30 | " <command> [<args>]"); | |
822a7d50 | 31 | |
b7d96819 | 32 | const char git_more_info_string[] = |
ad5fe377 | 33 | N_("'git help -a' and 'git help -g' list available subcommands and some\n" |
73903d0b PO |
34 | "concept guides. See 'git help <command>' or 'git help <concept>'\n" |
35 | "to read about a specific subcommand or concept."); | |
b7d96819 | 36 | |
4e10738a | 37 | static int use_pager = -1; |
c0562611 | 38 | |
e5d7a619 | 39 | static void list_builtins(struct string_list *list, unsigned int exclude_option); |
8893fd95 | 40 | |
0089521c NTND |
41 | static int match_token(const char *spec, int len, const char *token) |
42 | { | |
43 | int token_len = strlen(token); | |
44 | ||
45 | return len == token_len && !strncmp(spec, token, token_len); | |
46 | } | |
47 | ||
48 | static int list_cmds(const char *spec) | |
49 | { | |
e5d7a619 NTND |
50 | struct string_list list = STRING_LIST_INIT_DUP; |
51 | int i; | |
52 | ||
0089521c NTND |
53 | while (*spec) { |
54 | const char *sep = strchrnul(spec, ','); | |
55 | int len = sep - spec; | |
56 | ||
57 | if (match_token(spec, len, "builtins")) | |
e5d7a619 | 58 | list_builtins(&list, 0); |
6bb2dc0b NTND |
59 | else if (match_token(spec, len, "main")) |
60 | list_all_main_cmds(&list); | |
61 | else if (match_token(spec, len, "others")) | |
62 | list_all_other_cmds(&list); | |
0089521c NTND |
63 | else |
64 | die(_("unsupported command listing type '%s'"), spec); | |
65 | spec += len; | |
66 | if (*spec == ',') | |
67 | spec++; | |
68 | } | |
e5d7a619 NTND |
69 | for (i = 0; i < list.nr; i++) |
70 | puts(list.items[i].string); | |
71 | string_list_clear(&list, 0); | |
0089521c NTND |
72 | return 0; |
73 | } | |
74 | ||
4e10738a JK |
75 | static void commit_pager_choice(void) { |
76 | switch (use_pager) { | |
77 | case 0: | |
78 | setenv("GIT_PAGER", "cat", 1); | |
79 | break; | |
80 | case 1: | |
81 | setup_pager(); | |
82 | break; | |
83 | default: | |
84 | break; | |
85 | } | |
86 | } | |
87 | ||
033fe3d9 MÅ |
88 | void setup_auto_pager(const char *cmd, int def) |
89 | { | |
90 | if (use_pager != -1 || pager_in_use()) | |
91 | return; | |
92 | use_pager = check_pager_config(cmd); | |
93 | if (use_pager == -1) | |
94 | use_pager = def; | |
95 | commit_pager_choice(); | |
96 | } | |
97 | ||
4b25d091 | 98 | static int handle_options(const char ***argv, int *argc, int *envchanged) |
4ab243a9 | 99 | { |
73546c08 | 100 | const char **orig_argv = *argv; |
4ab243a9 JS |
101 | |
102 | while (*argc > 0) { | |
103 | const char *cmd = (*argv)[0]; | |
104 | if (cmd[0] != '-') | |
105 | break; | |
106 | ||
6acbcb92 JS |
107 | /* |
108 | * For legacy reasons, the "version" and "help" | |
109 | * commands can be written with "--" prepended | |
110 | * to make them look like flags. | |
111 | */ | |
112 | if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version")) | |
113 | break; | |
114 | ||
115 | /* | |
116 | * Check remaining flags. | |
117 | */ | |
ae021d87 | 118 | if (skip_prefix(cmd, "--exec-path", &cmd)) { |
6acbcb92 | 119 | if (*cmd == '=') |
384df833 | 120 | git_set_argv_exec_path(cmd + 1); |
6acbcb92 JS |
121 | else { |
122 | puts(git_exec_path()); | |
123 | exit(0); | |
124 | } | |
89a56bfb MH |
125 | } else if (!strcmp(cmd, "--html-path")) { |
126 | puts(system_path(GIT_HTML_PATH)); | |
127 | exit(0); | |
f2dd8c37 JS |
128 | } else if (!strcmp(cmd, "--man-path")) { |
129 | puts(system_path(GIT_MAN_PATH)); | |
130 | exit(0); | |
131 | } else if (!strcmp(cmd, "--info-path")) { | |
132 | puts(system_path(GIT_INFO_PATH)); | |
133 | exit(0); | |
6acbcb92 | 134 | } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { |
4e10738a | 135 | use_pager = 1; |
463a849d | 136 | } else if (!strcmp(cmd, "--no-pager")) { |
4e10738a | 137 | use_pager = 0; |
463a849d MM |
138 | if (envchanged) |
139 | *envchanged = 1; | |
b0fa7ab5 | 140 | } else if (!strcmp(cmd, "--no-replace-objects")) { |
afc711b8 | 141 | check_replace_refs = 0; |
6476b38b CC |
142 | setenv(NO_REPLACE_OBJECTS_ENVIRONMENT, "1", 1); |
143 | if (envchanged) | |
144 | *envchanged = 1; | |
6acbcb92 | 145 | } else if (!strcmp(cmd, "--git-dir")) { |
c321f00d | 146 | if (*argc < 2) { |
fc045fe7 | 147 | fprintf(stderr, _("no directory given for --git-dir\n" )); |
c321f00d BG |
148 | usage(git_usage_string); |
149 | } | |
45b09794 | 150 | setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1); |
4394efec ML |
151 | if (envchanged) |
152 | *envchanged = 1; | |
6acbcb92 JS |
153 | (*argv)++; |
154 | (*argc)--; | |
ae021d87 JK |
155 | } else if (skip_prefix(cmd, "--git-dir=", &cmd)) { |
156 | setenv(GIT_DIR_ENVIRONMENT, cmd, 1); | |
4394efec ML |
157 | if (envchanged) |
158 | *envchanged = 1; | |
a1bea2c1 JT |
159 | } else if (!strcmp(cmd, "--namespace")) { |
160 | if (*argc < 2) { | |
fc045fe7 | 161 | fprintf(stderr, _("no namespace given for --namespace\n" )); |
a1bea2c1 JT |
162 | usage(git_usage_string); |
163 | } | |
164 | setenv(GIT_NAMESPACE_ENVIRONMENT, (*argv)[1], 1); | |
165 | if (envchanged) | |
166 | *envchanged = 1; | |
167 | (*argv)++; | |
168 | (*argc)--; | |
ae021d87 JK |
169 | } else if (skip_prefix(cmd, "--namespace=", &cmd)) { |
170 | setenv(GIT_NAMESPACE_ENVIRONMENT, cmd, 1); | |
a1bea2c1 JT |
171 | if (envchanged) |
172 | *envchanged = 1; | |
892c41b9 ML |
173 | } else if (!strcmp(cmd, "--work-tree")) { |
174 | if (*argc < 2) { | |
fc045fe7 | 175 | fprintf(stderr, _("no directory given for --work-tree\n" )); |
892c41b9 ML |
176 | usage(git_usage_string); |
177 | } | |
178 | setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1); | |
4394efec ML |
179 | if (envchanged) |
180 | *envchanged = 1; | |
892c41b9 ML |
181 | (*argv)++; |
182 | (*argc)--; | |
ae021d87 JK |
183 | } else if (skip_prefix(cmd, "--work-tree=", &cmd)) { |
184 | setenv(GIT_WORK_TREE_ENVIRONMENT, cmd, 1); | |
4394efec ML |
185 | if (envchanged) |
186 | *envchanged = 1; | |
74866d75 BW |
187 | } else if (!strcmp(cmd, "--super-prefix")) { |
188 | if (*argc < 2) { | |
fc045fe7 | 189 | fprintf(stderr, _("no prefix given for --super-prefix\n" )); |
74866d75 BW |
190 | usage(git_usage_string); |
191 | } | |
192 | setenv(GIT_SUPER_PREFIX_ENVIRONMENT, (*argv)[1], 1); | |
193 | if (envchanged) | |
194 | *envchanged = 1; | |
195 | (*argv)++; | |
196 | (*argc)--; | |
197 | } else if (skip_prefix(cmd, "--super-prefix=", &cmd)) { | |
198 | setenv(GIT_SUPER_PREFIX_ENVIRONMENT, cmd, 1); | |
199 | if (envchanged) | |
200 | *envchanged = 1; | |
6acbcb92 | 201 | } else if (!strcmp(cmd, "--bare")) { |
4d3ab44d | 202 | char *cwd = xgetcwd(); |
6adcca3f | 203 | is_bare_repository_cfg = 1; |
4d3ab44d RS |
204 | setenv(GIT_DIR_ENVIRONMENT, cwd, 0); |
205 | free(cwd); | |
2cd83d10 | 206 | setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1); |
4394efec ML |
207 | if (envchanged) |
208 | *envchanged = 1; | |
8b1fa778 AR |
209 | } else if (!strcmp(cmd, "-c")) { |
210 | if (*argc < 2) { | |
fc045fe7 | 211 | fprintf(stderr, _("-c expects a configuration string\n" )); |
8b1fa778 AR |
212 | usage(git_usage_string); |
213 | } | |
2b64fc89 | 214 | git_config_push_parameter((*argv)[1]); |
8b1fa778 AR |
215 | (*argv)++; |
216 | (*argc)--; | |
823ab40f JK |
217 | } else if (!strcmp(cmd, "--literal-pathspecs")) { |
218 | setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "1", 1); | |
219 | if (envchanged) | |
220 | *envchanged = 1; | |
221 | } else if (!strcmp(cmd, "--no-literal-pathspecs")) { | |
222 | setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "0", 1); | |
223 | if (envchanged) | |
224 | *envchanged = 1; | |
bd30c2e4 NTND |
225 | } else if (!strcmp(cmd, "--glob-pathspecs")) { |
226 | setenv(GIT_GLOB_PATHSPECS_ENVIRONMENT, "1", 1); | |
227 | if (envchanged) | |
228 | *envchanged = 1; | |
229 | } else if (!strcmp(cmd, "--noglob-pathspecs")) { | |
230 | setenv(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, "1", 1); | |
231 | if (envchanged) | |
232 | *envchanged = 1; | |
93d93537 NTND |
233 | } else if (!strcmp(cmd, "--icase-pathspecs")) { |
234 | setenv(GIT_ICASE_PATHSPECS_ENVIRONMENT, "1", 1); | |
235 | if (envchanged) | |
236 | *envchanged = 1; | |
27344d6a JK |
237 | } else if (!strcmp(cmd, "--no-optional-locks")) { |
238 | setenv(GIT_OPTIONAL_LOCKS_ENVIRONMENT, "0", 1); | |
239 | if (envchanged) | |
240 | *envchanged = 1; | |
6035d6aa NTND |
241 | } else if (!strcmp(cmd, "--shallow-file")) { |
242 | (*argv)++; | |
243 | (*argc)--; | |
069c0532 | 244 | set_alternate_shallow_file((*argv)[0], 1); |
6035d6aa NTND |
245 | if (envchanged) |
246 | *envchanged = 1; | |
44e1e4d6 NR |
247 | } else if (!strcmp(cmd, "-C")) { |
248 | if (*argc < 2) { | |
fc045fe7 | 249 | fprintf(stderr, _("no directory given for -C\n" )); |
44e1e4d6 NR |
250 | usage(git_usage_string); |
251 | } | |
6a536e20 KN |
252 | if ((*argv)[1][0]) { |
253 | if (chdir((*argv)[1])) | |
fc045fe7 | 254 | die_errno("cannot change to '%s'", (*argv)[1]); |
6a536e20 KN |
255 | if (envchanged) |
256 | *envchanged = 1; | |
257 | } | |
44e1e4d6 NR |
258 | (*argv)++; |
259 | (*argc)--; | |
0089521c NTND |
260 | } else if (skip_prefix(cmd, "--list-cmds=", &cmd)) { |
261 | if (!strcmp(cmd, "parseopt")) { | |
e5d7a619 NTND |
262 | struct string_list list = STRING_LIST_INIT_DUP; |
263 | int i; | |
264 | ||
265 | list_builtins(&list, NO_PARSEOPT); | |
266 | for (i = 0; i < list.nr; i++) | |
267 | printf("%s ", list.items[i].string); | |
268 | string_list_clear(&list, 0); | |
0089521c NTND |
269 | exit(0); |
270 | } else { | |
271 | exit(list_cmds(cmd)); | |
272 | } | |
6acbcb92 | 273 | } else { |
fc045fe7 | 274 | fprintf(stderr, _("unknown option: %s\n"), cmd); |
822a7d50 | 275 | usage(git_usage_string); |
6acbcb92 | 276 | } |
4ab243a9 JS |
277 | |
278 | (*argv)++; | |
279 | (*argc)--; | |
4ab243a9 | 280 | } |
73546c08 | 281 | return (*argv) - orig_argv; |
4ab243a9 JS |
282 | } |
283 | ||
2b11e317 JS |
284 | static int handle_alias(int *argcp, const char ***argv) |
285 | { | |
af05d679 | 286 | int envchanged = 0, ret = 0, saved_errno = errno; |
0347a8c7 | 287 | int count, option_count; |
4b25d091 | 288 | const char **new_argv; |
94351118 JK |
289 | const char *alias_command; |
290 | char *alias_string; | |
0347a8c7 ML |
291 | |
292 | alias_command = (*argv)[0]; | |
94351118 | 293 | alias_string = alias_lookup(alias_command); |
0347a8c7 | 294 | if (alias_string) { |
dfd42a3c | 295 | if (alias_string[0] == '!') { |
850d2fec | 296 | struct child_process child = CHILD_PROCESS_INIT; |
a9bcf658 JS |
297 | int nongit_ok; |
298 | ||
299 | /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */ | |
300 | setup_git_directory_gently(&nongit_ok); | |
7f51f8bc | 301 | |
030149a4 | 302 | commit_pager_choice(); |
7f51f8bc | 303 | |
850d2fec JK |
304 | child.use_shell = 1; |
305 | argv_array_push(&child.args, alias_string + 1); | |
306 | argv_array_pushv(&child.args, (*argv) + 1); | |
7f51f8bc | 307 | |
850d2fec | 308 | ret = run_command(&child); |
7f51f8bc EFL |
309 | if (ret >= 0) /* normal exit */ |
310 | exit(ret); | |
311 | ||
fc045fe7 | 312 | die_errno("while expanding alias '%s': '%s'", |
7f51f8bc | 313 | alias_command, alias_string + 1); |
dfd42a3c | 314 | } |
0347a8c7 | 315 | count = split_cmdline(alias_string, &new_argv); |
dc4179f9 | 316 | if (count < 0) |
ad9ac6db GB |
317 | die("Bad alias.%s string: %s", alias_command, |
318 | split_cmdline_strerror(count)); | |
4394efec ML |
319 | option_count = handle_options(&new_argv, &count, &envchanged); |
320 | if (envchanged) | |
fc045fe7 AS |
321 | die("alias '%s' changes environment variables.\n" |
322 | "You can use '!git' in the alias to do this", | |
4394efec | 323 | alias_command); |
0347a8c7 ML |
324 | memmove(new_argv - option_count, new_argv, |
325 | count * sizeof(char *)); | |
326 | new_argv -= option_count; | |
327 | ||
328 | if (count < 1) | |
329 | die("empty alias for %s", alias_command); | |
330 | ||
331 | if (!strcmp(alias_command, new_argv[0])) | |
332 | die("recursive alias: %s", alias_command); | |
333 | ||
b319ce4c | 334 | trace_argv_printf(new_argv, |
6ce4e61f CC |
335 | "trace: alias expansion: %s =>", |
336 | alias_command); | |
575ba9d6 | 337 | |
2756ca43 | 338 | REALLOC_ARRAY(new_argv, count + *argcp); |
0347a8c7 | 339 | /* insert after command name */ |
4b25d091 | 340 | memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp); |
2b11e317 | 341 | |
0347a8c7 ML |
342 | *argv = new_argv; |
343 | *argcp += count - 1; | |
2b11e317 | 344 | |
0347a8c7 | 345 | ret = 1; |
2b11e317 JS |
346 | } |
347 | ||
47e5c0ca JS |
348 | errno = saved_errno; |
349 | ||
2b11e317 JS |
350 | return ret; |
351 | } | |
352 | ||
f172f334 | 353 | static int run_builtin(struct cmd_struct *p, int argc, const char **argv) |
47d0b4ff | 354 | { |
99caeed0 | 355 | int status, help; |
0f157315 | 356 | struct stat st; |
47d0b4ff LT |
357 | const char *prefix; |
358 | ||
359 | prefix = NULL; | |
99caeed0 JN |
360 | help = argc == 2 && !strcmp(argv[1], "-h"); |
361 | if (!help) { | |
362 | if (p->option & RUN_SETUP) | |
363 | prefix = setup_git_directory(); | |
27bd38d4 | 364 | else if (p->option & RUN_SETUP_GENTLY) { |
ee38dfb8 NTND |
365 | int nongit_ok; |
366 | prefix = setup_git_directory_gently(&nongit_ok); | |
367 | } | |
99caeed0 | 368 | |
c409824c MÅ |
369 | if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY) && |
370 | !(p->option & DELAY_PAGER_CONFIG)) | |
99caeed0 JN |
371 | use_pager = check_pager_config(p->cmd); |
372 | if (use_pager == -1 && p->option & USE_PAGER) | |
373 | use_pager = 1; | |
a9ca8a85 NTND |
374 | |
375 | if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) && | |
376 | startup_info->have_repository) /* get_git_dir() may set up repo, avoid that */ | |
377 | trace_repo_setup(prefix); | |
99caeed0 | 378 | } |
4e10738a JK |
379 | commit_pager_choice(); |
380 | ||
74866d75 BW |
381 | if (!help && get_super_prefix()) { |
382 | if (!(p->option & SUPPORT_SUPER_PREFIX)) | |
383 | die("%s doesn't support --super-prefix", p->cmd); | |
74866d75 BW |
384 | } |
385 | ||
99caeed0 | 386 | if (!help && p->option & NEED_WORK_TREE) |
59f0f2f3 MH |
387 | setup_work_tree(); |
388 | ||
b319ce4c | 389 | trace_argv_printf(argv, "trace: built-in: git"); |
47d0b4ff | 390 | |
0f157315 LT |
391 | status = p->fn(argc, argv, prefix); |
392 | if (status) | |
47e3de0e | 393 | return status; |
0f157315 LT |
394 | |
395 | /* Somebody closed stdout? */ | |
396 | if (fstat(fileno(stdout), &st)) | |
397 | return 0; | |
398 | /* Ignore write errors for pipes and sockets.. */ | |
399 | if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) | |
400 | return 0; | |
401 | ||
402 | /* Check for ENOSPC and EIO errors.. */ | |
0227f988 | 403 | if (fflush(stdout)) |
d824cbba | 404 | die_errno("write failure on standard output"); |
0227f988 LT |
405 | if (ferror(stdout)) |
406 | die("unknown write failure on standard output"); | |
407 | if (fclose(stdout)) | |
d824cbba | 408 | die_errno("close failed on standard output"); |
0f157315 | 409 | return 0; |
47d0b4ff LT |
410 | } |
411 | ||
c6127fa3 SS |
412 | static struct cmd_struct commands[] = { |
413 | { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE }, | |
783d7e86 | 414 | { "am", cmd_am, RUN_SETUP | NEED_WORK_TREE }, |
e9547947 | 415 | { "annotate", cmd_annotate, RUN_SETUP | NO_PARSEOPT }, |
c6127fa3 | 416 | { "apply", cmd_apply, RUN_SETUP_GENTLY }, |
eb0224c6 | 417 | { "archive", cmd_archive, RUN_SETUP_GENTLY }, |
c6127fa3 SS |
418 | { "bisect--helper", cmd_bisect__helper, RUN_SETUP }, |
419 | { "blame", cmd_blame, RUN_SETUP }, | |
d74b541e | 420 | { "branch", cmd_branch, RUN_SETUP | DELAY_PAGER_CONFIG }, |
e9547947 | 421 | { "bundle", cmd_bundle, RUN_SETUP_GENTLY | NO_PARSEOPT }, |
c6127fa3 SS |
422 | { "cat-file", cmd_cat_file, RUN_SETUP }, |
423 | { "check-attr", cmd_check_attr, RUN_SETUP }, | |
424 | { "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE }, | |
425 | { "check-mailmap", cmd_check_mailmap, RUN_SETUP }, | |
e9547947 | 426 | { "check-ref-format", cmd_check_ref_format, NO_PARSEOPT }, |
0ca560cb | 427 | { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE }, |
c6127fa3 SS |
428 | { "checkout-index", cmd_checkout_index, |
429 | RUN_SETUP | NEED_WORK_TREE}, | |
430 | { "cherry", cmd_cherry, RUN_SETUP }, | |
431 | { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE }, | |
432 | { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE }, | |
86d26f24 | 433 | { "clone", cmd_clone }, |
c6127fa3 SS |
434 | { "column", cmd_column, RUN_SETUP_GENTLY }, |
435 | { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE }, | |
e9547947 | 436 | { "commit-tree", cmd_commit_tree, RUN_SETUP | NO_PARSEOPT }, |
32888b8f | 437 | { "config", cmd_config, RUN_SETUP_GENTLY | DELAY_PAGER_CONFIG }, |
c6127fa3 | 438 | { "count-objects", cmd_count_objects, RUN_SETUP }, |
e9547947 | 439 | { "credential", cmd_credential, RUN_SETUP_GENTLY | NO_PARSEOPT }, |
c6127fa3 | 440 | { "describe", cmd_describe, RUN_SETUP }, |
e9547947 NTND |
441 | { "diff", cmd_diff, NO_PARSEOPT }, |
442 | { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, | |
443 | { "diff-index", cmd_diff_index, RUN_SETUP | NO_PARSEOPT }, | |
444 | { "diff-tree", cmd_diff_tree, RUN_SETUP | NO_PARSEOPT }, | |
019678d6 | 445 | { "difftool", cmd_difftool, RUN_SETUP | NEED_WORK_TREE }, |
c6127fa3 SS |
446 | { "fast-export", cmd_fast_export, RUN_SETUP }, |
447 | { "fetch", cmd_fetch, RUN_SETUP }, | |
e9547947 | 448 | { "fetch-pack", cmd_fetch_pack, RUN_SETUP | NO_PARSEOPT }, |
c6127fa3 SS |
449 | { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP }, |
450 | { "for-each-ref", cmd_for_each_ref, RUN_SETUP }, | |
451 | { "format-patch", cmd_format_patch, RUN_SETUP }, | |
452 | { "fsck", cmd_fsck, RUN_SETUP }, | |
453 | { "fsck-objects", cmd_fsck, RUN_SETUP }, | |
454 | { "gc", cmd_gc, RUN_SETUP }, | |
e9547947 | 455 | { "get-tar-commit-id", cmd_get_tar_commit_id, NO_PARSEOPT }, |
f9ee2fcd | 456 | { "grep", cmd_grep, RUN_SETUP_GENTLY }, |
c6127fa3 SS |
457 | { "hash-object", cmd_hash_object }, |
458 | { "help", cmd_help }, | |
e9547947 | 459 | { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY | NO_PARSEOPT }, |
86d26f24 NTND |
460 | { "init", cmd_init_db }, |
461 | { "init-db", cmd_init_db }, | |
cbd9fc23 | 462 | { "interpret-trailers", cmd_interpret_trailers, RUN_SETUP_GENTLY }, |
c6127fa3 | 463 | { "log", cmd_log, RUN_SETUP }, |
188dce13 | 464 | { "ls-files", cmd_ls_files, RUN_SETUP }, |
c6127fa3 SS |
465 | { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY }, |
466 | { "ls-tree", cmd_ls_tree, RUN_SETUP }, | |
e9547947 NTND |
467 | { "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY | NO_PARSEOPT }, |
468 | { "mailsplit", cmd_mailsplit, NO_PARSEOPT }, | |
c6127fa3 SS |
469 | { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE }, |
470 | { "merge-base", cmd_merge_base, RUN_SETUP }, | |
471 | { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY }, | |
e9547947 NTND |
472 | { "merge-index", cmd_merge_index, RUN_SETUP | NO_PARSEOPT }, |
473 | { "merge-ours", cmd_merge_ours, RUN_SETUP | NO_PARSEOPT }, | |
474 | { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, | |
475 | { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, | |
476 | { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, | |
477 | { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, | |
478 | { "merge-tree", cmd_merge_tree, RUN_SETUP | NO_PARSEOPT }, | |
479 | { "mktag", cmd_mktag, RUN_SETUP | NO_PARSEOPT }, | |
c6127fa3 SS |
480 | { "mktree", cmd_mktree, RUN_SETUP }, |
481 | { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE }, | |
482 | { "name-rev", cmd_name_rev, RUN_SETUP }, | |
483 | { "notes", cmd_notes, RUN_SETUP }, | |
484 | { "pack-objects", cmd_pack_objects, RUN_SETUP }, | |
e9547947 | 485 | { "pack-redundant", cmd_pack_redundant, RUN_SETUP | NO_PARSEOPT }, |
c6127fa3 | 486 | { "pack-refs", cmd_pack_refs, RUN_SETUP }, |
e9547947 | 487 | { "patch-id", cmd_patch_id, RUN_SETUP_GENTLY | NO_PARSEOPT }, |
c6127fa3 SS |
488 | { "pickaxe", cmd_blame, RUN_SETUP }, |
489 | { "prune", cmd_prune, RUN_SETUP }, | |
490 | { "prune-packed", cmd_prune_packed, RUN_SETUP }, | |
1e1ea69f | 491 | { "pull", cmd_pull, RUN_SETUP | NEED_WORK_TREE }, |
c6127fa3 | 492 | { "push", cmd_push, RUN_SETUP }, |
3d415425 | 493 | { "read-tree", cmd_read_tree, RUN_SETUP | SUPPORT_SUPER_PREFIX}, |
4557f1ad | 494 | { "rebase--helper", cmd_rebase__helper, RUN_SETUP | NEED_WORK_TREE }, |
c6127fa3 SS |
495 | { "receive-pack", cmd_receive_pack }, |
496 | { "reflog", cmd_reflog, RUN_SETUP }, | |
497 | { "remote", cmd_remote, RUN_SETUP }, | |
e9547947 NTND |
498 | { "remote-ext", cmd_remote_ext, NO_PARSEOPT }, |
499 | { "remote-fd", cmd_remote_fd, NO_PARSEOPT }, | |
c6127fa3 SS |
500 | { "repack", cmd_repack, RUN_SETUP }, |
501 | { "replace", cmd_replace, RUN_SETUP }, | |
502 | { "rerere", cmd_rerere, RUN_SETUP }, | |
503 | { "reset", cmd_reset, RUN_SETUP }, | |
e9547947 NTND |
504 | { "rev-list", cmd_rev_list, RUN_SETUP | NO_PARSEOPT }, |
505 | { "rev-parse", cmd_rev_parse, NO_PARSEOPT }, | |
c6127fa3 SS |
506 | { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE }, |
507 | { "rm", cmd_rm, RUN_SETUP }, | |
508 | { "send-pack", cmd_send_pack, RUN_SETUP }, | |
509 | { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER }, | |
510 | { "show", cmd_show, RUN_SETUP }, | |
511 | { "show-branch", cmd_show_branch, RUN_SETUP }, | |
512 | { "show-ref", cmd_show_ref, RUN_SETUP }, | |
513 | { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE }, | |
514 | { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE }, | |
515 | { "stripspace", cmd_stripspace }, | |
e9547947 | 516 | { "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT }, |
c6127fa3 | 517 | { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP }, |
de121ffe | 518 | { "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG }, |
e9547947 NTND |
519 | { "unpack-file", cmd_unpack_file, RUN_SETUP | NO_PARSEOPT }, |
520 | { "unpack-objects", cmd_unpack_objects, RUN_SETUP | NO_PARSEOPT }, | |
c6127fa3 SS |
521 | { "update-index", cmd_update_index, RUN_SETUP }, |
522 | { "update-ref", cmd_update_ref, RUN_SETUP }, | |
523 | { "update-server-info", cmd_update_server_info, RUN_SETUP }, | |
e9547947 NTND |
524 | { "upload-archive", cmd_upload_archive, NO_PARSEOPT }, |
525 | { "upload-archive--writer", cmd_upload_archive_writer, NO_PARSEOPT }, | |
526 | { "var", cmd_var, RUN_SETUP_GENTLY | NO_PARSEOPT }, | |
d07b00b7 | 527 | { "verify-commit", cmd_verify_commit, RUN_SETUP }, |
c6127fa3 SS |
528 | { "verify-pack", cmd_verify_pack }, |
529 | { "verify-tag", cmd_verify_tag, RUN_SETUP }, | |
530 | { "version", cmd_version }, | |
531 | { "whatchanged", cmd_whatchanged, RUN_SETUP }, | |
e9547947 | 532 | { "worktree", cmd_worktree, RUN_SETUP | NO_PARSEOPT }, |
c6127fa3 SS |
533 | { "write-tree", cmd_write_tree, RUN_SETUP }, |
534 | }; | |
535 | ||
c4f901d1 | 536 | static struct cmd_struct *get_builtin(const char *s) |
c6127fa3 SS |
537 | { |
538 | int i; | |
539 | for (i = 0; i < ARRAY_SIZE(commands); i++) { | |
c4f901d1 | 540 | struct cmd_struct *p = commands + i; |
c6127fa3 | 541 | if (!strcmp(s, p->cmd)) |
c4f901d1 | 542 | return p; |
c6127fa3 | 543 | } |
c4f901d1 SV |
544 | return NULL; |
545 | } | |
546 | ||
547 | int is_builtin(const char *s) | |
548 | { | |
549 | return !!get_builtin(s); | |
c6127fa3 SS |
550 | } |
551 | ||
e5d7a619 | 552 | static void list_builtins(struct string_list *out, unsigned int exclude_option) |
8893fd95 JK |
553 | { |
554 | int i; | |
e9547947 NTND |
555 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
556 | if (exclude_option && | |
557 | (commands[i].option & exclude_option)) | |
558 | continue; | |
e5d7a619 | 559 | string_list_append(out, commands[i].cmd); |
e9547947 | 560 | } |
8893fd95 JK |
561 | } |
562 | ||
63ca1c09 AK |
563 | #ifdef STRIP_EXTENSION |
564 | static void strip_extension(const char **argv) | |
565 | { | |
566 | size_t len; | |
567 | ||
568 | if (strip_suffix(argv[0], STRIP_EXTENSION, &len)) | |
569 | argv[0] = xmemdupz(argv[0], len); | |
570 | } | |
571 | #else | |
572 | #define strip_extension(cmd) | |
573 | #endif | |
574 | ||
3f784a4d | 575 | static void handle_builtin(int argc, const char **argv) |
231af832 | 576 | { |
2c6b6d9f | 577 | struct argv_array args = ARGV_ARRAY_INIT; |
63ca1c09 | 578 | const char *cmd; |
c4f901d1 | 579 | struct cmd_struct *builtin; |
23326d14 | 580 | |
63ca1c09 AK |
581 | strip_extension(argv); |
582 | cmd = argv[0]; | |
231af832 | 583 | |
2c6b6d9f | 584 | /* Turn "git cmd --help" into "git help --exclude-guides cmd" */ |
1cd95087 | 585 | if (argc > 1 && !strcmp(argv[1], "--help")) { |
2c6b6d9f RT |
586 | int i; |
587 | ||
1cd95087 LT |
588 | argv[1] = argv[0]; |
589 | argv[0] = cmd = "help"; | |
2c6b6d9f RT |
590 | |
591 | for (i = 0; i < argc; i++) { | |
592 | argv_array_push(&args, argv[i]); | |
593 | if (!i) | |
594 | argv_array_push(&args, "--exclude-guides"); | |
595 | } | |
596 | ||
597 | argc++; | |
598 | argv = args.argv; | |
1cd95087 LT |
599 | } |
600 | ||
c4f901d1 | 601 | builtin = get_builtin(cmd); |
441981bc JH |
602 | if (builtin) |
603 | exit(run_builtin(builtin, argc, argv)); | |
2c6b6d9f | 604 | argv_array_clear(&args); |
231af832 LT |
605 | } |
606 | ||
7550be0a JH |
607 | static void execv_dashed_external(const char **argv) |
608 | { | |
2b296c93 | 609 | struct child_process cmd = CHILD_PROCESS_INIT; |
d8e96fd8 | 610 | int status; |
7550be0a | 611 | |
74866d75 BW |
612 | if (get_super_prefix()) |
613 | die("%s doesn't support --super-prefix", argv[0]); | |
614 | ||
595d59e2 | 615 | if (use_pager == -1 && !is_builtin(argv[0])) |
92058e4d | 616 | use_pager = check_pager_config(argv[0]); |
030149a4 JN |
617 | commit_pager_choice(); |
618 | ||
2b296c93 JK |
619 | argv_array_pushf(&cmd.args, "git-%s", argv[0]); |
620 | argv_array_pushv(&cmd.args, argv + 1); | |
621 | cmd.clean_on_exit = 1; | |
46df6906 | 622 | cmd.wait_after_clean = 1; |
2b296c93 | 623 | cmd.silent_exec_failure = 1; |
7550be0a | 624 | |
2b296c93 | 625 | trace_argv_printf(cmd.args.argv, "trace: exec:"); |
7550be0a | 626 | |
d8e96fd8 | 627 | /* |
246f0ede JK |
628 | * If we fail because the command is not found, it is |
629 | * OK to return. Otherwise, we just pass along the status code, | |
630 | * or our usual generic code if we were not even able to exec | |
631 | * the program. | |
d8e96fd8 | 632 | */ |
2b296c93 | 633 | status = run_command(&cmd); |
246f0ede | 634 | if (status >= 0) |
5709e036 | 635 | exit(status); |
246f0ede JK |
636 | else if (errno != ENOENT) |
637 | exit(128); | |
7550be0a JH |
638 | } |
639 | ||
a907e1b6 AS |
640 | static int run_argv(int *argcp, const char ***argv) |
641 | { | |
642 | int done_alias = 0; | |
643 | ||
644 | while (1) { | |
441981bc JH |
645 | /* |
646 | * If we tried alias and futzed with our environment, | |
647 | * it no longer is safe to invoke builtins directly in | |
648 | * general. We have to spawn them as dashed externals. | |
649 | * | |
650 | * NEEDSWORK: if we can figure out cases | |
651 | * where it is safe to do, we can avoid spawning a new | |
652 | * process. | |
653 | */ | |
654 | if (!done_alias) | |
655 | handle_builtin(*argcp, *argv); | |
a907e1b6 AS |
656 | |
657 | /* .. then try the external ones */ | |
658 | execv_dashed_external(*argv); | |
659 | ||
660 | /* It could be an alias -- this works around the insanity | |
661 | * of overriding "git log" with "git show" by having | |
662 | * alias.log = show | |
663 | */ | |
c0562611 NTND |
664 | if (done_alias) |
665 | break; | |
c0562611 | 666 | if (!handle_alias(argcp, argv)) |
a907e1b6 AS |
667 | break; |
668 | done_alias = 1; | |
669 | } | |
670 | ||
671 | return done_alias; | |
672 | } | |
673 | ||
3f2e2297 | 674 | int cmd_main(int argc, const char **argv) |
8e49d503 | 675 | { |
4dd47c3b | 676 | const char *cmd; |
8fa7975b | 677 | int done_help = 0; |
231af832 | 678 | |
650c4492 | 679 | cmd = argv[0]; |
2cd72b0b | 680 | if (!cmd) |
4dd47c3b | 681 | cmd = "git-help"; |
6854a8f5 JK |
682 | else { |
683 | const char *slash = find_last_dir_sep(cmd); | |
684 | if (slash) | |
685 | cmd = slash + 1; | |
686 | } | |
8e49d503 | 687 | |
578da039 KB |
688 | trace_command_performance(argv); |
689 | ||
231af832 LT |
690 | /* |
691 | * "git-xxxx" is the same as "git xxxx", but we obviously: | |
692 | * | |
693 | * - cannot take flags in between the "git" and the "xxxx". | |
694 | * - cannot execute it externally (since it would just do | |
695 | * the same thing over again) | |
696 | * | |
3f784a4d SS |
697 | * So we just directly call the builtin handler, and die if |
698 | * that one cannot handle it. | |
231af832 | 699 | */ |
ae021d87 | 700 | if (skip_prefix(cmd, "git-", &cmd)) { |
231af832 | 701 | argv[0] = cmd; |
3f784a4d SS |
702 | handle_builtin(argc, argv); |
703 | die("cannot handle %s as a builtin", cmd); | |
231af832 | 704 | } |
8e49d503 | 705 | |
231af832 | 706 | /* Look for flags.. */ |
6acbcb92 JS |
707 | argv++; |
708 | argc--; | |
4394efec | 709 | handle_options(&argv, &argc, NULL); |
6acbcb92 | 710 | if (argc > 0) { |
6d877803 JK |
711 | /* translate --help and --version into commands */ |
712 | skip_prefix(argv[0], "--", &argv[0]); | |
6acbcb92 | 713 | } else { |
3d7e2d85 | 714 | /* The user didn't specify a command; give them help */ |
73e25e7c | 715 | commit_pager_choice(); |
3d7e2d85 SP |
716 | printf("usage: %s\n\n", git_usage_string); |
717 | list_common_cmds_help(); | |
421a9769 | 718 | printf("\n%s\n", _(git_more_info_string)); |
3d7e2d85 | 719 | exit(1); |
97fc6c5f | 720 | } |
6acbcb92 | 721 | cmd = argv[0]; |
231af832 LT |
722 | |
723 | /* | |
511707d4 | 724 | * We use PATH to find git commands, but we prepend some higher |
3ea3c215 | 725 | * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH |
511707d4 SP |
726 | * environment, and the $(gitexecdir) from the Makefile at build |
727 | * time. | |
231af832 | 728 | */ |
e1464ca7 | 729 | setup_path(); |
8e49d503 | 730 | |
a025463b | 731 | while (1) { |
8fa7975b | 732 | int was_alias = run_argv(&argc, &argv); |
a907e1b6 | 733 | if (errno != ENOENT) |
a025463b | 734 | break; |
a907e1b6 | 735 | if (was_alias) { |
fc045fe7 AS |
736 | fprintf(stderr, _("expansion of alias '%s' failed; " |
737 | "'%s' is not a git command\n"), | |
f3dd015c TT |
738 | cmd, argv[0]); |
739 | exit(1); | |
740 | } | |
a907e1b6 AS |
741 | if (!done_help) { |
742 | cmd = argv[0] = help_unknown_cmd(cmd); | |
743 | done_help = 1; | |
744 | } else | |
745 | break; | |
f3dd015c | 746 | } |
10b15b86 | 747 | |
fc045fe7 | 748 | fprintf(stderr, _("failed to run command '%s': %s\n"), |
33ebb871 | 749 | cmd, strerror(errno)); |
8e49d503 AE |
750 | |
751 | return 1; | |
752 | } |