Merge branch 'kf/askpass-config'
[git/git.git] / git.c
1 #include "builtin.h"
2 #include "exec_cmd.h"
3 #include "cache.h"
4 #include "quote.h"
5 #include "run-command.h"
6
7 const char git_usage_string[] =
8 "git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]\n"
9 " [-p|--paginate|--no-pager] [--no-replace-objects]\n"
10 " [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]\n"
11 " [-c name=value] [--help]\n"
12 " COMMAND [ARGS]";
13
14 const char git_more_info_string[] =
15 "See 'git help COMMAND' for more information on a specific command.";
16
17 static struct startup_info git_startup_info;
18 static int use_pager = -1;
19 struct pager_config {
20 const char *cmd;
21 int val;
22 };
23
24 static int pager_command_config(const char *var, const char *value, void *data)
25 {
26 struct pager_config *c = data;
27 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
28 c->val = git_config_bool(var, value);
29 return 0;
30 }
31
32 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
33 int check_pager_config(const char *cmd)
34 {
35 struct pager_config c;
36 c.cmd = cmd;
37 c.val = -1;
38 git_config(pager_command_config, &c);
39 return c.val;
40 }
41
42 static void commit_pager_choice(void) {
43 switch (use_pager) {
44 case 0:
45 setenv("GIT_PAGER", "cat", 1);
46 break;
47 case 1:
48 setup_pager();
49 break;
50 default:
51 break;
52 }
53 }
54
55 static int handle_options(const char ***argv, int *argc, int *envchanged)
56 {
57 int handled = 0;
58
59 while (*argc > 0) {
60 const char *cmd = (*argv)[0];
61 if (cmd[0] != '-')
62 break;
63
64 /*
65 * For legacy reasons, the "version" and "help"
66 * commands can be written with "--" prepended
67 * to make them look like flags.
68 */
69 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
70 break;
71
72 /*
73 * Check remaining flags.
74 */
75 if (!prefixcmp(cmd, "--exec-path")) {
76 cmd += 11;
77 if (*cmd == '=')
78 git_set_argv_exec_path(cmd + 1);
79 else {
80 puts(git_exec_path());
81 exit(0);
82 }
83 } else if (!strcmp(cmd, "--html-path")) {
84 puts(system_path(GIT_HTML_PATH));
85 exit(0);
86 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
87 use_pager = 1;
88 } else if (!strcmp(cmd, "--no-pager")) {
89 use_pager = 0;
90 if (envchanged)
91 *envchanged = 1;
92 } else if (!strcmp(cmd, "--no-replace-objects")) {
93 read_replace_refs = 0;
94 setenv(NO_REPLACE_OBJECTS_ENVIRONMENT, "1", 1);
95 if (envchanged)
96 *envchanged = 1;
97 } else if (!strcmp(cmd, "--git-dir")) {
98 if (*argc < 2) {
99 fprintf(stderr, "No directory given for --git-dir.\n" );
100 usage(git_usage_string);
101 }
102 setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1);
103 if (envchanged)
104 *envchanged = 1;
105 (*argv)++;
106 (*argc)--;
107 handled++;
108 } else if (!prefixcmp(cmd, "--git-dir=")) {
109 setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
110 if (envchanged)
111 *envchanged = 1;
112 } else if (!strcmp(cmd, "--work-tree")) {
113 if (*argc < 2) {
114 fprintf(stderr, "No directory given for --work-tree.\n" );
115 usage(git_usage_string);
116 }
117 setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
118 if (envchanged)
119 *envchanged = 1;
120 (*argv)++;
121 (*argc)--;
122 } else if (!prefixcmp(cmd, "--work-tree=")) {
123 setenv(GIT_WORK_TREE_ENVIRONMENT, cmd + 12, 1);
124 if (envchanged)
125 *envchanged = 1;
126 } else if (!strcmp(cmd, "--bare")) {
127 static char git_dir[PATH_MAX+1];
128 is_bare_repository_cfg = 1;
129 setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0);
130 if (envchanged)
131 *envchanged = 1;
132 } else if (!strcmp(cmd, "-c")) {
133 if (*argc < 2) {
134 fprintf(stderr, "-c expects a configuration string\n" );
135 usage(git_usage_string);
136 }
137 git_config_push_parameter((*argv)[1]);
138 (*argv)++;
139 (*argc)--;
140 } else {
141 fprintf(stderr, "Unknown option: %s\n", cmd);
142 usage(git_usage_string);
143 }
144
145 (*argv)++;
146 (*argc)--;
147 handled++;
148 }
149 return handled;
150 }
151
152 static int handle_alias(int *argcp, const char ***argv)
153 {
154 int envchanged = 0, ret = 0, saved_errno = errno;
155 const char *subdir;
156 int count, option_count;
157 const char **new_argv;
158 const char *alias_command;
159 char *alias_string;
160 int unused_nongit;
161
162 subdir = setup_git_directory_gently(&unused_nongit);
163
164 alias_command = (*argv)[0];
165 alias_string = alias_lookup(alias_command);
166 if (alias_string) {
167 if (alias_string[0] == '!') {
168 commit_pager_choice();
169 if (*argcp > 1) {
170 struct strbuf buf;
171
172 strbuf_init(&buf, PATH_MAX);
173 strbuf_addstr(&buf, alias_string);
174 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
175 free(alias_string);
176 alias_string = buf.buf;
177 }
178 trace_printf("trace: alias to shell cmd: %s => %s\n",
179 alias_command, alias_string + 1);
180 ret = system(alias_string + 1);
181 if (ret >= 0 && WIFEXITED(ret) &&
182 WEXITSTATUS(ret) != 127)
183 exit(WEXITSTATUS(ret));
184 die("Failed to run '%s' when expanding alias '%s'",
185 alias_string + 1, alias_command);
186 }
187 count = split_cmdline(alias_string, &new_argv);
188 if (count < 0)
189 die("Bad alias.%s string: %s", alias_command,
190 split_cmdline_strerror(count));
191 option_count = handle_options(&new_argv, &count, &envchanged);
192 if (envchanged)
193 die("alias '%s' changes environment variables\n"
194 "You can use '!git' in the alias to do this.",
195 alias_command);
196 memmove(new_argv - option_count, new_argv,
197 count * sizeof(char *));
198 new_argv -= option_count;
199
200 if (count < 1)
201 die("empty alias for %s", alias_command);
202
203 if (!strcmp(alias_command, new_argv[0]))
204 die("recursive alias: %s", alias_command);
205
206 trace_argv_printf(new_argv,
207 "trace: alias expansion: %s =>",
208 alias_command);
209
210 new_argv = xrealloc(new_argv, sizeof(char *) *
211 (count + *argcp));
212 /* insert after command name */
213 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
214
215 *argv = new_argv;
216 *argcp += count - 1;
217
218 ret = 1;
219 }
220
221 if (subdir && chdir(subdir))
222 die_errno("Cannot change to '%s'", subdir);
223
224 errno = saved_errno;
225
226 return ret;
227 }
228
229 const char git_version_string[] = GIT_VERSION;
230
231 #define RUN_SETUP (1<<0)
232 #define RUN_SETUP_GENTLY (1<<1)
233 #define USE_PAGER (1<<2)
234 /*
235 * require working tree to be present -- anything uses this needs
236 * RUN_SETUP for reading from the configuration file.
237 */
238 #define NEED_WORK_TREE (1<<3)
239
240 struct cmd_struct {
241 const char *cmd;
242 int (*fn)(int, const char **, const char *);
243 int option;
244 };
245
246 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
247 {
248 int status, help;
249 struct stat st;
250 const char *prefix;
251
252 prefix = NULL;
253 help = argc == 2 && !strcmp(argv[1], "-h");
254 if (!help) {
255 if (p->option & RUN_SETUP)
256 prefix = setup_git_directory();
257 if (p->option & RUN_SETUP_GENTLY) {
258 int nongit_ok;
259 prefix = setup_git_directory_gently(&nongit_ok);
260 }
261
262 if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY))
263 use_pager = check_pager_config(p->cmd);
264 if (use_pager == -1 && p->option & USE_PAGER)
265 use_pager = 1;
266 }
267 commit_pager_choice();
268
269 if (!help && p->option & NEED_WORK_TREE)
270 setup_work_tree();
271
272 trace_argv_printf(argv, "trace: built-in: git");
273
274 status = p->fn(argc, argv, prefix);
275 if (status)
276 return status;
277
278 /* Somebody closed stdout? */
279 if (fstat(fileno(stdout), &st))
280 return 0;
281 /* Ignore write errors for pipes and sockets.. */
282 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
283 return 0;
284
285 /* Check for ENOSPC and EIO errors.. */
286 if (fflush(stdout))
287 die_errno("write failure on standard output");
288 if (ferror(stdout))
289 die("unknown write failure on standard output");
290 if (fclose(stdout))
291 die_errno("close failed on standard output");
292 return 0;
293 }
294
295 static void handle_internal_command(int argc, const char **argv)
296 {
297 const char *cmd = argv[0];
298 static struct cmd_struct commands[] = {
299 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
300 { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
301 { "annotate", cmd_annotate, RUN_SETUP },
302 { "apply", cmd_apply, RUN_SETUP_GENTLY },
303 { "archive", cmd_archive },
304 { "bisect--helper", cmd_bisect__helper, RUN_SETUP | NEED_WORK_TREE },
305 { "blame", cmd_blame, RUN_SETUP },
306 { "branch", cmd_branch, RUN_SETUP },
307 { "bundle", cmd_bundle, RUN_SETUP_GENTLY },
308 { "cat-file", cmd_cat_file, RUN_SETUP },
309 { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
310 { "checkout-index", cmd_checkout_index,
311 RUN_SETUP | NEED_WORK_TREE},
312 { "check-ref-format", cmd_check_ref_format },
313 { "check-attr", cmd_check_attr, RUN_SETUP },
314 { "cherry", cmd_cherry, RUN_SETUP },
315 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
316 { "clone", cmd_clone },
317 { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
318 { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
319 { "commit-tree", cmd_commit_tree, RUN_SETUP },
320 { "config", cmd_config, RUN_SETUP_GENTLY },
321 { "count-objects", cmd_count_objects, RUN_SETUP },
322 { "describe", cmd_describe, RUN_SETUP },
323 { "diff", cmd_diff },
324 { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
325 { "diff-index", cmd_diff_index, RUN_SETUP },
326 { "diff-tree", cmd_diff_tree, RUN_SETUP },
327 { "fast-export", cmd_fast_export, RUN_SETUP },
328 { "fetch", cmd_fetch, RUN_SETUP },
329 { "fetch-pack", cmd_fetch_pack, RUN_SETUP },
330 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
331 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
332 { "format-patch", cmd_format_patch, RUN_SETUP },
333 { "fsck", cmd_fsck, RUN_SETUP },
334 { "fsck-objects", cmd_fsck, RUN_SETUP },
335 { "gc", cmd_gc, RUN_SETUP },
336 { "get-tar-commit-id", cmd_get_tar_commit_id },
337 { "grep", cmd_grep, RUN_SETUP_GENTLY },
338 { "hash-object", cmd_hash_object },
339 { "help", cmd_help },
340 { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
341 { "init", cmd_init_db },
342 { "init-db", cmd_init_db },
343 { "log", cmd_log, RUN_SETUP },
344 { "ls-files", cmd_ls_files, RUN_SETUP },
345 { "ls-tree", cmd_ls_tree, RUN_SETUP },
346 { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
347 { "mailinfo", cmd_mailinfo },
348 { "mailsplit", cmd_mailsplit },
349 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
350 { "merge-base", cmd_merge_base, RUN_SETUP },
351 { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
352 { "merge-index", cmd_merge_index, RUN_SETUP },
353 { "merge-ours", cmd_merge_ours, RUN_SETUP },
354 { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
355 { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
356 { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
357 { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
358 { "merge-tree", cmd_merge_tree, RUN_SETUP },
359 { "mktag", cmd_mktag, RUN_SETUP },
360 { "mktree", cmd_mktree, RUN_SETUP },
361 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
362 { "name-rev", cmd_name_rev, RUN_SETUP },
363 { "notes", cmd_notes, RUN_SETUP },
364 { "pack-objects", cmd_pack_objects, RUN_SETUP },
365 { "pack-redundant", cmd_pack_redundant, RUN_SETUP },
366 { "patch-id", cmd_patch_id },
367 { "peek-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
368 { "pickaxe", cmd_blame, RUN_SETUP },
369 { "prune", cmd_prune, RUN_SETUP },
370 { "prune-packed", cmd_prune_packed, RUN_SETUP },
371 { "push", cmd_push, RUN_SETUP },
372 { "read-tree", cmd_read_tree, RUN_SETUP },
373 { "receive-pack", cmd_receive_pack },
374 { "reflog", cmd_reflog, RUN_SETUP },
375 { "remote", cmd_remote, RUN_SETUP },
376 { "replace", cmd_replace, RUN_SETUP },
377 { "repo-config", cmd_config, RUN_SETUP_GENTLY },
378 { "rerere", cmd_rerere, RUN_SETUP },
379 { "reset", cmd_reset, RUN_SETUP },
380 { "rev-list", cmd_rev_list, RUN_SETUP },
381 { "rev-parse", cmd_rev_parse },
382 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
383 { "rm", cmd_rm, RUN_SETUP },
384 { "send-pack", cmd_send_pack, RUN_SETUP },
385 { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
386 { "show-branch", cmd_show_branch, RUN_SETUP },
387 { "show", cmd_show, RUN_SETUP },
388 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
389 { "stripspace", cmd_stripspace },
390 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
391 { "tag", cmd_tag, RUN_SETUP },
392 { "tar-tree", cmd_tar_tree },
393 { "unpack-file", cmd_unpack_file, RUN_SETUP },
394 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
395 { "update-index", cmd_update_index, RUN_SETUP },
396 { "update-ref", cmd_update_ref, RUN_SETUP },
397 { "update-server-info", cmd_update_server_info, RUN_SETUP },
398 { "upload-archive", cmd_upload_archive },
399 { "var", cmd_var, RUN_SETUP_GENTLY },
400 { "verify-tag", cmd_verify_tag, RUN_SETUP },
401 { "version", cmd_version },
402 { "whatchanged", cmd_whatchanged, RUN_SETUP },
403 { "write-tree", cmd_write_tree, RUN_SETUP },
404 { "verify-pack", cmd_verify_pack },
405 { "show-ref", cmd_show_ref, RUN_SETUP },
406 { "pack-refs", cmd_pack_refs, RUN_SETUP },
407 };
408 int i;
409 static const char ext[] = STRIP_EXTENSION;
410
411 if (sizeof(ext) > 1) {
412 i = strlen(argv[0]) - strlen(ext);
413 if (i > 0 && !strcmp(argv[0] + i, ext)) {
414 char *argv0 = xstrdup(argv[0]);
415 argv[0] = cmd = argv0;
416 argv0[i] = '\0';
417 }
418 }
419
420 /* Turn "git cmd --help" into "git help cmd" */
421 if (argc > 1 && !strcmp(argv[1], "--help")) {
422 argv[1] = argv[0];
423 argv[0] = cmd = "help";
424 }
425
426 for (i = 0; i < ARRAY_SIZE(commands); i++) {
427 struct cmd_struct *p = commands+i;
428 if (strcmp(p->cmd, cmd))
429 continue;
430 exit(run_builtin(p, argc, argv));
431 }
432 }
433
434 static void execv_dashed_external(const char **argv)
435 {
436 struct strbuf cmd = STRBUF_INIT;
437 const char *tmp;
438 int status;
439
440 commit_pager_choice();
441
442 strbuf_addf(&cmd, "git-%s", argv[0]);
443
444 /*
445 * argv[0] must be the git command, but the argv array
446 * belongs to the caller, and may be reused in
447 * subsequent loop iterations. Save argv[0] and
448 * restore it on error.
449 */
450 tmp = argv[0];
451 argv[0] = cmd.buf;
452
453 trace_argv_printf(argv, "trace: exec:");
454
455 /*
456 * if we fail because the command is not found, it is
457 * OK to return. Otherwise, we just pass along the status code.
458 */
459 status = run_command_v_opt(argv, RUN_SILENT_EXEC_FAILURE);
460 if (status >= 0 || errno != ENOENT)
461 exit(status);
462
463 argv[0] = tmp;
464
465 strbuf_release(&cmd);
466 }
467
468 static int run_argv(int *argcp, const char ***argv)
469 {
470 int done_alias = 0;
471
472 while (1) {
473 /* See if it's an internal command */
474 handle_internal_command(*argcp, *argv);
475
476 /* .. then try the external ones */
477 execv_dashed_external(*argv);
478
479 /* It could be an alias -- this works around the insanity
480 * of overriding "git log" with "git show" by having
481 * alias.log = show
482 */
483 if (done_alias || !handle_alias(argcp, argv))
484 break;
485 done_alias = 1;
486 }
487
488 return done_alias;
489 }
490
491
492 int main(int argc, const char **argv)
493 {
494 const char *cmd;
495
496 startup_info = &git_startup_info;
497
498 cmd = git_extract_argv0_path(argv[0]);
499 if (!cmd)
500 cmd = "git-help";
501
502 /*
503 * "git-xxxx" is the same as "git xxxx", but we obviously:
504 *
505 * - cannot take flags in between the "git" and the "xxxx".
506 * - cannot execute it externally (since it would just do
507 * the same thing over again)
508 *
509 * So we just directly call the internal command handler, and
510 * die if that one cannot handle it.
511 */
512 if (!prefixcmp(cmd, "git-")) {
513 cmd += 4;
514 argv[0] = cmd;
515 handle_internal_command(argc, argv);
516 die("cannot handle %s internally", cmd);
517 }
518
519 /* Look for flags.. */
520 argv++;
521 argc--;
522 handle_options(&argv, &argc, NULL);
523 if (argc > 0) {
524 if (!prefixcmp(argv[0], "--"))
525 argv[0] += 2;
526 } else {
527 /* The user didn't specify a command; give them help */
528 commit_pager_choice();
529 printf("usage: %s\n\n", git_usage_string);
530 list_common_cmds_help();
531 printf("\n%s\n", git_more_info_string);
532 exit(1);
533 }
534 cmd = argv[0];
535
536 /*
537 * We use PATH to find git commands, but we prepend some higher
538 * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH
539 * environment, and the $(gitexecdir) from the Makefile at build
540 * time.
541 */
542 setup_path();
543
544 while (1) {
545 static int done_help = 0;
546 static int was_alias = 0;
547 was_alias = run_argv(&argc, &argv);
548 if (errno != ENOENT)
549 break;
550 if (was_alias) {
551 fprintf(stderr, "Expansion of alias '%s' failed; "
552 "'%s' is not a git command\n",
553 cmd, argv[0]);
554 exit(1);
555 }
556 if (!done_help) {
557 cmd = argv[0] = help_unknown_cmd(cmd);
558 done_help = 1;
559 } else
560 break;
561 }
562
563 fprintf(stderr, "Failed to run command '%s': %s\n",
564 cmd, strerror(errno));
565
566 return 1;
567 }