]> git.ipfire.org Git - thirdparty/git.git/blame - git.c
builtin git-mv: support moving directories
[thirdparty/git.git] / git.c
CommitLineData
8e49d503 1#include <stdio.h>
7dbc2c04
JH
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <dirent.h>
8e49d503
AE
5#include <unistd.h>
6#include <stdlib.h>
7#include <string.h>
8#include <errno.h>
9#include <limits.h>
10#include <stdarg.h>
4050c0df 11#include "git-compat-util.h"
77cb17e9 12#include "exec_cmd.h"
2b11e317 13#include "cache.h"
575ba9d6 14#include "quote.h"
8e49d503 15
70827b15 16#include "builtin.h"
8e49d503
AE
17
18static void prepend_to_path(const char *dir, int len)
19{
554fe20d
TH
20 const char *old_path = getenv("PATH");
21 char *path;
8e49d503
AE
22 int path_len = len;
23
24 if (!old_path)
7dbc2c04 25 old_path = "/usr/local/bin:/usr/bin:/bin";
8e49d503
AE
26
27 path_len = len + strlen(old_path) + 1;
28
29 path = malloc(path_len + 1);
8e49d503
AE
30
31 memcpy(path, dir, len);
32 path[len] = ':';
33 memcpy(path + len + 1, old_path, path_len - len);
34
35 setenv("PATH", path, 1);
36}
37
2b11e317
JS
38static const char *alias_command;
39static char *alias_string = NULL;
40
41static int git_alias_config(const char *var, const char *value)
42{
43 if (!strncmp(var, "alias.", 6) && !strcmp(var + 6, alias_command)) {
44 alias_string = strdup(value);
45 }
46 return 0;
47}
48
49static int split_cmdline(char *cmdline, const char ***argv)
50{
51 int src, dst, count = 0, size = 16;
52 char quoted = 0;
53
54 *argv = malloc(sizeof(char*) * size);
55
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);
68 }
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 \\");
84 }
85 }
86 cmdline[dst++] = c;
87 src++;
88 }
89 }
90
91 cmdline[dst] = 0;
92
93 if (quoted) {
94 free(*argv);
95 *argv = NULL;
96 return error("unclosed quote");
97 }
98
99 return count;
100}
101
102static int handle_alias(int *argcp, const char ***argv)
103{
47e5c0ca 104 int nongit = 0, ret = 0, saved_errno = errno;
2b11e317
JS
105 const char *subdir;
106
107 subdir = setup_git_directory_gently(&nongit);
108 if (!nongit) {
109 int count;
110 const char** new_argv;
111
112 alias_command = (*argv)[0];
113 git_config(git_alias_config);
114 if (alias_string) {
115
116 count = split_cmdline(alias_string, &new_argv);
117
118 if (count < 1)
119 die("empty alias for %s", alias_command);
120
121 if (!strcmp(alias_command, new_argv[0]))
122 die("recursive alias: %s", alias_command);
123
575ba9d6
ML
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]);
131 }
132 fputc('\n', stderr);
133 fflush(stderr);
134 }
135
d5b9e6cf
ML
136 new_argv = realloc(new_argv, sizeof(char*) *
137 (count + *argcp + 1));
2b11e317 138 /* insert after command name */
d5b9e6cf
ML
139 memcpy(new_argv + count, *argv + 1,
140 sizeof(char*) * *argcp);
141 new_argv[count+*argcp] = NULL;
2b11e317
JS
142
143 *argv = new_argv;
144 *argcp += count - 1;
145
146 ret = 1;
147 }
148 }
149
150 if (subdir)
151 chdir(subdir);
152
47e5c0ca
JS
153 errno = saved_errno;
154
2b11e317
JS
155 return ret;
156}
157
70827b15 158const char git_version_string[] = GIT_VERSION;
5b84f4d8 159
9201c707 160static void handle_internal_command(int argc, const char **argv, char **envp)
231af832
LT
161{
162 const char *cmd = argv[0];
163 static struct cmd_struct {
164 const char *cmd;
9201c707 165 int (*fn)(int, const char **, char **);
231af832
LT
166 } commands[] = {
167 { "version", cmd_version },
168 { "help", cmd_help },
70b006b9 169 { "log", cmd_log },
70827b15 170 { "whatchanged", cmd_whatchanged },
ba1d4505 171 { "show", cmd_show },
755225de 172 { "push", cmd_push },
68563738 173 { "format-patch", cmd_format_patch },
c7432087 174 { "count-objects", cmd_count_objects },
8ab99476 175 { "diff", cmd_diff },
5010cb5f 176 { "grep", cmd_grep },
d9b814cc 177 { "rm", cmd_rm },
0d781539 178 { "add", cmd_add },
5fb61b8d 179 { "rev-list", cmd_rev_list },
c3c8835f 180 { "init-db", cmd_init_db },
52ba03cb 181 { "get-tar-commit-id", cmd_get_tar_commit_id },
21754264 182 { "upload-tar", cmd_upload_tar },
0864f264 183 { "check-ref-format", cmd_check_ref_format },
aae01bda 184 { "ls-files", cmd_ls_files },
56d1398a 185 { "ls-tree", cmd_ls_tree },
d147e501 186 { "tar-tree", cmd_tar_tree },
6d96ac18 187 { "read-tree", cmd_read_tree },
ac6245e3 188 { "commit-tree", cmd_commit_tree },
51ce34b9 189 { "apply", cmd_apply },
e8cc9cd9
PE
190 { "show-branch", cmd_show_branch },
191 { "diff-files", cmd_diff_files },
192 { "diff-index", cmd_diff_index },
193 { "diff-stages", cmd_diff_stages },
f81daefe 194 { "diff-tree", cmd_diff_tree },
895f10c3 195 { "cat-file", cmd_cat_file },
8ed05fb5 196 { "rev-parse", cmd_rev_parse },
e690e843 197 { "write-tree", cmd_write_tree },
34488e3c 198 { "mailsplit", cmd_mailsplit },
7499c996 199 { "mailinfo", cmd_mailinfo },
fefe81c9 200 { "stripspace", cmd_stripspace },
854b4629 201 { "update-index", cmd_update_index },
00449f99 202 { "update-ref", cmd_update_ref },
ba84a797
LT
203 { "fmt-merge-msg", cmd_fmt_merge_msg },
204 { "prune", cmd_prune },
11be42a4 205 { "mv", cmd_mv },
231af832
LT
206 };
207 int i;
208
1cd95087
LT
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";
213 }
214
231af832
LT
215 for (i = 0; i < ARRAY_SIZE(commands); i++) {
216 struct cmd_struct *p = commands+i;
217 if (strcmp(p->cmd, cmd))
218 continue;
575ba9d6
ML
219
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]);
226 }
227 putc('\n', stderr);
228 fflush(stderr);
229 }
230
231af832
LT
231 exit(p->fn(argc, argv, envp));
232 }
233}
234
9201c707 235int main(int argc, const char **argv, char **envp)
8e49d503 236{
9201c707 237 const char *cmd = argv[0];
231af832 238 char *slash = strrchr(cmd, '/');
231af832 239 const char *exec_path = NULL;
a025463b 240 int done_alias = 0;
231af832
LT
241
242 /*
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.
247 */
248 if (slash) {
249 *slash++ = 0;
250 if (*cmd == '/')
251 exec_path = cmd;
252 cmd = slash;
253 }
8e49d503 254
231af832
LT
255 /*
256 * "git-xxxx" is the same as "git xxxx", but we obviously:
257 *
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)
261 *
262 * So we just directly call the internal command handler, and
263 * die if that one cannot handle it.
264 */
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);
270 }
8e49d503 271
231af832
LT
272 /* Default command: "help" */
273 cmd = "help";
8e49d503 274
231af832
LT
275 /* Look for flags.. */
276 while (argc > 1) {
277 cmd = *++argv;
278 argc--;
da6bf70e 279
85fb65ed
JH
280 if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
281 setup_pager();
282 continue;
283 }
284
231af832 285 if (strncmp(cmd, "--", 2))
8e49d503
AE
286 break;
287
231af832
LT
288 cmd += 2;
289
290 /*
291 * For legacy reasons, the "version" and "help"
292 * commands can be written with "--" prepended
293 * to make them look like flags.
294 */
295 if (!strcmp(cmd, "help"))
296 break;
297 if (!strcmp(cmd, "version"))
298 break;
8e49d503 299
231af832
LT
300 /*
301 * Check remaining flags (which by now must be
302 * "--exec-path", but maybe we will accept
303 * other arguments some day)
304 */
305 if (!strncmp(cmd, "exec-path", 9)) {
306 cmd += 9;
307 if (*cmd == '=') {
308 git_set_exec_path(cmd + 1);
309 continue;
8e49d503 310 }
231af832 311 puts(git_exec_path());
8e49d503
AE
312 exit(0);
313 }
a87cd02c 314 cmd_usage(0, NULL, NULL);
97fc6c5f 315 }
231af832
LT
316 argv[0] = cmd;
317
318 /*
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.
324 */
325 if (exec_path)
326 prepend_to_path(exec_path, strlen(exec_path));
77cb17e9
MO
327 exec_path = git_exec_path();
328 prepend_to_path(exec_path, strlen(exec_path));
8e49d503 329
a025463b
JH
330 while (1) {
331 /* See if it's an internal command */
332 handle_internal_command(argc, argv, envp);
2b11e317 333
a025463b
JH
334 /* .. then try the external ones */
335 execv_git_cmd(argv);
231af832 336
a025463b
JH
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
340 */
341 if (done_alias || !handle_alias(&argc, &argv))
342 break;
343 done_alias = 1;
344 }
10b15b86
AR
345
346 if (errno == ENOENT)
a87cd02c 347 cmd_usage(0, exec_path, "'%s' is not a git-command", cmd);
10b15b86
AR
348
349 fprintf(stderr, "Failed to run command '%s': %s\n",
33ebb871 350 cmd, strerror(errno));
8e49d503
AE
351
352 return 1;
353}