4 * Builtin help-related commands (help, usage, version)
9 #include "common-cmds.h"
11 static const char *help_default_format;
13 static enum help_format {
17 } help_format = man_format;
19 static void parse_help_format(const char *format)
22 help_format = man_format;
25 if (!strcmp(format, "man")) {
26 help_format = man_format;
29 if (!strcmp(format, "info")) {
30 help_format = info_format;
33 if (!strcmp(format, "web") || !strcmp(format, "html")) {
34 help_format = web_format;
37 die("unrecognized help format '%s'", format);
40 static int git_help_config(const char *var, const char *value)
42 if (!strcmp(var, "help.format"))
43 return git_config_string(&help_default_format, var, value);
44 return git_default_config(var, value);
47 /* most GUI terminals set COLUMNS (although some don't export it) */
48 static int term_columns(void)
50 char *col_string = getenv("COLUMNS");
53 if (col_string && (n_cols = atoi(col_string)) > 0)
59 if (!ioctl(1, TIOCGWINSZ, &ws)) {
69 static inline void mput_char(char c, unsigned int num)
75 static struct cmdnames {
82 } main_cmds, other_cmds;
84 static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
86 struct cmdname *ent = xmalloc(sizeof(*ent) + len);
89 memcpy(ent->name, name, len);
92 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
93 cmds->names[cmds->cnt++] = ent;
96 static int cmdname_compare(const void *a_, const void *b_)
98 struct cmdname *a = *(struct cmdname **)a_;
99 struct cmdname *b = *(struct cmdname **)b_;
100 return strcmp(a->name, b->name);
103 static void uniq(struct cmdnames *cmds)
110 for (i = j = 1; i < cmds->cnt; i++)
111 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
112 cmds->names[j++] = cmds->names[i];
117 static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
123 while (ci < cmds->cnt && ei < excludes->cnt) {
124 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
126 cmds->names[cj++] = cmds->names[ci++];
133 while (ci < cmds->cnt)
134 cmds->names[cj++] = cmds->names[ci++];
139 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
142 int space = longest + 1; /* min 1 SP between words */
143 int max_cols = term_columns() - 1; /* don't print *on* the edge */
146 if (space < max_cols)
147 cols = max_cols / space;
148 rows = (cmds->cnt + cols - 1) / cols;
150 for (i = 0; i < rows; i++) {
153 for (j = 0; j < cols; j++) {
154 int n = j * rows + i;
158 if (j == cols-1 || n + rows >= cmds->cnt)
160 printf("%-*s", size, cmds->names[n]->name);
166 static unsigned int list_commands_in_dir(struct cmdnames *cmds,
169 unsigned int longest = 0;
170 const char *prefix = "git-";
171 int prefix_len = strlen(prefix);
172 DIR *dir = opendir(path);
175 if (!dir || chdir(path))
178 while ((de = readdir(dir)) != NULL) {
182 if (prefixcmp(de->d_name, prefix))
185 if (stat(de->d_name, &st) || /* stat, not lstat */
186 !S_ISREG(st.st_mode) ||
187 !(st.st_mode & S_IXUSR))
190 entlen = strlen(de->d_name) - prefix_len;
191 if (has_extension(de->d_name, ".exe"))
194 if (longest < entlen)
197 add_cmdname(cmds, de->d_name + prefix_len, entlen);
204 static void list_commands(void)
206 unsigned int longest = 0;
208 const char *env_path = getenv("PATH");
209 char *paths, *path, *colon;
210 const char *exec_path = git_exec_path();
213 longest = list_commands_in_dir(&main_cmds, exec_path);
216 fprintf(stderr, "PATH not set\n");
220 path = paths = xstrdup(env_path);
222 if ((colon = strchr(path, ':')))
225 len = list_commands_in_dir(&other_cmds, path);
235 qsort(main_cmds.names, main_cmds.cnt,
236 sizeof(*main_cmds.names), cmdname_compare);
239 qsort(other_cmds.names, other_cmds.cnt,
240 sizeof(*other_cmds.names), cmdname_compare);
242 exclude_cmds(&other_cmds, &main_cmds);
245 printf("available git commands in '%s'\n", exec_path);
246 printf("----------------------------");
247 mput_char('-', strlen(exec_path));
249 pretty_print_string_list(&main_cmds, longest);
253 if (other_cmds.cnt) {
254 printf("git commands available from elsewhere on your $PATH\n");
255 printf("---------------------------------------------------\n");
256 pretty_print_string_list(&other_cmds, longest);
261 void list_common_cmds_help(void)
265 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
266 if (longest < strlen(common_cmds[i].name))
267 longest = strlen(common_cmds[i].name);
270 puts("The most commonly used git commands are:");
271 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
272 printf(" %s ", common_cmds[i].name);
273 mput_char(' ', longest - strlen(common_cmds[i].name));
274 puts(common_cmds[i].help);
278 static const char *cmd_to_page(const char *git_cmd)
282 else if (!prefixcmp(git_cmd, "git"))
285 int page_len = strlen(git_cmd) + 4;
286 char *p = xmalloc(page_len + 1);
288 strcpy(p + 4, git_cmd);
294 static void setup_man_path(void)
296 struct strbuf new_path;
297 const char *old_path = getenv("MANPATH");
299 strbuf_init(&new_path, 0);
301 /* We should always put ':' after our path. If there is no
302 * old_path, the ':' at the end will let 'man' to try
303 * system-wide paths after ours to find the manual page. If
304 * there is old_path, we need ':' as delimiter. */
305 strbuf_addstr(&new_path, GIT_MAN_PATH);
306 strbuf_addch(&new_path, ':');
308 strbuf_addstr(&new_path, old_path);
310 setenv("MANPATH", new_path.buf, 1);
312 strbuf_release(&new_path);
315 static void show_man_page(const char *git_cmd)
317 const char *page = cmd_to_page(git_cmd);
319 execlp("man", "man", page, NULL);
322 static void show_info_page(const char *git_cmd)
324 const char *page = cmd_to_page(git_cmd);
325 setenv("INFOPATH", GIT_INFO_PATH, 1);
326 execlp("info", "info", "gitman", page, NULL);
329 static void get_html_page_path(struct strbuf *page_path, const char *page)
333 /* Check that we have a git documentation directory. */
334 if (stat(GIT_HTML_PATH "/git.html", &st) || !S_ISREG(st.st_mode))
335 die("'%s': not a documentation directory.", GIT_HTML_PATH);
337 strbuf_init(page_path, 0);
338 strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
341 static void show_html_page(const char *git_cmd)
343 const char *page = cmd_to_page(git_cmd);
344 struct strbuf page_path; /* it leaks but we exec bellow */
346 get_html_page_path(&page_path, page);
348 execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL);
351 void help_unknown_cmd(const char *cmd)
353 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
357 int cmd_version(int argc, const char **argv, const char *prefix)
359 printf("git version %s\n", git_version_string);
363 int cmd_help(int argc, const char **argv, const char *prefix)
365 const char *help_cmd = argv[1];
368 printf("usage: %s\n\n", git_usage_string);
369 list_common_cmds_help();
373 if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
374 printf("usage: %s\n\n", git_usage_string);
378 else if (!strcmp(help_cmd, "--web") || !strcmp(help_cmd, "-w")) {
379 show_html_page(argc > 2 ? argv[2] : NULL);
382 else if (!strcmp(help_cmd, "--info") || !strcmp(help_cmd, "-i")) {
383 show_info_page(argc > 2 ? argv[2] : NULL);
386 else if (!strcmp(help_cmd, "--man") || !strcmp(help_cmd, "-m")) {
387 show_man_page(argc > 2 ? argv[2] : NULL);
393 setup_git_directory_gently(&nongit);
394 git_config(git_help_config);
395 if (help_default_format)
396 parse_help_format(help_default_format);
398 switch (help_format) {
400 show_man_page(help_cmd);
403 show_info_page(help_cmd);
406 show_html_page(help_cmd);