Introduce git_etc_gitconfig() that encapsulates access of ETC_GITCONFIG.
[git/mingw.git] / git.c
blob690bf37f389d1e525923e360eb93b02b2a2880bd
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, RUN_SETUP | 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 { "check-attr", cmd_check_attr, RUN_SETUP | NOT_BARE },
242 { "cherry", cmd_cherry, RUN_SETUP },
243 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NOT_BARE },
244 { "commit-tree", cmd_commit_tree, RUN_SETUP },
245 { "config", cmd_config },
246 { "count-objects", cmd_count_objects, RUN_SETUP },
247 { "describe", cmd_describe, RUN_SETUP },
248 { "diff", cmd_diff, USE_PAGER },
249 { "diff-files", cmd_diff_files },
250 { "diff-index", cmd_diff_index, RUN_SETUP },
251 { "diff-tree", cmd_diff_tree, RUN_SETUP },
252 { "fetch--tool", cmd_fetch__tool, RUN_SETUP },
253 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
254 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
255 { "format-patch", cmd_format_patch, RUN_SETUP },
256 { "fsck", cmd_fsck, RUN_SETUP },
257 { "fsck-objects", cmd_fsck, RUN_SETUP },
258 { "gc", cmd_gc, RUN_SETUP },
259 { "get-tar-commit-id", cmd_get_tar_commit_id },
260 { "grep", cmd_grep, RUN_SETUP | USE_PAGER },
261 { "help", cmd_help },
262 { "init", cmd_init_db },
263 { "init-db", cmd_init_db },
264 { "log", cmd_log, RUN_SETUP | USE_PAGER },
265 { "ls-files", cmd_ls_files, RUN_SETUP },
266 { "ls-tree", cmd_ls_tree, RUN_SETUP },
267 { "mailinfo", cmd_mailinfo },
268 { "mailsplit", cmd_mailsplit },
269 { "merge-base", cmd_merge_base, RUN_SETUP },
270 { "merge-file", cmd_merge_file },
271 { "mv", cmd_mv, RUN_SETUP | NOT_BARE },
272 { "name-rev", cmd_name_rev, RUN_SETUP },
273 { "pack-objects", cmd_pack_objects, RUN_SETUP },
274 { "pickaxe", cmd_blame, RUN_SETUP | USE_PAGER },
275 { "prune", cmd_prune, RUN_SETUP },
276 { "prune-packed", cmd_prune_packed, RUN_SETUP },
277 { "push", cmd_push, RUN_SETUP },
278 { "read-tree", cmd_read_tree, RUN_SETUP },
279 { "reflog", cmd_reflog, RUN_SETUP },
280 { "repo-config", cmd_config },
281 { "rerere", cmd_rerere, RUN_SETUP },
282 { "rev-list", cmd_rev_list, RUN_SETUP },
283 { "rev-parse", cmd_rev_parse, RUN_SETUP },
284 { "revert", cmd_revert, RUN_SETUP | NOT_BARE },
285 { "rm", cmd_rm, RUN_SETUP | NOT_BARE },
286 { "runstatus", cmd_runstatus, RUN_SETUP | NOT_BARE },
287 { "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER },
288 { "show-branch", cmd_show_branch, RUN_SETUP },
289 { "show", cmd_show, RUN_SETUP | USE_PAGER },
290 { "stripspace", cmd_stripspace },
291 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
292 { "tar-tree", cmd_tar_tree },
293 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
294 { "update-index", cmd_update_index, RUN_SETUP },
295 { "update-ref", cmd_update_ref, RUN_SETUP },
296 { "upload-archive", cmd_upload_archive },
297 { "version", cmd_version },
298 { "whatchanged", cmd_whatchanged, RUN_SETUP | USE_PAGER },
299 { "write-tree", cmd_write_tree, RUN_SETUP },
300 { "verify-pack", cmd_verify_pack },
301 { "show-ref", cmd_show_ref, RUN_SETUP },
302 { "pack-refs", cmd_pack_refs, RUN_SETUP },
304 int i;
306 #ifdef STRIP_EXTENSION
307 i = strlen(argv[0]) - strlen(STRIP_EXTENSION);
308 if (i > 0 && !strcmp(argv[0] + i, STRIP_EXTENSION)) {
309 char *argv0 = strdup(argv[0]);
310 argv[0] = cmd = argv0;
311 argv0[i] = '\0';
313 #endif
315 /* Turn "git cmd --help" into "git help cmd" */
316 if (argc > 1 && !strcmp(argv[1], "--help")) {
317 argv[1] = argv[0];
318 argv[0] = cmd = "help";
321 for (i = 0; i < ARRAY_SIZE(commands); i++) {
322 struct cmd_struct *p = commands+i;
323 const char *prefix;
324 if (strcmp(p->cmd, cmd))
325 continue;
327 prefix = NULL;
328 if (p->option & RUN_SETUP)
329 prefix = setup_git_directory();
330 if (p->option & USE_PAGER)
331 setup_pager();
332 if ((p->option & NOT_BARE) &&
333 (is_bare_repository() || is_inside_git_dir()))
334 die("%s must be run in a work tree", cmd);
335 trace_argv_printf(argv, argc, "trace: built-in: git");
337 exit(p->fn(argc, argv, prefix));
341 int main(int argc, const char **argv, char **envp)
343 const char *cmd = argv[0] ? argv[0] : "git-help";
344 char *slash = strrchr(cmd, '/');
345 const char *exec_path = NULL;
346 int done_alias = 0;
349 * Take the basename of argv[0] as the command
350 * name, and the dirname as the default exec_path
351 * if it's an absolute path and we don't have
352 * anything better.
354 if (slash) {
355 *slash++ = 0;
356 if (*cmd == '/')
357 exec_path = cmd;
358 cmd = slash;
361 #ifdef __MINGW32__
362 slash = strrchr(cmd, '\\');
363 if (slash) {
364 *slash++ = 0;
365 if (cmd[1] == ':')
366 exec_path = cmd;
367 cmd = slash;
369 #endif
372 * "git-xxxx" is the same as "git xxxx", but we obviously:
374 * - cannot take flags in between the "git" and the "xxxx".
375 * - cannot execute it externally (since it would just do
376 * the same thing over again)
378 * So we just directly call the internal command handler, and
379 * die if that one cannot handle it.
381 if (!prefixcmp(cmd, "git-")) {
382 cmd += 4;
383 argv[0] = cmd;
384 handle_internal_command(argc, argv, envp);
385 die("cannot handle %s internally", cmd);
388 /* Look for flags.. */
389 argv++;
390 argc--;
391 handle_options(&argv, &argc);
392 if (argc > 0) {
393 if (!prefixcmp(argv[0], "--"))
394 argv[0] += 2;
395 } else {
396 /* Default command: "help" */
397 argv[0] = "help";
398 argc = 1;
400 cmd = argv[0];
403 * We search for git commands in the following order:
404 * - git_exec_path()
405 * - the path of the "git" command if we could find it
406 * in $0
407 * - the regular PATH.
409 if (exec_path)
410 prepend_to_path(exec_path, strlen(exec_path));
411 exec_path = git_exec_path();
412 prepend_to_path(exec_path, strlen(exec_path));
414 while (1) {
415 /* See if it's an internal command */
416 handle_internal_command(argc, argv, envp);
418 /* .. then try the external ones */
419 execv_git_cmd(argv);
421 /* It could be an alias -- this works around the insanity
422 * of overriding "git log" with "git show" by having
423 * alias.log = show
425 if (done_alias || !handle_alias(&argc, &argv))
426 break;
427 done_alias = 1;
430 if (errno == ENOENT) {
431 if (done_alias) {
432 fprintf(stderr, "Expansion of alias '%s' failed; "
433 "'%s' is not a git-command\n",
434 cmd, argv[0]);
435 exit(1);
437 help_unknown_cmd(cmd);
440 fprintf(stderr, "Failed to run command '%s': %s\n",
441 cmd, strerror(errno));
443 return 1;