Merge commit 'e8760cde01299817daae26c9ad074b776bbd8f88'
[git/mingw.git] / git.c
blob304c9b0a43c7546155268b8d3804770c9d40c4a1
1 #include "builtin.h"
2 #include "exec_cmd.h"
3 #include "cache.h"
4 #include "quote.h"
6 const char git_usage_string[] =
7 "git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate] [--bare] [--git-dir=GIT_DIR] [--help] COMMAND [ARGS]";
9 static void prepend_to_path(const char *dir, int len)
11 const char *old_path = getenv("PATH");
12 char *path;
13 int path_len = len;
15 if (!old_path)
16 old_path = "/usr/local/bin:/usr/bin:/bin";
18 path_len = len + strlen(old_path) + 1;
20 path = xmalloc(path_len + 1);
22 memcpy(path, dir, len);
23 #ifdef __MINGW32__
24 path[len] = ';';
25 #else
26 path[len] = ':';
27 #endif
28 memcpy(path + len + 1, old_path, path_len - len);
30 setenv("PATH", path, 1);
32 free(path);
35 static int handle_options(const char*** argv, int* argc)
37 int handled = 0;
39 while (*argc > 0) {
40 const char *cmd = (*argv)[0];
41 if (cmd[0] != '-')
42 break;
45 * For legacy reasons, the "version" and "help"
46 * commands can be written with "--" prepended
47 * to make them look like flags.
49 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
50 break;
53 * Check remaining flags.
55 if (!prefixcmp(cmd, "--exec-path")) {
56 cmd += 11;
57 if (*cmd == '=')
58 git_set_exec_path(cmd + 1);
59 else {
60 puts(git_exec_path());
61 exit(0);
63 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
64 setup_pager();
65 } else if (!strcmp(cmd, "--git-dir")) {
66 if (*argc < 2) {
67 fprintf(stderr, "No directory given for --git-dir.\n" );
68 usage(git_usage_string);
70 setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1);
71 (*argv)++;
72 (*argc)--;
73 handled++;
74 } else if (!prefixcmp(cmd, "--git-dir=")) {
75 setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
76 } else if (!strcmp(cmd, "--bare")) {
77 static char git_dir[PATH_MAX+1];
78 setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 1);
79 } else {
80 fprintf(stderr, "Unknown option: %s\n", cmd);
81 usage(git_usage_string);
84 (*argv)++;
85 (*argc)--;
86 handled++;
88 return handled;
91 static const char *alias_command;
92 static char *alias_string;
94 static int git_alias_config(const char *var, const char *value)
96 if (!prefixcmp(var, "alias.") && !strcmp(var + 6, alias_command)) {
97 alias_string = xstrdup(value);
99 return 0;
102 static int split_cmdline(char *cmdline, const char ***argv)
104 int src, dst, count = 0, size = 16;
105 char quoted = 0;
107 *argv = xmalloc(sizeof(char*) * size);
109 /* split alias_string */
110 (*argv)[count++] = cmdline;
111 for (src = dst = 0; cmdline[src];) {
112 char c = cmdline[src];
113 if (!quoted && isspace(c)) {
114 cmdline[dst++] = 0;
115 while (cmdline[++src]
116 && isspace(cmdline[src]))
117 ; /* skip */
118 if (count >= size) {
119 size += 16;
120 *argv = xrealloc(*argv, sizeof(char*) * size);
122 (*argv)[count++] = cmdline + dst;
123 } else if(!quoted && (c == '\'' || c == '"')) {
124 quoted = c;
125 src++;
126 } else if (c == quoted) {
127 quoted = 0;
128 src++;
129 } else {
130 if (c == '\\' && quoted != '\'') {
131 src++;
132 c = cmdline[src];
133 if (!c) {
134 free(*argv);
135 *argv = NULL;
136 return error("cmdline ends with \\");
139 cmdline[dst++] = c;
140 src++;
144 cmdline[dst] = 0;
146 if (quoted) {
147 free(*argv);
148 *argv = NULL;
149 return error("unclosed quote");
152 return count;
155 static int handle_alias(int *argcp, const char ***argv)
157 int nongit = 0, ret = 0, saved_errno = errno;
158 const char *subdir;
159 int count, option_count;
160 const char** new_argv;
162 subdir = setup_git_directory_gently(&nongit);
164 alias_command = (*argv)[0];
165 git_config(git_alias_config);
166 if (alias_string) {
167 if (alias_string[0] == '!') {
168 trace_printf("trace: alias to shell cmd: %s => %s\n",
169 alias_command, alias_string + 1);
170 ret = system(alias_string + 1);
171 if (ret >= 0 && WIFEXITED(ret) &&
172 WEXITSTATUS(ret) != 127)
173 exit(WEXITSTATUS(ret));
174 die("Failed to run '%s' when expanding alias '%s'\n",
175 alias_string + 1, alias_command);
177 count = split_cmdline(alias_string, &new_argv);
178 option_count = handle_options(&new_argv, &count);
179 memmove(new_argv - option_count, new_argv,
180 count * sizeof(char *));
181 new_argv -= option_count;
183 if (count < 1)
184 die("empty alias for %s", alias_command);
186 if (!strcmp(alias_command, new_argv[0]))
187 die("recursive alias: %s", alias_command);
189 trace_argv_printf(new_argv, count,
190 "trace: alias expansion: %s =>",
191 alias_command);
193 new_argv = xrealloc(new_argv, sizeof(char*) *
194 (count + *argcp + 1));
195 /* insert after command name */
196 memcpy(new_argv + count, *argv + 1, sizeof(char*) * *argcp);
197 new_argv[count+*argcp] = NULL;
199 *argv = new_argv;
200 *argcp += count - 1;
202 ret = 1;
205 if (subdir)
206 chdir(subdir);
208 errno = saved_errno;
210 return ret;
213 const char git_version_string[] = GIT_VERSION;
215 #define RUN_SETUP (1<<0)
216 #define USE_PAGER (1<<1)
218 * require working tree to be present -- anything uses this needs
219 * RUN_SETUP for reading from the configuration file.
221 #define NOT_BARE (1<<2)
223 static void handle_internal_command(int argc, const char **argv, char **envp)
225 const char *cmd = argv[0];
226 static struct cmd_struct {
227 const char *cmd;
228 int (*fn)(int, const char **, const char *);
229 int option;
230 } commands[] = {
231 { "add", cmd_add, RUN_SETUP | NOT_BARE },
232 { "annotate", cmd_annotate, USE_PAGER },
233 { "apply", cmd_apply },
234 { "archive", cmd_archive },
235 { "blame", cmd_blame, RUN_SETUP },
236 { "branch", cmd_branch, RUN_SETUP },
237 { "bundle", cmd_bundle },
238 { "cat-file", cmd_cat_file, RUN_SETUP },
239 { "checkout-index", cmd_checkout_index, RUN_SETUP },
240 { "check-ref-format", cmd_check_ref_format },
241 { "cherry", cmd_cherry, RUN_SETUP },
242 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NOT_BARE },
243 { "commit-tree", cmd_commit_tree, RUN_SETUP },
244 { "config", cmd_config },
245 { "count-objects", cmd_count_objects, RUN_SETUP },
246 { "describe", cmd_describe, RUN_SETUP },
247 { "diff", cmd_diff, USE_PAGER },
248 { "diff-files", cmd_diff_files },
249 { "diff-index", cmd_diff_index, RUN_SETUP },
250 { "diff-tree", cmd_diff_tree, RUN_SETUP },
251 { "fetch--tool", cmd_fetch__tool, RUN_SETUP },
252 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
253 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
254 { "format-patch", cmd_format_patch, RUN_SETUP },
255 { "fsck", cmd_fsck, RUN_SETUP },
256 { "fsck-objects", cmd_fsck, RUN_SETUP },
257 { "gc", cmd_gc, RUN_SETUP },
258 { "get-tar-commit-id", cmd_get_tar_commit_id },
259 { "grep", cmd_grep, RUN_SETUP | USE_PAGER },
260 { "help", cmd_help },
261 { "init", cmd_init_db },
262 { "init-db", cmd_init_db },
263 { "log", cmd_log, RUN_SETUP | USE_PAGER },
264 { "ls-files", cmd_ls_files, RUN_SETUP },
265 { "ls-tree", cmd_ls_tree, RUN_SETUP },
266 { "mailinfo", cmd_mailinfo },
267 { "mailsplit", cmd_mailsplit },
268 { "merge-base", cmd_merge_base, RUN_SETUP },
269 { "merge-file", cmd_merge_file },
270 { "mv", cmd_mv, RUN_SETUP | NOT_BARE },
271 { "name-rev", cmd_name_rev, RUN_SETUP },
272 { "pack-objects", cmd_pack_objects, RUN_SETUP },
273 { "pickaxe", cmd_blame, RUN_SETUP | USE_PAGER },
274 { "prune", cmd_prune, RUN_SETUP },
275 { "prune-packed", cmd_prune_packed, RUN_SETUP },
276 { "push", cmd_push, RUN_SETUP },
277 { "read-tree", cmd_read_tree, RUN_SETUP },
278 { "reflog", cmd_reflog, RUN_SETUP },
279 { "repo-config", cmd_config },
280 { "rerere", cmd_rerere, RUN_SETUP },
281 { "rev-list", cmd_rev_list, RUN_SETUP },
282 { "rev-parse", cmd_rev_parse, RUN_SETUP },
283 { "revert", cmd_revert, RUN_SETUP | NOT_BARE },
284 { "rm", cmd_rm, RUN_SETUP | NOT_BARE },
285 { "runstatus", cmd_runstatus, RUN_SETUP | NOT_BARE },
286 { "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER },
287 { "show-branch", cmd_show_branch, RUN_SETUP },
288 { "show", cmd_show, RUN_SETUP | USE_PAGER },
289 { "stripspace", cmd_stripspace },
290 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
291 { "tar-tree", cmd_tar_tree },
292 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
293 { "update-index", cmd_update_index, RUN_SETUP },
294 { "update-ref", cmd_update_ref, RUN_SETUP },
295 { "upload-archive", cmd_upload_archive },
296 { "version", cmd_version },
297 { "whatchanged", cmd_whatchanged, RUN_SETUP | USE_PAGER },
298 { "write-tree", cmd_write_tree, RUN_SETUP },
299 { "verify-pack", cmd_verify_pack },
300 { "show-ref", cmd_show_ref, RUN_SETUP },
301 { "pack-refs", cmd_pack_refs, RUN_SETUP },
303 int i;
305 #ifdef STRIP_EXTENSION
306 i = strlen(argv[0]) - strlen(STRIP_EXTENSION);
307 if (i > 0 && !strcmp(argv[0] + i, STRIP_EXTENSION)) {
308 char *argv0 = strdup(argv[0]);
309 argv[0] = cmd = argv0;
310 argv0[i] = '\0';
312 #endif
314 /* Turn "git cmd --help" into "git help cmd" */
315 if (argc > 1 && !strcmp(argv[1], "--help")) {
316 argv[1] = argv[0];
317 argv[0] = cmd = "help";
320 for (i = 0; i < ARRAY_SIZE(commands); i++) {
321 struct cmd_struct *p = commands+i;
322 const char *prefix;
323 if (strcmp(p->cmd, cmd))
324 continue;
326 prefix = NULL;
327 if (p->option & RUN_SETUP)
328 prefix = setup_git_directory();
329 if (p->option & USE_PAGER)
330 setup_pager();
331 if ((p->option & NOT_BARE) &&
332 (is_bare_repository() || is_inside_git_dir()))
333 die("%s must be run in a work tree", cmd);
334 trace_argv_printf(argv, argc, "trace: built-in: git");
336 exit(p->fn(argc, argv, prefix));
340 int main(int argc, const char **argv, char **envp)
342 const char *cmd = argv[0] ? argv[0] : "git-help";
343 char *slash = strrchr(cmd, '/');
344 const char *exec_path = NULL;
345 int done_alias = 0;
348 * Take the basename of argv[0] as the command
349 * name, and the dirname as the default exec_path
350 * if it's an absolute path and we don't have
351 * anything better.
353 if (slash) {
354 *slash++ = 0;
355 if (*cmd == '/')
356 exec_path = cmd;
357 cmd = slash;
360 #ifdef __MINGW32__
361 slash = strrchr(cmd, '\\');
362 if (slash) {
363 *slash++ = 0;
364 if (cmd[1] == ':')
365 exec_path = cmd;
366 cmd = slash;
368 #endif
371 * "git-xxxx" is the same as "git xxxx", but we obviously:
373 * - cannot take flags in between the "git" and the "xxxx".
374 * - cannot execute it externally (since it would just do
375 * the same thing over again)
377 * So we just directly call the internal command handler, and
378 * die if that one cannot handle it.
380 if (!prefixcmp(cmd, "git-")) {
381 cmd += 4;
382 argv[0] = cmd;
383 handle_internal_command(argc, argv, envp);
384 die("cannot handle %s internally", cmd);
387 /* Look for flags.. */
388 argv++;
389 argc--;
390 handle_options(&argv, &argc);
391 if (argc > 0) {
392 if (!prefixcmp(argv[0], "--"))
393 argv[0] += 2;
394 } else {
395 /* Default command: "help" */
396 argv[0] = "help";
397 argc = 1;
399 cmd = argv[0];
402 * We search for git commands in the following order:
403 * - git_exec_path()
404 * - the path of the "git" command if we could find it
405 * in $0
406 * - the regular PATH.
408 if (exec_path)
409 prepend_to_path(exec_path, strlen(exec_path));
410 exec_path = git_exec_path();
411 prepend_to_path(exec_path, strlen(exec_path));
413 while (1) {
414 /* See if it's an internal command */
415 handle_internal_command(argc, argv, envp);
417 /* .. then try the external ones */
418 execv_git_cmd(argv);
420 /* It could be an alias -- this works around the insanity
421 * of overriding "git log" with "git show" by having
422 * alias.log = show
424 if (done_alias || !handle_alias(&argc, &argv))
425 break;
426 done_alias = 1;
429 if (errno == ENOENT) {
430 if (done_alias) {
431 fprintf(stderr, "Expansion of alias '%s' failed; "
432 "'%s' is not a git-command\n",
433 cmd, argv[0]);
434 exit(1);
436 help_unknown_cmd(cmd);
439 fprintf(stderr, "Failed to run command '%s': %s\n",
440 cmd, strerror(errno));
442 return 1;