blob: c870b9719c21b2db23ea7ab5d56fdeda483cb02e [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"
Thiago Farinafd5c3632010-08-31 23:29:08 -03003#include "exec_cmd.h"
4#include "help.h"
Jeff Kingd8e96fd2009-01-28 02:38:14 -05005#include "run-command.h"
Andreas Ericsson8e49d502005-11-16 00:31:25 +01006
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +01007const char git_usage_string[] =
Nazri Ramliy44e1e4d2013-09-09 21:47:43 +08008 "git [--version] [--help] [-C <path>] [-c name=value]\n"
Kevin Bracey03a0fb02013-03-11 21:44:15 +02009 " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
Alex Henrie9c9b4f22015-01-13 00:44:47 -070010 " [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]\n"
Josh Tripletta1bea2c2011-07-05 10:54:44 -070011 " [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
Štěpán Němec62b46982010-10-08 19:31:15 +020012 " <command> [<args>]";
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +010013
Teemu Likonenb7d96812008-06-06 00:15:36 +030014const char git_more_info_string[] =
Alex Henriead5fe372014-08-30 13:56:01 -060015 N_("'git help -a' and 'git help -g' list available subcommands and some\n"
Philip Oakley73903d02013-04-02 23:39:48 +010016 "concept guides. See 'git help <command>' or 'git help <concept>'\n"
17 "to read about a specific subcommand or concept.");
Teemu Likonenb7d96812008-06-06 00:15:36 +030018
Jeff King4e107382008-07-03 07:46:57 -040019static int use_pager = -1;
Nguyễn Thái Ngọc Duyc0562612014-06-08 16:37:10 +070020
Jeff King8893fd92017-05-30 01:18:43 -040021static void list_builtins(void);
22
Jeff King4e107382008-07-03 07:46:57 -040023static void commit_pager_choice(void) {
24 switch (use_pager) {
25 case 0:
26 setenv("GIT_PAGER", "cat", 1);
27 break;
28 case 1:
29 setup_pager();
30 break;
31 default:
32 break;
33 }
34}
35
Martin Ågren033fe3d2017-08-02 21:40:51 +020036void setup_auto_pager(const char *cmd, int def)
37{
38 if (use_pager != -1 || pager_in_use())
39 return;
40 use_pager = check_pager_config(cmd);
41 if (use_pager == -1)
42 use_pager = def;
43 commit_pager_choice();
44}
45
Felipe Contreras4b25d092009-05-01 12:06:36 +030046static int handle_options(const char ***argv, int *argc, int *envchanged)
Johannes Schindelin4ab243a2006-07-24 14:10:45 +020047{
Junio C Hamano73546c02011-05-24 18:50:35 -040048 const char **orig_argv = *argv;
Johannes Schindelin4ab243a2006-07-24 14:10:45 +020049
50 while (*argc > 0) {
51 const char *cmd = (*argv)[0];
52 if (cmd[0] != '-')
53 break;
54
Johannes Schindelin6acbcb92006-07-25 20:24:22 +020055 /*
56 * For legacy reasons, the "version" and "help"
57 * commands can be written with "--" prepended
58 * to make them look like flags.
59 */
60 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
61 break;
62
63 /*
64 * Check remaining flags.
65 */
Jeff Kingae021d82014-06-18 15:47:50 -040066 if (skip_prefix(cmd, "--exec-path", &cmd)) {
Johannes Schindelin6acbcb92006-07-25 20:24:22 +020067 if (*cmd == '=')
Scott R Parish384df832007-10-27 01:36:51 -070068 git_set_argv_exec_path(cmd + 1);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +020069 else {
70 puts(git_exec_path());
71 exit(0);
72 }
Markus Heidelberg89a56bf2009-04-05 04:15:16 +020073 } else if (!strcmp(cmd, "--html-path")) {
74 puts(system_path(GIT_HTML_PATH));
75 exit(0);
Jon Seymourf2dd8c32011-05-01 18:16:25 +100076 } else if (!strcmp(cmd, "--man-path")) {
77 puts(system_path(GIT_MAN_PATH));
78 exit(0);
79 } else if (!strcmp(cmd, "--info-path")) {
80 puts(system_path(GIT_INFO_PATH));
81 exit(0);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +020082 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
Jeff King4e107382008-07-03 07:46:57 -040083 use_pager = 1;
Matthieu Moy463a8492007-08-19 19:24:36 +020084 } else if (!strcmp(cmd, "--no-pager")) {
Jeff King4e107382008-07-03 07:46:57 -040085 use_pager = 0;
Matthieu Moy463a8492007-08-19 19:24:36 +020086 if (envchanged)
87 *envchanged = 1;
Christian Couderb0fa7ab2009-10-12 22:30:32 +020088 } else if (!strcmp(cmd, "--no-replace-objects")) {
Michael Haggertyafc711b2014-02-18 12:24:55 +010089 check_replace_refs = 0;
Christian Couder6476b382009-11-18 07:50:58 +010090 setenv(NO_REPLACE_OBJECTS_ENVIRONMENT, "1", 1);
91 if (envchanged)
92 *envchanged = 1;
Johannes Schindelin6acbcb92006-07-25 20:24:22 +020093 } else if (!strcmp(cmd, "--git-dir")) {
Brian Gernhardtc321f002006-12-22 08:56:25 -050094 if (*argc < 2) {
95 fprintf(stderr, "No directory given for --git-dir.\n" );
96 usage(git_usage_string);
97 }
Shawn O. Pearce45b09792006-12-30 23:29:11 -050098 setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1);
Matthias Lederhofer4394efe2007-06-08 22:57:55 +020099 if (envchanged)
100 *envchanged = 1;
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200101 (*argv)++;
102 (*argc)--;
Jeff Kingae021d82014-06-18 15:47:50 -0400103 } else if (skip_prefix(cmd, "--git-dir=", &cmd)) {
104 setenv(GIT_DIR_ENVIRONMENT, cmd, 1);
Matthias Lederhofer4394efe2007-06-08 22:57:55 +0200105 if (envchanged)
106 *envchanged = 1;
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700107 } else if (!strcmp(cmd, "--namespace")) {
108 if (*argc < 2) {
109 fprintf(stderr, "No namespace given for --namespace.\n" );
110 usage(git_usage_string);
111 }
112 setenv(GIT_NAMESPACE_ENVIRONMENT, (*argv)[1], 1);
113 if (envchanged)
114 *envchanged = 1;
115 (*argv)++;
116 (*argc)--;
Jeff Kingae021d82014-06-18 15:47:50 -0400117 } else if (skip_prefix(cmd, "--namespace=", &cmd)) {
118 setenv(GIT_NAMESPACE_ENVIRONMENT, cmd, 1);
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700119 if (envchanged)
120 *envchanged = 1;
Matthias Lederhofer892c41b2007-06-06 09:10:42 +0200121 } else if (!strcmp(cmd, "--work-tree")) {
122 if (*argc < 2) {
123 fprintf(stderr, "No directory given for --work-tree.\n" );
124 usage(git_usage_string);
125 }
126 setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
Matthias Lederhofer4394efe2007-06-08 22:57:55 +0200127 if (envchanged)
128 *envchanged = 1;
Matthias Lederhofer892c41b2007-06-06 09:10:42 +0200129 (*argv)++;
130 (*argc)--;
Jeff Kingae021d82014-06-18 15:47:50 -0400131 } else if (skip_prefix(cmd, "--work-tree=", &cmd)) {
132 setenv(GIT_WORK_TREE_ENVIRONMENT, cmd, 1);
Matthias Lederhofer4394efe2007-06-08 22:57:55 +0200133 if (envchanged)
134 *envchanged = 1;
Brandon Williams74866d72016-10-07 11:18:48 -0700135 } else if (!strcmp(cmd, "--super-prefix")) {
136 if (*argc < 2) {
137 fprintf(stderr, "No prefix given for --super-prefix.\n" );
138 usage(git_usage_string);
139 }
140 setenv(GIT_SUPER_PREFIX_ENVIRONMENT, (*argv)[1], 1);
141 if (envchanged)
142 *envchanged = 1;
143 (*argv)++;
144 (*argc)--;
145 } else if (skip_prefix(cmd, "--super-prefix=", &cmd)) {
146 setenv(GIT_SUPER_PREFIX_ENVIRONMENT, cmd, 1);
147 if (envchanged)
148 *envchanged = 1;
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200149 } else if (!strcmp(cmd, "--bare")) {
René Scharfe4d3ab442014-07-28 20:31:57 +0200150 char *cwd = xgetcwd();
Junio C Hamano6adcca32007-08-27 00:58:06 -0700151 is_bare_repository_cfg = 1;
René Scharfe4d3ab442014-07-28 20:31:57 +0200152 setenv(GIT_DIR_ENVIRONMENT, cwd, 0);
153 free(cwd);
Jeff King2cd83d12013-03-08 04:32:22 -0500154 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
Matthias Lederhofer4394efe2007-06-08 22:57:55 +0200155 if (envchanged)
156 *envchanged = 1;
Alex Riesen8b1fa772010-03-26 23:53:57 +0100157 } else if (!strcmp(cmd, "-c")) {
158 if (*argc < 2) {
159 fprintf(stderr, "-c expects a configuration string\n" );
160 usage(git_usage_string);
161 }
Jeff King2b64fc82010-08-23 15:16:00 -0400162 git_config_push_parameter((*argv)[1]);
Alex Riesen8b1fa772010-03-26 23:53:57 +0100163 (*argv)++;
164 (*argc)--;
Jeff King823ab402012-12-19 17:37:30 -0500165 } else if (!strcmp(cmd, "--literal-pathspecs")) {
166 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "1", 1);
167 if (envchanged)
168 *envchanged = 1;
169 } else if (!strcmp(cmd, "--no-literal-pathspecs")) {
170 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "0", 1);
171 if (envchanged)
172 *envchanged = 1;
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +0700173 } else if (!strcmp(cmd, "--glob-pathspecs")) {
174 setenv(GIT_GLOB_PATHSPECS_ENVIRONMENT, "1", 1);
175 if (envchanged)
176 *envchanged = 1;
177 } else if (!strcmp(cmd, "--noglob-pathspecs")) {
178 setenv(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, "1", 1);
179 if (envchanged)
180 *envchanged = 1;
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700181 } else if (!strcmp(cmd, "--icase-pathspecs")) {
182 setenv(GIT_ICASE_PATHSPECS_ENVIRONMENT, "1", 1);
183 if (envchanged)
184 *envchanged = 1;
Jeff King27344d62017-09-27 02:54:30 -0400185 } else if (!strcmp(cmd, "--no-optional-locks")) {
186 setenv(GIT_OPTIONAL_LOCKS_ENVIRONMENT, "0", 1);
187 if (envchanged)
188 *envchanged = 1;
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700189 } else if (!strcmp(cmd, "--shallow-file")) {
190 (*argv)++;
191 (*argc)--;
Nguyễn Thái Ngọc Duy069c0532013-12-05 20:02:45 +0700192 set_alternate_shallow_file((*argv)[0], 1);
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700193 if (envchanged)
194 *envchanged = 1;
Nazri Ramliy44e1e4d2013-09-09 21:47:43 +0800195 } else if (!strcmp(cmd, "-C")) {
196 if (*argc < 2) {
197 fprintf(stderr, "No directory given for -C.\n" );
198 usage(git_usage_string);
199 }
Karthik Nayak6a536e22015-03-06 16:48:08 +0530200 if ((*argv)[1][0]) {
201 if (chdir((*argv)[1]))
202 die_errno("Cannot change to '%s'", (*argv)[1]);
203 if (envchanged)
204 *envchanged = 1;
205 }
Nazri Ramliy44e1e4d2013-09-09 21:47:43 +0800206 (*argv)++;
207 (*argc)--;
Jeff King8893fd92017-05-30 01:18:43 -0400208 } else if (!strcmp(cmd, "--list-builtins")) {
209 list_builtins();
210 exit(0);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200211 } else {
212 fprintf(stderr, "Unknown option: %s\n", cmd);
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +0100213 usage(git_usage_string);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200214 }
Johannes Schindelin4ab243a2006-07-24 14:10:45 +0200215
216 (*argv)++;
217 (*argc)--;
Johannes Schindelin4ab243a2006-07-24 14:10:45 +0200218 }
Junio C Hamano73546c02011-05-24 18:50:35 -0400219 return (*argv) - orig_argv;
Johannes Schindelin4ab243a2006-07-24 14:10:45 +0200220}
221
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200222static int handle_alias(int *argcp, const char ***argv)
223{
SZEDER Gáboraf05d672008-03-25 22:06:26 +0100224 int envchanged = 0, ret = 0, saved_errno = errno;
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200225 int count, option_count;
Felipe Contreras4b25d092009-05-01 12:06:36 +0300226 const char **new_argv;
Jeff King94351112008-02-24 17:17:14 -0500227 const char *alias_command;
228 char *alias_string;
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200229
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200230 alias_command = (*argv)[0];
Jeff King94351112008-02-24 17:17:14 -0500231 alias_string = alias_lookup(alias_command);
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200232 if (alias_string) {
Theodore Ts'odfd42a32007-02-10 19:33:58 -0500233 if (alias_string[0] == '!') {
Jeff King850d2fe2016-02-22 17:44:21 -0500234 struct child_process child = CHILD_PROCESS_INIT;
Johannes Schindelina9bcf652017-06-14 13:36:00 +0200235 int nongit_ok;
236
237 /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */
238 setup_git_directory_gently(&nongit_ok);
Johannes Schindelina2f80282007-07-01 22:51:58 +0100239
Erik Faye-Lund7f51f8b2011-01-07 00:00:38 +0100240 commit_pager_choice();
241
Jeff King850d2fe2016-02-22 17:44:21 -0500242 child.use_shell = 1;
243 argv_array_push(&child.args, alias_string + 1);
244 argv_array_pushv(&child.args, (*argv) + 1);
Erik Faye-Lund7f51f8b2011-01-07 00:00:38 +0100245
Jeff King850d2fe2016-02-22 17:44:21 -0500246 ret = run_command(&child);
Erik Faye-Lund7f51f8b2011-01-07 00:00:38 +0100247 if (ret >= 0) /* normal exit */
248 exit(ret);
249
250 die_errno("While expanding alias '%s': '%s'",
251 alias_command, alias_string + 1);
Theodore Ts'odfd42a32007-02-10 19:33:58 -0500252 }
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200253 count = split_cmdline(alias_string, &new_argv);
Deskin Millerdc4179f2008-09-22 11:06:41 -0400254 if (count < 0)
Greg Brockmanad9ac6d2010-08-07 01:13:39 -0400255 die("Bad alias.%s string: %s", alias_command,
256 split_cmdline_strerror(count));
Matthias Lederhofer4394efe2007-06-08 22:57:55 +0200257 option_count = handle_options(&new_argv, &count, &envchanged);
258 if (envchanged)
259 die("alias '%s' changes environment variables\n"
260 "You can use '!git' in the alias to do this.",
261 alias_command);
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200262 memmove(new_argv - option_count, new_argv,
263 count * sizeof(char *));
264 new_argv -= option_count;
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200265
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200266 if (count < 1)
267 die("empty alias for %s", alias_command);
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200268
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200269 if (!strcmp(alias_command, new_argv[0]))
270 die("recursive alias: %s", alias_command);
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200271
Christian Couderb319ce42007-12-03 05:51:50 +0100272 trace_argv_printf(new_argv,
Christian Couder6ce4e612006-09-02 18:23:48 +0200273 "trace: alias expansion: %s =>",
274 alias_command);
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200275
René Scharfe2756ca42014-09-16 20:56:57 +0200276 REALLOC_ARRAY(new_argv, count + *argcp);
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200277 /* insert after command name */
Felipe Contreras4b25d092009-05-01 12:06:36 +0300278 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200279
280 *argv = new_argv;
281 *argcp += count - 1;
282
283 ret = 1;
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200284 }
285
Johannes Schindelin47e5c0c2006-06-28 12:45:27 +0200286 errno = saved_errno;
287
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200288 return ret;
289}
290
Nguyễn Thái Ngọc Duyee38dfb2010-08-05 21:52:16 -0500291#define RUN_SETUP (1<<0)
292#define RUN_SETUP_GENTLY (1<<1)
293#define USE_PAGER (1<<2)
Shawn O. Pearce7eff28a2006-12-30 23:32:38 -0500294/*
295 * require working tree to be present -- anything uses this needs
296 * RUN_SETUP for reading from the configuration file.
297 */
Nguyễn Thái Ngọc Duyee38dfb2010-08-05 21:52:16 -0500298#define NEED_WORK_TREE (1<<3)
Brandon Williams74866d72016-10-07 11:18:48 -0700299#define SUPPORT_SUPER_PREFIX (1<<4)
Martin Ågrenc4098242017-08-02 21:40:50 +0200300#define DELAY_PAGER_CONFIG (1<<5)
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700301
Linus Torvalds47d0b4f2007-06-24 10:10:40 -0700302struct cmd_struct {
303 const char *cmd;
304 int (*fn)(int, const char **, const char *);
305 int option;
306};
307
Jeff Kingf172f332009-01-28 02:33:53 -0500308static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
Linus Torvalds47d0b4f2007-06-24 10:10:40 -0700309{
Jonathan Nieder99caeed2009-11-09 09:05:01 -0600310 int status, help;
Linus Torvalds0f157312007-06-24 10:29:33 -0700311 struct stat st;
Linus Torvalds47d0b4f2007-06-24 10:10:40 -0700312 const char *prefix;
313
314 prefix = NULL;
Jonathan Nieder99caeed2009-11-09 09:05:01 -0600315 help = argc == 2 && !strcmp(argv[1], "-h");
316 if (!help) {
317 if (p->option & RUN_SETUP)
318 prefix = setup_git_directory();
Luis R. Rodriguez27bd38d2014-04-21 17:47:56 -0700319 else if (p->option & RUN_SETUP_GENTLY) {
Nguyễn Thái Ngọc Duyee38dfb2010-08-05 21:52:16 -0500320 int nongit_ok;
321 prefix = setup_git_directory_gently(&nongit_ok);
322 }
Jeff King4e107382008-07-03 07:46:57 -0400323
Martin Ågrenc4098242017-08-02 21:40:50 +0200324 if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY) &&
325 !(p->option & DELAY_PAGER_CONFIG))
Jonathan Nieder99caeed2009-11-09 09:05:01 -0600326 use_pager = check_pager_config(p->cmd);
327 if (use_pager == -1 && p->option & USE_PAGER)
328 use_pager = 1;
Nguyễn Thái Ngọc Duya9ca8a82010-11-26 22:31:57 +0700329
330 if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) &&
331 startup_info->have_repository) /* get_git_dir() may set up repo, avoid that */
332 trace_repo_setup(prefix);
Jonathan Nieder99caeed2009-11-09 09:05:01 -0600333 }
Jeff King4e107382008-07-03 07:46:57 -0400334 commit_pager_choice();
335
Brandon Williams74866d72016-10-07 11:18:48 -0700336 if (!help && get_super_prefix()) {
337 if (!(p->option & SUPPORT_SUPER_PREFIX))
338 die("%s doesn't support --super-prefix", p->cmd);
Brandon Williams74866d72016-10-07 11:18:48 -0700339 }
340
Jonathan Nieder99caeed2009-11-09 09:05:01 -0600341 if (!help && p->option & NEED_WORK_TREE)
Mike Hommey59f0f2f2007-11-03 12:23:11 +0100342 setup_work_tree();
343
Christian Couderb319ce42007-12-03 05:51:50 +0100344 trace_argv_printf(argv, "trace: built-in: git");
Linus Torvalds47d0b4f2007-06-24 10:10:40 -0700345
Linus Torvalds0f157312007-06-24 10:29:33 -0700346 status = p->fn(argc, argv, prefix);
347 if (status)
Johannes Sixt47e3de02009-07-05 20:57:46 +0200348 return status;
Linus Torvalds0f157312007-06-24 10:29:33 -0700349
350 /* Somebody closed stdout? */
351 if (fstat(fileno(stdout), &st))
352 return 0;
353 /* Ignore write errors for pipes and sockets.. */
354 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
355 return 0;
356
357 /* Check for ENOSPC and EIO errors.. */
Linus Torvalds0227f982007-06-30 11:44:20 -0700358 if (fflush(stdout))
Thomas Rastd824cbb2009-06-27 17:58:46 +0200359 die_errno("write failure on standard output");
Linus Torvalds0227f982007-06-30 11:44:20 -0700360 if (ferror(stdout))
361 die("unknown write failure on standard output");
362 if (fclose(stdout))
Thomas Rastd824cbb2009-06-27 17:58:46 +0200363 die_errno("close failed on standard output");
Linus Torvalds0f157312007-06-24 10:29:33 -0700364 return 0;
Linus Torvalds47d0b4f2007-06-24 10:10:40 -0700365}
366
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100367static struct cmd_struct commands[] = {
368 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
Paul Tan783d7e82015-08-04 21:52:06 +0800369 { "am", cmd_am, RUN_SETUP | NEED_WORK_TREE },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100370 { "annotate", cmd_annotate, RUN_SETUP },
371 { "apply", cmd_apply, RUN_SETUP_GENTLY },
Junio C Hamanoeb0224c2016-11-22 13:37:04 -0800372 { "archive", cmd_archive, RUN_SETUP_GENTLY },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100373 { "bisect--helper", cmd_bisect__helper, RUN_SETUP },
374 { "blame", cmd_blame, RUN_SETUP },
Martin Ågrend74b5412017-11-19 16:03:49 +0100375 { "branch", cmd_branch, RUN_SETUP | DELAY_PAGER_CONFIG },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100376 { "bundle", cmd_bundle, RUN_SETUP_GENTLY },
377 { "cat-file", cmd_cat_file, RUN_SETUP },
378 { "check-attr", cmd_check_attr, RUN_SETUP },
379 { "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
380 { "check-mailmap", cmd_check_mailmap, RUN_SETUP },
381 { "check-ref-format", cmd_check_ref_format },
Eric Sunshine0ca560c2015-07-06 13:30:56 -0400382 { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100383 { "checkout-index", cmd_checkout_index,
384 RUN_SETUP | NEED_WORK_TREE},
385 { "cherry", cmd_cherry, RUN_SETUP },
386 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
387 { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
Nguyễn Thái Ngọc Duy86d26f22015-12-20 14:50:18 +0700388 { "clone", cmd_clone },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100389 { "column", cmd_column, RUN_SETUP_GENTLY },
390 { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
391 { "commit-tree", cmd_commit_tree, RUN_SETUP },
392 { "config", cmd_config, RUN_SETUP_GENTLY },
393 { "count-objects", cmd_count_objects, RUN_SETUP },
394 { "credential", cmd_credential, RUN_SETUP_GENTLY },
395 { "describe", cmd_describe, RUN_SETUP },
396 { "diff", cmd_diff },
397 { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
398 { "diff-index", cmd_diff_index, RUN_SETUP },
399 { "diff-tree", cmd_diff_tree, RUN_SETUP },
Johannes Schindelin019678d2017-01-19 21:30:40 +0100400 { "difftool", cmd_difftool, RUN_SETUP | NEED_WORK_TREE },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100401 { "fast-export", cmd_fast_export, RUN_SETUP },
402 { "fetch", cmd_fetch, RUN_SETUP },
403 { "fetch-pack", cmd_fetch_pack, RUN_SETUP },
404 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
405 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
406 { "format-patch", cmd_format_patch, RUN_SETUP },
407 { "fsck", cmd_fsck, RUN_SETUP },
408 { "fsck-objects", cmd_fsck, RUN_SETUP },
409 { "gc", cmd_gc, RUN_SETUP },
410 { "get-tar-commit-id", cmd_get_tar_commit_id },
Brandon Williamsf9ee2fc2017-08-02 12:49:23 -0700411 { "grep", cmd_grep, RUN_SETUP_GENTLY },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100412 { "hash-object", cmd_hash_object },
413 { "help", cmd_help },
414 { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
Nguyễn Thái Ngọc Duy86d26f22015-12-20 14:50:18 +0700415 { "init", cmd_init_db },
416 { "init-db", cmd_init_db },
John Keepingcbd9fc22015-09-05 14:39:24 +0100417 { "interpret-trailers", cmd_interpret_trailers, RUN_SETUP_GENTLY },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100418 { "log", cmd_log, RUN_SETUP },
Brandon Williams188dce12017-06-22 11:43:48 -0700419 { "ls-files", cmd_ls_files, RUN_SETUP },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100420 { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
421 { "ls-tree", cmd_ls_tree, RUN_SETUP },
Junio C Hamano3f0ec062016-11-22 13:13:16 -0800422 { "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100423 { "mailsplit", cmd_mailsplit },
424 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
425 { "merge-base", cmd_merge_base, RUN_SETUP },
426 { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
427 { "merge-index", cmd_merge_index, RUN_SETUP },
428 { "merge-ours", cmd_merge_ours, RUN_SETUP },
429 { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
430 { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
431 { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
432 { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
433 { "merge-tree", cmd_merge_tree, RUN_SETUP },
434 { "mktag", cmd_mktag, RUN_SETUP },
435 { "mktree", cmd_mktree, RUN_SETUP },
436 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
437 { "name-rev", cmd_name_rev, RUN_SETUP },
438 { "notes", cmd_notes, RUN_SETUP },
439 { "pack-objects", cmd_pack_objects, RUN_SETUP },
440 { "pack-redundant", cmd_pack_redundant, RUN_SETUP },
441 { "pack-refs", cmd_pack_refs, RUN_SETUP },
Jeff King4a73aaa2016-09-12 20:23:22 -0700442 { "patch-id", cmd_patch_id, RUN_SETUP_GENTLY },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100443 { "pickaxe", cmd_blame, RUN_SETUP },
444 { "prune", cmd_prune, RUN_SETUP },
445 { "prune-packed", cmd_prune_packed, RUN_SETUP },
Paul Tan1e1ea692015-06-14 16:41:51 +0800446 { "pull", cmd_pull, RUN_SETUP | NEED_WORK_TREE },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100447 { "push", cmd_push, RUN_SETUP },
Stefan Beller3d415422017-01-17 17:05:20 -0800448 { "read-tree", cmd_read_tree, RUN_SETUP | SUPPORT_SUPER_PREFIX},
Johannes Schindelin4557f1a2017-02-09 23:23:06 +0100449 { "rebase--helper", cmd_rebase__helper, RUN_SETUP | NEED_WORK_TREE },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100450 { "receive-pack", cmd_receive_pack },
451 { "reflog", cmd_reflog, RUN_SETUP },
452 { "remote", cmd_remote, RUN_SETUP },
453 { "remote-ext", cmd_remote_ext },
454 { "remote-fd", cmd_remote_fd },
455 { "repack", cmd_repack, RUN_SETUP },
456 { "replace", cmd_replace, RUN_SETUP },
457 { "rerere", cmd_rerere, RUN_SETUP },
458 { "reset", cmd_reset, RUN_SETUP },
459 { "rev-list", cmd_rev_list, RUN_SETUP },
460 { "rev-parse", cmd_rev_parse },
461 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
462 { "rm", cmd_rm, RUN_SETUP },
463 { "send-pack", cmd_send_pack, RUN_SETUP },
464 { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
465 { "show", cmd_show, RUN_SETUP },
466 { "show-branch", cmd_show_branch, RUN_SETUP },
467 { "show-ref", cmd_show_ref, RUN_SETUP },
468 { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
469 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
470 { "stripspace", cmd_stripspace },
Stefan Beller89c86262016-12-08 13:03:25 -0800471 { "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX},
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100472 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
Martin Ågrende121ff2017-08-02 21:40:53 +0200473 { "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100474 { "unpack-file", cmd_unpack_file, RUN_SETUP },
475 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
476 { "update-index", cmd_update_index, RUN_SETUP },
477 { "update-ref", cmd_update_ref, RUN_SETUP },
478 { "update-server-info", cmd_update_server_info, RUN_SETUP },
479 { "upload-archive", cmd_upload_archive },
480 { "upload-archive--writer", cmd_upload_archive_writer },
481 { "var", cmd_var, RUN_SETUP_GENTLY },
Michael J Gruberd07b00b2014-06-23 09:05:49 +0200482 { "verify-commit", cmd_verify_commit, RUN_SETUP },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100483 { "verify-pack", cmd_verify_pack },
484 { "verify-tag", cmd_verify_tag, RUN_SETUP },
485 { "version", cmd_version },
486 { "whatchanged", cmd_whatchanged, RUN_SETUP },
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700487 { "worktree", cmd_worktree, RUN_SETUP },
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100488 { "write-tree", cmd_write_tree, RUN_SETUP },
489};
490
Slavomir Vlcekc4f901d2014-11-12 14:10:22 +0100491static struct cmd_struct *get_builtin(const char *s)
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100492{
493 int i;
494 for (i = 0; i < ARRAY_SIZE(commands); i++) {
Slavomir Vlcekc4f901d2014-11-12 14:10:22 +0100495 struct cmd_struct *p = commands + i;
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100496 if (!strcmp(s, p->cmd))
Slavomir Vlcekc4f901d2014-11-12 14:10:22 +0100497 return p;
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100498 }
Slavomir Vlcekc4f901d2014-11-12 14:10:22 +0100499 return NULL;
500}
501
502int is_builtin(const char *s)
503{
504 return !!get_builtin(s);
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100505}
506
Jeff King8893fd92017-05-30 01:18:43 -0400507static void list_builtins(void)
508{
509 int i;
510 for (i = 0; i < ARRAY_SIZE(commands); i++)
511 printf("%s\n", commands[i].cmd);
512}
513
Alexander Kuleshov63ca1c02016-02-22 13:18:29 +0600514#ifdef STRIP_EXTENSION
515static void strip_extension(const char **argv)
516{
517 size_t len;
518
519 if (strip_suffix(argv[0], STRIP_EXTENSION, &len))
520 argv[0] = xmemdupz(argv[0], len);
521}
522#else
523#define strip_extension(cmd)
524#endif
525
Sebastian Schuberth3f784a42014-01-02 17:15:44 +0100526static void handle_builtin(int argc, const char **argv)
Linus Torvalds231af832006-02-26 12:34:51 -0800527{
Ralf Thielow2c6b6d92016-08-26 19:58:36 +0200528 struct argv_array args = ARGV_ARRAY_INIT;
Alexander Kuleshov63ca1c02016-02-22 13:18:29 +0600529 const char *cmd;
Slavomir Vlcekc4f901d2014-11-12 14:10:22 +0100530 struct cmd_struct *builtin;
Johannes Sixt23326d12007-12-08 20:57:25 +0100531
Alexander Kuleshov63ca1c02016-02-22 13:18:29 +0600532 strip_extension(argv);
533 cmd = argv[0];
Linus Torvalds231af832006-02-26 12:34:51 -0800534
Ralf Thielow2c6b6d92016-08-26 19:58:36 +0200535 /* Turn "git cmd --help" into "git help --exclude-guides cmd" */
Linus Torvalds1cd95082006-04-15 11:13:49 -0700536 if (argc > 1 && !strcmp(argv[1], "--help")) {
Ralf Thielow2c6b6d92016-08-26 19:58:36 +0200537 int i;
538
Linus Torvalds1cd95082006-04-15 11:13:49 -0700539 argv[1] = argv[0];
540 argv[0] = cmd = "help";
Ralf Thielow2c6b6d92016-08-26 19:58:36 +0200541
542 for (i = 0; i < argc; i++) {
543 argv_array_push(&args, argv[i]);
544 if (!i)
545 argv_array_push(&args, "--exclude-guides");
546 }
547
548 argc++;
549 argv = args.argv;
Linus Torvalds1cd95082006-04-15 11:13:49 -0700550 }
551
Slavomir Vlcekc4f901d2014-11-12 14:10:22 +0100552 builtin = get_builtin(cmd);
Junio C Hamano441981b2016-01-26 22:52:02 -0800553 if (builtin)
554 exit(run_builtin(builtin, argc, argv));
Ralf Thielow2c6b6d92016-08-26 19:58:36 +0200555 argv_array_clear(&args);
Linus Torvalds231af832006-02-26 12:34:51 -0800556}
557
Junio C Hamano7550be02007-12-01 22:09:22 -0800558static void execv_dashed_external(const char **argv)
559{
Jeff King2b296c92017-01-06 20:16:24 -0500560 struct child_process cmd = CHILD_PROCESS_INIT;
Jeff Kingd8e96fd2009-01-28 02:38:14 -0500561 int status;
Junio C Hamano7550be02007-12-01 22:09:22 -0800562
Brandon Williams74866d72016-10-07 11:18:48 -0700563 if (get_super_prefix())
564 die("%s doesn't support --super-prefix", argv[0]);
565
Martin Ågren595d59e2017-08-02 21:40:55 +0200566 if (use_pager == -1 && !is_builtin(argv[0]))
Jeff King92058e42011-08-18 15:01:32 -0700567 use_pager = check_pager_config(argv[0]);
Jonathan Nieder030149a2010-07-14 17:55:12 -0500568 commit_pager_choice();
569
Jeff King2b296c92017-01-06 20:16:24 -0500570 argv_array_pushf(&cmd.args, "git-%s", argv[0]);
571 argv_array_pushv(&cmd.args, argv + 1);
572 cmd.clean_on_exit = 1;
Jeff King46df6902017-01-06 20:22:23 -0500573 cmd.wait_after_clean = 1;
Jeff King2b296c92017-01-06 20:16:24 -0500574 cmd.silent_exec_failure = 1;
Junio C Hamano7550be02007-12-01 22:09:22 -0800575
Jeff King2b296c92017-01-06 20:16:24 -0500576 trace_argv_printf(cmd.args.argv, "trace: exec:");
Junio C Hamano7550be02007-12-01 22:09:22 -0800577
578 /*
Jeff King246f0ed2017-01-06 20:17:48 -0500579 * If we fail because the command is not found, it is
580 * OK to return. Otherwise, we just pass along the status code,
581 * or our usual generic code if we were not even able to exec
582 * the program.
Junio C Hamano7550be02007-12-01 22:09:22 -0800583 */
Jeff King2b296c92017-01-06 20:16:24 -0500584 status = run_command(&cmd);
Jeff King246f0ed2017-01-06 20:17:48 -0500585 if (status >= 0)
Johannes Sixt5709e032009-07-04 21:26:39 +0200586 exit(status);
Jeff King246f0ed2017-01-06 20:17:48 -0500587 else if (errno != ENOENT)
588 exit(128);
Junio C Hamano7550be02007-12-01 22:09:22 -0800589}
590
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100591static int run_argv(int *argcp, const char ***argv)
592{
593 int done_alias = 0;
594
595 while (1) {
Junio C Hamano441981b2016-01-26 22:52:02 -0800596 /*
597 * If we tried alias and futzed with our environment,
598 * it no longer is safe to invoke builtins directly in
599 * general. We have to spawn them as dashed externals.
600 *
601 * NEEDSWORK: if we can figure out cases
602 * where it is safe to do, we can avoid spawning a new
603 * process.
604 */
605 if (!done_alias)
606 handle_builtin(*argcp, *argv);
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100607
608 /* .. then try the external ones */
609 execv_dashed_external(*argv);
610
611 /* It could be an alias -- this works around the insanity
612 * of overriding "git log" with "git show" by having
613 * alias.log = show
614 */
Nguyễn Thái Ngọc Duyc0562612014-06-08 16:37:10 +0700615 if (done_alias)
616 break;
Nguyễn Thái Ngọc Duyc0562612014-06-08 16:37:10 +0700617 if (!handle_alias(argcp, argv))
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100618 break;
619 done_alias = 1;
620 }
621
622 return done_alias;
623}
624
Jeff King3f2e2292016-07-01 01:58:58 -0400625int cmd_main(int argc, const char **argv)
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100626{
Steve Haslam4dd47c32009-01-18 13:00:10 +0100627 const char *cmd;
Alexander Kuleshov8fa79752015-03-02 18:02:37 +0600628 int done_help = 0;
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100629
Jeff King650c4492016-07-01 02:04:04 -0400630 cmd = argv[0];
Steffen Prohaska2cd72b02009-01-18 13:00:11 +0100631 if (!cmd)
Steve Haslam4dd47c32009-01-18 13:00:10 +0100632 cmd = "git-help";
Jeff King6854a8f2016-11-26 23:31:13 -0500633 else {
634 const char *slash = find_last_dir_sep(cmd);
635 if (slash)
636 cmd = slash + 1;
637 }
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100638
Karsten Blees578da032014-07-12 02:07:01 +0200639 trace_command_performance(argv);
640
Linus Torvalds231af832006-02-26 12:34:51 -0800641 /*
642 * "git-xxxx" is the same as "git xxxx", but we obviously:
643 *
644 * - cannot take flags in between the "git" and the "xxxx".
645 * - cannot execute it externally (since it would just do
646 * the same thing over again)
647 *
Sebastian Schuberth3f784a42014-01-02 17:15:44 +0100648 * So we just directly call the builtin handler, and die if
649 * that one cannot handle it.
Linus Torvalds231af832006-02-26 12:34:51 -0800650 */
Jeff Kingae021d82014-06-18 15:47:50 -0400651 if (skip_prefix(cmd, "git-", &cmd)) {
Linus Torvalds231af832006-02-26 12:34:51 -0800652 argv[0] = cmd;
Sebastian Schuberth3f784a42014-01-02 17:15:44 +0100653 handle_builtin(argc, argv);
654 die("cannot handle %s as a builtin", cmd);
Linus Torvalds231af832006-02-26 12:34:51 -0800655 }
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100656
Linus Torvalds231af832006-02-26 12:34:51 -0800657 /* Look for flags.. */
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200658 argv++;
659 argc--;
Matthias Lederhofer4394efe2007-06-08 22:57:55 +0200660 handle_options(&argv, &argc, NULL);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200661 if (argc > 0) {
Jeff King6d877802014-06-18 15:56:48 -0400662 /* translate --help and --version into commands */
663 skip_prefix(argv[0], "--", &argv[0]);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200664 } else {
Scott R Parish3d7e2d82007-10-27 01:36:49 -0700665 /* The user didn't specify a command; give them help */
Nguyễn Thái Ngọc Duy73e25e72010-06-26 14:26:37 -0500666 commit_pager_choice();
Scott R Parish3d7e2d82007-10-27 01:36:49 -0700667 printf("usage: %s\n\n", git_usage_string);
668 list_common_cmds_help();
Kevin Bracey421a9762013-03-10 17:10:20 +0200669 printf("\n%s\n", _(git_more_info_string));
Scott R Parish3d7e2d82007-10-27 01:36:49 -0700670 exit(1);
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100671 }
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200672 cmd = argv[0];
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100673
Linus Torvalds231af832006-02-26 12:34:51 -0800674 /*
Scott R Parish511707d2007-10-28 04:17:20 -0700675 * We use PATH to find git commands, but we prepend some higher
Mike Ralphson3ea3c212009-04-17 19:13:30 +0100676 * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH
Scott R Parish511707d2007-10-28 04:17:20 -0700677 * environment, and the $(gitexecdir) from the Makefile at build
678 * time.
Linus Torvalds231af832006-02-26 12:34:51 -0800679 */
Johannes Sixte1464ca2008-07-21 21:19:52 +0200680 setup_path();
Junio C Hamano7dbc2c02005-11-15 23:13:30 -0800681
Junio C Hamanoa0254632006-06-05 18:09:40 -0700682 while (1) {
Alexander Kuleshov8fa79752015-03-02 18:02:37 +0600683 int was_alias = run_argv(&argc, &argv);
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100684 if (errno != ENOENT)
Junio C Hamanoa0254632006-06-05 18:09:40 -0700685 break;
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100686 if (was_alias) {
Theodore Ts'of3dd0152007-02-10 19:33:57 -0500687 fprintf(stderr, "Expansion of alias '%s' failed; "
Pete Harlan7283bbc2010-02-15 15:33:18 -0800688 "'%s' is not a git command\n",
Theodore Ts'of3dd0152007-02-10 19:33:57 -0500689 cmd, argv[0]);
690 exit(1);
691 }
Adeodato Simóa907e1b2009-01-04 18:16:01 +0100692 if (!done_help) {
693 cmd = argv[0] = help_unknown_cmd(cmd);
694 done_help = 1;
695 } else
696 break;
Theodore Ts'of3dd0152007-02-10 19:33:57 -0500697 }
Alex Riesen10b15b82005-12-01 13:48:35 +0100698
699 fprintf(stderr, "Failed to run command '%s': %s\n",
Andreas Ericsson33ebb872006-06-28 02:17:21 -0700700 cmd, strerror(errno));
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100701
702 return 1;
703}