13 # define PATH_MAX 4096
17 extern int gitsetenv(const char *, const char *, int);
20 static const char git_usage[] =
21 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
23 /* most gui terms set COLUMNS (although some don't export it) */
24 static int term_columns(void)
26 char *col_string = getenv("COLUMNS");
29 if (col_string && (n_cols = atoi(col_string)) > 0)
37 fprintf(stderr, "git: out of memory\n");
41 static inline void mput_char(char c, unsigned int num)
47 static struct cmdname {
51 static int cmdname_alloc, cmdname_cnt;
53 static void add_cmdname(const char *name, int len)
56 if (cmdname_alloc <= cmdname_cnt) {
57 cmdname_alloc = cmdname_alloc + 200;
58 cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
62 ent = malloc(sizeof(*ent) + len);
66 memcpy(ent->name, name, len);
68 cmdname[cmdname_cnt++] = ent;
71 static int cmdname_compare(const void *a_, const void *b_)
73 struct cmdname *a = *(struct cmdname **)a_;
74 struct cmdname *b = *(struct cmdname **)b_;
75 return strcmp(a->name, b->name);
78 static void pretty_print_string_list(struct cmdname **cmdname, int longest)
81 int space = longest + 1; /* min 1 SP between words */
82 int max_cols = term_columns() - 1; /* don't print *on* the edge */
86 cols = max_cols / space;
88 qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
90 for (i = 0; i < cmdname_cnt; ) {
94 for (c = cols; c && i < cmdname_cnt; i++) {
95 printf("%s", cmdname[i]->name);
98 mput_char(' ', space - cmdname[i]->len);
104 static void list_commands(const char *exec_path, const char *pattern)
106 unsigned int longest = 0;
109 DIR *dir = opendir(exec_path);
113 fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
117 dirlen = strlen(exec_path);
118 if (PATH_MAX - 20 < dirlen) {
119 fprintf(stderr, "git: insanely long exec-path '%s'\n",
124 memcpy(path, exec_path, dirlen);
125 path[dirlen++] = '/';
127 while ((de = readdir(dir)) != NULL) {
131 if (strncmp(de->d_name, "git-", 4))
133 strcpy(path+dirlen, de->d_name);
134 if (stat(path, &st) || /* stat, not lstat */
135 !S_ISREG(st.st_mode) ||
136 !(st.st_mode & S_IXUSR))
139 entlen = strlen(de->d_name);
140 if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
143 if (longest < entlen)
146 add_cmdname(de->d_name + 4, entlen-4);
150 printf("git commands available in '%s'\n", exec_path);
151 printf("----------------------------");
152 mput_char('-', strlen(exec_path));
154 pretty_print_string_list(cmdname, longest - 4);
159 static void usage(const char *exec_path, const char *fmt, ...)
160 __attribute__((__format__(__printf__, 2, 3), __noreturn__));
162 static void usage(const char *exec_path, const char *fmt, ...)
179 list_commands(exec_path, "git-*");
184 static void prepend_to_path(const char *dir, int len)
186 char *path, *old_path = getenv("PATH");
190 old_path = "/usr/local/bin:/usr/bin:/bin";
192 path_len = len + strlen(old_path) + 1;
194 path = malloc(path_len + 1);
196 memcpy(path, dir, len);
198 memcpy(path + len + 1, old_path, path_len - len);
200 setenv("PATH", path, 1);
203 static void show_man_page(char *git_cmd)
207 if (!strncmp(git_cmd, "git", 3))
210 int page_len = strlen(git_cmd) + 4;
212 page = malloc(page_len + 1);
213 strcpy(page, "git-");
214 strcpy(page + 4, git_cmd);
218 execlp("man", "man", page, NULL);
221 int main(int argc, char **argv, char **envp)
223 char git_command[PATH_MAX + 1];
224 char wd[PATH_MAX + 1];
225 int i, len, show_help = 0;
226 char *exec_path = getenv("GIT_EXEC_PATH");
228 getcwd(wd, PATH_MAX);
231 exec_path = GIT_EXEC_PATH;
233 for (i = 1; i < argc; i++) {
236 if (strncmp(arg, "--", 2))
241 if (!strncmp(arg, "exec-path", 9)) {
250 else if (!strcmp(arg, "version")) {
251 printf("git version %s\n", GIT_VERSION);
254 else if (!strcmp(arg, "help"))
260 if (i >= argc || show_help) {
262 usage(exec_path, NULL);
264 show_man_page(argv[i]);
267 if (*exec_path != '/') {
268 if (!getcwd(git_command, sizeof(git_command))) {
270 "git: cannot determine current directory");
273 len = strlen(git_command);
275 /* Trivial cleanup */
276 while (!strncmp(exec_path, "./", 2)) {
278 while (*exec_path == '/')
281 snprintf(git_command + len, sizeof(git_command) - len,
285 strcpy(git_command, exec_path);
286 len = strlen(git_command);
287 prepend_to_path(git_command, len);
289 len += snprintf(git_command + len, sizeof(git_command) - len,
291 if (sizeof(git_command) <= len) {
292 fprintf(stderr, "git: command name given is too long (%d)\n", len);
296 /* execve() can only ever return if it fails */
297 execve(git_command, &argv[i], envp);
300 usage(exec_path, "'%s' is not a git-command", argv[i]);
302 fprintf(stderr, "Failed to run command '%s': %s\n",
303 git_command, strerror(errno));