4 * Builtin help-related commands (help, usage, version)
10 #include "common-cmds.h"
12 static const char git_usage
[] =
13 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
15 /* most GUI terminals set COLUMNS (although some don't export it) */
16 static int term_columns(void)
18 char *col_string
= getenv("COLUMNS");
21 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
27 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
39 fprintf(stderr
, "git: out of memory\n");
43 static inline void mput_char(char c
, unsigned int num
)
49 static struct cmdname
{
53 static int cmdname_alloc
, cmdname_cnt
;
55 static void add_cmdname(const char *name
, int len
)
58 if (cmdname_alloc
<= cmdname_cnt
) {
59 cmdname_alloc
= cmdname_alloc
+ 200;
60 cmdname
= realloc(cmdname
, cmdname_alloc
* sizeof(*cmdname
));
64 ent
= malloc(sizeof(*ent
) + len
);
68 memcpy(ent
->name
, name
, len
);
70 cmdname
[cmdname_cnt
++] = ent
;
73 static int cmdname_compare(const void *a_
, const void *b_
)
75 struct cmdname
*a
= *(struct cmdname
**)a_
;
76 struct cmdname
*b
= *(struct cmdname
**)b_
;
77 return strcmp(a
->name
, b
->name
);
80 static void pretty_print_string_list(struct cmdname
**cmdname
, int longest
)
83 int space
= longest
+ 1; /* min 1 SP between words */
84 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
88 cols
= max_cols
/ space
;
89 rows
= (cmdname_cnt
+ cols
- 1) / cols
;
91 qsort(cmdname
, cmdname_cnt
, sizeof(*cmdname
), cmdname_compare
);
93 for (i
= 0; i
< rows
; i
++) {
96 for (j
= 0; j
< cols
; j
++) {
101 if (j
== cols
-1 || n
+ rows
>= cmdname_cnt
)
103 printf("%-*s", size
, cmdname
[n
]->name
);
109 static void list_commands(const char *exec_path
, const char *pattern
)
111 unsigned int longest
= 0;
114 DIR *dir
= opendir(exec_path
);
118 fprintf(stderr
, "git: '%s': %s\n", exec_path
, strerror(errno
));
122 dirlen
= strlen(exec_path
);
123 if (PATH_MAX
- 20 < dirlen
) {
124 fprintf(stderr
, "git: insanely long exec-path '%s'\n",
129 memcpy(path
, exec_path
, dirlen
);
130 path
[dirlen
++] = '/';
132 while ((de
= readdir(dir
)) != NULL
) {
136 if (strncmp(de
->d_name
, "git-", 4))
138 strcpy(path
+dirlen
, de
->d_name
);
139 if (stat(path
, &st
) || /* stat, not lstat */
140 !S_ISREG(st
.st_mode
) ||
141 !(st
.st_mode
& S_IXUSR
))
144 entlen
= strlen(de
->d_name
);
145 if (4 < entlen
&& !strcmp(de
->d_name
+ entlen
- 4, ".exe"))
148 if (longest
< entlen
)
151 add_cmdname(de
->d_name
+ 4, entlen
-4);
155 printf("git commands available in '%s'\n", exec_path
);
156 printf("----------------------------");
157 mput_char('-', strlen(exec_path
));
159 pretty_print_string_list(cmdname
, longest
- 4);
163 static void list_common_cmds_help(void)
167 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
168 if (longest
< strlen(common_cmds
[i
].name
))
169 longest
= strlen(common_cmds
[i
].name
);
172 puts("The most commonly used git commands are:");
173 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
174 printf(" %s", common_cmds
[i
].name
);
175 mput_char(' ', longest
- strlen(common_cmds
[i
].name
) + 4);
176 puts(common_cmds
[i
].help
);
178 puts("(use 'git help -a' to get a list of all installed git commands)");
181 void cmd_usage(int show_all
, const char *exec_path
, const char *fmt
, ...)
198 list_commands(exec_path
, "git-*");
200 list_common_cmds_help();
206 static void show_man_page(const char *git_cmd
)
210 if (!strncmp(git_cmd
, "git", 3))
213 int page_len
= strlen(git_cmd
) + 4;
214 char *p
= malloc(page_len
+ 1);
216 strcpy(p
+ 4, git_cmd
);
221 execlp("man", "man", page
, NULL
);
224 int cmd_version(int argc
, const char **argv
, char **envp
)
226 printf("git version %s\n", git_version_string
);
230 int cmd_help(int argc
, const char **argv
, char **envp
)
232 const char *help_cmd
= argc
> 1 ? argv
[1] : NULL
;
234 cmd_usage(0, git_exec_path(), NULL
);
235 else if (!strcmp(help_cmd
, "--all") || !strcmp(help_cmd
, "-a"))
236 cmd_usage(1, git_exec_path(), NULL
);
238 show_man_page(help_cmd
);