11 #include <sys/ioctl.h>
12 #include "git-compat-util.h"
15 # define PATH_MAX 4096
18 static const char git_usage
[] =
19 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
21 /* most gui terms set COLUMNS (although some don't export it) */
22 static int term_columns(void)
24 char *col_string
= getenv("COLUMNS");
27 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
33 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
45 fprintf(stderr
, "git: out of memory\n");
49 static inline void mput_char(char c
, unsigned int num
)
55 static struct cmdname
{
59 static int cmdname_alloc
, cmdname_cnt
;
61 static void add_cmdname(const char *name
, int len
)
64 if (cmdname_alloc
<= cmdname_cnt
) {
65 cmdname_alloc
= cmdname_alloc
+ 200;
66 cmdname
= realloc(cmdname
, cmdname_alloc
* sizeof(*cmdname
));
70 ent
= malloc(sizeof(*ent
) + len
);
74 memcpy(ent
->name
, name
, len
);
76 cmdname
[cmdname_cnt
++] = ent
;
79 static int cmdname_compare(const void *a_
, const void *b_
)
81 struct cmdname
*a
= *(struct cmdname
**)a_
;
82 struct cmdname
*b
= *(struct cmdname
**)b_
;
83 return strcmp(a
->name
, b
->name
);
86 static void pretty_print_string_list(struct cmdname
**cmdname
, int longest
)
89 int space
= longest
+ 1; /* min 1 SP between words */
90 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
94 cols
= max_cols
/ space
;
95 rows
= (cmdname_cnt
+ cols
- 1) / cols
;
97 qsort(cmdname
, cmdname_cnt
, sizeof(*cmdname
), cmdname_compare
);
99 for (i
= 0; i
< rows
; i
++) {
102 for (j
= 0; j
< cols
; j
++) {
103 int n
= j
* rows
+ i
;
105 if (n
>= cmdname_cnt
)
107 if (j
== cols
-1 || n
+ rows
>= cmdname_cnt
)
109 printf("%-*s", size
, cmdname
[n
]->name
);
115 static void list_commands(const char *exec_path
, const char *pattern
)
117 unsigned int longest
= 0;
120 DIR *dir
= opendir(exec_path
);
124 fprintf(stderr
, "git: '%s': %s\n", exec_path
, strerror(errno
));
128 dirlen
= strlen(exec_path
);
129 if (PATH_MAX
- 20 < dirlen
) {
130 fprintf(stderr
, "git: insanely long exec-path '%s'\n",
135 memcpy(path
, exec_path
, dirlen
);
136 path
[dirlen
++] = '/';
138 while ((de
= readdir(dir
)) != NULL
) {
142 if (strncmp(de
->d_name
, "git-", 4))
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
))
150 entlen
= strlen(de
->d_name
);
151 if (4 < entlen
&& !strcmp(de
->d_name
+ entlen
- 4, ".exe"))
154 if (longest
< entlen
)
157 add_cmdname(de
->d_name
+ 4, entlen
-4);
161 printf("git commands available in '%s'\n", exec_path
);
162 printf("----------------------------");
163 mput_char('-', strlen(exec_path
));
165 pretty_print_string_list(cmdname
, longest
- 4);
170 static void cmd_usage(const char *exec_path
, const char *fmt
, ...)
171 __attribute__((__format__(__printf__
, 2, 3), __noreturn__
));
173 static void cmd_usage(const char *exec_path
, const char *fmt
, ...)
190 list_commands(exec_path
, "git-*");
195 static void prepend_to_path(const char *dir
, int len
)
197 char *path
, *old_path
= getenv("PATH");
201 old_path
= "/usr/local/bin:/usr/bin:/bin";
203 path_len
= len
+ strlen(old_path
) + 1;
205 path
= malloc(path_len
+ 1);
207 memcpy(path
, dir
, len
);
209 memcpy(path
+ len
+ 1, old_path
, path_len
- len
);
211 setenv("PATH", path
, 1);
214 static void show_man_page(char *git_cmd
)
218 if (!strncmp(git_cmd
, "git", 3))
221 int page_len
= strlen(git_cmd
) + 4;
223 page
= malloc(page_len
+ 1);
224 strcpy(page
, "git-");
225 strcpy(page
+ 4, git_cmd
);
229 execlp("man", "man", page
, NULL
);
232 int main(int argc
, char **argv
, char **envp
)
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");
239 getcwd(wd
, PATH_MAX
);
242 exec_path
= GIT_EXEC_PATH
;
244 for (i
= 1; i
< argc
; i
++) {
247 if (strncmp(arg
, "--", 2))
252 if (!strncmp(arg
, "exec-path", 9)) {
261 else if (!strcmp(arg
, "version")) {
262 printf("git version %s\n", GIT_VERSION
);
265 else if (!strcmp(arg
, "help"))
268 cmd_usage(NULL
, NULL
);
271 if (i
>= argc
|| show_help
) {
273 cmd_usage(exec_path
, NULL
);
275 show_man_page(argv
[i
]);
278 if (*exec_path
!= '/') {
279 if (!getcwd(git_command
, sizeof(git_command
))) {
281 "git: cannot determine current directory\n");
284 len
= strlen(git_command
);
286 /* Trivial cleanup */
287 while (!strncmp(exec_path
, "./", 2)) {
289 while (*exec_path
== '/')
292 snprintf(git_command
+ len
, sizeof(git_command
) - len
,
296 strcpy(git_command
, exec_path
);
297 len
= strlen(git_command
);
298 prepend_to_path(git_command
, len
);
300 len
+= snprintf(git_command
+ len
, sizeof(git_command
) - len
,
302 if (sizeof(git_command
) <= len
) {
303 fprintf(stderr
, "git: command name given is too long.\n");
307 /* execve() can only ever return if it fails */
308 execve(git_command
, &argv
[i
], envp
);
311 cmd_usage(exec_path
, "'%s' is not a git-command", argv
[i
]);
313 fprintf(stderr
, "Failed to run command '%s': %s\n",
314 git_command
, strerror(errno
));