Fix "git-merge -s bogo" help text
[alt-git.git] / help.c
blob1afbac0927cdf2ba395126a7fe87d96a9ea87a94
1 #include "cache.h"
2 #include "builtin.h"
3 #include "exec_cmd.h"
4 #include "help.h"
6 /* most GUI terminals set COLUMNS (although some don't export it) */
7 static int term_columns(void)
9 char *col_string = getenv("COLUMNS");
10 int n_cols;
12 if (col_string && (n_cols = atoi(col_string)) > 0)
13 return n_cols;
15 #ifdef TIOCGWINSZ
17 struct winsize ws;
18 if (!ioctl(1, TIOCGWINSZ, &ws)) {
19 if (ws.ws_col)
20 return ws.ws_col;
23 #endif
25 return 80;
28 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
30 struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
32 ent->len = len;
33 memcpy(ent->name, name, len);
34 ent->name[len] = 0;
36 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
37 cmds->names[cmds->cnt++] = ent;
40 static int cmdname_compare(const void *a_, const void *b_)
42 struct cmdname *a = *(struct cmdname **)a_;
43 struct cmdname *b = *(struct cmdname **)b_;
44 return strcmp(a->name, b->name);
47 static void uniq(struct cmdnames *cmds)
49 int i, j;
51 if (!cmds->cnt)
52 return;
54 for (i = j = 1; i < cmds->cnt; i++)
55 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
56 cmds->names[j++] = cmds->names[i];
58 cmds->cnt = j;
61 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
63 int ci, cj, ei;
64 int cmp;
66 ci = cj = ei = 0;
67 while (ci < cmds->cnt && ei < excludes->cnt) {
68 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
69 if (cmp < 0)
70 cmds->names[cj++] = cmds->names[ci++];
71 else if (cmp == 0)
72 ci++, ei++;
73 else if (cmp > 0)
74 ei++;
77 while (ci < cmds->cnt)
78 cmds->names[cj++] = cmds->names[ci++];
80 cmds->cnt = cj;
83 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
85 int cols = 1, rows;
86 int space = longest + 1; /* min 1 SP between words */
87 int max_cols = term_columns() - 1; /* don't print *on* the edge */
88 int i, j;
90 if (space < max_cols)
91 cols = max_cols / space;
92 rows = (cmds->cnt + cols - 1) / cols;
94 for (i = 0; i < rows; i++) {
95 printf(" ");
97 for (j = 0; j < cols; j++) {
98 int n = j * rows + i;
99 int size = space;
100 if (n >= cmds->cnt)
101 break;
102 if (j == cols-1 || n + rows >= cmds->cnt)
103 size = 1;
104 printf("%-*s", size, cmds->names[n]->name);
106 putchar('\n');
110 static int is_executable(const char *name)
112 struct stat st;
114 if (stat(name, &st) || /* stat, not lstat */
115 !S_ISREG(st.st_mode))
116 return 0;
118 #ifdef __MINGW32__
119 /* cannot trust the executable bit, peek into the file instead */
120 char buf[3] = { 0 };
121 int n;
122 int fd = open(name, O_RDONLY);
123 st.st_mode &= ~S_IXUSR;
124 if (fd >= 0) {
125 n = read(fd, buf, 2);
126 if (n == 2)
127 /* DOS executables start with "MZ" */
128 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
129 st.st_mode |= S_IXUSR;
130 close(fd);
132 #endif
133 return st.st_mode & S_IXUSR;
136 static unsigned int list_commands_in_dir(struct cmdnames *cmds,
137 const char *path,
138 const char *prefix)
140 unsigned int longest = 0;
141 int prefix_len;
142 DIR *dir = opendir(path);
143 struct dirent *de;
144 struct strbuf buf = STRBUF_INIT;
145 int len;
147 if (!dir)
148 return 0;
149 if (!prefix)
150 prefix = "git-";
151 prefix_len = strlen(prefix);
153 strbuf_addf(&buf, "%s/", path);
154 len = buf.len;
156 while ((de = readdir(dir)) != NULL) {
157 int entlen;
159 if (prefixcmp(de->d_name, prefix))
160 continue;
162 strbuf_setlen(&buf, len);
163 strbuf_addstr(&buf, de->d_name);
164 if (!is_executable(buf.buf))
165 continue;
167 entlen = strlen(de->d_name) - prefix_len;
168 if (has_extension(de->d_name, ".exe"))
169 entlen -= 4;
171 if (longest < entlen)
172 longest = entlen;
174 add_cmdname(cmds, de->d_name + prefix_len, entlen);
176 closedir(dir);
177 strbuf_release(&buf);
179 return longest;
182 unsigned int load_command_list(const char *prefix,
183 struct cmdnames *main_cmds,
184 struct cmdnames *other_cmds)
186 unsigned int longest = 0;
187 unsigned int len;
188 const char *env_path = getenv("PATH");
189 char *paths, *path, *colon;
190 const char *exec_path = git_exec_path();
192 if (exec_path)
193 longest = list_commands_in_dir(main_cmds, exec_path, prefix);
195 if (!env_path) {
196 fprintf(stderr, "PATH not set\n");
197 exit(1);
200 path = paths = xstrdup(env_path);
201 while (1) {
202 if ((colon = strchr(path, PATH_SEP)))
203 *colon = 0;
205 len = list_commands_in_dir(other_cmds, path, prefix);
206 if (len > longest)
207 longest = len;
209 if (!colon)
210 break;
211 path = colon + 1;
213 free(paths);
215 qsort(main_cmds->names, main_cmds->cnt,
216 sizeof(*main_cmds->names), cmdname_compare);
217 uniq(main_cmds);
219 qsort(other_cmds->names, other_cmds->cnt,
220 sizeof(*other_cmds->names), cmdname_compare);
221 uniq(other_cmds);
222 exclude_cmds(other_cmds, main_cmds);
224 return longest;
227 void list_commands(const char *title, unsigned int longest,
228 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
230 const char *exec_path = git_exec_path();
232 if (main_cmds->cnt) {
233 printf("available %s in '%s'\n", title, exec_path);
234 printf("----------------");
235 mput_char('-', strlen(title) + strlen(exec_path));
236 putchar('\n');
237 pretty_print_string_list(main_cmds, longest);
238 putchar('\n');
241 if (other_cmds->cnt) {
242 printf("%s available from elsewhere on your $PATH\n", title);
243 printf("---------------------------------------");
244 mput_char('-', strlen(title));
245 putchar('\n');
246 pretty_print_string_list(other_cmds, longest);
247 putchar('\n');
251 int is_in_cmdlist(struct cmdnames *c, const char *s)
253 int i;
254 for (i = 0; i < c->cnt; i++)
255 if (!strcmp(s, c->names[i]->name))
256 return 1;
257 return 0;
260 void help_unknown_cmd(const char *cmd)
262 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
263 exit(1);
266 int cmd_version(int argc, const char **argv, const char *prefix)
268 printf("git version %s\n", git_version_string);
269 return 0;