]> git.ipfire.org Git - thirdparty/git.git/blob - git.c
Merge branch 'js/repoconfig'
[thirdparty/git.git] / git.c
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
14 #include "builtin.h"
15
16 static void prepend_to_path(const char *dir, int len)
17 {
18 char *path, *old_path = getenv("PATH");
19 int path_len = len;
20
21 if (!old_path)
22 old_path = "/usr/local/bin:/usr/bin:/bin";
23
24 path_len = len + strlen(old_path) + 1;
25
26 path = malloc(path_len + 1);
27
28 memcpy(path, dir, len);
29 path[len] = ':';
30 memcpy(path + len + 1, old_path, path_len - len);
31
32 setenv("PATH", path, 1);
33 }
34
35 const char git_version_string[] = GIT_VERSION;
36
37 static void handle_internal_command(int argc, const char **argv, char **envp)
38 {
39 const char *cmd = argv[0];
40 static struct cmd_struct {
41 const char *cmd;
42 int (*fn)(int, const char **, char **);
43 } commands[] = {
44 { "version", cmd_version },
45 { "help", cmd_help },
46 { "log", cmd_log },
47 { "whatchanged", cmd_whatchanged },
48 { "show", cmd_show },
49 { "push", cmd_push },
50 { "count-objects", cmd_count_objects },
51 };
52 int i;
53
54 /* Turn "git cmd --help" into "git help cmd" */
55 if (argc > 1 && !strcmp(argv[1], "--help")) {
56 argv[1] = argv[0];
57 argv[0] = cmd = "help";
58 }
59
60 for (i = 0; i < ARRAY_SIZE(commands); i++) {
61 struct cmd_struct *p = commands+i;
62 if (strcmp(p->cmd, cmd))
63 continue;
64 exit(p->fn(argc, argv, envp));
65 }
66 }
67
68 int main(int argc, const char **argv, char **envp)
69 {
70 const char *cmd = argv[0];
71 char *slash = strrchr(cmd, '/');
72 char git_command[PATH_MAX + 1];
73 const char *exec_path = NULL;
74
75 /*
76 * Take the basename of argv[0] as the command
77 * name, and the dirname as the default exec_path
78 * if it's an absolute path and we don't have
79 * anything better.
80 */
81 if (slash) {
82 *slash++ = 0;
83 if (*cmd == '/')
84 exec_path = cmd;
85 cmd = slash;
86 }
87
88 /*
89 * "git-xxxx" is the same as "git xxxx", but we obviously:
90 *
91 * - cannot take flags in between the "git" and the "xxxx".
92 * - cannot execute it externally (since it would just do
93 * the same thing over again)
94 *
95 * So we just directly call the internal command handler, and
96 * die if that one cannot handle it.
97 */
98 if (!strncmp(cmd, "git-", 4)) {
99 cmd += 4;
100 argv[0] = cmd;
101 handle_internal_command(argc, argv, envp);
102 die("cannot handle %s internally", cmd);
103 }
104
105 /* Default command: "help" */
106 cmd = "help";
107
108 /* Look for flags.. */
109 while (argc > 1) {
110 cmd = *++argv;
111 argc--;
112
113 if (strncmp(cmd, "--", 2))
114 break;
115
116 cmd += 2;
117
118 /*
119 * For legacy reasons, the "version" and "help"
120 * commands can be written with "--" prepended
121 * to make them look like flags.
122 */
123 if (!strcmp(cmd, "help"))
124 break;
125 if (!strcmp(cmd, "version"))
126 break;
127
128 /*
129 * Check remaining flags (which by now must be
130 * "--exec-path", but maybe we will accept
131 * other arguments some day)
132 */
133 if (!strncmp(cmd, "exec-path", 9)) {
134 cmd += 9;
135 if (*cmd == '=') {
136 git_set_exec_path(cmd + 1);
137 continue;
138 }
139 puts(git_exec_path());
140 exit(0);
141 }
142 cmd_usage(0, NULL, NULL);
143 }
144 argv[0] = cmd;
145
146 /*
147 * We search for git commands in the following order:
148 * - git_exec_path()
149 * - the path of the "git" command if we could find it
150 * in $0
151 * - the regular PATH.
152 */
153 if (exec_path)
154 prepend_to_path(exec_path, strlen(exec_path));
155 exec_path = git_exec_path();
156 prepend_to_path(exec_path, strlen(exec_path));
157
158 /* See if it's an internal command */
159 handle_internal_command(argc, argv, envp);
160
161 /* .. then try the external ones */
162 execv_git_cmd(argv);
163
164 if (errno == ENOENT)
165 cmd_usage(0, exec_path, "'%s' is not a git-command", cmd);
166
167 fprintf(stderr, "Failed to run command '%s': %s\n",
168 git_command, strerror(errno));
169
170 return 1;
171 }