blob: 29b55a16047837084fd9e2e8238137b8a2fe44ea [file] [log] [blame]
Junio C Hamano85023572006-12-19 14:34:12 -08001#include "builtin.h"
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05002#include "exec_cmd.h"
Johannes Schindelin2b11e312006-06-05 19:43:52 +02003#include "cache.h"
Matthias Lederhofer575ba9d2006-06-25 15:56:18 +02004#include "quote.h"
Andreas Ericsson8e49d502005-11-16 00:31:25 +01005
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +01006const char git_usage_string[] =
Alan Chandler281e67d2006-10-03 21:11:25 +01007 "git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate] [--bare] [--git-dir=GIT_DIR] [--help] COMMAND [ARGS]";
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +01008
Andreas Ericsson8e49d502005-11-16 00:31:25 +01009static void prepend_to_path(const char *dir, int len)
10{
Timo Hirvonen554fe202006-06-28 12:04:39 +030011 const char *old_path = getenv("PATH");
12 char *path;
Andreas Ericsson8e49d502005-11-16 00:31:25 +010013 int path_len = len;
14
15 if (!old_path)
Junio C Hamano7dbc2c02005-11-15 23:13:30 -080016 old_path = "/usr/local/bin:/usr/bin:/bin";
Andreas Ericsson8e49d502005-11-16 00:31:25 +010017
18 path_len = len + strlen(old_path) + 1;
19
Jonas Fonseca2d7320d2006-09-01 00:32:39 +020020 path = xmalloc(path_len + 1);
Andreas Ericsson8e49d502005-11-16 00:31:25 +010021
22 memcpy(path, dir, len);
23 path[len] = ':';
24 memcpy(path + len + 1, old_path, path_len - len);
25
26 setenv("PATH", path, 1);
Christian Couder0caea902006-09-05 08:22:16 +020027
28 free(path);
Andreas Ericsson8e49d502005-11-16 00:31:25 +010029}
30
Johannes Schindelin4ab243a2006-07-24 14:10:45 +020031static int handle_options(const char*** argv, int* argc)
32{
33 int handled = 0;
34
35 while (*argc > 0) {
36 const char *cmd = (*argv)[0];
37 if (cmd[0] != '-')
38 break;
39
Johannes Schindelin6acbcb92006-07-25 20:24:22 +020040 /*
41 * For legacy reasons, the "version" and "help"
42 * commands can be written with "--" prepended
43 * to make them look like flags.
44 */
45 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
46 break;
47
48 /*
49 * Check remaining flags.
50 */
Junio C Hamanocc44c762007-02-20 01:53:29 -080051 if (!prefixcmp(cmd, "--exec-path")) {
Johannes Schindelin6acbcb92006-07-25 20:24:22 +020052 cmd += 11;
53 if (*cmd == '=')
54 git_set_exec_path(cmd + 1);
55 else {
56 puts(git_exec_path());
57 exit(0);
58 }
59 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
Johannes Schindelin4ab243a2006-07-24 14:10:45 +020060 setup_pager();
Johannes Schindelin6acbcb92006-07-25 20:24:22 +020061 } else if (!strcmp(cmd, "--git-dir")) {
Brian Gernhardtc321f002006-12-22 08:56:25 -050062 if (*argc < 2) {
63 fprintf(stderr, "No directory given for --git-dir.\n" );
64 usage(git_usage_string);
65 }
Shawn O. Pearce45b09792006-12-30 23:29:11 -050066 setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +020067 (*argv)++;
68 (*argc)--;
Matthias Lederhofere4b02332007-04-12 20:52:03 +020069 handled++;
Junio C Hamanocc44c762007-02-20 01:53:29 -080070 } else if (!prefixcmp(cmd, "--git-dir=")) {
Shawn O. Pearce45b09792006-12-30 23:29:11 -050071 setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +020072 } else if (!strcmp(cmd, "--bare")) {
Shawn O. Pearceef5ddb22006-12-30 23:28:53 -050073 static char git_dir[PATH_MAX+1];
Shawn O. Pearce45b09792006-12-30 23:29:11 -050074 setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 1);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +020075 } else {
76 fprintf(stderr, "Unknown option: %s\n", cmd);
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +010077 usage(git_usage_string);
Johannes Schindelin6acbcb92006-07-25 20:24:22 +020078 }
Johannes Schindelin4ab243a2006-07-24 14:10:45 +020079
80 (*argv)++;
81 (*argc)--;
82 handled++;
83 }
84 return handled;
85}
86
Johannes Schindelin2b11e312006-06-05 19:43:52 +020087static const char *alias_command;
David Rientjes96f1e582006-08-15 10:23:48 -070088static char *alias_string;
Johannes Schindelin2b11e312006-06-05 19:43:52 +020089
90static int git_alias_config(const char *var, const char *value)
91{
Junio C Hamanocc44c762007-02-20 01:53:29 -080092 if (!prefixcmp(var, "alias.") && !strcmp(var + 6, alias_command)) {
Shawn Pearce9befac42006-09-02 00:16:31 -040093 alias_string = xstrdup(value);
Johannes Schindelin2b11e312006-06-05 19:43:52 +020094 }
95 return 0;
96}
97
98static int split_cmdline(char *cmdline, const char ***argv)
99{
100 int src, dst, count = 0, size = 16;
101 char quoted = 0;
102
James Bowes33015212007-03-25 20:39:36 -0400103 *argv = xmalloc(sizeof(char*) * size);
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200104
105 /* split alias_string */
106 (*argv)[count++] = cmdline;
107 for (src = dst = 0; cmdline[src];) {
108 char c = cmdline[src];
109 if (!quoted && isspace(c)) {
110 cmdline[dst++] = 0;
111 while (cmdline[++src]
112 && isspace(cmdline[src]))
113 ; /* skip */
114 if (count >= size) {
115 size += 16;
Jonas Fonseca83572c12006-08-26 16:16:18 +0200116 *argv = xrealloc(*argv, sizeof(char*) * size);
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200117 }
118 (*argv)[count++] = cmdline + dst;
119 } else if(!quoted && (c == '\'' || c == '"')) {
120 quoted = c;
121 src++;
122 } else if (c == quoted) {
123 quoted = 0;
124 src++;
125 } else {
126 if (c == '\\' && quoted != '\'') {
127 src++;
128 c = cmdline[src];
129 if (!c) {
130 free(*argv);
131 *argv = NULL;
132 return error("cmdline ends with \\");
133 }
134 }
135 cmdline[dst++] = c;
136 src++;
137 }
138 }
139
140 cmdline[dst] = 0;
141
142 if (quoted) {
143 free(*argv);
144 *argv = NULL;
145 return error("unclosed quote");
146 }
147
148 return count;
149}
150
151static int handle_alias(int *argcp, const char ***argv)
152{
Johannes Schindelin47e5c0c2006-06-28 12:45:27 +0200153 int nongit = 0, ret = 0, saved_errno = errno;
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200154 const char *subdir;
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200155 int count, option_count;
156 const char** new_argv;
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200157
158 subdir = setup_git_directory_gently(&nongit);
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200159
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200160 alias_command = (*argv)[0];
161 git_config(git_alias_config);
162 if (alias_string) {
Theodore Ts'odfd42a32007-02-10 19:33:58 -0500163 if (alias_string[0] == '!') {
164 trace_printf("trace: alias to shell cmd: %s => %s\n",
165 alias_command, alias_string + 1);
166 ret = system(alias_string + 1);
167 if (ret >= 0 && WIFEXITED(ret) &&
168 WEXITSTATUS(ret) != 127)
169 exit(WEXITSTATUS(ret));
170 die("Failed to run '%s' when expanding alias '%s'\n",
171 alias_string + 1, alias_command);
172 }
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200173 count = split_cmdline(alias_string, &new_argv);
174 option_count = handle_options(&new_argv, &count);
175 memmove(new_argv - option_count, new_argv,
176 count * sizeof(char *));
177 new_argv -= option_count;
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200178
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200179 if (count < 1)
180 die("empty alias for %s", alias_command);
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200181
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200182 if (!strcmp(alias_command, new_argv[0]))
183 die("recursive alias: %s", alias_command);
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200184
Christian Couder6ce4e612006-09-02 18:23:48 +0200185 trace_argv_printf(new_argv, count,
186 "trace: alias expansion: %s =>",
187 alias_command);
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200188
Jonas Fonseca83572c12006-08-26 16:16:18 +0200189 new_argv = xrealloc(new_argv, sizeof(char*) *
190 (count + *argcp + 1));
Matthias Lederhofer0347a8c2006-07-30 03:30:29 +0200191 /* insert after command name */
192 memcpy(new_argv + count, *argv + 1, sizeof(char*) * *argcp);
193 new_argv[count+*argcp] = NULL;
194
195 *argv = new_argv;
196 *argcp += count - 1;
197
198 ret = 1;
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200199 }
200
201 if (subdir)
202 chdir(subdir);
203
Johannes Schindelin47e5c0c2006-06-28 12:45:27 +0200204 errno = saved_errno;
205
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200206 return ret;
207}
208
Linus Torvalds70827b12006-04-21 10:27:34 -0700209const char git_version_string[] = GIT_VERSION;
Junio C Hamano5b84f4d2006-04-16 00:07:41 -0700210
Junio C Hamanoefffea02006-08-04 02:04:00 -0700211#define RUN_SETUP (1<<0)
212#define USE_PAGER (1<<1)
Shawn O. Pearce7eff28a2006-12-30 23:32:38 -0500213/*
214 * require working tree to be present -- anything uses this needs
215 * RUN_SETUP for reading from the configuration file.
216 */
217#define NOT_BARE (1<<2)
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700218
Junio C Hamano9201c702006-03-05 02:47:29 -0800219static void handle_internal_command(int argc, const char **argv, char **envp)
Linus Torvalds231af832006-02-26 12:34:51 -0800220{
221 const char *cmd = argv[0];
222 static struct cmd_struct {
223 const char *cmd;
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700224 int (*fn)(int, const char **, const char *);
Junio C Hamano9590b042006-07-31 02:53:46 -0700225 int option;
Linus Torvalds231af832006-02-26 12:34:51 -0800226 } commands[] = {
Shawn O. Pearce7eff28a2006-12-30 23:32:38 -0500227 { "add", cmd_add, RUN_SETUP | NOT_BARE },
Junio C Hamano5b6dedd2007-05-20 19:56:28 -0700228 { "annotate", cmd_annotate, RUN_SETUP | USE_PAGER },
Junio C Hamanof754fa92006-08-04 01:51:04 -0700229 { "apply", cmd_apply },
René Scharfe265d5282007-04-05 22:55:43 +0200230 { "archive", cmd_archive },
Ren,Ai(B Scharfe4f0219a2007-01-28 15:25:55 +0100231 { "blame", cmd_blame, RUN_SETUP },
Junio C Hamano1da1b3a2006-11-01 12:53:13 -0800232 { "branch", cmd_branch, RUN_SETUP },
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100233 { "bundle", cmd_bundle },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700234 { "cat-file", cmd_cat_file, RUN_SETUP },
235 { "checkout-index", cmd_checkout_index, RUN_SETUP },
Junio C Hamanof754fa92006-08-04 01:51:04 -0700236 { "check-ref-format", cmd_check_ref_format },
Junio C Hamanod0bfd022007-04-12 01:07:32 -0700237 { "check-attr", cmd_check_attr, RUN_SETUP | NOT_BARE },
Rene Scharfee8276332006-10-24 01:01:57 +0200238 { "cherry", cmd_cherry, RUN_SETUP },
Johannes Schindelin9509af62007-03-01 05:26:30 +0100239 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NOT_BARE },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700240 { "commit-tree", cmd_commit_tree, RUN_SETUP },
Tom Princee0d10e12007-01-28 16:16:53 -0800241 { "config", cmd_config },
Dmitry V. Levin81128942006-09-14 05:03:59 +0400242 { "count-objects", cmd_count_objects, RUN_SETUP },
Shawn O. Pearce9a0eaf82007-01-10 06:36:36 -0500243 { "describe", cmd_describe, RUN_SETUP },
Johannes Schindelind516c2d2007-02-22 21:50:10 +0100244 { "diff", cmd_diff, USE_PAGER },
245 { "diff-files", cmd_diff_files },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700246 { "diff-index", cmd_diff_index, RUN_SETUP },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700247 { "diff-tree", cmd_diff_tree, RUN_SETUP },
Junio C Hamanod4289ff2007-01-16 00:23:24 -0800248 { "fetch--tool", cmd_fetch__tool, RUN_SETUP },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700249 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700250 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700251 { "format-patch", cmd_format_patch, RUN_SETUP },
Mark Woodingb4dfefe2007-01-29 15:48:06 +0000252 { "fsck", cmd_fsck, RUN_SETUP },
253 { "fsck-objects", cmd_fsck, RUN_SETUP },
James Bowes6757ada2007-03-13 21:58:22 -0400254 { "gc", cmd_gc, RUN_SETUP },
Junio C Hamanof754fa92006-08-04 01:51:04 -0700255 { "get-tar-commit-id", cmd_get_tar_commit_id },
Johannes Schindelin34c6a822007-02-19 15:56:04 +0100256 { "grep", cmd_grep, RUN_SETUP | USE_PAGER },
Junio C Hamanof754fa92006-08-04 01:51:04 -0700257 { "help", cmd_help },
Nicolas Pitre515377e2007-01-07 12:31:29 -0500258 { "init", cmd_init_db },
Junio C Hamanof754fa92006-08-04 01:51:04 -0700259 { "init-db", cmd_init_db },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700260 { "log", cmd_log, RUN_SETUP | USE_PAGER },
261 { "ls-files", cmd_ls_files, RUN_SETUP },
262 { "ls-tree", cmd_ls_tree, RUN_SETUP },
Junio C Hamanof754fa92006-08-04 01:51:04 -0700263 { "mailinfo", cmd_mailinfo },
264 { "mailsplit", cmd_mailsplit },
Junio C Hamano71dfbf22007-01-09 00:50:02 -0800265 { "merge-base", cmd_merge_base, RUN_SETUP },
Johannes Schindelinba1f5f32006-12-06 16:26:06 +0100266 { "merge-file", cmd_merge_file },
Shawn O. Pearce7eff28a2006-12-30 23:32:38 -0500267 { "mv", cmd_mv, RUN_SETUP | NOT_BARE },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700268 { "name-rev", cmd_name_rev, RUN_SETUP },
269 { "pack-objects", cmd_pack_objects, RUN_SETUP },
Junio C Hamanoacca6872006-11-08 18:47:54 -0800270 { "pickaxe", cmd_blame, RUN_SETUP | USE_PAGER },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700271 { "prune", cmd_prune, RUN_SETUP },
272 { "prune-packed", cmd_prune_packed, RUN_SETUP },
Junio C Hamano102cb082006-08-08 15:42:20 -0700273 { "push", cmd_push, RUN_SETUP },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700274 { "read-tree", cmd_read_tree, RUN_SETUP },
Junio C Hamano4264dc12006-12-19 00:23:12 -0800275 { "reflog", cmd_reflog, RUN_SETUP },
Tom Princee0d10e12007-01-28 16:16:53 -0800276 { "repo-config", cmd_config },
Johannes Schindelin658f3652006-12-20 17:39:41 +0100277 { "rerere", cmd_rerere, RUN_SETUP },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700278 { "rev-list", cmd_rev_list, RUN_SETUP },
279 { "rev-parse", cmd_rev_parse, RUN_SETUP },
Johannes Schindelin9509af62007-03-01 05:26:30 +0100280 { "revert", cmd_revert, RUN_SETUP | NOT_BARE },
Shawn O. Pearce7eff28a2006-12-30 23:32:38 -0500281 { "rm", cmd_rm, RUN_SETUP | NOT_BARE },
282 { "runstatus", cmd_runstatus, RUN_SETUP | NOT_BARE },
Johannes Schindelin1d541c12006-11-28 00:29:21 +0100283 { "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700284 { "show-branch", cmd_show_branch, RUN_SETUP },
285 { "show", cmd_show, RUN_SETUP | USE_PAGER },
Junio C Hamanof754fa92006-08-04 01:51:04 -0700286 { "stripspace", cmd_stripspace },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700287 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
Rene Scharfe9cb90b82006-10-08 15:44:50 +0200288 { "tar-tree", cmd_tar_tree },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700289 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
290 { "update-index", cmd_update_index, RUN_SETUP },
291 { "update-ref", cmd_update_ref, RUN_SETUP },
Franck Bui-Huu39345a22006-09-07 15:12:05 +0200292 { "upload-archive", cmd_upload_archive },
Junio C Hamanof754fa92006-08-04 01:51:04 -0700293 { "version", cmd_version },
Junio C Hamanoefffea02006-08-04 02:04:00 -0700294 { "whatchanged", cmd_whatchanged, RUN_SETUP | USE_PAGER },
295 { "write-tree", cmd_write_tree, RUN_SETUP },
Rene Scharfe2e3ed672006-08-10 17:02:38 +0200296 { "verify-pack", cmd_verify_pack },
Linus Torvalds358ddb62006-09-15 11:19:32 -0700297 { "show-ref", cmd_show_ref, RUN_SETUP },
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700298 { "pack-refs", cmd_pack_refs, RUN_SETUP },
Linus Torvalds231af832006-02-26 12:34:51 -0800299 };
300 int i;
301
Linus Torvalds1cd95082006-04-15 11:13:49 -0700302 /* Turn "git cmd --help" into "git help cmd" */
303 if (argc > 1 && !strcmp(argv[1], "--help")) {
304 argv[1] = argv[0];
305 argv[0] = cmd = "help";
306 }
307
Linus Torvalds231af832006-02-26 12:34:51 -0800308 for (i = 0; i < ARRAY_SIZE(commands); i++) {
309 struct cmd_struct *p = commands+i;
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700310 const char *prefix;
Linus Torvalds231af832006-02-26 12:34:51 -0800311 if (strcmp(p->cmd, cmd))
312 continue;
Matthias Lederhofer575ba9d2006-06-25 15:56:18 +0200313
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700314 prefix = NULL;
Junio C Hamanoefffea02006-08-04 02:04:00 -0700315 if (p->option & RUN_SETUP)
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700316 prefix = setup_git_directory();
Junio C Hamano9590b042006-07-31 02:53:46 -0700317 if (p->option & USE_PAGER)
318 setup_pager();
Johannes Schindelin6d9ba672007-01-23 13:30:20 +0100319 if ((p->option & NOT_BARE) &&
320 (is_bare_repository() || is_inside_git_dir()))
321 die("%s must be run in a work tree", cmd);
Christian Couder6ce4e612006-09-02 18:23:48 +0200322 trace_argv_printf(argv, argc, "trace: built-in: git");
Matthias Lederhofer575ba9d2006-06-25 15:56:18 +0200323
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700324 exit(p->fn(argc, argv, prefix));
Linus Torvalds231af832006-02-26 12:34:51 -0800325 }
326}
327
Junio C Hamano9201c702006-03-05 02:47:29 -0800328int main(int argc, const char **argv, char **envp)
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100329{
Dmitry V. Levin5b6df8e2006-09-14 05:04:09 +0400330 const char *cmd = argv[0] ? argv[0] : "git-help";
Linus Torvalds231af832006-02-26 12:34:51 -0800331 char *slash = strrchr(cmd, '/');
Linus Torvalds231af832006-02-26 12:34:51 -0800332 const char *exec_path = NULL;
Junio C Hamanoa0254632006-06-05 18:09:40 -0700333 int done_alias = 0;
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100334
Linus Torvalds231af832006-02-26 12:34:51 -0800335 /*
336 * Take the basename of argv[0] as the command
337 * name, and the dirname as the default exec_path
338 * if it's an absolute path and we don't have
339 * anything better.
340 */
341 if (slash) {
342 *slash++ = 0;
343 if (*cmd == '/')
344 exec_path = cmd;
345 cmd = slash;
346 }
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100347
Linus Torvalds231af832006-02-26 12:34:51 -0800348 /*
349 * "git-xxxx" is the same as "git xxxx", but we obviously:
350 *
351 * - cannot take flags in between the "git" and the "xxxx".
352 * - cannot execute it externally (since it would just do
353 * the same thing over again)
354 *
355 * So we just directly call the internal command handler, and
356 * die if that one cannot handle it.
357 */
Junio C Hamanocc44c762007-02-20 01:53:29 -0800358 if (!prefixcmp(cmd, "git-")) {
Linus Torvalds231af832006-02-26 12:34:51 -0800359 cmd += 4;
360 argv[0] = cmd;
361 handle_internal_command(argc, argv, envp);
362 die("cannot handle %s internally", cmd);
363 }
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100364
Linus Torvalds231af832006-02-26 12:34:51 -0800365 /* Look for flags.. */
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200366 argv++;
367 argc--;
368 handle_options(&argv, &argc);
369 if (argc > 0) {
Junio C Hamanocc44c762007-02-20 01:53:29 -0800370 if (!prefixcmp(argv[0], "--"))
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200371 argv[0] += 2;
372 } else {
373 /* Default command: "help" */
374 argv[0] = "help";
375 argc = 1;
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100376 }
Johannes Schindelin6acbcb92006-07-25 20:24:22 +0200377 cmd = argv[0];
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100378
Linus Torvalds231af832006-02-26 12:34:51 -0800379 /*
380 * We search for git commands in the following order:
381 * - git_exec_path()
382 * - the path of the "git" command if we could find it
383 * in $0
384 * - the regular PATH.
385 */
386 if (exec_path)
387 prepend_to_path(exec_path, strlen(exec_path));
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500388 exec_path = git_exec_path();
389 prepend_to_path(exec_path, strlen(exec_path));
Junio C Hamano7dbc2c02005-11-15 23:13:30 -0800390
Junio C Hamanoa0254632006-06-05 18:09:40 -0700391 while (1) {
392 /* See if it's an internal command */
393 handle_internal_command(argc, argv, envp);
Johannes Schindelin2b11e312006-06-05 19:43:52 +0200394
Junio C Hamanoa0254632006-06-05 18:09:40 -0700395 /* .. then try the external ones */
396 execv_git_cmd(argv);
Linus Torvalds231af832006-02-26 12:34:51 -0800397
Junio C Hamanoa0254632006-06-05 18:09:40 -0700398 /* It could be an alias -- this works around the insanity
399 * of overriding "git log" with "git show" by having
400 * alias.log = show
401 */
402 if (done_alias || !handle_alias(&argc, &argv))
403 break;
404 done_alias = 1;
405 }
Alex Riesen10b15b82005-12-01 13:48:35 +0100406
Theodore Ts'of3dd0152007-02-10 19:33:57 -0500407 if (errno == ENOENT) {
408 if (done_alias) {
409 fprintf(stderr, "Expansion of alias '%s' failed; "
410 "'%s' is not a git-command\n",
411 cmd, argv[0]);
412 exit(1);
413 }
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +0100414 help_unknown_cmd(cmd);
Theodore Ts'of3dd0152007-02-10 19:33:57 -0500415 }
Alex Riesen10b15b82005-12-01 13:48:35 +0100416
417 fprintf(stderr, "Failed to run command '%s': %s\n",
Andreas Ericsson33ebb872006-06-28 02:17:21 -0700418 cmd, strerror(errno));
Andreas Ericsson8e49d502005-11-16 00:31:25 +0100419
420 return 1;
421}