4 * Builtin help-related commands (help, usage, version)
10 #include "common-cmds.h"
13 /* most GUI terminals set COLUMNS (although some don't export it) */
14 static int term_columns(void)
16 char *col_string = getenv("COLUMNS");
19 if (col_string && (n_cols = atoi(col_string)) > 0)
25 if (!ioctl(1, TIOCGWINSZ, &ws)) {
37 fprintf(stderr, "git: out of memory\n");
41 static inline void mput_char(char c, unsigned int num)
47 static struct cmdname {
51 static int cmdname_alloc, cmdname_cnt;
53 static void add_cmdname(const char *name, int len)
56 if (cmdname_alloc <= cmdname_cnt) {
57 cmdname_alloc = cmdname_alloc + 200;
58 cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
62 ent = malloc(sizeof(*ent) + len);
66 memcpy(ent->name, name, len);
68 cmdname[cmdname_cnt++] = ent;
71 static int cmdname_compare(const void *a_, const void *b_)
73 struct cmdname *a = *(struct cmdname **)a_;
74 struct cmdname *b = *(struct cmdname **)b_;
75 return strcmp(a->name, b->name);
78 static void pretty_print_string_list(struct cmdname **cmdname, int longest)
81 int space = longest + 1; /* min 1 SP between words */
82 int max_cols = term_columns() - 1; /* don't print *on* the edge */
86 cols = max_cols / space;
87 rows = (cmdname_cnt + cols - 1) / cols;
89 qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
91 for (i = 0; i < rows; i++) {
94 for (j = 0; j < cols; j++) {
99 if (j == cols-1 || n + rows >= cmdname_cnt)
101 printf("%-*s", size, cmdname[n]->name);
107 static void list_commands(const char *exec_path, const char *pattern)
109 unsigned int longest = 0;
112 DIR *dir = opendir(exec_path);
116 fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
120 dirlen = strlen(exec_path);
121 if (PATH_MAX - 20 < dirlen) {
122 fprintf(stderr, "git: insanely long exec-path '%s'\n",
127 memcpy(path, exec_path, dirlen);
128 path[dirlen++] = '/';
130 while ((de = readdir(dir)) != NULL) {
134 if (strncmp(de->d_name, "git-", 4))
136 strcpy(path+dirlen, de->d_name);
137 if (stat(path, &st) || /* stat, not lstat */
138 !S_ISREG(st.st_mode) ||
139 !(st.st_mode & S_IXUSR))
142 entlen = strlen(de->d_name);
143 if (has_extension(de->d_name, ".exe"))
146 if (longest < entlen)
149 add_cmdname(de->d_name + 4, entlen-4);
153 printf("git commands available in '%s'\n", exec_path);
154 printf("----------------------------");
155 mput_char('-', strlen(exec_path));
157 pretty_print_string_list(cmdname, longest - 4);
161 static void list_common_cmds_help(void)
165 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
166 if (longest < strlen(common_cmds[i].name))
167 longest = strlen(common_cmds[i].name);
170 puts("The most commonly used git commands are:");
171 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
172 printf(" %s", common_cmds[i].name);
173 mput_char(' ', longest - strlen(common_cmds[i].name) + 4);
174 puts(common_cmds[i].help);
176 puts("(use 'git help -a' to get a list of all installed git commands)");
179 static void show_man_page(const char *git_cmd)
183 if (!strncmp(git_cmd, "git", 3))
186 int page_len = strlen(git_cmd) + 4;
187 char *p = xmalloc(page_len + 1);
189 strcpy(p + 4, git_cmd);
194 execlp("man", "man", page, NULL);
197 void help_unknown_cmd(const char *cmd)
199 printf("git: '%s' is not a git-command\n\n", cmd);
200 list_common_cmds_help();
204 int cmd_version(int argc, const char **argv, const char *prefix)
206 printf("git version %s\n", git_version_string);
210 int cmd_help(int argc, const char **argv, const char *prefix)
212 const char *help_cmd = argc > 1 ? argv[1] : NULL;
213 const char *exec_path = git_exec_path();
216 printf("usage: %s\n\n", git_usage_string);
217 list_common_cmds_help();
221 else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
222 printf("usage: %s\n\n", git_usage_string);
224 list_commands(exec_path, "git-*");
229 show_man_page(help_cmd);