octopus: allow criss-cross and clarify the message when it rejects
[git/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"
8e49d503
AE
13
14#ifndef PATH_MAX
15# define PATH_MAX 4096
16#endif
17
18static const char git_usage[] =
19 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
20
8e49d503
AE
21/* most gui terms set COLUMNS (although some don't export it) */
22static int term_columns(void)
23{
24 char *col_string = getenv("COLUMNS");
25 int n_cols = 0;
26
27 if (col_string && (n_cols = atoi(col_string)) > 0)
28 return n_cols;
29
ea77e675
LT
30#ifdef TIOCGWINSZ
31 {
32 struct winsize ws;
33 if (!ioctl(1, TIOCGWINSZ, &ws)) {
34 if (ws.ws_col)
35 return ws.ws_col;
36 }
37 }
38#endif
39
8e49d503
AE
40 return 80;
41}
42
7dbc2c04
JH
43static void oom(void)
44{
45 fprintf(stderr, "git: out of memory\n");
46 exit(1);
47}
48
8e49d503
AE
49static inline void mput_char(char c, unsigned int num)
50{
51 while(num--)
52 putchar(c);
53}
54
7dbc2c04
JH
55static struct cmdname {
56 size_t len;
57 char name[1];
58} **cmdname;
59static int cmdname_alloc, cmdname_cnt;
60
61static void add_cmdname(const char *name, int len)
62{
63 struct cmdname *ent;
64 if (cmdname_alloc <= cmdname_cnt) {
65 cmdname_alloc = cmdname_alloc + 200;
66 cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
67 if (!cmdname)
68 oom();
69 }
70 ent = malloc(sizeof(*ent) + len);
71 if (!ent)
72 oom();
73 ent->len = len;
f9039f30
JH
74 memcpy(ent->name, name, len);
75 ent->name[len] = 0;
7dbc2c04
JH
76 cmdname[cmdname_cnt++] = ent;
77}
78
79static int cmdname_compare(const void *a_, const void *b_)
80{
81 struct cmdname *a = *(struct cmdname **)a_;
82 struct cmdname *b = *(struct cmdname **)b_;
83 return strcmp(a->name, b->name);
84}
85
86static void pretty_print_string_list(struct cmdname **cmdname, int longest)
8e49d503 87{
112d0baf 88 int cols = 1, rows;
8e49d503
AE
89 int space = longest + 1; /* min 1 SP between words */
90 int max_cols = term_columns() - 1; /* don't print *on* the edge */
112d0baf 91 int i, j;
8e49d503
AE
92
93 if (space < max_cols)
94 cols = max_cols / space;
112d0baf 95 rows = (cmdname_cnt + cols - 1) / cols;
8e49d503 96
7dbc2c04
JH
97 qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
98
112d0baf 99 for (i = 0; i < rows; i++) {
8e49d503
AE
100 printf(" ");
101
112d0baf
LT
102 for (j = 0; j < cols; j++) {
103 int n = j * rows + i;
104 int size = space;
105 if (n >= cmdname_cnt)
106 break;
107 if (j == cols-1 || n + rows >= cmdname_cnt)
108 size = 1;
109 printf("%-*s", size, cmdname[n]->name);
8e49d503
AE
110 }
111 putchar('\n');
112 }
113}
114
115static void list_commands(const char *exec_path, const char *pattern)
116{
7dbc2c04
JH
117 unsigned int longest = 0;
118 char path[PATH_MAX];
119 int dirlen;
120 DIR *dir = opendir(exec_path);
121 struct dirent *de;
122
123 if (!dir) {
124 fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
8e49d503
AE
125 exit(1);
126 }
127
7dbc2c04
JH
128 dirlen = strlen(exec_path);
129 if (PATH_MAX - 20 < dirlen) {
130 fprintf(stderr, "git: insanely long exec-path '%s'\n",
131 exec_path);
8e49d503
AE
132 exit(1);
133 }
134
7dbc2c04
JH
135 memcpy(path, exec_path, dirlen);
136 path[dirlen++] = '/';
137
138 while ((de = readdir(dir)) != NULL) {
139 struct stat st;
140 int entlen;
8e49d503 141
7dbc2c04
JH
142 if (strncmp(de->d_name, "git-", 4))
143 continue;
144 strcpy(path+dirlen, de->d_name);
145 if (stat(path, &st) || /* stat, not lstat */
146 !S_ISREG(st.st_mode) ||
147 !(st.st_mode & S_IXUSR))
8e49d503
AE
148 continue;
149
7dbc2c04 150 entlen = strlen(de->d_name);
f9039f30
JH
151 if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
152 entlen -= 4;
8e49d503 153
7dbc2c04
JH
154 if (longest < entlen)
155 longest = entlen;
156
157 add_cmdname(de->d_name + 4, entlen-4);
8e49d503 158 }
7dbc2c04 159 closedir(dir);
8e49d503
AE
160
161 printf("git commands available in '%s'\n", exec_path);
162 printf("----------------------------");
163 mput_char('-', strlen(exec_path));
164 putchar('\n');
7dbc2c04 165 pretty_print_string_list(cmdname, longest - 4);
8e49d503
AE
166 putchar('\n');
167}
168
169#ifdef __GNUC__
4050c0df 170static void cmd_usage(const char *exec_path, const char *fmt, ...)
8e49d503
AE
171 __attribute__((__format__(__printf__, 2, 3), __noreturn__));
172#endif
4050c0df 173static void cmd_usage(const char *exec_path, const char *fmt, ...)
8e49d503
AE
174{
175 if (fmt) {
176 va_list ap;
177
178 va_start(ap, fmt);
179 printf("git: ");
180 vprintf(fmt, ap);
181 va_end(ap);
182 putchar('\n');
183 }
184 else
185 puts(git_usage);
186
187 putchar('\n');
188
189 if(exec_path)
190 list_commands(exec_path, "git-*");
191
192 exit(1);
193}
194
195static void prepend_to_path(const char *dir, int len)
196{
197 char *path, *old_path = getenv("PATH");
198 int path_len = len;
199
200 if (!old_path)
7dbc2c04 201 old_path = "/usr/local/bin:/usr/bin:/bin";
8e49d503
AE
202
203 path_len = len + strlen(old_path) + 1;
204
205 path = malloc(path_len + 1);
8e49d503
AE
206
207 memcpy(path, dir, len);
208 path[len] = ':';
209 memcpy(path + len + 1, old_path, path_len - len);
210
211 setenv("PATH", path, 1);
212}
213
97fc6c5f
AE
214static void show_man_page(char *git_cmd)
215{
216 char *page;
217
218 if (!strncmp(git_cmd, "git", 3))
219 page = git_cmd;
220 else {
221 int page_len = strlen(git_cmd) + 4;
222
223 page = malloc(page_len + 1);
224 strcpy(page, "git-");
225 strcpy(page + 4, git_cmd);
226 page[page_len] = 0;
227 }
228
7dbc2c04 229 execlp("man", "man", page, NULL);
97fc6c5f
AE
230}
231
8e49d503
AE
232int main(int argc, char **argv, char **envp)
233{
234 char git_command[PATH_MAX + 1];
235 char wd[PATH_MAX + 1];
236 int i, len, show_help = 0;
237 char *exec_path = getenv("GIT_EXEC_PATH");
238
239 getcwd(wd, PATH_MAX);
240
241 if (!exec_path)
242 exec_path = GIT_EXEC_PATH;
243
244 for (i = 1; i < argc; i++) {
245 char *arg = argv[i];
246
da6bf70e
AE
247 if (!strcmp(arg, "help")) {
248 show_help = 1;
249 continue;
250 }
251
8e49d503
AE
252 if (strncmp(arg, "--", 2))
253 break;
254
255 arg += 2;
256
257 if (!strncmp(arg, "exec-path", 9)) {
258 arg += 9;
259 if (*arg == '=')
260 exec_path = arg + 1;
261 else {
262 puts(exec_path);
263 exit(0);
264 }
265 }
266 else if (!strcmp(arg, "version")) {
267 printf("git version %s\n", GIT_VERSION);
268 exit(0);
269 }
270 else if (!strcmp(arg, "help"))
271 show_help = 1;
272 else if (!show_help)
4050c0df 273 cmd_usage(NULL, NULL);
8e49d503
AE
274 }
275
97fc6c5f
AE
276 if (i >= argc || show_help) {
277 if (i >= argc)
4050c0df 278 cmd_usage(exec_path, NULL);
97fc6c5f
AE
279
280 show_man_page(argv[i]);
281 }
8e49d503 282
7dbc2c04
JH
283 if (*exec_path != '/') {
284 if (!getcwd(git_command, sizeof(git_command))) {
285 fprintf(stderr,
7246ed43 286 "git: cannot determine current directory\n");
7dbc2c04
JH
287 exit(1);
288 }
289 len = strlen(git_command);
8e49d503 290
7dbc2c04
JH
291 /* Trivial cleanup */
292 while (!strncmp(exec_path, "./", 2)) {
293 exec_path += 2;
294 while (*exec_path == '/')
874fbc33 295 exec_path++;
7dbc2c04
JH
296 }
297 snprintf(git_command + len, sizeof(git_command) - len,
298 "/%s", exec_path);
299 }
300 else
301 strcpy(git_command, exec_path);
8e49d503
AE
302 len = strlen(git_command);
303 prepend_to_path(git_command, len);
304
10b15b86
AR
305 len += snprintf(git_command + len, sizeof(git_command) - len,
306 "/git-%s", argv[i]);
307 if (sizeof(git_command) <= len) {
b270c634 308 fprintf(stderr, "git: command name given is too long.\n");
10b15b86
AR
309 exit(1);
310 }
8e49d503
AE
311
312 /* execve() can only ever return if it fails */
313 execve(git_command, &argv[i], envp);
10b15b86
AR
314
315 if (errno == ENOENT)
4050c0df 316 cmd_usage(exec_path, "'%s' is not a git-command", argv[i]);
10b15b86
AR
317
318 fprintf(stderr, "Failed to run command '%s': %s\n",
319 git_command, strerror(errno));
8e49d503
AE
320
321 return 1;
322}