blob: 2e4efb4ff08f16cd6040350bf2720e0375adab96 [file] [log] [blame]
Junio C Hamano85023572006-12-19 14:34:12 -08001#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -07003#include "exec-cmd.h"
Thiago Farinafd5c3632010-08-31 23:29:08 -03004#include "help.h"
Jeff Kingd8e96fd2009-01-28 02:38:14 -05005#include "run-command.h"
Nguyễn Thái Ngọc Duy65b5f942018-05-20 20:40:06 +02006#include "alias.h"
Andreas Ericsson8e49d502005-11-16 00:31:25 +01007
Nguyễn Thái Ngọc Duy007aa8d2018-03-24 21:35:18 +01008#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)
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +010018#define NO_PARSEOPT (1<<6) /* parse-options is not used */
Nguyễn Thái Ngọc Duy007aa8d2018-03-24 21:35:18 +010019
20struct cmd_struct {
21 const char *cmd;
22 int (*fn)(int, const char **, const char *);
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +010023 unsigned int option;
Nguyễn Thái Ngọc Duy007aa8d2018-03-24 21:35:18 +010024};
25
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +010026const char git_usage_string[] =
Alexander Shopovfc045fe2018-02-13 14:19:15 +010027 N_("git [--version] [--help] [-C <path>] [-c <name>=<value>]\n"
28 " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
Johannes Sixt7213c282018-05-03 19:15:08 +020029 " [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]\n"
Alexander Shopovfc045fe2018-02-13 14:19:15 +010030 " [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
31 " <command> [<args>]");
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +010032
Teemu Likonenb7d96812008-06-06 00:15:36 +030033const char git_more_info_string[] =
Alex Henriead5fe372014-08-30 13:56:01 -060034 N_("'git help -a' and 'git help -g' list available subcommands and some\n"
Philip Oakley73903d02013-04-02 23:39:48 +010035 "concept guides. See 'git help <command>' or 'git help <concept>'\n"
Philip Oakleye55f36b2019-05-15 23:47:08 +010036 "to read about a specific subcommand or concept.\n"
37 "See 'git help git' for an overview of the system.");
Teemu Likonenb7d96812008-06-06 00:15:36 +030038
Jeff King4e107382008-07-03 07:46:57 -040039static int use_pager = -1;
Nguyễn Thái Ngọc Duyc0562612014-06-08 16:37:10 +070040
Nguyễn Thái Ngọc Duye5d7a612018-05-20 20:39:58 +020041static void list_builtins(struct string_list *list, unsigned int exclude_option);
Jeff King8893fd92017-05-30 01:18:43 -040042
Nguyễn Thái Ngọc Duye11dca12018-05-20 20:40:07 +020043static void exclude_helpers_from_list(struct string_list *list)
44{
45 int i = 0;
46
47 while (i < list->nr) {
48 if (strstr(list->items[i].string, "--"))
49 unsorted_string_list_delete_item(list, i, 0);
50 else
51 i++;
52 }
53}
54
Nguyễn Thái Ngọc Duy00895212018-05-20 20:39:57 +020055static int match_token(const char *spec, int len, const char *token)
56{
57 int token_len = strlen(token);
58
59 return len == token_len && !strncmp(spec, token, token_len);
60}
61
62static int list_cmds(const char *spec)
63{
Nguyễn Thái Ngọc Duye5d7a612018-05-20 20:39:58 +020064 struct string_list list = STRING_LIST_INIT_DUP;
65 int i;
Jeff King83b0ecf2019-03-20 14:03:26 -040066 int nongit;
67
68 /*
69 * Set up the repository so we can pick up any repo-level config (like
70 * completion.commands).
71 */
72 setup_git_directory_gently(&nongit);
Nguyễn Thái Ngọc Duye5d7a612018-05-20 20:39:58 +020073
Nguyễn Thái Ngọc Duy00895212018-05-20 20:39:57 +020074 while (*spec) {
75 const char *sep = strchrnul(spec, ',');
76 int len = sep - spec;
77
78 if (match_token(spec, len, "builtins"))
Nguyễn Thái Ngọc Duye5d7a612018-05-20 20:39:58 +020079 list_builtins(&list, 0);
Nguyễn Thái Ngọc Duy6bb2dc02018-05-20 20:39:59 +020080 else if (match_token(spec, len, "main"))
81 list_all_main_cmds(&list);
82 else if (match_token(spec, len, "others"))
83 list_all_other_cmds(&list);
Nguyễn Thái Ngọc Duye11dca12018-05-20 20:40:07 +020084 else if (match_token(spec, len, "nohelpers"))
85 exclude_helpers_from_list(&list);
Nguyễn Thái Ngọc Duy3301d362018-05-20 20:40:08 +020086 else if (match_token(spec, len, "alias"))
87 list_aliases(&list);
Nguyễn Thái Ngọc Duy6532f372018-05-20 20:40:09 +020088 else if (match_token(spec, len, "config"))
89 list_cmds_by_config(&list);
Nguyễn Thái Ngọc Duy3c777762018-05-20 20:40:00 +020090 else if (len > 5 && !strncmp(spec, "list-", 5)) {
91 struct strbuf sb = STRBUF_INIT;
92
93 strbuf_add(&sb, spec + 5, len - 5);
94 list_cmds_by_category(&list, sb.buf);
95 strbuf_release(&sb);
96 }
Nguyễn Thái Ngọc Duy00895212018-05-20 20:39:57 +020097 else
98 die(_("unsupported command listing type '%s'"), spec);
99 spec += len;
100 if (*spec == ',')
101 spec++;
102 }
Nguyễn Thái Ngọc Duye5d7a612018-05-20 20:39:58 +0200103 for (i = 0; i < list.nr; i++)
104 puts(list.items[i].string);
105 string_list_clear(&list, 0);
Nguyễn Thái Ngọc Duy00895212018-05-20 20:39:57 +0200106 return 0;
107}
Nguyễn Thái Ngọc Duyc0562612014-06-08 16:37:10 +0700108
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100109static void commit_pager_choice(void)
110{
Jeff King4e107382008-07-03 07:46:57 -0400111 switch (use_pager) {
112 case 0:
113 setenv("GIT_PAGER", "cat", 1);
114 break;
115 case 1:
116 setup_pager();
117 break;
118 default:
119 break;
120 }
121}
122
Martin Ågren033fe3d2017-08-02 21:40:51 +0200123void setup_auto_pager(const char *cmd, int def)
124{
125 if (use_pager != -1 || pager_in_use())
126 return;
127 use_pager = check_pager_config(cmd);
128 if (use_pager == -1)
129 use_pager = def;
130 commit_pager_choice();
131}
132
Felipe Contreras4b25d092009-05-01 12:06:36 +0300133static int handle_options(const char ***argv, int *argc, int *envchanged)
Johannes Schindelin4ab243a2006-07-24 14:10:45 +0200134{
Junio C Hamano73546c02011-05-24 18:50:35 -0400135 const char **orig_argv = *argv;
Johannes Schindelin4ab243a2006-07-24 14:10:45 +0200136
137 while (*argc > 0) {
138 const char *cmd = (*argv)[0];
139 if (cmd[0] != '-')
140 break;
141
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200142 /*
143 * For legacy reasons, the "version" and "help"
144 * commands can be written with "--" prepended
145 * to make them look like flags.
146 */
147 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
148 break;
149
150 /*
151 * Check remaining flags.
152 */
Jeff Kingae021d82014-06-18 15:47:50 -0400153 if (skip_prefix(cmd, "--exec-path", &cmd)) {
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200154 if (*cmd == '=')
Dan Jacques226c0dd2018-04-10 11:05:44 -0400155 git_set_exec_path(cmd + 1);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200156 else {
157 puts(git_exec_path());
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800158 trace2_cmd_name("_query_");
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200159 exit(0);
160 }
Markus Heidelberg89a56bf2009-04-05 04:15:16 +0200161 } else if (!strcmp(cmd, "--html-path")) {
162 puts(system_path(GIT_HTML_PATH));
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800163 trace2_cmd_name("_query_");
Markus Heidelberg89a56bf2009-04-05 04:15:16 +0200164 exit(0);
Jon Seymourf2dd8c32011-05-01 18:16:25 +1000165 } else if (!strcmp(cmd, "--man-path")) {
166 puts(system_path(GIT_MAN_PATH));
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800167 trace2_cmd_name("_query_");
Jon Seymourf2dd8c32011-05-01 18:16:25 +1000168 exit(0);
169 } else if (!strcmp(cmd, "--info-path")) {
170 puts(system_path(GIT_INFO_PATH));
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800171 trace2_cmd_name("_query_");
Jon Seymourf2dd8c32011-05-01 18:16:25 +1000172 exit(0);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200173 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
Jeff King4e107382008-07-03 07:46:57 -0400174 use_pager = 1;
Johannes Sixt7213c282018-05-03 19:15:08 +0200175 } else if (!strcmp(cmd, "-P") || !strcmp(cmd, "--no-pager")) {
Jeff King4e107382008-07-03 07:46:57 -0400176 use_pager = 0;
Matthieu Moy463a8492007-08-19 19:24:36 +0200177 if (envchanged)
178 *envchanged = 1;
Christian Couderb0fa7ab2009-10-12 22:30:32 +0200179 } else if (!strcmp(cmd, "--no-replace-objects")) {
Jeff King6ebd1ca2018-07-18 16:45:20 -0400180 read_replace_refs = 0;
Christian Couder6476b382009-11-18 07:50:58 +0100181 setenv(NO_REPLACE_OBJECTS_ENVIRONMENT, "1", 1);
182 if (envchanged)
183 *envchanged = 1;
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200184 } else if (!strcmp(cmd, "--git-dir")) {
Brian Gernhardtc321f002006-12-22 08:56:25 -0500185 if (*argc < 2) {
Alexander Shopovfc045fe2018-02-13 14:19:15 +0100186 fprintf(stderr, _("no directory given for --git-dir\n" ));
Brian Gernhardtc321f002006-12-22 08:56:25 -0500187 usage(git_usage_string);
188 }
Shawn O. Pearce45b09792006-12-30 23:29:11 -0500189 setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1);
Matthias Lederhofer4394efe2007-06-08 22:57:55 +0200190 if (envchanged)
191 *envchanged = 1;
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200192 (*argv)++;
193 (*argc)--;
Jeff Kingae021d82014-06-18 15:47:50 -0400194 } else if (skip_prefix(cmd, "--git-dir=", &cmd)) {
195 setenv(GIT_DIR_ENVIRONMENT, cmd, 1);
Matthias Lederhofer4394efe2007-06-08 22:57:55 +0200196 if (envchanged)
197 *envchanged = 1;
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700198 } else if (!strcmp(cmd, "--namespace")) {
199 if (*argc < 2) {
Alexander Shopovfc045fe2018-02-13 14:19:15 +0100200 fprintf(stderr, _("no namespace given for --namespace\n" ));
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700201 usage(git_usage_string);
202 }
203 setenv(GIT_NAMESPACE_ENVIRONMENT, (*argv)[1], 1);
204 if (envchanged)
205 *envchanged = 1;
206 (*argv)++;
207 (*argc)--;
Jeff Kingae021d82014-06-18 15:47:50 -0400208 } else if (skip_prefix(cmd, "--namespace=", &cmd)) {
209 setenv(GIT_NAMESPACE_ENVIRONMENT, cmd, 1);
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700210 if (envchanged)
211 *envchanged = 1;
Matthias Lederhofer892c41b2007-06-06 09:10:42 +0200212 } else if (!strcmp(cmd, "--work-tree")) {
213 if (*argc < 2) {
Alexander Shopovfc045fe2018-02-13 14:19:15 +0100214 fprintf(stderr, _("no directory given for --work-tree\n" ));
Matthias Lederhofer892c41b2007-06-06 09:10:42 +0200215 usage(git_usage_string);
216 }
217 setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
Matthias Lederhofer4394efe2007-06-08 22:57:55 +0200218 if (envchanged)
219 *envchanged = 1;
Matthias Lederhofer892c41b2007-06-06 09:10:42 +0200220 (*argv)++;
221 (*argc)--;
Jeff Kingae021d82014-06-18 15:47:50 -0400222 } else if (skip_prefix(cmd, "--work-tree=", &cmd)) {
223 setenv(GIT_WORK_TREE_ENVIRONMENT, cmd, 1);
Matthias Lederhofer4394efe2007-06-08 22:57:55 +0200224 if (envchanged)
225 *envchanged = 1;
Brandon Williams74866d72016-10-07 11:18:48 -0700226 } else if (!strcmp(cmd, "--super-prefix")) {
227 if (*argc < 2) {
Alexander Shopovfc045fe2018-02-13 14:19:15 +0100228 fprintf(stderr, _("no prefix given for --super-prefix\n" ));
Brandon Williams74866d72016-10-07 11:18:48 -0700229 usage(git_usage_string);
230 }
231 setenv(GIT_SUPER_PREFIX_ENVIRONMENT, (*argv)[1], 1);
232 if (envchanged)
233 *envchanged = 1;
234 (*argv)++;
235 (*argc)--;
236 } else if (skip_prefix(cmd, "--super-prefix=", &cmd)) {
237 setenv(GIT_SUPER_PREFIX_ENVIRONMENT, cmd, 1);
238 if (envchanged)
239 *envchanged = 1;
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200240 } else if (!strcmp(cmd, "--bare")) {
René Scharfe4d3ab442014-07-28 20:31:57 +0200241 char *cwd = xgetcwd();
Junio C Hamano6adcca32007-08-27 00:58:06 -0700242 is_bare_repository_cfg = 1;
René Scharfe4d3ab442014-07-28 20:31:57 +0200243 setenv(GIT_DIR_ENVIRONMENT, cwd, 0);
244 free(cwd);
Jeff King2cd83d12013-03-08 04:32:22 -0500245 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
Matthias Lederhofer4394efe2007-06-08 22:57:55 +0200246 if (envchanged)
247 *envchanged = 1;
Alex Riesen8b1fa772010-03-26 23:53:57 +0100248 } else if (!strcmp(cmd, "-c")) {
249 if (*argc < 2) {
Alexander Shopovfc045fe2018-02-13 14:19:15 +0100250 fprintf(stderr, _("-c expects a configuration string\n" ));
Alex Riesen8b1fa772010-03-26 23:53:57 +0100251 usage(git_usage_string);
252 }
Jeff King2b64fc82010-08-23 15:16:00 -0400253 git_config_push_parameter((*argv)[1]);
Alex Riesen8b1fa772010-03-26 23:53:57 +0100254 (*argv)++;
255 (*argc)--;
Jeff King823ab402012-12-19 17:37:30 -0500256 } else if (!strcmp(cmd, "--literal-pathspecs")) {
257 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "1", 1);
258 if (envchanged)
259 *envchanged = 1;
260 } else if (!strcmp(cmd, "--no-literal-pathspecs")) {
261 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "0", 1);
262 if (envchanged)
263 *envchanged = 1;
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +0700264 } else if (!strcmp(cmd, "--glob-pathspecs")) {
265 setenv(GIT_GLOB_PATHSPECS_ENVIRONMENT, "1", 1);
266 if (envchanged)
267 *envchanged = 1;
268 } else if (!strcmp(cmd, "--noglob-pathspecs")) {
269 setenv(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, "1", 1);
270 if (envchanged)
271 *envchanged = 1;
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700272 } else if (!strcmp(cmd, "--icase-pathspecs")) {
273 setenv(GIT_ICASE_PATHSPECS_ENVIRONMENT, "1", 1);
274 if (envchanged)
275 *envchanged = 1;
Jeff King27344d62017-09-27 02:54:30 -0400276 } else if (!strcmp(cmd, "--no-optional-locks")) {
277 setenv(GIT_OPTIONAL_LOCKS_ENVIRONMENT, "0", 1);
278 if (envchanged)
279 *envchanged = 1;
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700280 } else if (!strcmp(cmd, "--shallow-file")) {
281 (*argv)++;
282 (*argc)--;
Stefan Beller6a2df512018-05-17 15:51:43 -0700283 set_alternate_shallow_file(the_repository, (*argv)[0], 1);
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700284 if (envchanged)
285 *envchanged = 1;
Nazri Ramliy44e1e4d2013-09-09 21:47:43 +0800286 } else if (!strcmp(cmd, "-C")) {
287 if (*argc < 2) {
Alexander Shopovfc045fe2018-02-13 14:19:15 +0100288 fprintf(stderr, _("no directory given for -C\n" ));
Nazri Ramliy44e1e4d2013-09-09 21:47:43 +0800289 usage(git_usage_string);
290 }
Karthik Nayak6a536e22015-03-06 16:48:08 +0530291 if ((*argv)[1][0]) {
292 if (chdir((*argv)[1]))
Alexander Shopovfc045fe2018-02-13 14:19:15 +0100293 die_errno("cannot change to '%s'", (*argv)[1]);
Karthik Nayak6a536e22015-03-06 16:48:08 +0530294 if (envchanged)
295 *envchanged = 1;
296 }
Nazri Ramliy44e1e4d2013-09-09 21:47:43 +0800297 (*argv)++;
298 (*argc)--;
Nguyễn Thái Ngọc Duy00895212018-05-20 20:39:57 +0200299 } else if (skip_prefix(cmd, "--list-cmds=", &cmd)) {
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800300 trace2_cmd_name("_query_");
Nguyễn Thái Ngọc Duy00895212018-05-20 20:39:57 +0200301 if (!strcmp(cmd, "parseopt")) {
Nguyễn Thái Ngọc Duye5d7a612018-05-20 20:39:58 +0200302 struct string_list list = STRING_LIST_INIT_DUP;
303 int i;
304
305 list_builtins(&list, NO_PARSEOPT);
306 for (i = 0; i < list.nr; i++)
307 printf("%s ", list.items[i].string);
308 string_list_clear(&list, 0);
Nguyễn Thái Ngọc Duy00895212018-05-20 20:39:57 +0200309 exit(0);
310 } else {
311 exit(list_cmds(cmd));
312 }
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200313 } else {
Alexander Shopovfc045fe2018-02-13 14:19:15 +0100314 fprintf(stderr, _("unknown option: %s\n"), cmd);
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +0100315 usage(git_usage_string);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200316 }
Johannes Schindelin4ab243a2006-07-24 14:10:45 +0200317
318 (*argv)++;
319 (*argc)--;
Johannes Schindelin4ab243a2006-07-24 14:10:45 +0200320 }
Junio C Hamano73546c02011-05-24 18:50:35 -0400321 return (*argv) - orig_argv;
Johannes Schindelin4ab243a2006-07-24 14:10:45 +0200322}
323
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200324static int handle_alias(int *argcp, const char ***argv)
325{
SZEDER Gáboraf05d672008-03-25 22:06:26 +0100326 int envchanged = 0, ret = 0, saved_errno = errno;
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200327 int count, option_count;
Felipe Contreras4b25d092009-05-01 12:06:36 +0300328 const char **new_argv;
Jeff King94351112008-02-24 17:17:14 -0500329 const char *alias_command;
330 char *alias_string;
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200331
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200332 alias_command = (*argv)[0];
Jeff King94351112008-02-24 17:17:14 -0500333 alias_string = alias_lookup(alias_command);
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200334 if (alias_string) {
Rasmus Villemoesa9a60b92018-10-09 13:59:08 +0200335 if (*argcp > 1 && !strcmp((*argv)[1], "-h"))
336 fprintf_ln(stderr, _("'%s' is aliased to '%s'"),
337 alias_command, alias_string);
Theodore Ts'odfd42a32007-02-10 19:33:58 -0500338 if (alias_string[0] == '!') {
Jeff King850d2fe2016-02-22 17:44:21 -0500339 struct child_process child = CHILD_PROCESS_INIT;
Johannes Schindelina9bcf652017-06-14 13:36:00 +0200340 int nongit_ok;
341
342 /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */
343 setup_git_directory_gently(&nongit_ok);
Johannes Schindelina2f80282007-07-01 22:51:58 +0100344
Erik Faye-Lund7f51f8b2011-01-07 00:00:38 +0100345 commit_pager_choice();
346
Jeff King850d2fe2016-02-22 17:44:21 -0500347 child.use_shell = 1;
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800348 child.trace2_child_class = "shell_alias";
Jeff King850d2fe2016-02-22 17:44:21 -0500349 argv_array_push(&child.args, alias_string + 1);
350 argv_array_pushv(&child.args, (*argv) + 1);
Erik Faye-Lund7f51f8b2011-01-07 00:00:38 +0100351
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800352 trace2_cmd_alias(alias_command, child.args.argv);
353 trace2_cmd_list_config();
Josh Steadmon3d3adaa2020-03-20 14:06:15 -0700354 trace2_cmd_list_env_vars();
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800355 trace2_cmd_name("_run_shell_alias_");
356
Jeff King850d2fe2016-02-22 17:44:21 -0500357 ret = run_command(&child);
Erik Faye-Lund7f51f8b2011-01-07 00:00:38 +0100358 if (ret >= 0) /* normal exit */
359 exit(ret);
360
Nguyễn Thái Ngọc Duy8aa8c142018-11-10 06:16:00 +0100361 die_errno(_("while expanding alias '%s': '%s'"),
362 alias_command, alias_string + 1);
Theodore Ts'odfd42a32007-02-10 19:33:58 -0500363 }
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200364 count = split_cmdline(alias_string, &new_argv);
Deskin Millerdc4179f2008-09-22 11:06:41 -0400365 if (count < 0)
Nguyễn Thái Ngọc Duy8aa8c142018-11-10 06:16:00 +0100366 die(_("bad alias.%s string: %s"), alias_command,
Nguyễn Thái Ngọc Duya7412ae2018-11-10 06:16:01 +0100367 _(split_cmdline_strerror(count)));
Matthias Lederhofer4394efe2007-06-08 22:57:55 +0200368 option_count = handle_options(&new_argv, &count, &envchanged);
369 if (envchanged)
Nguyễn Thái Ngọc Duy8aa8c142018-11-10 06:16:00 +0100370 die(_("alias '%s' changes environment variables.\n"
371 "You can use '!git' in the alias to do this"),
372 alias_command);
René Scharfe7bd97d62019-09-19 22:48:30 +0200373 MOVE_ARRAY(new_argv - option_count, new_argv, count);
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200374 new_argv -= option_count;
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200375
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200376 if (count < 1)
Nguyễn Thái Ngọc Duy8aa8c142018-11-10 06:16:00 +0100377 die(_("empty alias for %s"), alias_command);
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200378
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200379 if (!strcmp(alias_command, new_argv[0]))
Nguyễn Thái Ngọc Duy8aa8c142018-11-10 06:16:00 +0100380 die(_("recursive alias: %s"), alias_command);
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200381
Christian Couderb319ce42007-12-03 05:51:50 +0100382 trace_argv_printf(new_argv,
Christian Couder6ce4e612006-09-02 18:23:48 +0200383 "trace: alias expansion: %s =>",
384 alias_command);
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200385
René Scharfe2756ca42014-09-16 20:56:57 +0200386 REALLOC_ARRAY(new_argv, count + *argcp);
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200387 /* insert after command name */
René Scharfe7bd97d62019-09-19 22:48:30 +0200388 COPY_ARRAY(new_argv + count, *argv + 1, *argcp);
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200389
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800390 trace2_cmd_alias(alias_command, new_argv);
391 trace2_cmd_list_config();
Josh Steadmon3d3adaa2020-03-20 14:06:15 -0700392 trace2_cmd_list_env_vars();
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800393
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200394 *argv = new_argv;
395 *argcp += count - 1;
396
397 ret = 1;
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200398 }
399
Johannes Schindelin47e5c0c2006-06-28 12:45:27 +0200400 errno = saved_errno;
401
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200402 return ret;
403}
404
Jeff Kingf172f332009-01-28 02:33:53 -0500405static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
Linus Torvalds47d0b4f2007-06-24 10:10:40 -0700406{
Jonathan Nieder99caeed2009-11-09 09:05:01 -0600407 int status, help;
Linus Torvalds0f157312007-06-24 10:29:33 -0700408 struct stat st;
Linus Torvalds47d0b4f2007-06-24 10:10:40 -0700409 const char *prefix;
410
411 prefix = NULL;
Jonathan Nieder99caeed2009-11-09 09:05:01 -0600412 help = argc == 2 && !strcmp(argv[1], "-h");
413 if (!help) {
414 if (p->option & RUN_SETUP)
415 prefix = setup_git_directory();
Luis R. Rodriguez27bd38d2014-04-21 17:47:56 -0700416 else if (p->option & RUN_SETUP_GENTLY) {
Nguyễn Thái Ngọc Duyee38dfb2010-08-05 21:52:16 -0500417 int nongit_ok;
418 prefix = setup_git_directory_gently(&nongit_ok);
419 }
Jeff King4e107382008-07-03 07:46:57 -0400420
Martin Ågrenc4098242017-08-02 21:40:50 +0200421 if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY) &&
422 !(p->option & DELAY_PAGER_CONFIG))
Jonathan Nieder99caeed2009-11-09 09:05:01 -0600423 use_pager = check_pager_config(p->cmd);
424 if (use_pager == -1 && p->option & USE_PAGER)
425 use_pager = 1;
Nguyễn Thái Ngọc Duya9ca8a82010-11-26 22:31:57 +0700426
427 if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) &&
428 startup_info->have_repository) /* get_git_dir() may set up repo, avoid that */
429 trace_repo_setup(prefix);
Jonathan Nieder99caeed2009-11-09 09:05:01 -0600430 }
Jeff King4e107382008-07-03 07:46:57 -0400431 commit_pager_choice();
432
Brandon Williams74866d72016-10-07 11:18:48 -0700433 if (!help && get_super_prefix()) {
434 if (!(p->option & SUPPORT_SUPER_PREFIX))
Nguyễn Thái Ngọc Duy8aa8c142018-11-10 06:16:00 +0100435 die(_("%s doesn't support --super-prefix"), p->cmd);
Brandon Williams74866d72016-10-07 11:18:48 -0700436 }
437
Jonathan Nieder99caeed2009-11-09 09:05:01 -0600438 if (!help && p->option & NEED_WORK_TREE)
Mike Hommey59f0f2f2007-11-03 12:23:11 +0100439 setup_work_tree();
440
Christian Couderb319ce42007-12-03 05:51:50 +0100441 trace_argv_printf(argv, "trace: built-in: git");
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800442 trace2_cmd_name(p->cmd);
443 trace2_cmd_list_config();
Josh Steadmon3d3adaa2020-03-20 14:06:15 -0700444 trace2_cmd_list_env_vars();
Linus Torvalds47d0b4f2007-06-24 10:10:40 -0700445
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +0700446 validate_cache_entries(the_repository->index);
Linus Torvalds0f157312007-06-24 10:29:33 -0700447 status = p->fn(argc, argv, prefix);
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +0700448 validate_cache_entries(the_repository->index);
Jameson Miller8616a2d2018-07-02 19:49:39 +0000449
Linus Torvalds0f157312007-06-24 10:29:33 -0700450 if (status)
Johannes Sixt47e3de02009-07-05 20:57:46 +0200451 return status;
Linus Torvalds0f157312007-06-24 10:29:33 -0700452
453 /* Somebody closed stdout? */
454 if (fstat(fileno(stdout), &st))
455 return 0;
456 /* Ignore write errors for pipes and sockets.. */
457 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
458 return 0;
459
460 /* Check for ENOSPC and EIO errors.. */
Linus Torvalds0227f982007-06-30 11:44:20 -0700461 if (fflush(stdout))
Nguyễn Thái Ngọc Duy8aa8c142018-11-10 06:16:00 +0100462 die_errno(_("write failure on standard output"));
Linus Torvalds0227f982007-06-30 11:44:20 -0700463 if (ferror(stdout))
Nguyễn Thái Ngọc Duy8aa8c142018-11-10 06:16:00 +0100464 die(_("unknown write failure on standard output"));
Linus Torvalds0227f982007-06-30 11:44:20 -0700465 if (fclose(stdout))
Nguyễn Thái Ngọc Duy8aa8c142018-11-10 06:16:00 +0100466 die_errno(_("close failed on standard output"));
Linus Torvalds0f157312007-06-24 10:29:33 -0700467 return 0;
Linus Torvalds47d0b4f2007-06-24 10:10:40 -0700468}
469
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100470static struct cmd_struct commands[] = {
471 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
Paul Tan783d7e82015-08-04 21:52:06 +0800472 { "am", cmd_am, RUN_SETUP | NEED_WORK_TREE },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100473 { "annotate", cmd_annotate, RUN_SETUP | NO_PARSEOPT },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100474 { "apply", cmd_apply, RUN_SETUP_GENTLY },
Junio C Hamanoeb0224c2016-11-22 13:37:04 -0800475 { "archive", cmd_archive, RUN_SETUP_GENTLY },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100476 { "bisect--helper", cmd_bisect__helper, RUN_SETUP },
477 { "blame", cmd_blame, RUN_SETUP },
Martin Ågrend74b5412017-11-19 16:03:49 +0100478 { "branch", cmd_branch, RUN_SETUP | DELAY_PAGER_CONFIG },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100479 { "bundle", cmd_bundle, RUN_SETUP_GENTLY | NO_PARSEOPT },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100480 { "cat-file", cmd_cat_file, RUN_SETUP },
481 { "check-attr", cmd_check_attr, RUN_SETUP },
482 { "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
483 { "check-mailmap", cmd_check_mailmap, RUN_SETUP },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100484 { "check-ref-format", cmd_check_ref_format, NO_PARSEOPT },
Eric Sunshine0ca560c2015-07-06 13:30:56 -0400485 { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100486 { "checkout-index", cmd_checkout_index,
487 RUN_SETUP | NEED_WORK_TREE},
488 { "cherry", cmd_cherry, RUN_SETUP },
489 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
490 { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
Nguyễn Thái Ngọc Duy86d26f22015-12-20 14:50:18 +0700491 { "clone", cmd_clone },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100492 { "column", cmd_column, RUN_SETUP_GENTLY },
493 { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
Derrick Stolee4ce58ee2018-04-02 16:34:18 -0400494 { "commit-graph", cmd_commit_graph, RUN_SETUP },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100495 { "commit-tree", cmd_commit_tree, RUN_SETUP | NO_PARSEOPT },
Martin Ågren32888b82018-02-21 19:51:43 +0100496 { "config", cmd_config, RUN_SETUP_GENTLY | DELAY_PAGER_CONFIG },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100497 { "count-objects", cmd_count_objects, RUN_SETUP },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100498 { "credential", cmd_credential, RUN_SETUP_GENTLY | NO_PARSEOPT },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100499 { "describe", cmd_describe, RUN_SETUP },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100500 { "diff", cmd_diff, NO_PARSEOPT },
501 { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
502 { "diff-index", cmd_diff_index, RUN_SETUP | NO_PARSEOPT },
503 { "diff-tree", cmd_diff_tree, RUN_SETUP | NO_PARSEOPT },
Johannes Schindelin20de3162019-03-14 04:25:04 -0700504 { "difftool", cmd_difftool, RUN_SETUP_GENTLY },
Ævar Arnfjörð Bjarmasonb4f207f2019-06-21 12:18:06 +0200505 { "env--helper", cmd_env__helper },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100506 { "fast-export", cmd_fast_export, RUN_SETUP },
507 { "fetch", cmd_fetch, RUN_SETUP },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100508 { "fetch-pack", cmd_fetch_pack, RUN_SETUP | NO_PARSEOPT },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100509 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
510 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
511 { "format-patch", cmd_format_patch, RUN_SETUP },
512 { "fsck", cmd_fsck, RUN_SETUP },
513 { "fsck-objects", cmd_fsck, RUN_SETUP },
514 { "gc", cmd_gc, RUN_SETUP },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100515 { "get-tar-commit-id", cmd_get_tar_commit_id, NO_PARSEOPT },
Brandon Williamsf9ee2fc2017-08-02 12:49:23 -0700516 { "grep", cmd_grep, RUN_SETUP_GENTLY },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100517 { "hash-object", cmd_hash_object },
518 { "help", cmd_help },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100519 { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY | NO_PARSEOPT },
Nguyễn Thái Ngọc Duy86d26f22015-12-20 14:50:18 +0700520 { "init", cmd_init_db },
521 { "init-db", cmd_init_db },
John Keepingcbd9fc22015-09-05 14:39:24 +0100522 { "interpret-trailers", cmd_interpret_trailers, RUN_SETUP_GENTLY },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100523 { "log", cmd_log, RUN_SETUP },
Brandon Williams188dce12017-06-22 11:43:48 -0700524 { "ls-files", cmd_ls_files, RUN_SETUP },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100525 { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
526 { "ls-tree", cmd_ls_tree, RUN_SETUP },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100527 { "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY | NO_PARSEOPT },
528 { "mailsplit", cmd_mailsplit, NO_PARSEOPT },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100529 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
530 { "merge-base", cmd_merge_base, RUN_SETUP },
531 { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100532 { "merge-index", cmd_merge_index, RUN_SETUP | NO_PARSEOPT },
533 { "merge-ours", cmd_merge_ours, RUN_SETUP | NO_PARSEOPT },
534 { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
535 { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
536 { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
537 { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
538 { "merge-tree", cmd_merge_tree, RUN_SETUP | NO_PARSEOPT },
539 { "mktag", cmd_mktag, RUN_SETUP | NO_PARSEOPT },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100540 { "mktree", cmd_mktree, RUN_SETUP },
Derrick Stolee6a257f02018-07-12 15:39:20 -0400541 { "multi-pack-index", cmd_multi_pack_index, RUN_SETUP_GENTLY },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100542 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
543 { "name-rev", cmd_name_rev, RUN_SETUP },
544 { "notes", cmd_notes, RUN_SETUP },
545 { "pack-objects", cmd_pack_objects, RUN_SETUP },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100546 { "pack-redundant", cmd_pack_redundant, RUN_SETUP | NO_PARSEOPT },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100547 { "pack-refs", cmd_pack_refs, RUN_SETUP },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100548 { "patch-id", cmd_patch_id, RUN_SETUP_GENTLY | NO_PARSEOPT },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100549 { "pickaxe", cmd_blame, RUN_SETUP },
550 { "prune", cmd_prune, RUN_SETUP },
551 { "prune-packed", cmd_prune_packed, RUN_SETUP },
Paul Tan1e1ea692015-06-14 16:41:51 +0800552 { "pull", cmd_pull, RUN_SETUP | NEED_WORK_TREE },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100553 { "push", cmd_push, RUN_SETUP },
Johannes Schindelin348ae562018-08-13 04:33:02 -0700554 { "range-diff", cmd_range_diff, RUN_SETUP | USE_PAGER },
Stefan Beller3d415422017-01-17 17:05:20 -0800555 { "read-tree", cmd_read_tree, RUN_SETUP | SUPPORT_SUPER_PREFIX},
Johannes Schindelin80dfc922019-07-24 14:15:00 -0700556 { "rebase", cmd_rebase, RUN_SETUP | NEED_WORK_TREE },
Alban Gruincf808202018-09-27 23:56:09 +0200557 { "rebase--interactive", cmd_rebase__interactive, RUN_SETUP | NEED_WORK_TREE },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100558 { "receive-pack", cmd_receive_pack },
559 { "reflog", cmd_reflog, RUN_SETUP },
560 { "remote", cmd_remote, RUN_SETUP },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100561 { "remote-ext", cmd_remote_ext, NO_PARSEOPT },
562 { "remote-fd", cmd_remote_fd, NO_PARSEOPT },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100563 { "repack", cmd_repack, RUN_SETUP },
564 { "replace", cmd_replace, RUN_SETUP },
565 { "rerere", cmd_rerere, RUN_SETUP },
566 { "reset", cmd_reset, RUN_SETUP },
Nguyễn Thái Ngọc Duy46e91b62019-04-25 16:45:45 +0700567 { "restore", cmd_restore, RUN_SETUP | NEED_WORK_TREE },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100568 { "rev-list", cmd_rev_list, RUN_SETUP | NO_PARSEOPT },
569 { "rev-parse", cmd_rev_parse, NO_PARSEOPT },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100570 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
571 { "rm", cmd_rm, RUN_SETUP },
572 { "send-pack", cmd_send_pack, RUN_SETUP },
573 { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
574 { "show", cmd_show, RUN_SETUP },
575 { "show-branch", cmd_show_branch, RUN_SETUP },
Jeff Kingff417262018-05-28 05:38:53 -0400576 { "show-index", cmd_show_index },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100577 { "show-ref", cmd_show_ref, RUN_SETUP },
Derrick Stolee94c09562019-11-21 22:04:33 +0000578 { "sparse-checkout", cmd_sparse_checkout, RUN_SETUP | NEED_WORK_TREE },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100579 { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
Thomas Gummerer8a2cd3f2020-03-03 17:46:13 +0000580 { "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100581 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
582 { "stripspace", cmd_stripspace },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100583 { "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT },
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +0700584 { "switch", cmd_switch, RUN_SETUP | NEED_WORK_TREE },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100585 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
Martin Ågrende121ff2017-08-02 21:40:53 +0200586 { "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100587 { "unpack-file", cmd_unpack_file, RUN_SETUP | NO_PARSEOPT },
588 { "unpack-objects", cmd_unpack_objects, RUN_SETUP | NO_PARSEOPT },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100589 { "update-index", cmd_update_index, RUN_SETUP },
590 { "update-ref", cmd_update_ref, RUN_SETUP },
591 { "update-server-info", cmd_update_server_info, RUN_SETUP },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100592 { "upload-archive", cmd_upload_archive, NO_PARSEOPT },
593 { "upload-archive--writer", cmd_upload_archive_writer, NO_PARSEOPT },
Brandon Williamsa3d6b532018-03-14 11:31:41 -0700594 { "upload-pack", cmd_upload_pack },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100595 { "var", cmd_var, RUN_SETUP_GENTLY | NO_PARSEOPT },
Michael J Gruberd07b00b2014-06-23 09:05:49 +0200596 { "verify-commit", cmd_verify_commit, RUN_SETUP },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100597 { "verify-pack", cmd_verify_pack },
598 { "verify-tag", cmd_verify_tag, RUN_SETUP },
599 { "version", cmd_version },
600 { "whatchanged", cmd_whatchanged, RUN_SETUP },
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100601 { "worktree", cmd_worktree, RUN_SETUP | NO_PARSEOPT },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100602 { "write-tree", cmd_write_tree, RUN_SETUP },
603};
604
Slavomir Vlcekc4f901d2014-11-12 14:10:22 +0100605static struct cmd_struct *get_builtin(const char *s)
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100606{
607 int i;
608 for (i = 0; i < ARRAY_SIZE(commands); i++) {
Slavomir Vlcekc4f901d2014-11-12 14:10:22 +0100609 struct cmd_struct *p = commands + i;
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100610 if (!strcmp(s, p->cmd))
Slavomir Vlcekc4f901d2014-11-12 14:10:22 +0100611 return p;
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100612 }
Slavomir Vlcekc4f901d2014-11-12 14:10:22 +0100613 return NULL;
614}
615
616int is_builtin(const char *s)
617{
618 return !!get_builtin(s);
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100619}
620
Nguyễn Thái Ngọc Duye5d7a612018-05-20 20:39:58 +0200621static void list_builtins(struct string_list *out, unsigned int exclude_option)
Jeff King8893fd92017-05-30 01:18:43 -0400622{
623 int i;
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100624 for (i = 0; i < ARRAY_SIZE(commands); i++) {
625 if (exclude_option &&
626 (commands[i].option & exclude_option))
627 continue;
Nguyễn Thái Ngọc Duye5d7a612018-05-20 20:39:58 +0200628 string_list_append(out, commands[i].cmd);
Nguyễn Thái Ngọc Duye9547942018-03-24 21:35:19 +0100629 }
Jeff King8893fd92017-05-30 01:18:43 -0400630}
631
Alexander Kuleshov63ca1c02016-02-22 13:18:29 +0600632#ifdef STRIP_EXTENSION
633static void strip_extension(const char **argv)
634{
635 size_t len;
636
637 if (strip_suffix(argv[0], STRIP_EXTENSION, &len))
638 argv[0] = xmemdupz(argv[0], len);
639}
640#else
641#define strip_extension(cmd)
642#endif
643
Sebastian Schuberth3f784a42014-01-02 17:15:44 +0100644static void handle_builtin(int argc, const char **argv)
Linus Torvalds231af832006-02-26 12:34:51 -0800645{
Ralf Thielow2c6b6d92016-08-26 19:58:36 +0200646 struct argv_array args = ARGV_ARRAY_INIT;
Alexander Kuleshov63ca1c02016-02-22 13:18:29 +0600647 const char *cmd;
Slavomir Vlcekc4f901d2014-11-12 14:10:22 +0100648 struct cmd_struct *builtin;
Johannes Sixt23326d12007-12-08 20:57:25 +0100649
Alexander Kuleshov63ca1c02016-02-22 13:18:29 +0600650 strip_extension(argv);
651 cmd = argv[0];
Linus Torvalds231af832006-02-26 12:34:51 -0800652
Ralf Thielow2c6b6d92016-08-26 19:58:36 +0200653 /* Turn "git cmd --help" into "git help --exclude-guides cmd" */
Linus Torvalds1cd95082006-04-15 11:13:49 -0700654 if (argc > 1 && !strcmp(argv[1], "--help")) {
Ralf Thielow2c6b6d92016-08-26 19:58:36 +0200655 int i;
656
Linus Torvalds1cd95082006-04-15 11:13:49 -0700657 argv[1] = argv[0];
658 argv[0] = cmd = "help";
Ralf Thielow2c6b6d92016-08-26 19:58:36 +0200659
660 for (i = 0; i < argc; i++) {
661 argv_array_push(&args, argv[i]);
662 if (!i)
663 argv_array_push(&args, "--exclude-guides");
664 }
665
666 argc++;
667 argv = args.argv;
Linus Torvalds1cd95082006-04-15 11:13:49 -0700668 }
669
Slavomir Vlcekc4f901d2014-11-12 14:10:22 +0100670 builtin = get_builtin(cmd);
Junio C Hamano441981b2016-01-26 22:52:02 -0800671 if (builtin)
672 exit(run_builtin(builtin, argc, argv));
Ralf Thielow2c6b6d92016-08-26 19:58:36 +0200673 argv_array_clear(&args);
Linus Torvalds231af832006-02-26 12:34:51 -0800674}
675
Junio C Hamano7550be02007-12-01 22:09:22 -0800676static void execv_dashed_external(const char **argv)
677{
Jeff King2b296c92017-01-06 20:16:24 -0500678 struct child_process cmd = CHILD_PROCESS_INIT;
Jeff Kingd8e96fd2009-01-28 02:38:14 -0500679 int status;
Junio C Hamano7550be02007-12-01 22:09:22 -0800680
Brandon Williams74866d72016-10-07 11:18:48 -0700681 if (get_super_prefix())
Nguyễn Thái Ngọc Duy8aa8c142018-11-10 06:16:00 +0100682 die(_("%s doesn't support --super-prefix"), argv[0]);
Brandon Williams74866d72016-10-07 11:18:48 -0700683
Martin Ågren595d59e2017-08-02 21:40:55 +0200684 if (use_pager == -1 && !is_builtin(argv[0]))
Jeff King92058e42011-08-18 15:01:32 -0700685 use_pager = check_pager_config(argv[0]);
Jonathan Nieder030149a2010-07-14 17:55:12 -0500686 commit_pager_choice();
687
Jeff King2b296c92017-01-06 20:16:24 -0500688 argv_array_pushf(&cmd.args, "git-%s", argv[0]);
689 argv_array_pushv(&cmd.args, argv + 1);
690 cmd.clean_on_exit = 1;
Jeff King46df6902017-01-06 20:22:23 -0500691 cmd.wait_after_clean = 1;
Jeff King2b296c92017-01-06 20:16:24 -0500692 cmd.silent_exec_failure = 1;
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800693 cmd.trace2_child_class = "dashed";
Junio C Hamano7550be02007-12-01 22:09:22 -0800694
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800695 trace2_cmd_name("_run_dashed_");
696
697 /*
698 * The code in run_command() logs trace2 child_start/child_exit
699 * events, so we do not need to report exec/exec_result events here.
700 */
Jeff King2b296c92017-01-06 20:16:24 -0500701 trace_argv_printf(cmd.args.argv, "trace: exec:");
Junio C Hamano7550be02007-12-01 22:09:22 -0800702
703 /*
Jeff King246f0ed2017-01-06 20:17:48 -0500704 * If we fail because the command is not found, it is
705 * OK to return. Otherwise, we just pass along the status code,
706 * or our usual generic code if we were not even able to exec
707 * the program.
Junio C Hamano7550be02007-12-01 22:09:22 -0800708 */
Jeff King2b296c92017-01-06 20:16:24 -0500709 status = run_command(&cmd);
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800710
711 /*
712 * If the child process ran and we are now going to exit, emit a
713 * generic string as our trace2 command verb to indicate that we
714 * launched a dashed command.
715 */
Jeff King246f0ed2017-01-06 20:17:48 -0500716 if (status >= 0)
Johannes Sixt5709e032009-07-04 21:26:39 +0200717 exit(status);
Jeff King246f0ed2017-01-06 20:17:48 -0500718 else if (errno != ENOENT)
719 exit(128);
Junio C Hamano7550be02007-12-01 22:09:22 -0800720}
721
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100722static int run_argv(int *argcp, const char ***argv)
723{
724 int done_alias = 0;
Tim Schumacherc6d75bc2018-09-16 09:50:00 +0200725 struct string_list cmd_list = STRING_LIST_INIT_NODUP;
Tim Schumacher82f71d92018-09-16 09:50:01 +0200726 struct string_list_item *seen;
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100727
728 while (1) {
Junio C Hamano441981b2016-01-26 22:52:02 -0800729 /*
730 * If we tried alias and futzed with our environment,
731 * it no longer is safe to invoke builtins directly in
732 * general. We have to spawn them as dashed externals.
733 *
734 * NEEDSWORK: if we can figure out cases
735 * where it is safe to do, we can avoid spawning a new
736 * process.
737 */
738 if (!done_alias)
739 handle_builtin(*argcp, *argv);
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800740 else if (get_builtin(**argv)) {
741 struct argv_array args = ARGV_ARRAY_INIT;
742 int i;
743
744 /*
745 * The current process is committed to launching a
746 * child process to run the command named in (**argv)
747 * and exiting. Log a generic string as the trace2
748 * command verb to indicate this. Note that the child
749 * process will log the actual verb when it runs.
750 */
751 trace2_cmd_name("_run_git_alias_");
752
753 if (get_super_prefix())
754 die("%s doesn't support --super-prefix", **argv);
755
756 commit_pager_choice();
757
758 argv_array_push(&args, "git");
759 for (i = 0; i < *argcp; i++)
760 argv_array_push(&args, (*argv)[i]);
761
762 trace_argv_printf(args.argv, "trace: exec:");
763
764 /*
765 * if we fail because the command is not found, it is
766 * OK to return. Otherwise, we just pass along the status code.
767 */
768 i = run_command_v_opt_tr2(args.argv, RUN_SILENT_EXEC_FAILURE |
769 RUN_CLEAN_ON_EXIT, "git_alias");
770 if (i >= 0 || errno != ENOENT)
771 exit(i);
772 die("could not execute builtin %s", **argv);
773 }
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800774
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100775 /* .. then try the external ones */
776 execv_dashed_external(*argv);
777
Tim Schumacher82f71d92018-09-16 09:50:01 +0200778 seen = unsorted_string_list_lookup(&cmd_list, *argv[0]);
779 if (seen) {
780 int i;
781 struct strbuf sb = STRBUF_INIT;
782 for (i = 0; i < cmd_list.nr; i++) {
783 struct string_list_item *item = &cmd_list.items[i];
784
785 strbuf_addf(&sb, "\n %s", item->string);
786 if (item == seen)
787 strbuf_addstr(&sb, " <==");
788 else if (i == cmd_list.nr - 1)
789 strbuf_addstr(&sb, " ==>");
790 }
Tim Schumacherc6d75bc2018-09-16 09:50:00 +0200791 die(_("alias loop detected: expansion of '%s' does"
Tim Schumacher82f71d92018-09-16 09:50:01 +0200792 " not terminate:%s"), cmd_list.items[0].string, sb.buf);
Tim Schumacherc6d75bc2018-09-16 09:50:00 +0200793 }
794
795 string_list_append(&cmd_list, *argv[0]);
796
797 /*
798 * It could be an alias -- this works around the insanity
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100799 * of overriding "git log" with "git show" by having
800 * alias.log = show
801 */
Nguyễn Thái Ngọc Duyc0562612014-06-08 16:37:10 +0700802 if (!handle_alias(argcp, argv))
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100803 break;
804 done_alias = 1;
805 }
806
Tim Schumacherc6d75bc2018-09-16 09:50:00 +0200807 string_list_clear(&cmd_list, 0);
808
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100809 return done_alias;
810}
811
Jeff King3f2e2292016-07-01 01:58:58 -0400812int cmd_main(int argc, const char **argv)
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100813{
Steve Haslam4dd47c32009-01-18 13:00:10 +0100814 const char *cmd;
Alexander Kuleshov8fa79752015-03-02 18:02:37 +0600815 int done_help = 0;
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100816
Jeff King650c4492016-07-01 02:04:04 -0400817 cmd = argv[0];
Steffen Prohaska2cd72b02009-01-18 13:00:11 +0100818 if (!cmd)
Steve Haslam4dd47c32009-01-18 13:00:10 +0100819 cmd = "git-help";
Jeff King6854a8f2016-11-26 23:31:13 -0500820 else {
821 const char *slash = find_last_dir_sep(cmd);
822 if (slash)
823 cmd = slash + 1;
824 }
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100825
Karsten Blees578da032014-07-12 02:07:01 +0200826 trace_command_performance(argv);
827
Linus Torvalds231af832006-02-26 12:34:51 -0800828 /*
829 * "git-xxxx" is the same as "git xxxx", but we obviously:
830 *
831 * - cannot take flags in between the "git" and the "xxxx".
832 * - cannot execute it externally (since it would just do
833 * the same thing over again)
834 *
Sebastian Schuberth3f784a42014-01-02 17:15:44 +0100835 * So we just directly call the builtin handler, and die if
836 * that one cannot handle it.
Linus Torvalds231af832006-02-26 12:34:51 -0800837 */
Jeff Kingae021d82014-06-18 15:47:50 -0400838 if (skip_prefix(cmd, "git-", &cmd)) {
Linus Torvalds231af832006-02-26 12:34:51 -0800839 argv[0] = cmd;
Sebastian Schuberth3f784a42014-01-02 17:15:44 +0100840 handle_builtin(argc, argv);
Nguyễn Thái Ngọc Duy8aa8c142018-11-10 06:16:00 +0100841 die(_("cannot handle %s as a builtin"), cmd);
Linus Torvalds231af832006-02-26 12:34:51 -0800842 }
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100843
Linus Torvalds231af832006-02-26 12:34:51 -0800844 /* Look for flags.. */
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200845 argv++;
846 argc--;
Matthias Lederhofer4394efe2007-06-08 22:57:55 +0200847 handle_options(&argv, &argc, NULL);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200848 if (argc > 0) {
Jeff King6d877802014-06-18 15:56:48 -0400849 /* translate --help and --version into commands */
850 skip_prefix(argv[0], "--", &argv[0]);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200851 } else {
Scott R Parish3d7e2d82007-10-27 01:36:49 -0700852 /* The user didn't specify a command; give them help */
Nguyễn Thái Ngọc Duy73e25e72010-06-26 14:26:37 -0500853 commit_pager_choice();
Nguyễn Thái Ngọc Duy8aa8c142018-11-10 06:16:00 +0100854 printf(_("usage: %s\n\n"), git_usage_string);
Scott R Parish3d7e2d82007-10-27 01:36:49 -0700855 list_common_cmds_help();
Kevin Bracey421a9762013-03-10 17:10:20 +0200856 printf("\n%s\n", _(git_more_info_string));
Scott R Parish3d7e2d82007-10-27 01:36:49 -0700857 exit(1);
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100858 }
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200859 cmd = argv[0];
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100860
Linus Torvalds231af832006-02-26 12:34:51 -0800861 /*
Scott R Parish511707d2007-10-28 04:17:20 -0700862 * We use PATH to find git commands, but we prepend some higher
Mike Ralphson3ea3c212009-04-17 19:13:30 +0100863 * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH
Scott R Parish511707d2007-10-28 04:17:20 -0700864 * environment, and the $(gitexecdir) from the Makefile at build
865 * time.
Linus Torvalds231af832006-02-26 12:34:51 -0800866 */
Johannes Sixte1464ca2008-07-21 21:19:52 +0200867 setup_path();
Junio C Hamano7dbc2c02005-11-15 23:13:30 -0800868
Junio C Hamanoa0254632006-06-05 18:09:40 -0700869 while (1) {
Alexander Kuleshov8fa79752015-03-02 18:02:37 +0600870 int was_alias = run_argv(&argc, &argv);
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100871 if (errno != ENOENT)
Junio C Hamanoa0254632006-06-05 18:09:40 -0700872 break;
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100873 if (was_alias) {
Alexander Shopovfc045fe2018-02-13 14:19:15 +0100874 fprintf(stderr, _("expansion of alias '%s' failed; "
875 "'%s' is not a git command\n"),
Theodore Ts'of3dd0152007-02-10 19:33:57 -0500876 cmd, argv[0]);
877 exit(1);
878 }
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100879 if (!done_help) {
880 cmd = argv[0] = help_unknown_cmd(cmd);
881 done_help = 1;
882 } else
883 break;
Theodore Ts'of3dd0152007-02-10 19:33:57 -0500884 }
Alex Riesen10b15b82005-12-01 13:48:35 +0100885
Alexander Shopovfc045fe2018-02-13 14:19:15 +0100886 fprintf(stderr, _("failed to run command '%s': %s\n"),
Andreas Ericsson33ebb872006-06-28 02:17:21 -0700887 cmd, strerror(errno));
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100888
889 return 1;
890}