13 # define PATH_MAX 4096
16 static const char git_usage
[] =
17 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
19 /* most gui terms set COLUMNS (although some don't export it) */
20 static int term_columns(void)
22 char *col_string
= getenv("COLUMNS");
25 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
33 fprintf(stderr
, "git: out of memory\n");
37 static inline void mput_char(char c
, unsigned int num
)
43 static struct cmdname
{
47 static int cmdname_alloc
, cmdname_cnt
;
49 static void add_cmdname(const char *name
, int len
)
52 if (cmdname_alloc
<= cmdname_cnt
) {
53 cmdname_alloc
= cmdname_alloc
+ 200;
54 cmdname
= realloc(cmdname
, cmdname_alloc
* sizeof(*cmdname
));
58 ent
= malloc(sizeof(*ent
) + len
);
62 memcpy(ent
->name
, name
, len
+1);
63 cmdname
[cmdname_cnt
++] = ent
;
66 static int cmdname_compare(const void *a_
, const void *b_
)
68 struct cmdname
*a
= *(struct cmdname
**)a_
;
69 struct cmdname
*b
= *(struct cmdname
**)b_
;
70 return strcmp(a
->name
, b
->name
);
73 static void pretty_print_string_list(struct cmdname
**cmdname
, int longest
)
76 int space
= longest
+ 1; /* min 1 SP between words */
77 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
81 cols
= max_cols
/ space
;
83 qsort(cmdname
, cmdname_cnt
, sizeof(*cmdname
), cmdname_compare
);
85 for (i
= 0; i
< cmdname_cnt
; ) {
89 for (c
= cols
; c
&& i
< cmdname_cnt
; i
++) {
90 printf("%s", cmdname
[i
]->name
);
93 mput_char(' ', space
- cmdname
[i
]->len
);
99 static void list_commands(const char *exec_path
, const char *pattern
)
101 unsigned int longest
= 0;
104 DIR *dir
= opendir(exec_path
);
108 fprintf(stderr
, "git: '%s': %s\n", exec_path
, strerror(errno
));
112 dirlen
= strlen(exec_path
);
113 if (PATH_MAX
- 20 < dirlen
) {
114 fprintf(stderr
, "git: insanely long exec-path '%s'\n",
119 memcpy(path
, exec_path
, dirlen
);
120 path
[dirlen
++] = '/';
122 while ((de
= readdir(dir
)) != NULL
) {
126 if (strncmp(de
->d_name
, "git-", 4))
128 strcpy(path
+dirlen
, de
->d_name
);
129 if (stat(path
, &st
) || /* stat, not lstat */
130 !S_ISREG(st
.st_mode
) ||
131 !(st
.st_mode
& S_IXUSR
))
134 entlen
= strlen(de
->d_name
);
136 if (longest
< entlen
)
139 add_cmdname(de
->d_name
+ 4, entlen
-4);
143 printf("git commands available in '%s'\n", exec_path
);
144 printf("----------------------------");
145 mput_char('-', strlen(exec_path
));
147 pretty_print_string_list(cmdname
, longest
- 4);
152 static void usage(const char *exec_path
, const char *fmt
, ...)
153 __attribute__((__format__(__printf__
, 2, 3), __noreturn__
));
155 static void usage(const char *exec_path
, const char *fmt
, ...)
172 list_commands(exec_path
, "git-*");
177 static void prepend_to_path(const char *dir
, int len
)
179 char *path
, *old_path
= getenv("PATH");
183 old_path
= "/usr/local/bin:/usr/bin:/bin";
185 path_len
= len
+ strlen(old_path
) + 1;
187 path
= malloc(path_len
+ 1);
188 path
[path_len
+ 1] = '\0';
190 memcpy(path
, dir
, len
);
192 memcpy(path
+ len
+ 1, old_path
, path_len
- len
);
194 setenv("PATH", path
, 1);
197 static void show_man_page(char *git_cmd
)
201 if (!strncmp(git_cmd
, "git", 3))
204 int page_len
= strlen(git_cmd
) + 4;
206 page
= malloc(page_len
+ 1);
207 strcpy(page
, "git-");
208 strcpy(page
+ 4, git_cmd
);
212 execlp("man", "man", page
, NULL
);
215 int main(int argc
, char **argv
, char **envp
)
217 char git_command
[PATH_MAX
+ 1];
218 char wd
[PATH_MAX
+ 1];
219 int i
, len
, show_help
= 0;
220 char *exec_path
= getenv("GIT_EXEC_PATH");
222 getcwd(wd
, PATH_MAX
);
225 exec_path
= GIT_EXEC_PATH
;
227 for (i
= 1; i
< argc
; i
++) {
230 if (strncmp(arg
, "--", 2))
235 if (!strncmp(arg
, "exec-path", 9)) {
244 else if (!strcmp(arg
, "version")) {
245 printf("git version %s\n", GIT_VERSION
);
248 else if (!strcmp(arg
, "help"))
254 if (i
>= argc
|| show_help
) {
256 usage(exec_path
, NULL
);
258 show_man_page(argv
[i
]);
261 if (*exec_path
!= '/') {
262 if (!getcwd(git_command
, sizeof(git_command
))) {
264 "git: cannot determine current directory");
267 len
= strlen(git_command
);
269 /* Trivial cleanup */
270 while (!strncmp(exec_path
, "./", 2)) {
272 while (*exec_path
== '/')
275 snprintf(git_command
+ len
, sizeof(git_command
) - len
,
279 strcpy(git_command
, exec_path
);
280 len
= strlen(git_command
);
281 prepend_to_path(git_command
, len
);
283 strncat(&git_command
[len
], "/git-", sizeof(git_command
) - len
);
285 strncat(&git_command
[len
], argv
[i
], sizeof(git_command
) - len
);
287 if (access(git_command
, X_OK
))
288 usage(exec_path
, "'%s' is not a git-command", argv
[i
]);
290 /* execve() can only ever return if it fails */
291 execve(git_command
, &argv
[i
], envp
);
292 printf("Failed to run command '%s': %s\n", git_command
, strerror(errno
));