11 #include "git-compat-util.h"
14 # define PATH_MAX 4096
17 static const char git_usage
[] =
18 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
20 /* most gui terms set COLUMNS (although some don't export it) */
21 static int term_columns(void)
23 char *col_string
= getenv("COLUMNS");
26 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
34 fprintf(stderr
, "git: out of memory\n");
38 static inline void mput_char(char c
, unsigned int num
)
44 static struct cmdname
{
48 static int cmdname_alloc
, cmdname_cnt
;
50 static void add_cmdname(const char *name
, int len
)
53 if (cmdname_alloc
<= cmdname_cnt
) {
54 cmdname_alloc
= cmdname_alloc
+ 200;
55 cmdname
= realloc(cmdname
, cmdname_alloc
* sizeof(*cmdname
));
59 ent
= malloc(sizeof(*ent
) + len
);
63 memcpy(ent
->name
, name
, len
);
65 cmdname
[cmdname_cnt
++] = ent
;
68 static int cmdname_compare(const void *a_
, const void *b_
)
70 struct cmdname
*a
= *(struct cmdname
**)a_
;
71 struct cmdname
*b
= *(struct cmdname
**)b_
;
72 return strcmp(a
->name
, b
->name
);
75 static void pretty_print_string_list(struct cmdname
**cmdname
, int longest
)
78 int space
= longest
+ 1; /* min 1 SP between words */
79 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
83 cols
= max_cols
/ space
;
85 qsort(cmdname
, cmdname_cnt
, sizeof(*cmdname
), cmdname_compare
);
87 for (i
= 0; i
< cmdname_cnt
; ) {
91 for (c
= cols
; c
&& i
< cmdname_cnt
; i
++) {
92 printf("%s", cmdname
[i
]->name
);
95 mput_char(' ', space
- cmdname
[i
]->len
);
101 static void list_commands(const char *exec_path
, const char *pattern
)
103 unsigned int longest
= 0;
106 DIR *dir
= opendir(exec_path
);
110 fprintf(stderr
, "git: '%s': %s\n", exec_path
, strerror(errno
));
114 dirlen
= strlen(exec_path
);
115 if (PATH_MAX
- 20 < dirlen
) {
116 fprintf(stderr
, "git: insanely long exec-path '%s'\n",
121 memcpy(path
, exec_path
, dirlen
);
122 path
[dirlen
++] = '/';
124 while ((de
= readdir(dir
)) != NULL
) {
128 if (strncmp(de
->d_name
, "git-", 4))
130 strcpy(path
+dirlen
, de
->d_name
);
131 if (stat(path
, &st
) || /* stat, not lstat */
132 !S_ISREG(st
.st_mode
) ||
133 !(st
.st_mode
& S_IXUSR
))
136 entlen
= strlen(de
->d_name
);
137 if (4 < entlen
&& !strcmp(de
->d_name
+ entlen
- 4, ".exe"))
140 if (longest
< entlen
)
143 add_cmdname(de
->d_name
+ 4, entlen
-4);
147 printf("git commands available in '%s'\n", exec_path
);
148 printf("----------------------------");
149 mput_char('-', strlen(exec_path
));
151 pretty_print_string_list(cmdname
, longest
- 4);
156 static void cmd_usage(const char *exec_path
, const char *fmt
, ...)
157 __attribute__((__format__(__printf__
, 2, 3), __noreturn__
));
159 static void cmd_usage(const char *exec_path
, const char *fmt
, ...)
176 list_commands(exec_path
, "git-*");
181 static void prepend_to_path(const char *dir
, int len
)
183 char *path
, *old_path
= getenv("PATH");
187 old_path
= "/usr/local/bin:/usr/bin:/bin";
189 path_len
= len
+ strlen(old_path
) + 1;
191 path
= malloc(path_len
+ 1);
193 memcpy(path
, dir
, len
);
195 memcpy(path
+ len
+ 1, old_path
, path_len
- len
);
197 setenv("PATH", path
, 1);
200 static void show_man_page(char *git_cmd
)
204 if (!strncmp(git_cmd
, "git", 3))
207 int page_len
= strlen(git_cmd
) + 4;
209 page
= malloc(page_len
+ 1);
210 strcpy(page
, "git-");
211 strcpy(page
+ 4, git_cmd
);
215 execlp("man", "man", page
, NULL
);
218 int main(int argc
, char **argv
, char **envp
)
220 char git_command
[PATH_MAX
+ 1];
221 char wd
[PATH_MAX
+ 1];
222 int i
, len
, show_help
= 0;
223 char *exec_path
= getenv("GIT_EXEC_PATH");
225 getcwd(wd
, PATH_MAX
);
228 exec_path
= GIT_EXEC_PATH
;
230 for (i
= 1; i
< argc
; i
++) {
233 if (strncmp(arg
, "--", 2))
238 if (!strncmp(arg
, "exec-path", 9)) {
247 else if (!strcmp(arg
, "version")) {
248 printf("git version %s\n", GIT_VERSION
);
251 else if (!strcmp(arg
, "help"))
254 cmd_usage(NULL
, NULL
);
257 if (i
>= argc
|| show_help
) {
259 cmd_usage(exec_path
, NULL
);
261 show_man_page(argv
[i
]);
264 if (*exec_path
!= '/') {
265 if (!getcwd(git_command
, sizeof(git_command
))) {
267 "git: cannot determine current directory");
270 len
= strlen(git_command
);
272 /* Trivial cleanup */
273 while (!strncmp(exec_path
, "./", 2)) {
275 while (*exec_path
== '/')
278 snprintf(git_command
+ len
, sizeof(git_command
) - len
,
282 strcpy(git_command
, exec_path
);
283 len
= strlen(git_command
);
284 prepend_to_path(git_command
, len
);
286 len
+= snprintf(git_command
+ len
, sizeof(git_command
) - len
,
288 if (sizeof(git_command
) <= len
) {
289 fprintf(stderr
, "git: command name given is too long.\n");
293 /* execve() can only ever return if it fails */
294 execve(git_command
, &argv
[i
], envp
);
297 cmd_usage(exec_path
, "'%s' is not a git-command", argv
[i
]);
299 fprintf(stderr
, "Failed to run command '%s': %s\n",
300 git_command
, strerror(errno
));