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")) {
44 return config_error_nonbool(var);
45 help_default_format = xstrdup(value);
48 return git_default_config(var, value);
51 /* most GUI terminals set COLUMNS (although some don't export it) */
52 static int term_columns(void)
54 char *col_string = getenv("COLUMNS");
57 if (col_string && (n_cols = atoi(col_string)) > 0)
63 if (!ioctl(1, TIOCGWINSZ, &ws)) {
73 static inline void mput_char(char c, unsigned int num)
79 static struct cmdnames {
86 } main_cmds, other_cmds;
88 static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
90 struct cmdname *ent = xmalloc(sizeof(*ent) + len);
93 memcpy(ent->name, name, len);
96 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
97 cmds->names[cmds->cnt++] = ent;
100 static int cmdname_compare(const void *a_, const void *b_)
102 struct cmdname *a = *(struct cmdname **)a_;
103 struct cmdname *b = *(struct cmdname **)b_;
104 return strcmp(a->name, b->name);
107 static void uniq(struct cmdnames *cmds)
114 for (i = j = 1; i < cmds->cnt; i++)
115 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
116 cmds->names[j++] = cmds->names[i];
121 static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
127 while (ci < cmds->cnt && ei < excludes->cnt) {
128 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
130 cmds->names[cj++] = cmds->names[ci++];
137 while (ci < cmds->cnt)
138 cmds->names[cj++] = cmds->names[ci++];
143 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
146 int space = longest + 1; /* min 1 SP between words */
147 int max_cols = term_columns() - 1; /* don't print *on* the edge */
150 if (space < max_cols)
151 cols = max_cols / space;
152 rows = (cmds->cnt + cols - 1) / cols;
154 for (i = 0; i < rows; i++) {
157 for (j = 0; j < cols; j++) {
158 int n = j * rows + i;
162 if (j == cols-1 || n + rows >= cmds->cnt)
164 printf("%-*s", size, cmds->names[n]->name);
170 static unsigned int list_commands_in_dir(struct cmdnames *cmds,
173 unsigned int longest = 0;
174 const char *prefix = "git-";
175 int prefix_len = strlen(prefix);
176 DIR *dir = opendir(path);
179 if (!dir || chdir(path))
182 while ((de = readdir(dir)) != NULL) {
186 if (prefixcmp(de->d_name, prefix))
189 if (stat(de->d_name, &st) || /* stat, not lstat */
190 !S_ISREG(st.st_mode) ||
191 !(st.st_mode & S_IXUSR))
194 entlen = strlen(de->d_name) - prefix_len;
195 if (has_extension(de->d_name, ".exe"))
198 if (longest < entlen)
201 add_cmdname(cmds, de->d_name + prefix_len, entlen);
208 static void list_commands(void)
210 unsigned int longest = 0;
212 const char *env_path = getenv("PATH");
213 char *paths, *path, *colon;
214 const char *exec_path = git_exec_path();
217 longest = list_commands_in_dir(&main_cmds, exec_path);
220 fprintf(stderr, "PATH not set\n");
224 path = paths = xstrdup(env_path);
226 if ((colon = strchr(path, ':')))
229 len = list_commands_in_dir(&other_cmds, path);
239 qsort(main_cmds.names, main_cmds.cnt,
240 sizeof(*main_cmds.names), cmdname_compare);
243 qsort(other_cmds.names, other_cmds.cnt,
244 sizeof(*other_cmds.names), cmdname_compare);
246 exclude_cmds(&other_cmds, &main_cmds);
249 printf("available git commands in '%s'\n", exec_path);
250 printf("----------------------------");
251 mput_char('-', strlen(exec_path));
253 pretty_print_string_list(&main_cmds, longest);
257 if (other_cmds.cnt) {
258 printf("git commands available from elsewhere on your $PATH\n");
259 printf("---------------------------------------------------\n");
260 pretty_print_string_list(&other_cmds, longest);
265 void list_common_cmds_help(void)
269 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
270 if (longest < strlen(common_cmds[i].name))
271 longest = strlen(common_cmds[i].name);
274 puts("The most commonly used git commands are:");
275 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
276 printf(" %s ", common_cmds[i].name);
277 mput_char(' ', longest - strlen(common_cmds[i].name));
278 puts(common_cmds[i].help);
282 static const char *cmd_to_page(const char *git_cmd)
286 else if (!prefixcmp(git_cmd, "git"))
289 int page_len = strlen(git_cmd) + 4;
290 char *p = xmalloc(page_len + 1);
292 strcpy(p + 4, git_cmd);
298 static void setup_man_path(void)
300 struct strbuf new_path;
301 const char *old_path = getenv("MANPATH");
303 strbuf_init(&new_path, 0);
305 /* We should always put ':' after our path. If there is no
306 * old_path, the ':' at the end will let 'man' to try
307 * system-wide paths after ours to find the manual page. If
308 * there is old_path, we need ':' as delimiter. */
309 strbuf_addstr(&new_path, GIT_MAN_PATH);
310 strbuf_addch(&new_path, ':');
312 strbuf_addstr(&new_path, old_path);
314 setenv("MANPATH", new_path.buf, 1);
316 strbuf_release(&new_path);
319 static void show_man_page(const char *git_cmd)
321 const char *page = cmd_to_page(git_cmd);
323 execlp("man", "man", page, NULL);
326 static void show_info_page(const char *git_cmd)
328 const char *page = cmd_to_page(git_cmd);
329 setenv("INFOPATH", GIT_INFO_PATH, 1);
330 execlp("info", "info", "gitman", page, NULL);
333 static void show_html_page(const char *git_cmd)
335 const char *page = cmd_to_page(git_cmd);
336 execl_git_cmd("help--browse", page, NULL);
339 void help_unknown_cmd(const char *cmd)
341 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
345 int cmd_version(int argc, const char **argv, const char *prefix)
347 printf("git version %s\n", git_version_string);
351 int cmd_help(int argc, const char **argv, const char *prefix)
353 const char *help_cmd = argv[1];
356 printf("usage: %s\n\n", git_usage_string);
357 list_common_cmds_help();
361 if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
362 printf("usage: %s\n\n", git_usage_string);
366 else if (!strcmp(help_cmd, "--web") || !strcmp(help_cmd, "-w")) {
367 show_html_page(argc > 2 ? argv[2] : NULL);
370 else if (!strcmp(help_cmd, "--info") || !strcmp(help_cmd, "-i")) {
371 show_info_page(argc > 2 ? argv[2] : NULL);
374 else if (!strcmp(help_cmd, "--man") || !strcmp(help_cmd, "-m")) {
375 show_man_page(argc > 2 ? argv[2] : NULL);
381 setup_git_directory_gently(&nongit);
382 git_config(git_help_config);
383 if (help_default_format)
384 parse_help_format(help_default_format);
386 switch (help_format) {
388 show_man_page(help_cmd);
391 show_info_page(help_cmd);
394 show_html_page(help_cmd);