4 * Builtin help-related commands (help, usage, version)
9 #include "common-cmds.h"
11 static const char git_usage[] =
12 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
14 /* most gui terms set COLUMNS (although some don't export it) */
15 static int term_columns(void)
17 char *col_string = getenv("COLUMNS");
20 if (col_string && (n_cols = atoi(col_string)) > 0)
26 if (!ioctl(1, TIOCGWINSZ, &ws)) {
38 fprintf(stderr, "git: out of memory\n");
42 static inline void mput_char(char c, unsigned int num)
48 static struct cmdname {
52 static int cmdname_alloc, cmdname_cnt;
54 static void add_cmdname(const char *name, int len)
57 if (cmdname_alloc <= cmdname_cnt) {
58 cmdname_alloc = cmdname_alloc + 200;
59 cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
63 ent = malloc(sizeof(*ent) + len);
67 memcpy(ent->name, name, len);
69 cmdname[cmdname_cnt++] = ent;
72 static int cmdname_compare(const void *a_, const void *b_)
74 struct cmdname *a = *(struct cmdname **)a_;
75 struct cmdname *b = *(struct cmdname **)b_;
76 return strcmp(a->name, b->name);
79 static void pretty_print_string_list(struct cmdname **cmdname, int longest)
82 int space = longest + 1; /* min 1 SP between words */
83 int max_cols = term_columns() - 1; /* don't print *on* the edge */
87 cols = max_cols / space;
88 rows = (cmdname_cnt + cols - 1) / cols;
90 qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
92 for (i = 0; i < rows; i++) {
95 for (j = 0; j < cols; j++) {
100 if (j == cols-1 || n + rows >= cmdname_cnt)
102 printf("%-*s", size, cmdname[n]->name);
108 static void list_commands(const char *exec_path, const char *pattern)
110 unsigned int longest = 0;
113 DIR *dir = opendir(exec_path);
117 fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
121 dirlen = strlen(exec_path);
122 if (PATH_MAX - 20 < dirlen) {
123 fprintf(stderr, "git: insanely long exec-path '%s'\n",
128 memcpy(path, exec_path, dirlen);
129 path[dirlen++] = '/';
131 while ((de = readdir(dir)) != NULL) {
135 if (strncmp(de->d_name, "git-", 4))
137 strcpy(path+dirlen, de->d_name);
138 if (stat(path, &st) || /* stat, not lstat */
139 !S_ISREG(st.st_mode) ||
140 !(st.st_mode & S_IXUSR))
143 entlen = strlen(de->d_name);
144 if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
147 if (longest < entlen)
150 add_cmdname(de->d_name + 4, entlen-4);
154 printf("git commands available in '%s'\n", exec_path);
155 printf("----------------------------");
156 mput_char('-', strlen(exec_path));
158 pretty_print_string_list(cmdname, longest - 4);
162 static void list_common_cmds_help(void)
166 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
167 if (longest < strlen(common_cmds[i].name))
168 longest = strlen(common_cmds[i].name);
171 puts("The most commonly used git commands are:");
172 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
173 printf(" %s", common_cmds[i].name);
174 mput_char(' ', longest - strlen(common_cmds[i].name) + 4);
175 puts(common_cmds[i].help);
177 puts("(use 'git help -a' to get a list of all installed git commands)");
180 void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
197 list_commands(exec_path, "git-*");
199 list_common_cmds_help();
205 static void show_man_page(const char *git_cmd)
209 if (!strncmp(git_cmd, "git", 3))
212 int page_len = strlen(git_cmd) + 4;
213 char *p = malloc(page_len + 1);
215 strcpy(p + 4, git_cmd);
220 execlp("man", "man", page, NULL);
223 int cmd_version(int argc, const char **argv, char **envp)
225 printf("git version %s\n", git_version_string);
229 int cmd_help(int argc, const char **argv, char **envp)
231 const char *help_cmd = argv[1];
233 cmd_usage(0, git_exec_path(), NULL);
234 else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a"))
235 cmd_usage(1, git_exec_path(), NULL);
237 show_man_page(help_cmd);