11 #include <sys/ioctl.h>
12 #include "git-compat-util.h"
16 # define PATH_MAX 4096
19 static const char git_usage
[] =
20 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
22 /* most gui terms set COLUMNS (although some don't export it) */
23 static int term_columns(void)
25 char *col_string
= getenv("COLUMNS");
28 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
34 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
46 fprintf(stderr
, "git: out of memory\n");
50 static inline void mput_char(char c
, unsigned int num
)
56 static struct cmdname
{
60 static int cmdname_alloc
, cmdname_cnt
;
62 static void add_cmdname(const char *name
, int len
)
65 if (cmdname_alloc
<= cmdname_cnt
) {
66 cmdname_alloc
= cmdname_alloc
+ 200;
67 cmdname
= realloc(cmdname
, cmdname_alloc
* sizeof(*cmdname
));
71 ent
= malloc(sizeof(*ent
) + len
);
75 memcpy(ent
->name
, name
, len
);
77 cmdname
[cmdname_cnt
++] = ent
;
80 static int cmdname_compare(const void *a_
, const void *b_
)
82 struct cmdname
*a
= *(struct cmdname
**)a_
;
83 struct cmdname
*b
= *(struct cmdname
**)b_
;
84 return strcmp(a
->name
, b
->name
);
87 static void pretty_print_string_list(struct cmdname
**cmdname
, int longest
)
90 int space
= longest
+ 1; /* min 1 SP between words */
91 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
95 cols
= max_cols
/ space
;
96 rows
= (cmdname_cnt
+ cols
- 1) / cols
;
98 qsort(cmdname
, cmdname_cnt
, sizeof(*cmdname
), cmdname_compare
);
100 for (i
= 0; i
< rows
; i
++) {
103 for (j
= 0; j
< cols
; j
++) {
104 int n
= j
* rows
+ i
;
106 if (n
>= cmdname_cnt
)
108 if (j
== cols
-1 || n
+ rows
>= cmdname_cnt
)
110 printf("%-*s", size
, cmdname
[n
]->name
);
116 static void list_commands(const char *exec_path
, const char *pattern
)
118 unsigned int longest
= 0;
121 DIR *dir
= opendir(exec_path
);
125 fprintf(stderr
, "git: '%s': %s\n", exec_path
, strerror(errno
));
129 dirlen
= strlen(exec_path
);
130 if (PATH_MAX
- 20 < dirlen
) {
131 fprintf(stderr
, "git: insanely long exec-path '%s'\n",
136 memcpy(path
, exec_path
, dirlen
);
137 path
[dirlen
++] = '/';
139 while ((de
= readdir(dir
)) != NULL
) {
143 if (strncmp(de
->d_name
, "git-", 4))
145 strcpy(path
+dirlen
, de
->d_name
);
146 if (stat(path
, &st
) || /* stat, not lstat */
147 !S_ISREG(st
.st_mode
) ||
148 !(st
.st_mode
& S_IXUSR
))
151 entlen
= strlen(de
->d_name
);
152 if (4 < entlen
&& !strcmp(de
->d_name
+ entlen
- 4, ".exe"))
155 if (longest
< entlen
)
158 add_cmdname(de
->d_name
+ 4, entlen
-4);
162 printf("git commands available in '%s'\n", exec_path
);
163 printf("----------------------------");
164 mput_char('-', strlen(exec_path
));
166 pretty_print_string_list(cmdname
, longest
- 4);
171 static void cmd_usage(const char *exec_path
, const char *fmt
, ...)
172 __attribute__((__format__(__printf__
, 2, 3), __noreturn__
));
174 static void cmd_usage(const char *exec_path
, const char *fmt
, ...)
191 list_commands(exec_path
, "git-*");
196 static void prepend_to_path(const char *dir
, int len
)
198 char *path
, *old_path
= getenv("PATH");
202 old_path
= "/usr/local/bin:/usr/bin:/bin";
204 path_len
= len
+ strlen(old_path
) + 1;
206 path
= malloc(path_len
+ 1);
208 memcpy(path
, dir
, len
);
210 memcpy(path
+ len
+ 1, old_path
, path_len
- len
);
212 setenv("PATH", path
, 1);
215 static void show_man_page(char *git_cmd
)
219 if (!strncmp(git_cmd
, "git", 3))
222 int page_len
= strlen(git_cmd
) + 4;
224 page
= malloc(page_len
+ 1);
225 strcpy(page
, "git-");
226 strcpy(page
+ 4, git_cmd
);
230 execlp("man", "man", page
, NULL
);
233 int main(int argc
, char **argv
, char **envp
)
235 char git_command
[PATH_MAX
+ 1];
236 char wd
[PATH_MAX
+ 1];
237 int i
, show_help
= 0;
238 const char *exec_path
;
240 getcwd(wd
, PATH_MAX
);
242 for (i
= 1; i
< argc
; i
++) {
245 if (!strcmp(arg
, "help")) {
250 if (strncmp(arg
, "--", 2))
255 if (!strncmp(arg
, "exec-path", 9)) {
259 git_set_exec_path(exec_path
);
261 puts(git_exec_path());
265 else if (!strcmp(arg
, "version")) {
266 printf("git version %s\n", GIT_VERSION
);
269 else if (!strcmp(arg
, "help"))
272 cmd_usage(NULL
, NULL
);
275 if (i
>= argc
|| show_help
) {
277 cmd_usage(git_exec_path(), NULL
);
279 show_man_page(argv
[i
]);
282 exec_path
= git_exec_path();
283 prepend_to_path(exec_path
, strlen(exec_path
));
285 execv_git_cmd(argv
+ i
);
288 cmd_usage(exec_path
, "'%s' is not a git-command", argv
[i
]);
290 fprintf(stderr
, "Failed to run command '%s': %s\n",
291 git_command
, strerror(errno
));