]> git.ipfire.org Git - thirdparty/git.git/blame - git.c
Merge branch 'ml/cvsserver'
[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>
ea77e675 11#include <sys/ioctl.h>
4050c0df 12#include "git-compat-util.h"
77cb17e9 13#include "exec_cmd.h"
8e49d503
AE
14
15#ifndef PATH_MAX
16# define PATH_MAX 4096
17#endif
18
19static const char git_usage[] =
20 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
21
8e49d503
AE
22/* most gui terms set COLUMNS (although some don't export it) */
23static int term_columns(void)
24{
25 char *col_string = getenv("COLUMNS");
26 int n_cols = 0;
27
28 if (col_string && (n_cols = atoi(col_string)) > 0)
29 return n_cols;
30
ea77e675
LT
31#ifdef TIOCGWINSZ
32 {
33 struct winsize ws;
34 if (!ioctl(1, TIOCGWINSZ, &ws)) {
35 if (ws.ws_col)
36 return ws.ws_col;
37 }
38 }
39#endif
40
8e49d503
AE
41 return 80;
42}
43
7dbc2c04
JH
44static void oom(void)
45{
46 fprintf(stderr, "git: out of memory\n");
47 exit(1);
48}
49
8e49d503
AE
50static inline void mput_char(char c, unsigned int num)
51{
52 while(num--)
53 putchar(c);
54}
55
7dbc2c04
JH
56static struct cmdname {
57 size_t len;
58 char name[1];
59} **cmdname;
60static int cmdname_alloc, cmdname_cnt;
61
62static void add_cmdname(const char *name, int len)
63{
64 struct cmdname *ent;
65 if (cmdname_alloc <= cmdname_cnt) {
66 cmdname_alloc = cmdname_alloc + 200;
67 cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
68 if (!cmdname)
69 oom();
70 }
71 ent = malloc(sizeof(*ent) + len);
72 if (!ent)
73 oom();
74 ent->len = len;
f9039f30
JH
75 memcpy(ent->name, name, len);
76 ent->name[len] = 0;
7dbc2c04
JH
77 cmdname[cmdname_cnt++] = ent;
78}
79
80static int cmdname_compare(const void *a_, const void *b_)
81{
82 struct cmdname *a = *(struct cmdname **)a_;
83 struct cmdname *b = *(struct cmdname **)b_;
84 return strcmp(a->name, b->name);
85}
86
87static void pretty_print_string_list(struct cmdname **cmdname, int longest)
8e49d503 88{
112d0baf 89 int cols = 1, rows;
8e49d503
AE
90 int space = longest + 1; /* min 1 SP between words */
91 int max_cols = term_columns() - 1; /* don't print *on* the edge */
112d0baf 92 int i, j;
8e49d503
AE
93
94 if (space < max_cols)
95 cols = max_cols / space;
112d0baf 96 rows = (cmdname_cnt + cols - 1) / cols;
8e49d503 97
7dbc2c04
JH
98 qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
99
112d0baf 100 for (i = 0; i < rows; i++) {
8e49d503
AE
101 printf(" ");
102
112d0baf
LT
103 for (j = 0; j < cols; j++) {
104 int n = j * rows + i;
105 int size = space;
106 if (n >= cmdname_cnt)
107 break;
108 if (j == cols-1 || n + rows >= cmdname_cnt)
109 size = 1;
110 printf("%-*s", size, cmdname[n]->name);
8e49d503
AE
111 }
112 putchar('\n');
113 }
114}
115
116static void list_commands(const char *exec_path, const char *pattern)
117{
7dbc2c04
JH
118 unsigned int longest = 0;
119 char path[PATH_MAX];
120 int dirlen;
121 DIR *dir = opendir(exec_path);
122 struct dirent *de;
123
124 if (!dir) {
125 fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
8e49d503
AE
126 exit(1);
127 }
128
7dbc2c04
JH
129 dirlen = strlen(exec_path);
130 if (PATH_MAX - 20 < dirlen) {
131 fprintf(stderr, "git: insanely long exec-path '%s'\n",
132 exec_path);
8e49d503
AE
133 exit(1);
134 }
135
7dbc2c04
JH
136 memcpy(path, exec_path, dirlen);
137 path[dirlen++] = '/';
138
139 while ((de = readdir(dir)) != NULL) {
140 struct stat st;
141 int entlen;
8e49d503 142
7dbc2c04
JH
143 if (strncmp(de->d_name, "git-", 4))
144 continue;
145 strcpy(path+dirlen, de->d_name);
146 if (stat(path, &st) || /* stat, not lstat */
147 !S_ISREG(st.st_mode) ||
148 !(st.st_mode & S_IXUSR))
8e49d503
AE
149 continue;
150
7dbc2c04 151 entlen = strlen(de->d_name);
f9039f30
JH
152 if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
153 entlen -= 4;
8e49d503 154
7dbc2c04
JH
155 if (longest < entlen)
156 longest = entlen;
157
158 add_cmdname(de->d_name + 4, entlen-4);
8e49d503 159 }
7dbc2c04 160 closedir(dir);
8e49d503
AE
161
162 printf("git commands available in '%s'\n", exec_path);
163 printf("----------------------------");
164 mput_char('-', strlen(exec_path));
165 putchar('\n');
7dbc2c04 166 pretty_print_string_list(cmdname, longest - 4);
8e49d503
AE
167 putchar('\n');
168}
169
170#ifdef __GNUC__
4050c0df 171static void cmd_usage(const char *exec_path, const char *fmt, ...)
8e49d503
AE
172 __attribute__((__format__(__printf__, 2, 3), __noreturn__));
173#endif
4050c0df 174static void cmd_usage(const char *exec_path, const char *fmt, ...)
8e49d503
AE
175{
176 if (fmt) {
177 va_list ap;
178
179 va_start(ap, fmt);
180 printf("git: ");
181 vprintf(fmt, ap);
182 va_end(ap);
183 putchar('\n');
184 }
185 else
186 puts(git_usage);
187
188 putchar('\n');
189
190 if(exec_path)
191 list_commands(exec_path, "git-*");
192
193 exit(1);
194}
195
196static void prepend_to_path(const char *dir, int len)
197{
198 char *path, *old_path = getenv("PATH");
199 int path_len = len;
200
201 if (!old_path)
7dbc2c04 202 old_path = "/usr/local/bin:/usr/bin:/bin";
8e49d503
AE
203
204 path_len = len + strlen(old_path) + 1;
205
206 path = malloc(path_len + 1);
8e49d503
AE
207
208 memcpy(path, dir, len);
209 path[len] = ':';
210 memcpy(path + len + 1, old_path, path_len - len);
211
212 setenv("PATH", path, 1);
213}
214
97fc6c5f
AE
215static void show_man_page(char *git_cmd)
216{
217 char *page;
218
219 if (!strncmp(git_cmd, "git", 3))
220 page = git_cmd;
221 else {
222 int page_len = strlen(git_cmd) + 4;
223
224 page = malloc(page_len + 1);
225 strcpy(page, "git-");
226 strcpy(page + 4, git_cmd);
227 page[page_len] = 0;
228 }
229
7dbc2c04 230 execlp("man", "man", page, NULL);
97fc6c5f
AE
231}
232
231af832
LT
233static int cmd_version(int argc, char **argv, char **envp)
234{
235 printf("git version %s\n", GIT_VERSION);
236 return 0;
237}
238
239static int cmd_help(int argc, char **argv, char **envp)
240{
241 char *help_cmd = argv[1];
242 if (!help_cmd)
243 cmd_usage(git_exec_path(), NULL);
244 show_man_page(help_cmd);
245 return 0;
246}
247
248#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
249
250static void handle_internal_command(int argc, char **argv, char **envp)
251{
252 const char *cmd = argv[0];
253 static struct cmd_struct {
254 const char *cmd;
255 int (*fn)(int, char **, char **);
256 } commands[] = {
257 { "version", cmd_version },
258 { "help", cmd_help },
259 };
260 int i;
261
262 for (i = 0; i < ARRAY_SIZE(commands); i++) {
263 struct cmd_struct *p = commands+i;
264 if (strcmp(p->cmd, cmd))
265 continue;
266 exit(p->fn(argc, argv, envp));
267 }
268}
269
8e49d503
AE
270int main(int argc, char **argv, char **envp)
271{
231af832
LT
272 char *cmd = argv[0];
273 char *slash = strrchr(cmd, '/');
8e49d503 274 char git_command[PATH_MAX + 1];
231af832
LT
275 const char *exec_path = NULL;
276
277 /*
278 * Take the basename of argv[0] as the command
279 * name, and the dirname as the default exec_path
280 * if it's an absolute path and we don't have
281 * anything better.
282 */
283 if (slash) {
284 *slash++ = 0;
285 if (*cmd == '/')
286 exec_path = cmd;
287 cmd = slash;
288 }
8e49d503 289
231af832
LT
290 /*
291 * "git-xxxx" is the same as "git xxxx", but we obviously:
292 *
293 * - cannot take flags in between the "git" and the "xxxx".
294 * - cannot execute it externally (since it would just do
295 * the same thing over again)
296 *
297 * So we just directly call the internal command handler, and
298 * die if that one cannot handle it.
299 */
300 if (!strncmp(cmd, "git-", 4)) {
301 cmd += 4;
302 argv[0] = cmd;
303 handle_internal_command(argc, argv, envp);
304 die("cannot handle %s internally", cmd);
305 }
8e49d503 306
231af832
LT
307 /* Default command: "help" */
308 cmd = "help";
8e49d503 309
231af832
LT
310 /* Look for flags.. */
311 while (argc > 1) {
312 cmd = *++argv;
313 argc--;
da6bf70e 314
231af832 315 if (strncmp(cmd, "--", 2))
8e49d503
AE
316 break;
317
231af832
LT
318 cmd += 2;
319
320 /*
321 * For legacy reasons, the "version" and "help"
322 * commands can be written with "--" prepended
323 * to make them look like flags.
324 */
325 if (!strcmp(cmd, "help"))
326 break;
327 if (!strcmp(cmd, "version"))
328 break;
8e49d503 329
231af832
LT
330 /*
331 * Check remaining flags (which by now must be
332 * "--exec-path", but maybe we will accept
333 * other arguments some day)
334 */
335 if (!strncmp(cmd, "exec-path", 9)) {
336 cmd += 9;
337 if (*cmd == '=') {
338 git_set_exec_path(cmd + 1);
339 continue;
8e49d503 340 }
231af832 341 puts(git_exec_path());
8e49d503
AE
342 exit(0);
343 }
231af832 344 cmd_usage(NULL, NULL);
97fc6c5f 345 }
231af832
LT
346 argv[0] = cmd;
347
348 /*
349 * We search for git commands in the following order:
350 * - git_exec_path()
351 * - the path of the "git" command if we could find it
352 * in $0
353 * - the regular PATH.
354 */
355 if (exec_path)
356 prepend_to_path(exec_path, strlen(exec_path));
77cb17e9
MO
357 exec_path = git_exec_path();
358 prepend_to_path(exec_path, strlen(exec_path));
8e49d503 359
231af832
LT
360 /* See if it's an internal command */
361 handle_internal_command(argc, argv, envp);
362
363 /* .. then try the external ones */
364 execv_git_cmd(argv);
10b15b86
AR
365
366 if (errno == ENOENT)
231af832 367 cmd_usage(exec_path, "'%s' is not a git-command", cmd);
10b15b86
AR
368
369 fprintf(stderr, "Failed to run command '%s': %s\n",
370 git_command, strerror(errno));
8e49d503
AE
371
372 return 1;
373}