]> git.ipfire.org Git - thirdparty/git.git/blame - help.c
config: remove git_config_iter
[thirdparty/git.git] / help.c
CommitLineData
70827b15
LT
1#include "cache.h"
2#include "builtin.h"
3#include "exec_cmd.h"
8af84dad 4#include "levenshtein.h"
940208a7 5#include "help.h"
6612b9e4 6#include "common-cmds.h"
dbfae689
NTND
7#include "string-list.h"
8#include "column.h"
816fb46b 9#include "version.h"
e5618106 10#include "refs.h"
64949984 11
940208a7 12void add_cmdname(struct cmdnames *cmds, const char *name, int len)
70827b15 13{
96ffc06f
JK
14 struct cmdname *ent;
15 FLEX_ALLOC_MEM(ent, name, name, len);
70827b15 16 ent->len = len;
1eb05690
SP
17
18 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
19 cmds->names[cmds->cnt++] = ent;
70827b15
LT
20}
21
8af84dad
JS
22static void clean_cmdnames(struct cmdnames *cmds)
23{
24 int i;
25 for (i = 0; i < cmds->cnt; ++i)
26 free(cmds->names[i]);
27 free(cmds->names);
28 cmds->cnt = 0;
29 cmds->alloc = 0;
30}
31
70827b15
LT
32static int cmdname_compare(const void *a_, const void *b_)
33{
34 struct cmdname *a = *(struct cmdname **)a_;
35 struct cmdname *b = *(struct cmdname **)b_;
36 return strcmp(a->name, b->name);
37}
38
1eb05690
SP
39static void uniq(struct cmdnames *cmds)
40{
41 int i, j;
42
43 if (!cmds->cnt)
44 return;
45
4a15758f
JK
46 for (i = j = 1; i < cmds->cnt; i++) {
47 if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
48 free(cmds->names[i]);
49 else
1eb05690 50 cmds->names[j++] = cmds->names[i];
4a15758f 51 }
1eb05690
SP
52
53 cmds->cnt = j;
54}
55
940208a7 56void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
f3fa1838 57{
1eb05690
SP
58 int ci, cj, ei;
59 int cmp;
60
61 ci = cj = ei = 0;
62 while (ci < cmds->cnt && ei < excludes->cnt) {
63 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
64 if (cmp < 0)
65 cmds->names[cj++] = cmds->names[ci++];
6a17f583
JH
66 else if (cmp == 0) {
67 ei++;
68 free(cmds->names[ci++]);
69 } else if (cmp > 0)
1eb05690
SP
70 ei++;
71 }
72
73 while (ci < cmds->cnt)
74 cmds->names[cj++] = cmds->names[ci++];
75
76 cmds->cnt = cj;
77}
78
d10cb3df 79static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
70827b15 80{
dbfae689
NTND
81 struct string_list list = STRING_LIST_INIT_NODUP;
82 struct column_options copts;
83 int i;
70827b15 84
dbfae689
NTND
85 for (i = 0; i < cmds->cnt; i++)
86 string_list_append(&list, cmds->names[i]->name);
87 /*
88 * always enable column display, we only consult column.*
89 * about layout strategy and stuff
90 */
91 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
92 memset(&copts, 0, sizeof(copts));
93 copts.indent = " ";
94 copts.padding = 2;
95 print_columns(&list, colopts, &copts);
96 string_list_clear(&list, 0);
70827b15
LT
97}
98
cc3b7a97
JS
99static int is_executable(const char *name)
100{
101 struct stat st;
102
103 if (stat(name, &st) || /* stat, not lstat */
104 !S_ISREG(st.st_mode))
105 return 0;
106
f66450ae 107#if defined(GIT_WINDOWS_NATIVE)
c755015f
HV
108 /*
109 * On Windows there is no executable bit. The file extension
110 * indicates whether it can be run as an executable, and Git
111 * has special-handling to detect scripts and launch them
112 * through the indicated script interpreter. We test for the
113 * file extension first because virus scanners may make
114 * it quite expensive to open many files.
115 */
116 if (ends_with(name, ".exe"))
117 return S_IXUSR;
118
119{
120 /*
121 * Now that we know it does not have an executable extension,
122 * peek into the file instead.
123 */
cc3b7a97
JS
124 char buf[3] = { 0 };
125 int n;
126 int fd = open(name, O_RDONLY);
127 st.st_mode &= ~S_IXUSR;
128 if (fd >= 0) {
129 n = read(fd, buf, 2);
130 if (n == 2)
c755015f
HV
131 /* look for a she-bang */
132 if (!strcmp(buf, "#!"))
cc3b7a97
JS
133 st.st_mode |= S_IXUSR;
134 close(fd);
135 }
0d30ad71 136}
cc3b7a97
JS
137#endif
138 return st.st_mode & S_IXUSR;
139}
140
e321180e 141static void list_commands_in_dir(struct cmdnames *cmds,
940208a7
MV
142 const char *path,
143 const char *prefix)
70827b15 144{
1eb05690 145 DIR *dir = opendir(path);
70827b15 146 struct dirent *de;
f5d600e2
JS
147 struct strbuf buf = STRBUF_INIT;
148 int len;
70827b15 149
f5d600e2 150 if (!dir)
e321180e 151 return;
940208a7
MV
152 if (!prefix)
153 prefix = "git-";
70827b15 154
f5d600e2
JS
155 strbuf_addf(&buf, "%s/", path);
156 len = buf.len;
157
70827b15 158 while ((de = readdir(dir)) != NULL) {
de8118e1 159 const char *ent;
26936bfd 160 size_t entlen;
70827b15 161
de8118e1 162 if (!skip_prefix(de->d_name, prefix, &ent))
70827b15 163 continue;
0966003c 164
f5d600e2
JS
165 strbuf_setlen(&buf, len);
166 strbuf_addstr(&buf, de->d_name);
167 if (!is_executable(buf.buf))
70827b15
LT
168 continue;
169
de8118e1 170 entlen = strlen(ent);
6e409473 171 strip_suffix(ent, ".exe", &entlen);
70827b15 172
de8118e1 173 add_cmdname(cmds, ent, entlen);
70827b15
LT
174 }
175 closedir(dir);
f5d600e2 176 strbuf_release(&buf);
1eb05690
SP
177}
178
e321180e 179void load_command_list(const char *prefix,
940208a7
MV
180 struct cmdnames *main_cmds,
181 struct cmdnames *other_cmds)
1eb05690 182{
1eb05690 183 const char *env_path = getenv("PATH");
1eb05690
SP
184 const char *exec_path = git_exec_path();
185
1f08e5ce 186 if (exec_path) {
e321180e 187 list_commands_in_dir(main_cmds, exec_path, prefix);
9ed0d8d6 188 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
1f08e5ce 189 uniq(main_cmds);
1eb05690
SP
190 }
191
1f08e5ce
AR
192 if (env_path) {
193 char *paths, *path, *colon;
194 path = paths = xstrdup(env_path);
195 while (1) {
196 if ((colon = strchr(path, PATH_SEP)))
197 *colon = 0;
746c221a
PB
198 if (!exec_path || strcmp(path, exec_path))
199 list_commands_in_dir(other_cmds, path, prefix);
1eb05690 200
1f08e5ce
AR
201 if (!colon)
202 break;
203 path = colon + 1;
204 }
205 free(paths);
1eb05690 206
9ed0d8d6 207 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
1f08e5ce
AR
208 uniq(other_cmds);
209 }
940208a7 210 exclude_cmds(other_cmds, main_cmds);
2156435f
JK
211}
212
f4ed0af6 213void list_commands(unsigned int colopts,
dbfae689 214 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
2156435f 215{
940208a7 216 if (main_cmds->cnt) {
61c5d431 217 const char *exec_path = git_exec_path();
4470ef94 218 printf_ln(_("available git commands in '%s'"), exec_path);
1eb05690 219 putchar('\n');
d10cb3df 220 pretty_print_cmdnames(main_cmds, colopts);
1eb05690
SP
221 putchar('\n');
222 }
223
940208a7 224 if (other_cmds->cnt) {
4470ef94 225 printf_ln(_("git commands available from elsewhere on your $PATH"));
940208a7 226 putchar('\n');
d10cb3df 227 pretty_print_cmdnames(other_cmds, colopts);
1eb05690
SP
228 putchar('\n');
229 }
70827b15
LT
230}
231
22414770
SG
232static int cmd_group_cmp(const void *elem1, const void *elem2)
233{
234 const struct cmdname_help *e1 = elem1;
235 const struct cmdname_help *e2 = elem2;
236
237 if (e1->group < e2->group)
238 return -1;
239 if (e1->group > e2->group)
240 return 1;
241 return strcmp(e1->name, e2->name);
242}
243
1542d4cd
JH
244void list_common_cmds_help(void)
245{
246 int i, longest = 0;
22414770 247 int current_grp = -1;
1542d4cd
JH
248
249 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
250 if (longest < strlen(common_cmds[i].name))
251 longest = strlen(common_cmds[i].name);
252 }
253
9ed0d8d6 254 QSORT(common_cmds, ARRAY_SIZE(common_cmds), cmd_group_cmp);
22414770
SG
255
256 puts(_("These are common Git commands used in various situations:"));
257
1542d4cd 258 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
22414770
SG
259 if (common_cmds[i].group != current_grp) {
260 printf("\n%s\n", _(common_cmd_groups[common_cmds[i].group]));
261 current_grp = common_cmds[i].group;
262 }
263
1542d4cd
JH
264 printf(" %s ", common_cmds[i].name);
265 mput_char(' ', longest - strlen(common_cmds[i].name));
266 puts(_(common_cmds[i].help));
267 }
268}
269
940208a7 270int is_in_cmdlist(struct cmdnames *c, const char *s)
2156435f
JK
271{
272 int i;
273 for (i = 0; i < c->cnt; i++)
274 if (!strcmp(s, c->names[i]->name))
275 return 1;
276 return 0;
277}
278
f0e90716 279static int autocorrect;
746c221a 280static struct cmdnames aliases;
f0e90716
AR
281
282static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
283{
ae021d87
JK
284 const char *p;
285
f0e90716
AR
286 if (!strcmp(var, "help.autocorrect"))
287 autocorrect = git_config_int(var,value);
746c221a 288 /* Also use aliases for command lookup */
ae021d87
JK
289 if (skip_prefix(var, "alias.", &p))
290 add_cmdname(&aliases, p, strlen(p));
f0e90716
AR
291
292 return git_default_config(var, value, cb);
293}
294
8af84dad 295static int levenshtein_compare(const void *p1, const void *p2)
822a7d50 296{
8af84dad
JS
297 const struct cmdname *const *c1 = p1, *const *c2 = p2;
298 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
299 int l1 = (*c1)->len;
300 int l2 = (*c2)->len;
301 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
302}
303
746c221a
PB
304static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
305{
306 int i;
307 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
308
309 for (i = 0; i < old->cnt; i++)
310 cmds->names[cmds->cnt++] = old->names[i];
311 free(old->names);
312 old->cnt = 0;
313 old->names = NULL;
314}
315
06500a02 316/* An empirically derived magic number */
6612b9e4
EFL
317#define SIMILARITY_FLOOR 7
318#define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
06500a02 319
823e0ded
MS
320static const char bad_interpreter_advice[] =
321 N_("'%s' appears to be a git command, but we were not\n"
322 "able to execute it. Maybe git-%s is broken?");
323
8af84dad 324const char *help_unknown_cmd(const char *cmd)
822a7d50 325{
8af84dad
JS
326 int i, n, best_similarity = 0;
327 struct cmdnames main_cmds, other_cmds;
328
329 memset(&main_cmds, 0, sizeof(main_cmds));
0b74f5dc 330 memset(&other_cmds, 0, sizeof(other_cmds));
746c221a 331 memset(&aliases, 0, sizeof(aliases));
8af84dad 332
659fef19 333 read_early_config(git_unknown_cmd_config, NULL);
f0e90716 334
8af84dad
JS
335 load_command_list("git-", &main_cmds, &other_cmds);
336
746c221a
PB
337 add_cmd_list(&main_cmds, &aliases);
338 add_cmd_list(&main_cmds, &other_cmds);
9ed0d8d6 339 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
746c221a 340 uniq(&main_cmds);
8af84dad 341
6612b9e4
EFL
342 /* This abuses cmdname->len for levenshtein distance */
343 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
344 int cmp = 0; /* avoid compiler stupidity */
345 const char *candidate = main_cmds.names[i]->name;
346
823e0ded
MS
347 /*
348 * An exact match means we have the command, but
349 * for some reason exec'ing it gave us ENOENT; probably
350 * it's a bad interpreter in the #! line.
351 */
352 if (!strcmp(candidate, cmd))
353 die(_(bad_interpreter_advice), cmd, cmd);
354
6612b9e4
EFL
355 /* Does the candidate appear in common_cmds list? */
356 while (n < ARRAY_SIZE(common_cmds) &&
357 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
358 n++;
359 if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
360 /* Yes, this is one of the common commands */
361 n++; /* use the entry from common_cmds[] */
59556548 362 if (starts_with(candidate, cmd)) {
6612b9e4
EFL
363 /* Give prefix match a very good score */
364 main_cmds.names[i]->len = 0;
365 continue;
366 }
367 }
368
8af84dad 369 main_cmds.names[i]->len =
c41494f8 370 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
6612b9e4 371 }
8af84dad 372
9ed0d8d6 373 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
8af84dad
JS
374
375 if (!main_cmds.cnt)
9665627d 376 die(_("Uh oh. Your system reports no Git commands at all."));
8af84dad 377
6612b9e4
EFL
378 /* skip and count prefix matches */
379 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
380 ; /* still counting */
381
382 if (main_cmds.cnt <= n) {
383 /* prefix matches with everything? that is too ambiguous */
384 best_similarity = SIMILARITY_FLOOR + 1;
385 } else {
386 /* count all the most similar ones */
387 for (best_similarity = main_cmds.names[n++]->len;
388 (n < main_cmds.cnt &&
389 best_similarity == main_cmds.names[n]->len);
390 n++)
391 ; /* still counting */
392 }
06500a02 393 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
8af84dad
JS
394 const char *assumed = main_cmds.names[0]->name;
395 main_cmds.names[0] = NULL;
396 clean_cmdnames(&main_cmds);
9665627d
NTND
397 fprintf_ln(stderr,
398 _("WARNING: You called a Git command named '%s', "
399 "which does not exist.\n"
400 "Continuing under the assumption that you meant '%s'"),
8af84dad 401 cmd, assumed);
f0e90716 402 if (autocorrect > 0) {
9665627d 403 fprintf_ln(stderr, _("in %0.1f seconds automatically..."),
f0e90716 404 (float)autocorrect/10.0);
2024d317 405 sleep_millisec(autocorrect * 100);
f0e90716 406 }
8af84dad
JS
407 return assumed;
408 }
409
9665627d 410 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
8af84dad 411
06500a02 412 if (SIMILAR_ENOUGH(best_similarity)) {
9665627d
NTND
413 fprintf_ln(stderr,
414 Q_("\nDid you mean this?",
415 "\nDid you mean one of these?",
416 n));
8af84dad
JS
417
418 for (i = 0; i < n; i++)
419 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
420 }
421
822a7d50
RJ
422 exit(1);
423}
424
a633fca0 425int cmd_version(int argc, const char **argv, const char *prefix)
70827b15 426{
f2de0b97
DA
427 /*
428 * The format of this string should be kept stable for compatibility
429 * with external projects that rely on the output of "git version".
430 */
70827b15 431 printf("git version %s\n", git_version_string);
6b9c38e1
JK
432 while (*++argv) {
433 if (!strcmp(*argv, "--build-options")) {
434 printf("sizeof-long: %d\n", (int)sizeof(long));
435 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
436 }
437 }
70827b15
LT
438 return 0;
439}
e5618106
VV
440
441struct similar_ref_cb {
442 const char *base_ref;
443 struct string_list *similar_refs;
444};
445
91d6e94e 446static int append_similar_ref(const char *refname, const struct object_id *oid,
e5618106
VV
447 int flags, void *cb_data)
448{
449 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
450 char *branch = strrchr(refname, '/') + 1;
95b567c7
JK
451 const char *remote;
452
e5618106 453 /* A remote branch of the same name is deemed similar */
95b567c7 454 if (skip_prefix(refname, "refs/remotes/", &remote) &&
e5618106 455 !strcmp(branch, cb->base_ref))
95b567c7 456 string_list_append(cb->similar_refs, remote);
e5618106
VV
457 return 0;
458}
459
460static struct string_list guess_refs(const char *ref)
461{
462 struct similar_ref_cb ref_cb;
463 struct string_list similar_refs = STRING_LIST_INIT_NODUP;
464
465 ref_cb.base_ref = ref;
466 ref_cb.similar_refs = &similar_refs;
467 for_each_ref(append_similar_ref, &ref_cb);
468 return similar_refs;
469}
470
471void help_unknown_ref(const char *ref, const char *cmd, const char *error)
472{
473 int i;
474 struct string_list suggested_refs = guess_refs(ref);
475
476 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
477
478 if (suggested_refs.nr > 0) {
479 fprintf_ln(stderr,
480 Q_("\nDid you mean this?",
481 "\nDid you mean one of these?",
482 suggested_refs.nr));
483 for (i = 0; i < suggested_refs.nr; i++)
484 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
485 }
486
487 string_list_clear(&suggested_refs, 0);
488 exit(1);
489}