git.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. #include "builtin.h"
  2. #include "config.h"
  3. #include "exec-cmd.h"
  4. #include "help.h"
  5. #include "run-command.h"
  6. #include "alias.h"
  7. #include "shallow.h"
  8. #define RUN_SETUP (1<<0)
  9. #define RUN_SETUP_GENTLY (1<<1)
  10. #define USE_PAGER (1<<2)
  11. /*
  12. * require working tree to be present -- anything uses this needs
  13. * RUN_SETUP for reading from the configuration file.
  14. */
  15. #define NEED_WORK_TREE (1<<3)
  16. #define SUPPORT_SUPER_PREFIX (1<<4)
  17. #define DELAY_PAGER_CONFIG (1<<5)
  18. #define NO_PARSEOPT (1<<6) /* parse-options is not used */
  19. struct cmd_struct {
  20. const char *cmd;
  21. int (*fn)(int, const char **, const char *);
  22. unsigned int option;
  23. };
  24. const char git_usage_string[] =
  25. N_("git [--version] [--help] [-C <path>] [-c <name>=<value>]\n"
  26. " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
  27. " [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]\n"
  28. " [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
  29. " <command> [<args>]");
  30. const char git_more_info_string[] =
  31. N_("'git help -a' and 'git help -g' list available subcommands and some\n"
  32. "concept guides. See 'git help <command>' or 'git help <concept>'\n"
  33. "to read about a specific subcommand or concept.\n"
  34. "See 'git help git' for an overview of the system.");
  35. static int use_pager = -1;
  36. static void list_builtins(struct string_list *list, unsigned int exclude_option);
  37. static void exclude_helpers_from_list(struct string_list *list)
  38. {
  39. int i = 0;
  40. while (i < list->nr) {
  41. if (strstr(list->items[i].string, "--"))
  42. unsorted_string_list_delete_item(list, i, 0);
  43. else
  44. i++;
  45. }
  46. }
  47. static int match_token(const char *spec, int len, const char *token)
  48. {
  49. int token_len = strlen(token);
  50. return len == token_len && !strncmp(spec, token, token_len);
  51. }
  52. static int list_cmds(const char *spec)
  53. {
  54. struct string_list list = STRING_LIST_INIT_DUP;
  55. int i;
  56. int nongit;
  57. /*
  58. * Set up the repository so we can pick up any repo-level config (like
  59. * completion.commands).
  60. */
  61. setup_git_directory_gently(&nongit);
  62. while (*spec) {
  63. const char *sep = strchrnul(spec, ',');
  64. int len = sep - spec;
  65. if (match_token(spec, len, "builtins"))
  66. list_builtins(&list, 0);
  67. else if (match_token(spec, len, "main"))
  68. list_all_main_cmds(&list);
  69. else if (match_token(spec, len, "others"))
  70. list_all_other_cmds(&list);
  71. else if (match_token(spec, len, "nohelpers"))
  72. exclude_helpers_from_list(&list);
  73. else if (match_token(spec, len, "alias"))
  74. list_aliases(&list);
  75. else if (match_token(spec, len, "config"))
  76. list_cmds_by_config(&list);
  77. else if (len > 5 && !strncmp(spec, "list-", 5)) {
  78. struct strbuf sb = STRBUF_INIT;
  79. strbuf_add(&sb, spec + 5, len - 5);
  80. list_cmds_by_category(&list, sb.buf);
  81. strbuf_release(&sb);
  82. }
  83. else
  84. die(_("unsupported command listing type '%s'"), spec);
  85. spec += len;
  86. if (*spec == ',')
  87. spec++;
  88. }
  89. for (i = 0; i < list.nr; i++)
  90. puts(list.items[i].string);
  91. string_list_clear(&list, 0);
  92. return 0;
  93. }
  94. static void commit_pager_choice(void)
  95. {
  96. switch (use_pager) {
  97. case 0:
  98. setenv("GIT_PAGER", "cat", 1);
  99. break;
  100. case 1:
  101. setup_pager();
  102. break;
  103. default:
  104. break;
  105. }
  106. }
  107. void setup_auto_pager(const char *cmd, int def)
  108. {
  109. if (use_pager != -1 || pager_in_use())
  110. return;
  111. use_pager = check_pager_config(cmd);
  112. if (use_pager == -1)
  113. use_pager = def;
  114. commit_pager_choice();
  115. }
  116. static int handle_options(const char ***argv, int *argc, int *envchanged)
  117. {
  118. const char **orig_argv = *argv;
  119. while (*argc > 0) {
  120. const char *cmd = (*argv)[0];
  121. if (cmd[0] != '-')
  122. break;
  123. /*
  124. * For legacy reasons, the "version" and "help"
  125. * commands can be written with "--" prepended
  126. * to make them look like flags.
  127. */
  128. if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
  129. break;
  130. /*
  131. * Check remaining flags.
  132. */
  133. if (skip_prefix(cmd, "--exec-path", &cmd)) {
  134. if (*cmd == '=')
  135. git_set_exec_path(cmd + 1);
  136. else {
  137. puts(git_exec_path());
  138. trace2_cmd_name("_query_");
  139. exit(0);
  140. }
  141. } else if (!strcmp(cmd, "--html-path")) {
  142. puts(system_path(GIT_HTML_PATH));
  143. trace2_cmd_name("_query_");
  144. exit(0);
  145. } else if (!strcmp(cmd, "--man-path")) {
  146. puts(system_path(GIT_MAN_PATH));
  147. trace2_cmd_name("_query_");
  148. exit(0);
  149. } else if (!strcmp(cmd, "--info-path")) {
  150. puts(system_path(GIT_INFO_PATH));
  151. trace2_cmd_name("_query_");
  152. exit(0);
  153. } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
  154. use_pager = 1;
  155. } else if (!strcmp(cmd, "-P") || !strcmp(cmd, "--no-pager")) {
  156. use_pager = 0;
  157. if (envchanged)
  158. *envchanged = 1;
  159. } else if (!strcmp(cmd, "--no-replace-objects")) {
  160. read_replace_refs = 0;
  161. setenv(NO_REPLACE_OBJECTS_ENVIRONMENT, "1", 1);
  162. if (envchanged)
  163. *envchanged = 1;
  164. } else if (!strcmp(cmd, "--git-dir")) {
  165. if (*argc < 2) {
  166. fprintf(stderr, _("no directory given for --git-dir\n" ));
  167. usage(git_usage_string);
  168. }
  169. setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1);
  170. if (envchanged)
  171. *envchanged = 1;
  172. (*argv)++;
  173. (*argc)--;
  174. } else if (skip_prefix(cmd, "--git-dir=", &cmd)) {
  175. setenv(GIT_DIR_ENVIRONMENT, cmd, 1);
  176. if (envchanged)
  177. *envchanged = 1;
  178. } else if (!strcmp(cmd, "--namespace")) {
  179. if (*argc < 2) {
  180. fprintf(stderr, _("no namespace given for --namespace\n" ));
  181. usage(git_usage_string);
  182. }
  183. setenv(GIT_NAMESPACE_ENVIRONMENT, (*argv)[1], 1);
  184. if (envchanged)
  185. *envchanged = 1;
  186. (*argv)++;
  187. (*argc)--;
  188. } else if (skip_prefix(cmd, "--namespace=", &cmd)) {
  189. setenv(GIT_NAMESPACE_ENVIRONMENT, cmd, 1);
  190. if (envchanged)
  191. *envchanged = 1;
  192. } else if (!strcmp(cmd, "--work-tree")) {
  193. if (*argc < 2) {
  194. fprintf(stderr, _("no directory given for --work-tree\n" ));
  195. usage(git_usage_string);
  196. }
  197. setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
  198. if (envchanged)
  199. *envchanged = 1;
  200. (*argv)++;
  201. (*argc)--;
  202. } else if (skip_prefix(cmd, "--work-tree=", &cmd)) {
  203. setenv(GIT_WORK_TREE_ENVIRONMENT, cmd, 1);
  204. if (envchanged)
  205. *envchanged = 1;
  206. } else if (!strcmp(cmd, "--super-prefix")) {
  207. if (*argc < 2) {
  208. fprintf(stderr, _("no prefix given for --super-prefix\n" ));
  209. usage(git_usage_string);
  210. }
  211. setenv(GIT_SUPER_PREFIX_ENVIRONMENT, (*argv)[1], 1);
  212. if (envchanged)
  213. *envchanged = 1;
  214. (*argv)++;
  215. (*argc)--;
  216. } else if (skip_prefix(cmd, "--super-prefix=", &cmd)) {
  217. setenv(GIT_SUPER_PREFIX_ENVIRONMENT, cmd, 1);
  218. if (envchanged)
  219. *envchanged = 1;
  220. } else if (!strcmp(cmd, "--bare")) {
  221. char *cwd = xgetcwd();
  222. is_bare_repository_cfg = 1;
  223. setenv(GIT_DIR_ENVIRONMENT, cwd, 0);
  224. free(cwd);
  225. setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
  226. if (envchanged)
  227. *envchanged = 1;
  228. } else if (!strcmp(cmd, "-c")) {
  229. if (*argc < 2) {
  230. fprintf(stderr, _("-c expects a configuration string\n" ));
  231. usage(git_usage_string);
  232. }
  233. git_config_push_parameter((*argv)[1]);
  234. (*argv)++;
  235. (*argc)--;
  236. } else if (!strcmp(cmd, "--literal-pathspecs")) {
  237. setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "1", 1);
  238. if (envchanged)
  239. *envchanged = 1;
  240. } else if (!strcmp(cmd, "--no-literal-pathspecs")) {
  241. setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "0", 1);
  242. if (envchanged)
  243. *envchanged = 1;
  244. } else if (!strcmp(cmd, "--glob-pathspecs")) {
  245. setenv(GIT_GLOB_PATHSPECS_ENVIRONMENT, "1", 1);
  246. if (envchanged)
  247. *envchanged = 1;
  248. } else if (!strcmp(cmd, "--noglob-pathspecs")) {
  249. setenv(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, "1", 1);
  250. if (envchanged)
  251. *envchanged = 1;
  252. } else if (!strcmp(cmd, "--icase-pathspecs")) {
  253. setenv(GIT_ICASE_PATHSPECS_ENVIRONMENT, "1", 1);
  254. if (envchanged)
  255. *envchanged = 1;
  256. } else if (!strcmp(cmd, "--no-optional-locks")) {
  257. setenv(GIT_OPTIONAL_LOCKS_ENVIRONMENT, "0", 1);
  258. if (envchanged)
  259. *envchanged = 1;
  260. } else if (!strcmp(cmd, "--shallow-file")) {
  261. (*argv)++;
  262. (*argc)--;
  263. set_alternate_shallow_file(the_repository, (*argv)[0], 1);
  264. if (envchanged)
  265. *envchanged = 1;
  266. } else if (!strcmp(cmd, "-C")) {
  267. if (*argc < 2) {
  268. fprintf(stderr, _("no directory given for -C\n" ));
  269. usage(git_usage_string);
  270. }
  271. if ((*argv)[1][0]) {
  272. if (chdir((*argv)[1]))
  273. die_errno("cannot change to '%s'", (*argv)[1]);
  274. if (envchanged)
  275. *envchanged = 1;
  276. }
  277. (*argv)++;
  278. (*argc)--;
  279. } else if (skip_prefix(cmd, "--list-cmds=", &cmd)) {
  280. trace2_cmd_name("_query_");
  281. if (!strcmp(cmd, "parseopt")) {
  282. struct string_list list = STRING_LIST_INIT_DUP;
  283. int i;
  284. list_builtins(&list, NO_PARSEOPT);
  285. for (i = 0; i < list.nr; i++)
  286. printf("%s ", list.items[i].string);
  287. string_list_clear(&list, 0);
  288. exit(0);
  289. } else {
  290. exit(list_cmds(cmd));
  291. }
  292. } else {
  293. fprintf(stderr, _("unknown option: %s\n"), cmd);
  294. usage(git_usage_string);
  295. }
  296. (*argv)++;
  297. (*argc)--;
  298. }
  299. return (*argv) - orig_argv;
  300. }
  301. static int handle_alias(int *argcp, const char ***argv)
  302. {
  303. int envchanged = 0, ret = 0, saved_errno = errno;
  304. int count, option_count;
  305. const char **new_argv;
  306. const char *alias_command;
  307. char *alias_string;
  308. alias_command = (*argv)[0];
  309. alias_string = alias_lookup(alias_command);
  310. if (alias_string) {
  311. if (*argcp > 1 && !strcmp((*argv)[1], "-h"))
  312. fprintf_ln(stderr, _("'%s' is aliased to '%s'"),
  313. alias_command, alias_string);
  314. if (alias_string[0] == '!') {
  315. struct child_process child = CHILD_PROCESS_INIT;
  316. int nongit_ok;
  317. /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */
  318. setup_git_directory_gently(&nongit_ok);
  319. commit_pager_choice();
  320. child.use_shell = 1;
  321. child.clean_on_exit = 1;
  322. child.wait_after_clean = 1;
  323. child.trace2_child_class = "shell_alias";
  324. strvec_push(&child.args, alias_string + 1);
  325. strvec_pushv(&child.args, (*argv) + 1);
  326. trace2_cmd_alias(alias_command, child.args.v);
  327. trace2_cmd_list_config();
  328. trace2_cmd_list_env_vars();
  329. trace2_cmd_name("_run_shell_alias_");
  330. ret = run_command(&child);
  331. if (ret >= 0) /* normal exit */
  332. exit(ret);
  333. die_errno(_("while expanding alias '%s': '%s'"),
  334. alias_command, alias_string + 1);
  335. }
  336. count = split_cmdline(alias_string, &new_argv);
  337. if (count < 0)
  338. die(_("bad alias.%s string: %s"), alias_command,
  339. _(split_cmdline_strerror(count)));
  340. option_count = handle_options(&new_argv, &count, &envchanged);
  341. if (envchanged)
  342. die(_("alias '%s' changes environment variables.\n"
  343. "You can use '!git' in the alias to do this"),
  344. alias_command);
  345. MOVE_ARRAY(new_argv - option_count, new_argv, count);
  346. new_argv -= option_count;
  347. if (count < 1)
  348. die(_("empty alias for %s"), alias_command);
  349. if (!strcmp(alias_command, new_argv[0]))
  350. die(_("recursive alias: %s"), alias_command);
  351. trace_argv_printf(new_argv,
  352. "trace: alias expansion: %s =>",
  353. alias_command);
  354. REALLOC_ARRAY(new_argv, count + *argcp);
  355. /* insert after command name */
  356. COPY_ARRAY(new_argv + count, *argv + 1, *argcp);
  357. trace2_cmd_alias(alias_command, new_argv);
  358. trace2_cmd_list_config();
  359. trace2_cmd_list_env_vars();
  360. *argv = new_argv;
  361. *argcp += count - 1;
  362. ret = 1;
  363. }
  364. errno = saved_errno;
  365. return ret;
  366. }
  367. static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
  368. {
  369. int status, help;
  370. struct stat st;
  371. const char *prefix;
  372. prefix = NULL;
  373. help = argc == 2 && !strcmp(argv[1], "-h");
  374. if (!help) {
  375. if (p->option & RUN_SETUP)
  376. prefix = setup_git_directory();
  377. else if (p->option & RUN_SETUP_GENTLY) {
  378. int nongit_ok;
  379. prefix = setup_git_directory_gently(&nongit_ok);
  380. }
  381. if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY) &&
  382. !(p->option & DELAY_PAGER_CONFIG))
  383. use_pager = check_pager_config(p->cmd);
  384. if (use_pager == -1 && p->option & USE_PAGER)
  385. use_pager = 1;
  386. if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) &&
  387. startup_info->have_repository) /* get_git_dir() may set up repo, avoid that */
  388. trace_repo_setup(prefix);
  389. }
  390. commit_pager_choice();
  391. if (!help && get_super_prefix()) {
  392. if (!(p->option & SUPPORT_SUPER_PREFIX))
  393. die(_("%s doesn't support --super-prefix"), p->cmd);
  394. }
  395. if (!help && p->option & NEED_WORK_TREE)
  396. setup_work_tree();
  397. trace_argv_printf(argv, "trace: built-in: git");
  398. trace2_cmd_name(p->cmd);
  399. trace2_cmd_list_config();
  400. trace2_cmd_list_env_vars();
  401. validate_cache_entries(the_repository->index);
  402. status = p->fn(argc, argv, prefix);
  403. validate_cache_entries(the_repository->index);
  404. if (status)
  405. return status;
  406. /* Somebody closed stdout? */
  407. if (fstat(fileno(stdout), &st))
  408. return 0;
  409. /* Ignore write errors for pipes and sockets.. */
  410. if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
  411. return 0;
  412. /* Check for ENOSPC and EIO errors.. */
  413. if (fflush(stdout))
  414. die_errno(_("write failure on standard output"));
  415. if (ferror(stdout))
  416. die(_("unknown write failure on standard output"));
  417. if (fclose(stdout))
  418. die_errno(_("close failed on standard output"));
  419. return 0;
  420. }
  421. static struct cmd_struct commands[] = {
  422. { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
  423. { "am", cmd_am, RUN_SETUP | NEED_WORK_TREE },
  424. { "annotate", cmd_annotate, RUN_SETUP | NO_PARSEOPT },
  425. { "apply", cmd_apply, RUN_SETUP_GENTLY },
  426. { "archive", cmd_archive, RUN_SETUP_GENTLY },
  427. { "bisect--helper", cmd_bisect__helper, RUN_SETUP },
  428. { "blame", cmd_blame, RUN_SETUP },
  429. { "branch", cmd_branch, RUN_SETUP | DELAY_PAGER_CONFIG },
  430. { "bugreport", cmd_bugreport, RUN_SETUP_GENTLY },
  431. { "bundle", cmd_bundle, RUN_SETUP_GENTLY | NO_PARSEOPT },
  432. { "cat-file", cmd_cat_file, RUN_SETUP },
  433. { "check-attr", cmd_check_attr, RUN_SETUP },
  434. { "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
  435. { "check-mailmap", cmd_check_mailmap, RUN_SETUP },
  436. { "check-ref-format", cmd_check_ref_format, NO_PARSEOPT },
  437. { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
  438. { "checkout-index", cmd_checkout_index,
  439. RUN_SETUP | NEED_WORK_TREE},
  440. { "cherry", cmd_cherry, RUN_SETUP },
  441. { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
  442. { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
  443. { "clone", cmd_clone },
  444. { "column", cmd_column, RUN_SETUP_GENTLY },
  445. { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
  446. { "commit-graph", cmd_commit_graph, RUN_SETUP },
  447. { "commit-tree", cmd_commit_tree, RUN_SETUP | NO_PARSEOPT },
  448. { "config", cmd_config, RUN_SETUP_GENTLY | DELAY_PAGER_CONFIG },
  449. { "count-objects", cmd_count_objects, RUN_SETUP },
  450. { "credential", cmd_credential, RUN_SETUP_GENTLY | NO_PARSEOPT },
  451. { "credential-cache", cmd_credential_cache },
  452. { "credential-cache--daemon", cmd_credential_cache_daemon },
  453. { "credential-store", cmd_credential_store },
  454. { "describe", cmd_describe, RUN_SETUP },
  455. { "diff", cmd_diff, NO_PARSEOPT },
  456. { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
  457. { "diff-index", cmd_diff_index, RUN_SETUP | NO_PARSEOPT },
  458. { "diff-tree", cmd_diff_tree, RUN_SETUP | NO_PARSEOPT },
  459. { "difftool", cmd_difftool, RUN_SETUP_GENTLY },
  460. { "env--helper", cmd_env__helper },
  461. { "fast-export", cmd_fast_export, RUN_SETUP },
  462. { "fast-import", cmd_fast_import, RUN_SETUP | NO_PARSEOPT },
  463. { "fetch", cmd_fetch, RUN_SETUP },
  464. { "fetch-pack", cmd_fetch_pack, RUN_SETUP | NO_PARSEOPT },
  465. { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
  466. { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
  467. { "format-patch", cmd_format_patch, RUN_SETUP },
  468. { "fsck", cmd_fsck, RUN_SETUP },
  469. { "fsck-objects", cmd_fsck, RUN_SETUP },
  470. { "gc", cmd_gc, RUN_SETUP },
  471. { "get-tar-commit-id", cmd_get_tar_commit_id, NO_PARSEOPT },
  472. { "grep", cmd_grep, RUN_SETUP_GENTLY },
  473. { "hash-object", cmd_hash_object },
  474. { "help", cmd_help },
  475. { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY | NO_PARSEOPT },
  476. { "init", cmd_init_db },
  477. { "init-db", cmd_init_db },
  478. { "interpret-trailers", cmd_interpret_trailers, RUN_SETUP_GENTLY },
  479. { "log", cmd_log, RUN_SETUP },
  480. { "ls-files", cmd_ls_files, RUN_SETUP },
  481. { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
  482. { "ls-tree", cmd_ls_tree, RUN_SETUP },
  483. { "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY | NO_PARSEOPT },
  484. { "mailsplit", cmd_mailsplit, NO_PARSEOPT },
  485. { "maintenance", cmd_maintenance, RUN_SETUP_GENTLY | NO_PARSEOPT },
  486. { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
  487. { "merge-base", cmd_merge_base, RUN_SETUP },
  488. { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
  489. { "merge-index", cmd_merge_index, RUN_SETUP | NO_PARSEOPT },
  490. { "merge-ours", cmd_merge_ours, RUN_SETUP | NO_PARSEOPT },
  491. { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
  492. { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
  493. { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
  494. { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
  495. { "merge-tree", cmd_merge_tree, RUN_SETUP | NO_PARSEOPT },
  496. { "mktag", cmd_mktag, RUN_SETUP | NO_PARSEOPT },
  497. { "mktree", cmd_mktree, RUN_SETUP },
  498. { "multi-pack-index", cmd_multi_pack_index, RUN_SETUP_GENTLY },
  499. { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
  500. { "name-rev", cmd_name_rev, RUN_SETUP },
  501. { "notes", cmd_notes, RUN_SETUP },
  502. { "pack-objects", cmd_pack_objects, RUN_SETUP },
  503. { "pack-redundant", cmd_pack_redundant, RUN_SETUP | NO_PARSEOPT },
  504. { "pack-refs", cmd_pack_refs, RUN_SETUP },
  505. { "patch-id", cmd_patch_id, RUN_SETUP_GENTLY | NO_PARSEOPT },
  506. { "pickaxe", cmd_blame, RUN_SETUP },
  507. { "prune", cmd_prune, RUN_SETUP },
  508. { "prune-packed", cmd_prune_packed, RUN_SETUP },
  509. { "pull", cmd_pull, RUN_SETUP | NEED_WORK_TREE },
  510. { "push", cmd_push, RUN_SETUP },
  511. { "range-diff", cmd_range_diff, RUN_SETUP | USE_PAGER },
  512. { "read-tree", cmd_read_tree, RUN_SETUP | SUPPORT_SUPER_PREFIX},
  513. { "rebase", cmd_rebase, RUN_SETUP | NEED_WORK_TREE },
  514. { "rebase--interactive", cmd_rebase__interactive, RUN_SETUP | NEED_WORK_TREE },
  515. { "receive-pack", cmd_receive_pack },
  516. { "reflog", cmd_reflog, RUN_SETUP },
  517. { "remote", cmd_remote, RUN_SETUP },
  518. { "remote-ext", cmd_remote_ext, NO_PARSEOPT },
  519. { "remote-fd", cmd_remote_fd, NO_PARSEOPT },
  520. { "repack", cmd_repack, RUN_SETUP },
  521. { "replace", cmd_replace, RUN_SETUP },
  522. { "rerere", cmd_rerere, RUN_SETUP },
  523. { "reset", cmd_reset, RUN_SETUP },
  524. { "restore", cmd_restore, RUN_SETUP | NEED_WORK_TREE },
  525. { "rev-list", cmd_rev_list, RUN_SETUP | NO_PARSEOPT },
  526. { "rev-parse", cmd_rev_parse, NO_PARSEOPT },
  527. { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
  528. { "rm", cmd_rm, RUN_SETUP },
  529. { "send-pack", cmd_send_pack, RUN_SETUP },
  530. { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
  531. { "show", cmd_show, RUN_SETUP },
  532. { "show-branch", cmd_show_branch, RUN_SETUP },
  533. { "show-index", cmd_show_index, RUN_SETUP_GENTLY },
  534. { "show-ref", cmd_show_ref, RUN_SETUP },
  535. { "sparse-checkout", cmd_sparse_checkout, RUN_SETUP | NEED_WORK_TREE },
  536. { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
  537. { "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE },
  538. { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
  539. { "stripspace", cmd_stripspace },
  540. { "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT },
  541. { "switch", cmd_switch, RUN_SETUP | NEED_WORK_TREE },
  542. { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
  543. { "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG },
  544. { "unpack-file", cmd_unpack_file, RUN_SETUP | NO_PARSEOPT },
  545. { "unpack-objects", cmd_unpack_objects, RUN_SETUP | NO_PARSEOPT },
  546. { "update-index", cmd_update_index, RUN_SETUP },
  547. { "update-ref", cmd_update_ref, RUN_SETUP },
  548. { "update-server-info", cmd_update_server_info, RUN_SETUP },
  549. { "upload-archive", cmd_upload_archive, NO_PARSEOPT },
  550. { "upload-archive--writer", cmd_upload_archive_writer, NO_PARSEOPT },
  551. { "upload-pack", cmd_upload_pack },
  552. { "var", cmd_var, RUN_SETUP_GENTLY | NO_PARSEOPT },
  553. { "verify-commit", cmd_verify_commit, RUN_SETUP },
  554. { "verify-pack", cmd_verify_pack },
  555. { "verify-tag", cmd_verify_tag, RUN_SETUP },
  556. { "version", cmd_version },
  557. { "whatchanged", cmd_whatchanged, RUN_SETUP },
  558. { "worktree", cmd_worktree, RUN_SETUP | NO_PARSEOPT },
  559. { "write-tree", cmd_write_tree, RUN_SETUP },
  560. };
  561. static struct cmd_struct *get_builtin(const char *s)
  562. {
  563. int i;
  564. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  565. struct cmd_struct *p = commands + i;
  566. if (!strcmp(s, p->cmd))
  567. return p;
  568. }
  569. return NULL;
  570. }
  571. int is_builtin(const char *s)
  572. {
  573. return !!get_builtin(s);
  574. }
  575. static void list_builtins(struct string_list *out, unsigned int exclude_option)
  576. {
  577. int i;
  578. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  579. if (exclude_option &&
  580. (commands[i].option & exclude_option))
  581. continue;
  582. string_list_append(out, commands[i].cmd);
  583. }
  584. }
  585. #ifdef STRIP_EXTENSION
  586. static void strip_extension(const char **argv)
  587. {
  588. size_t len;
  589. if (strip_suffix(argv[0], STRIP_EXTENSION, &len))
  590. argv[0] = xmemdupz(argv[0], len);
  591. }
  592. #else
  593. #define strip_extension(cmd)
  594. #endif
  595. static void handle_builtin(int argc, const char **argv)
  596. {
  597. struct strvec args = STRVEC_INIT;
  598. const char *cmd;
  599. struct cmd_struct *builtin;
  600. strip_extension(argv);
  601. cmd = argv[0];
  602. /* Turn "git cmd --help" into "git help --exclude-guides cmd" */
  603. if (argc > 1 && !strcmp(argv[1], "--help")) {
  604. int i;
  605. argv[1] = argv[0];
  606. argv[0] = cmd = "help";
  607. for (i = 0; i < argc; i++) {
  608. strvec_push(&args, argv[i]);
  609. if (!i)
  610. strvec_push(&args, "--exclude-guides");
  611. }
  612. argc++;
  613. argv = args.v;
  614. }
  615. builtin = get_builtin(cmd);
  616. if (builtin)
  617. exit(run_builtin(builtin, argc, argv));
  618. strvec_clear(&args);
  619. }
  620. static void execv_dashed_external(const char **argv)
  621. {
  622. struct child_process cmd = CHILD_PROCESS_INIT;
  623. int status;
  624. if (get_super_prefix())
  625. die(_("%s doesn't support --super-prefix"), argv[0]);
  626. if (use_pager == -1 && !is_builtin(argv[0]))
  627. use_pager = check_pager_config(argv[0]);
  628. commit_pager_choice();
  629. strvec_pushf(&cmd.args, "git-%s", argv[0]);
  630. strvec_pushv(&cmd.args, argv + 1);
  631. cmd.clean_on_exit = 1;
  632. cmd.wait_after_clean = 1;
  633. cmd.silent_exec_failure = 1;
  634. cmd.trace2_child_class = "dashed";
  635. trace2_cmd_name("_run_dashed_");
  636. /*
  637. * The code in run_command() logs trace2 child_start/child_exit
  638. * events, so we do not need to report exec/exec_result events here.
  639. */
  640. trace_argv_printf(cmd.args.v, "trace: exec:");
  641. /*
  642. * If we fail because the command is not found, it is
  643. * OK to return. Otherwise, we just pass along the status code,
  644. * or our usual generic code if we were not even able to exec
  645. * the program.
  646. */
  647. status = run_command(&cmd);
  648. /*
  649. * If the child process ran and we are now going to exit, emit a
  650. * generic string as our trace2 command verb to indicate that we
  651. * launched a dashed command.
  652. */
  653. if (status >= 0)
  654. exit(status);
  655. else if (errno != ENOENT)
  656. exit(128);
  657. }
  658. static int run_argv(int *argcp, const char ***argv)
  659. {
  660. int done_alias = 0;
  661. struct string_list cmd_list = STRING_LIST_INIT_NODUP;
  662. struct string_list_item *seen;
  663. while (1) {
  664. /*
  665. * If we tried alias and futzed with our environment,
  666. * it no longer is safe to invoke builtins directly in
  667. * general. We have to spawn them as dashed externals.
  668. *
  669. * NEEDSWORK: if we can figure out cases
  670. * where it is safe to do, we can avoid spawning a new
  671. * process.
  672. */
  673. if (!done_alias)
  674. handle_builtin(*argcp, *argv);
  675. else if (get_builtin(**argv)) {
  676. struct strvec args = STRVEC_INIT;
  677. int i;
  678. /*
  679. * The current process is committed to launching a
  680. * child process to run the command named in (**argv)
  681. * and exiting. Log a generic string as the trace2
  682. * command verb to indicate this. Note that the child
  683. * process will log the actual verb when it runs.
  684. */
  685. trace2_cmd_name("_run_git_alias_");
  686. if (get_super_prefix())
  687. die("%s doesn't support --super-prefix", **argv);
  688. commit_pager_choice();
  689. strvec_push(&args, "git");
  690. for (i = 0; i < *argcp; i++)
  691. strvec_push(&args, (*argv)[i]);
  692. trace_argv_printf(args.v, "trace: exec:");
  693. /*
  694. * if we fail because the command is not found, it is
  695. * OK to return. Otherwise, we just pass along the status code.
  696. */
  697. i = run_command_v_opt_tr2(args.v, RUN_SILENT_EXEC_FAILURE |
  698. RUN_CLEAN_ON_EXIT | RUN_WAIT_AFTER_CLEAN, "git_alias");
  699. if (i >= 0 || errno != ENOENT)
  700. exit(i);
  701. die("could not execute builtin %s", **argv);
  702. }
  703. /* .. then try the external ones */
  704. execv_dashed_external(*argv);
  705. seen = unsorted_string_list_lookup(&cmd_list, *argv[0]);
  706. if (seen) {
  707. int i;
  708. struct strbuf sb = STRBUF_INIT;
  709. for (i = 0; i < cmd_list.nr; i++) {
  710. struct string_list_item *item = &cmd_list.items[i];
  711. strbuf_addf(&sb, "\n %s", item->string);
  712. if (item == seen)
  713. strbuf_addstr(&sb, " <==");
  714. else if (i == cmd_list.nr - 1)
  715. strbuf_addstr(&sb, " ==>");
  716. }
  717. die(_("alias loop detected: expansion of '%s' does"
  718. " not terminate:%s"), cmd_list.items[0].string, sb.buf);
  719. }
  720. string_list_append(&cmd_list, *argv[0]);
  721. /*
  722. * It could be an alias -- this works around the insanity
  723. * of overriding "git log" with "git show" by having
  724. * alias.log = show
  725. */
  726. if (!handle_alias(argcp, argv))
  727. break;
  728. done_alias = 1;
  729. }
  730. string_list_clear(&cmd_list, 0);
  731. return done_alias;
  732. }
  733. int cmd_main(int argc, const char **argv)
  734. {
  735. const char *cmd;
  736. int done_help = 0;
  737. cmd = argv[0];
  738. if (!cmd)
  739. cmd = "git-help";
  740. else {
  741. const char *slash = find_last_dir_sep(cmd);
  742. if (slash)
  743. cmd = slash + 1;
  744. }
  745. trace_command_performance(argv);
  746. /*
  747. * "git-xxxx" is the same as "git xxxx", but we obviously:
  748. *
  749. * - cannot take flags in between the "git" and the "xxxx".
  750. * - cannot execute it externally (since it would just do
  751. * the same thing over again)
  752. *
  753. * So we just directly call the builtin handler, and die if
  754. * that one cannot handle it.
  755. */
  756. if (skip_prefix(cmd, "git-", &cmd)) {
  757. argv[0] = cmd;
  758. handle_builtin(argc, argv);
  759. die(_("cannot handle %s as a builtin"), cmd);
  760. }
  761. /* Look for flags.. */
  762. argv++;
  763. argc--;
  764. handle_options(&argv, &argc, NULL);
  765. if (argc > 0) {
  766. /* translate --help and --version into commands */
  767. skip_prefix(argv[0], "--", &argv[0]);
  768. } else {
  769. /* The user didn't specify a command; give them help */
  770. commit_pager_choice();
  771. printf(_("usage: %s\n\n"), git_usage_string);
  772. list_common_cmds_help();
  773. printf("\n%s\n", _(git_more_info_string));
  774. exit(1);
  775. }
  776. cmd = argv[0];
  777. /*
  778. * We use PATH to find git commands, but we prepend some higher
  779. * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH
  780. * environment, and the $(gitexecdir) from the Makefile at build
  781. * time.
  782. */
  783. setup_path();
  784. while (1) {
  785. int was_alias = run_argv(&argc, &argv);
  786. if (errno != ENOENT)
  787. break;
  788. if (was_alias) {
  789. fprintf(stderr, _("expansion of alias '%s' failed; "
  790. "'%s' is not a git command\n"),
  791. cmd, argv[0]);
  792. exit(1);
  793. }
  794. if (!done_help) {
  795. cmd = argv[0] = help_unknown_cmd(cmd);
  796. done_help = 1;
  797. } else
  798. break;
  799. }
  800. fprintf(stderr, _("failed to run command '%s': %s\n"),
  801. cmd, strerror(errno));
  802. return 1;
  803. }