Merge branch 'ei/worktree+filter'
[git/jrn.git] / git.c
blobb6bf5ad5ec97699f0a1f4d6c6616c7c9a240d144
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] [--work-tree=GIT_WORK_TREE] [--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 path[len] = ':';
24 memcpy(path + len + 1, old_path, path_len - len);
26 setenv("PATH", path, 1);
28 free(path);
31 static int handle_options(const char*** argv, int* argc)
33 int handled = 0;
35 while (*argc > 0) {
36 const char *cmd = (*argv)[0];
37 if (cmd[0] != '-')
38 break;
41 * For legacy reasons, the "version" and "help"
42 * commands can be written with "--" prepended
43 * to make them look like flags.
45 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
46 break;
49 * Check remaining flags.
51 if (!prefixcmp(cmd, "--exec-path")) {
52 cmd += 11;
53 if (*cmd == '=')
54 git_set_exec_path(cmd + 1);
55 else {
56 puts(git_exec_path());
57 exit(0);
59 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
60 setup_pager();
61 } else if (!strcmp(cmd, "--git-dir")) {
62 if (*argc < 2) {
63 fprintf(stderr, "No directory given for --git-dir.\n" );
64 usage(git_usage_string);
66 setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1);
67 (*argv)++;
68 (*argc)--;
69 handled++;
70 } else if (!prefixcmp(cmd, "--git-dir=")) {
71 setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
72 } else if (!strcmp(cmd, "--work-tree")) {
73 if (*argc < 2) {
74 fprintf(stderr, "No directory given for --work-tree.\n" );
75 usage(git_usage_string);
77 setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
78 (*argv)++;
79 (*argc)--;
80 } else if (!prefixcmp(cmd, "--work-tree=")) {
81 setenv(GIT_WORK_TREE_ENVIRONMENT, cmd + 12, 1);
82 } else if (!strcmp(cmd, "--bare")) {
83 static char git_dir[PATH_MAX+1];
84 setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 1);
85 } else {
86 fprintf(stderr, "Unknown option: %s\n", cmd);
87 usage(git_usage_string);
90 (*argv)++;
91 (*argc)--;
92 handled++;
94 return handled;
97 static const char *alias_command;
98 static char *alias_string;
100 static int git_alias_config(const char *var, const char *value)
102 if (!prefixcmp(var, "alias.") && !strcmp(var + 6, alias_command)) {
103 alias_string = xstrdup(value);
105 return 0;
108 static int split_cmdline(char *cmdline, const char ***argv)
110 int src, dst, count = 0, size = 16;
111 char quoted = 0;
113 *argv = xmalloc(sizeof(char*) * size);
115 /* split alias_string */
116 (*argv)[count++] = cmdline;
117 for (src = dst = 0; cmdline[src];) {
118 char c = cmdline[src];
119 if (!quoted && isspace(c)) {
120 cmdline[dst++] = 0;
121 while (cmdline[++src]
122 && isspace(cmdline[src]))
123 ; /* skip */
124 if (count >= size) {
125 size += 16;
126 *argv = xrealloc(*argv, sizeof(char*) * size);
128 (*argv)[count++] = cmdline + dst;
129 } else if(!quoted && (c == '\'' || c == '"')) {
130 quoted = c;
131 src++;
132 } else if (c == quoted) {
133 quoted = 0;
134 src++;
135 } else {
136 if (c == '\\' && quoted != '\'') {
137 src++;
138 c = cmdline[src];
139 if (!c) {
140 free(*argv);
141 *argv = NULL;
142 return error("cmdline ends with \\");
145 cmdline[dst++] = c;
146 src++;
150 cmdline[dst] = 0;
152 if (quoted) {
153 free(*argv);
154 *argv = NULL;
155 return error("unclosed quote");
158 return count;
161 static int handle_alias(int *argcp, const char ***argv)
163 int nongit = 0, ret = 0, saved_errno = errno;
164 const char *subdir;
165 int count, option_count;
166 const char** new_argv;
168 subdir = setup_git_directory_gently(&nongit);
170 alias_command = (*argv)[0];
171 git_config(git_alias_config);
172 if (alias_string) {
173 if (alias_string[0] == '!') {
174 trace_printf("trace: alias to shell cmd: %s => %s\n",
175 alias_command, alias_string + 1);
176 ret = system(alias_string + 1);
177 if (ret >= 0 && WIFEXITED(ret) &&
178 WEXITSTATUS(ret) != 127)
179 exit(WEXITSTATUS(ret));
180 die("Failed to run '%s' when expanding alias '%s'\n",
181 alias_string + 1, alias_command);
183 count = split_cmdline(alias_string, &new_argv);
184 option_count = handle_options(&new_argv, &count);
185 memmove(new_argv - option_count, new_argv,
186 count * sizeof(char *));
187 new_argv -= option_count;
189 if (count < 1)
190 die("empty alias for %s", alias_command);
192 if (!strcmp(alias_command, new_argv[0]))
193 die("recursive alias: %s", alias_command);
195 trace_argv_printf(new_argv, count,
196 "trace: alias expansion: %s =>",
197 alias_command);
199 new_argv = xrealloc(new_argv, sizeof(char*) *
200 (count + *argcp + 1));
201 /* insert after command name */
202 memcpy(new_argv + count, *argv + 1, sizeof(char*) * *argcp);
203 new_argv[count+*argcp] = NULL;
205 *argv = new_argv;
206 *argcp += count - 1;
208 ret = 1;
211 if (subdir)
212 chdir(subdir);
214 errno = saved_errno;
216 return ret;
219 const char git_version_string[] = GIT_VERSION;
221 #define RUN_SETUP (1<<0)
222 #define USE_PAGER (1<<1)
224 * require working tree to be present -- anything uses this needs
225 * RUN_SETUP for reading from the configuration file.
227 #define NEED_WORK_TREE (1<<2)
229 struct cmd_struct {
230 const char *cmd;
231 int (*fn)(int, const char **, const char *);
232 int option;
235 static int run_command(struct cmd_struct *p, int argc, const char **argv)
237 int status;
238 struct stat st;
239 const char *prefix;
241 prefix = NULL;
242 if (p->option & RUN_SETUP)
243 prefix = setup_git_directory();
244 if (p->option & USE_PAGER)
245 setup_pager();
246 if ((p->option & NEED_WORK_TREE) &&
247 (!is_inside_work_tree() || is_inside_git_dir()))
248 die("%s must be run in a work tree", p->cmd);
249 trace_argv_printf(argv, argc, "trace: built-in: git");
251 status = p->fn(argc, argv, prefix);
252 if (status)
253 return status;
255 /* Somebody closed stdout? */
256 if (fstat(fileno(stdout), &st))
257 return 0;
258 /* Ignore write errors for pipes and sockets.. */
259 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
260 return 0;
262 /* Check for ENOSPC and EIO errors.. */
263 if (fflush(stdout))
264 die("write failure on standard output: %s", strerror(errno));
265 if (ferror(stdout))
266 die("unknown write failure on standard output");
267 if (fclose(stdout))
268 die("close failed on standard output: %s", strerror(errno));
269 return 0;
272 static void handle_internal_command(int argc, const char **argv)
274 const char *cmd = argv[0];
275 static struct cmd_struct commands[] = {
276 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
277 { "annotate", cmd_annotate, RUN_SETUP | USE_PAGER },
278 { "apply", cmd_apply },
279 { "archive", cmd_archive },
280 { "blame", cmd_blame, RUN_SETUP },
281 { "branch", cmd_branch, RUN_SETUP },
282 { "bundle", cmd_bundle },
283 { "cat-file", cmd_cat_file, RUN_SETUP },
284 { "checkout-index", cmd_checkout_index, RUN_SETUP },
285 { "check-ref-format", cmd_check_ref_format },
286 { "check-attr", cmd_check_attr, RUN_SETUP | NEED_WORK_TREE },
287 { "cherry", cmd_cherry, RUN_SETUP },
288 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
289 { "commit-tree", cmd_commit_tree, RUN_SETUP },
290 { "config", cmd_config },
291 { "count-objects", cmd_count_objects, RUN_SETUP },
292 { "describe", cmd_describe, RUN_SETUP },
293 { "diff", cmd_diff, USE_PAGER },
294 { "diff-files", cmd_diff_files },
295 { "diff-index", cmd_diff_index, RUN_SETUP },
296 { "diff-tree", cmd_diff_tree, RUN_SETUP },
297 { "fetch--tool", cmd_fetch__tool, RUN_SETUP },
298 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
299 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
300 { "format-patch", cmd_format_patch, RUN_SETUP },
301 { "fsck", cmd_fsck, RUN_SETUP },
302 { "fsck-objects", cmd_fsck, RUN_SETUP },
303 { "gc", cmd_gc, RUN_SETUP },
304 { "get-tar-commit-id", cmd_get_tar_commit_id },
305 { "grep", cmd_grep, RUN_SETUP | USE_PAGER },
306 { "help", cmd_help },
307 { "init", cmd_init_db },
308 { "init-db", cmd_init_db },
309 { "log", cmd_log, RUN_SETUP | USE_PAGER },
310 { "ls-files", cmd_ls_files, RUN_SETUP },
311 { "ls-tree", cmd_ls_tree, RUN_SETUP },
312 { "mailinfo", cmd_mailinfo },
313 { "mailsplit", cmd_mailsplit },
314 { "merge-base", cmd_merge_base, RUN_SETUP },
315 { "merge-file", cmd_merge_file },
316 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
317 { "name-rev", cmd_name_rev, RUN_SETUP },
318 { "pack-objects", cmd_pack_objects, RUN_SETUP },
319 { "pickaxe", cmd_blame, RUN_SETUP | USE_PAGER },
320 { "prune", cmd_prune, RUN_SETUP },
321 { "prune-packed", cmd_prune_packed, RUN_SETUP },
322 { "push", cmd_push, RUN_SETUP },
323 { "read-tree", cmd_read_tree, RUN_SETUP },
324 { "reflog", cmd_reflog, RUN_SETUP },
325 { "repo-config", cmd_config },
326 { "rerere", cmd_rerere, RUN_SETUP },
327 { "rev-list", cmd_rev_list, RUN_SETUP },
328 { "rev-parse", cmd_rev_parse, RUN_SETUP },
329 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
330 { "rm", cmd_rm, RUN_SETUP | NEED_WORK_TREE },
331 { "runstatus", cmd_runstatus, RUN_SETUP | NEED_WORK_TREE },
332 { "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER },
333 { "show-branch", cmd_show_branch, RUN_SETUP },
334 { "show", cmd_show, RUN_SETUP | USE_PAGER },
335 { "stripspace", cmd_stripspace },
336 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
337 { "tar-tree", cmd_tar_tree },
338 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
339 { "update-index", cmd_update_index, RUN_SETUP },
340 { "update-ref", cmd_update_ref, RUN_SETUP },
341 { "upload-archive", cmd_upload_archive },
342 { "version", cmd_version },
343 { "whatchanged", cmd_whatchanged, RUN_SETUP | USE_PAGER },
344 { "write-tree", cmd_write_tree, RUN_SETUP },
345 { "verify-pack", cmd_verify_pack },
346 { "show-ref", cmd_show_ref, RUN_SETUP },
347 { "pack-refs", cmd_pack_refs, RUN_SETUP },
349 int i;
351 /* Turn "git cmd --help" into "git help cmd" */
352 if (argc > 1 && !strcmp(argv[1], "--help")) {
353 argv[1] = argv[0];
354 argv[0] = cmd = "help";
357 for (i = 0; i < ARRAY_SIZE(commands); i++) {
358 struct cmd_struct *p = commands+i;
359 if (strcmp(p->cmd, cmd))
360 continue;
361 exit(run_command(p, argc, argv));
365 int main(int argc, const char **argv)
367 const char *cmd = argv[0] ? argv[0] : "git-help";
368 char *slash = strrchr(cmd, '/');
369 const char *exec_path = NULL;
370 int done_alias = 0;
373 * Take the basename of argv[0] as the command
374 * name, and the dirname as the default exec_path
375 * if it's an absolute path and we don't have
376 * anything better.
378 if (slash) {
379 *slash++ = 0;
380 if (*cmd == '/')
381 exec_path = cmd;
382 cmd = slash;
386 * "git-xxxx" is the same as "git xxxx", but we obviously:
388 * - cannot take flags in between the "git" and the "xxxx".
389 * - cannot execute it externally (since it would just do
390 * the same thing over again)
392 * So we just directly call the internal command handler, and
393 * die if that one cannot handle it.
395 if (!prefixcmp(cmd, "git-")) {
396 cmd += 4;
397 argv[0] = cmd;
398 handle_internal_command(argc, argv);
399 die("cannot handle %s internally", cmd);
402 /* Look for flags.. */
403 argv++;
404 argc--;
405 handle_options(&argv, &argc);
406 if (argc > 0) {
407 if (!prefixcmp(argv[0], "--"))
408 argv[0] += 2;
409 } else {
410 /* Default command: "help" */
411 argv[0] = "help";
412 argc = 1;
414 cmd = argv[0];
417 * We search for git commands in the following order:
418 * - git_exec_path()
419 * - the path of the "git" command if we could find it
420 * in $0
421 * - the regular PATH.
423 if (exec_path)
424 prepend_to_path(exec_path, strlen(exec_path));
425 exec_path = git_exec_path();
426 prepend_to_path(exec_path, strlen(exec_path));
428 while (1) {
429 /* See if it's an internal command */
430 handle_internal_command(argc, argv);
432 /* .. then try the external ones */
433 execv_git_cmd(argv);
435 /* It could be an alias -- this works around the insanity
436 * of overriding "git log" with "git show" by having
437 * alias.log = show
439 if (done_alias || !handle_alias(&argc, &argv))
440 break;
441 done_alias = 1;
444 if (errno == ENOENT) {
445 if (done_alias) {
446 fprintf(stderr, "Expansion of alias '%s' failed; "
447 "'%s' is not a git-command\n",
448 cmd, argv[0]);
449 exit(1);
451 help_unknown_cmd(cmd);
454 fprintf(stderr, "Failed to run command '%s': %s\n",
455 cmd, strerror(errno));
457 return 1;