gitweb.css: Use monospace fonts for commits and tree-diff.
[git/jrn.git] / git.c
blob102735af6c0cfb496a04593ba466a9fbb18f693d
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <dirent.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <limits.h>
10 #include <stdarg.h>
11 #include "git-compat-util.h"
12 #include "exec_cmd.h"
13 #include "cache.h"
14 #include "quote.h"
16 #include "builtin.h"
18 static void prepend_to_path(const char *dir, int len)
20 const char *old_path = getenv("PATH");
21 char *path;
22 int path_len = len;
24 if (!old_path)
25 old_path = "/usr/local/bin:/usr/bin:/bin";
27 path_len = len + strlen(old_path) + 1;
29 path = malloc(path_len + 1);
31 memcpy(path, dir, len);
32 path[len] = ':';
33 memcpy(path + len + 1, old_path, path_len - len);
35 setenv("PATH", path, 1);
38 static const char *alias_command;
39 static char *alias_string = NULL;
41 static int git_alias_config(const char *var, const char *value)
43 if (!strncmp(var, "alias.", 6) && !strcmp(var + 6, alias_command)) {
44 alias_string = strdup(value);
46 return 0;
49 static int split_cmdline(char *cmdline, const char ***argv)
51 int src, dst, count = 0, size = 16;
52 char quoted = 0;
54 *argv = malloc(sizeof(char*) * size);
56 /* split alias_string */
57 (*argv)[count++] = cmdline;
58 for (src = dst = 0; cmdline[src];) {
59 char c = cmdline[src];
60 if (!quoted && isspace(c)) {
61 cmdline[dst++] = 0;
62 while (cmdline[++src]
63 && isspace(cmdline[src]))
64 ; /* skip */
65 if (count >= size) {
66 size += 16;
67 *argv = realloc(*argv, sizeof(char*) * size);
69 (*argv)[count++] = cmdline + dst;
70 } else if(!quoted && (c == '\'' || c == '"')) {
71 quoted = c;
72 src++;
73 } else if (c == quoted) {
74 quoted = 0;
75 src++;
76 } else {
77 if (c == '\\' && quoted != '\'') {
78 src++;
79 c = cmdline[src];
80 if (!c) {
81 free(*argv);
82 *argv = NULL;
83 return error("cmdline ends with \\");
86 cmdline[dst++] = c;
87 src++;
91 cmdline[dst] = 0;
93 if (quoted) {
94 free(*argv);
95 *argv = NULL;
96 return error("unclosed quote");
99 return count;
102 static int handle_alias(int *argcp, const char ***argv)
104 int nongit = 0, ret = 0, saved_errno = errno;
105 const char *subdir;
107 subdir = setup_git_directory_gently(&nongit);
108 if (!nongit) {
109 int count;
110 const char** new_argv;
112 alias_command = (*argv)[0];
113 git_config(git_alias_config);
114 if (alias_string) {
116 count = split_cmdline(alias_string, &new_argv);
118 if (count < 1)
119 die("empty alias for %s", alias_command);
121 if (!strcmp(alias_command, new_argv[0]))
122 die("recursive alias: %s", alias_command);
124 if (getenv("GIT_TRACE")) {
125 int i;
126 fprintf(stderr, "trace: alias expansion: %s =>",
127 alias_command);
128 for (i = 0; i < count; ++i) {
129 fputc(' ', stderr);
130 sq_quote_print(stderr, new_argv[i]);
132 fputc('\n', stderr);
133 fflush(stderr);
136 /* insert after command name */
137 if (*argcp > 1) {
138 new_argv = realloc(new_argv, sizeof(char*) *
139 (count + *argcp));
140 memcpy(new_argv + count, *argv + 1,
141 sizeof(char*) * *argcp);
144 *argv = new_argv;
145 *argcp += count - 1;
147 ret = 1;
151 if (subdir)
152 chdir(subdir);
154 errno = saved_errno;
156 return ret;
159 const char git_version_string[] = GIT_VERSION;
161 static void handle_internal_command(int argc, const char **argv, char **envp)
163 const char *cmd = argv[0];
164 static struct cmd_struct {
165 const char *cmd;
166 int (*fn)(int, const char **, char **);
167 } commands[] = {
168 { "version", cmd_version },
169 { "help", cmd_help },
170 { "log", cmd_log },
171 { "whatchanged", cmd_whatchanged },
172 { "show", cmd_show },
173 { "push", cmd_push },
174 { "format-patch", cmd_format_patch },
175 { "count-objects", cmd_count_objects },
176 { "diff", cmd_diff },
177 { "grep", cmd_grep },
178 { "rm", cmd_rm },
179 { "add", cmd_add },
180 { "rev-list", cmd_rev_list },
181 { "init-db", cmd_init_db },
182 { "get-tar-commit-id", cmd_get_tar_commit_id },
183 { "upload-tar", cmd_upload_tar },
184 { "check-ref-format", cmd_check_ref_format },
185 { "ls-files", cmd_ls_files },
186 { "ls-tree", cmd_ls_tree },
187 { "tar-tree", cmd_tar_tree },
188 { "read-tree", cmd_read_tree },
189 { "commit-tree", cmd_commit_tree },
190 { "apply", cmd_apply },
191 { "show-branch", cmd_show_branch },
192 { "diff-files", cmd_diff_files },
193 { "diff-index", cmd_diff_index },
194 { "diff-stages", cmd_diff_stages },
195 { "diff-tree", cmd_diff_tree },
196 { "cat-file", cmd_cat_file },
197 { "rev-parse", cmd_rev_parse },
198 { "write-tree", cmd_write_tree },
199 { "mailsplit", cmd_mailsplit },
200 { "mailinfo", cmd_mailinfo },
201 { "stripspace", cmd_stripspace },
202 { "update-index", cmd_update_index },
203 { "update-ref", cmd_update_ref },
204 { "fmt-merge-msg", cmd_fmt_merge_msg },
205 { "prune", cmd_prune },
207 int i;
209 /* Turn "git cmd --help" into "git help cmd" */
210 if (argc > 1 && !strcmp(argv[1], "--help")) {
211 argv[1] = argv[0];
212 argv[0] = cmd = "help";
215 for (i = 0; i < ARRAY_SIZE(commands); i++) {
216 struct cmd_struct *p = commands+i;
217 if (strcmp(p->cmd, cmd))
218 continue;
220 if (getenv("GIT_TRACE")) {
221 int i;
222 fprintf(stderr, "trace: built-in: git");
223 for (i = 0; i < argc; ++i) {
224 fputc(' ', stderr);
225 sq_quote_print(stderr, argv[i]);
227 putc('\n', stderr);
228 fflush(stderr);
231 exit(p->fn(argc, argv, envp));
235 int main(int argc, const char **argv, char **envp)
237 const char *cmd = argv[0];
238 char *slash = strrchr(cmd, '/');
239 const char *exec_path = NULL;
240 int done_alias = 0;
243 * Take the basename of argv[0] as the command
244 * name, and the dirname as the default exec_path
245 * if it's an absolute path and we don't have
246 * anything better.
248 if (slash) {
249 *slash++ = 0;
250 if (*cmd == '/')
251 exec_path = cmd;
252 cmd = slash;
256 * "git-xxxx" is the same as "git xxxx", but we obviously:
258 * - cannot take flags in between the "git" and the "xxxx".
259 * - cannot execute it externally (since it would just do
260 * the same thing over again)
262 * So we just directly call the internal command handler, and
263 * die if that one cannot handle it.
265 if (!strncmp(cmd, "git-", 4)) {
266 cmd += 4;
267 argv[0] = cmd;
268 handle_internal_command(argc, argv, envp);
269 die("cannot handle %s internally", cmd);
272 /* Default command: "help" */
273 cmd = "help";
275 /* Look for flags.. */
276 while (argc > 1) {
277 cmd = *++argv;
278 argc--;
280 if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
281 setup_pager();
282 continue;
285 if (strncmp(cmd, "--", 2))
286 break;
288 cmd += 2;
291 * For legacy reasons, the "version" and "help"
292 * commands can be written with "--" prepended
293 * to make them look like flags.
295 if (!strcmp(cmd, "help"))
296 break;
297 if (!strcmp(cmd, "version"))
298 break;
301 * Check remaining flags (which by now must be
302 * "--exec-path", but maybe we will accept
303 * other arguments some day)
305 if (!strncmp(cmd, "exec-path", 9)) {
306 cmd += 9;
307 if (*cmd == '=') {
308 git_set_exec_path(cmd + 1);
309 continue;
311 puts(git_exec_path());
312 exit(0);
314 cmd_usage(0, NULL, NULL);
316 argv[0] = cmd;
319 * We search for git commands in the following order:
320 * - git_exec_path()
321 * - the path of the "git" command if we could find it
322 * in $0
323 * - the regular PATH.
325 if (exec_path)
326 prepend_to_path(exec_path, strlen(exec_path));
327 exec_path = git_exec_path();
328 prepend_to_path(exec_path, strlen(exec_path));
330 while (1) {
331 /* See if it's an internal command */
332 handle_internal_command(argc, argv, envp);
334 /* .. then try the external ones */
335 execv_git_cmd(argv);
337 /* It could be an alias -- this works around the insanity
338 * of overriding "git log" with "git show" by having
339 * alias.log = show
341 if (done_alias || !handle_alias(&argc, &argv))
342 break;
343 done_alias = 1;
346 if (errno == ENOENT)
347 cmd_usage(0, exec_path, "'%s' is not a git-command", cmd);
349 fprintf(stderr, "Failed to run command '%s': %s\n",
350 cmd, strerror(errno));
352 return 1;