Coverage Report

Created: 2024-09-08 06:23

/src/git/help.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "config.h"
5
#include "builtin.h"
6
#include "exec-cmd.h"
7
#include "run-command.h"
8
#include "levenshtein.h"
9
#include "gettext.h"
10
#include "help.h"
11
#include "command-list.h"
12
#include "string-list.h"
13
#include "column.h"
14
#include "version.h"
15
#include "refs.h"
16
#include "parse-options.h"
17
#include "prompt.h"
18
#include "fsmonitor-ipc.h"
19
20
#ifndef NO_CURL
21
#include "git-curl-compat.h" /* For LIBCURL_VERSION only */
22
#endif
23
24
struct category_description {
25
  uint32_t category;
26
  const char *desc;
27
};
28
static uint32_t common_mask =
29
  CAT_init | CAT_worktree | CAT_info |
30
  CAT_history | CAT_remote;
31
static struct category_description common_categories[] = {
32
  { CAT_init, N_("start a working area (see also: git help tutorial)") },
33
  { CAT_worktree, N_("work on the current change (see also: git help everyday)") },
34
  { CAT_info, N_("examine the history and state (see also: git help revisions)") },
35
  { CAT_history, N_("grow, mark and tweak your common history") },
36
  { CAT_remote, N_("collaborate (see also: git help workflows)") },
37
  { 0, NULL }
38
};
39
static struct category_description main_categories[] = {
40
  { CAT_mainporcelain, N_("Main Porcelain Commands") },
41
  { CAT_ancillarymanipulators, N_("Ancillary Commands / Manipulators") },
42
  { CAT_ancillaryinterrogators, N_("Ancillary Commands / Interrogators") },
43
  { CAT_foreignscminterface, N_("Interacting with Others") },
44
  { CAT_plumbingmanipulators, N_("Low-level Commands / Manipulators") },
45
  { CAT_plumbinginterrogators, N_("Low-level Commands / Interrogators") },
46
  { CAT_synchingrepositories, N_("Low-level Commands / Syncing Repositories") },
47
  { CAT_purehelpers, N_("Low-level Commands / Internal Helpers") },
48
  { CAT_userinterfaces, N_("User-facing repository, command and file interfaces") },
49
  { CAT_developerinterfaces, N_("Developer-facing file formats, protocols and other interfaces") },
50
  { 0, NULL }
51
};
52
53
static const char *drop_prefix(const char *name, uint32_t category)
54
0
{
55
0
  const char *new_name;
56
0
  const char *prefix;
57
58
0
  switch (category) {
59
0
  case CAT_guide:
60
0
  case CAT_userinterfaces:
61
0
  case CAT_developerinterfaces:
62
0
    prefix = "git";
63
0
    break;
64
0
  default:
65
0
    prefix = "git-";
66
0
    break;
67
0
  }
68
0
  if (skip_prefix(name, prefix, &new_name))
69
0
    return new_name;
70
71
0
  return name;
72
0
}
73
74
static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask)
75
0
{
76
0
  int i, nr = 0;
77
0
  struct cmdname_help *cmds;
78
79
0
  if (ARRAY_SIZE(command_list) == 0)
80
0
    BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
81
82
0
  ALLOC_ARRAY(cmds, ARRAY_SIZE(command_list) + 1);
83
84
0
  for (i = 0; i < ARRAY_SIZE(command_list); i++) {
85
0
    const struct cmdname_help *cmd = command_list + i;
86
87
0
    if (!(cmd->category & mask))
88
0
      continue;
89
90
0
    cmds[nr] = *cmd;
91
0
    cmds[nr].name = drop_prefix(cmd->name, cmd->category);
92
93
0
    nr++;
94
0
  }
95
0
  cmds[nr].name = NULL;
96
0
  *p_cmds = cmds;
97
0
}
98
99
static void print_command_list(const struct cmdname_help *cmds,
100
             uint32_t mask, int longest)
101
0
{
102
0
  int i;
103
104
0
  for (i = 0; cmds[i].name; i++) {
105
0
    if (cmds[i].category & mask) {
106
0
      size_t len = strlen(cmds[i].name);
107
0
      printf("   %s   ", cmds[i].name);
108
0
      if (longest > len)
109
0
        mput_char(' ', longest - len);
110
0
      puts(_(cmds[i].help));
111
0
    }
112
0
  }
113
0
}
114
115
static int cmd_name_cmp(const void *elem1, const void *elem2)
116
0
{
117
0
  const struct cmdname_help *e1 = elem1;
118
0
  const struct cmdname_help *e2 = elem2;
119
120
0
  return strcmp(e1->name, e2->name);
121
0
}
122
123
static void print_cmd_by_category(const struct category_description *catdesc,
124
          int *longest_p)
125
0
{
126
0
  struct cmdname_help *cmds;
127
0
  int longest = 0;
128
0
  int i, nr = 0;
129
0
  uint32_t mask = 0;
130
131
0
  for (i = 0; catdesc[i].desc; i++)
132
0
    mask |= catdesc[i].category;
133
134
0
  extract_cmds(&cmds, mask);
135
136
0
  for (i = 0; cmds[i].name; i++, nr++) {
137
0
    if (longest < strlen(cmds[i].name))
138
0
      longest = strlen(cmds[i].name);
139
0
  }
140
0
  QSORT(cmds, nr, cmd_name_cmp);
141
142
0
  for (i = 0; catdesc[i].desc; i++) {
143
0
    uint32_t mask = catdesc[i].category;
144
0
    const char *desc = catdesc[i].desc;
145
146
0
    if (i)
147
0
      putchar('\n');
148
0
    puts(_(desc));
149
0
    print_command_list(cmds, mask, longest);
150
0
  }
151
0
  free(cmds);
152
0
  if (longest_p)
153
0
    *longest_p = longest;
154
0
}
155
156
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
157
0
{
158
0
  struct cmdname *ent;
159
0
  FLEX_ALLOC_MEM(ent, name, name, len);
160
0
  ent->len = len;
161
162
0
  ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
163
0
  cmds->names[cmds->cnt++] = ent;
164
0
}
165
166
void cmdnames_release(struct cmdnames *cmds)
167
0
{
168
0
  int i;
169
0
  for (i = 0; i < cmds->cnt; ++i)
170
0
    free(cmds->names[i]);
171
0
  free(cmds->names);
172
0
  cmds->cnt = 0;
173
0
  cmds->alloc = 0;
174
0
}
175
176
static int cmdname_compare(const void *a_, const void *b_)
177
0
{
178
0
  struct cmdname *a = *(struct cmdname **)a_;
179
0
  struct cmdname *b = *(struct cmdname **)b_;
180
0
  return strcmp(a->name, b->name);
181
0
}
182
183
static void uniq(struct cmdnames *cmds)
184
0
{
185
0
  int i, j;
186
187
0
  if (!cmds->cnt)
188
0
    return;
189
190
0
  for (i = j = 1; i < cmds->cnt; i++) {
191
0
    if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
192
0
      free(cmds->names[i]);
193
0
    else
194
0
      cmds->names[j++] = cmds->names[i];
195
0
  }
196
197
0
  cmds->cnt = j;
198
0
}
199
200
void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
201
0
{
202
0
  int ci, cj, ei;
203
0
  int cmp;
204
205
0
  ci = cj = ei = 0;
206
0
  while (ci < cmds->cnt && ei < excludes->cnt) {
207
0
    cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
208
0
    if (cmp < 0)
209
0
      cmds->names[cj++] = cmds->names[ci++];
210
0
    else if (cmp == 0) {
211
0
      ei++;
212
0
      free(cmds->names[ci++]);
213
0
    } else if (cmp > 0)
214
0
      ei++;
215
0
  }
216
217
0
  while (ci < cmds->cnt)
218
0
    cmds->names[cj++] = cmds->names[ci++];
219
220
0
  cmds->cnt = cj;
221
0
}
222
223
static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
224
0
{
225
0
  struct string_list list = STRING_LIST_INIT_NODUP;
226
0
  struct column_options copts;
227
0
  int i;
228
229
0
  for (i = 0; i < cmds->cnt; i++)
230
0
    string_list_append(&list, cmds->names[i]->name);
231
  /*
232
   * always enable column display, we only consult column.*
233
   * about layout strategy and stuff
234
   */
235
0
  colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
236
0
  memset(&copts, 0, sizeof(copts));
237
0
  copts.indent = "  ";
238
0
  copts.padding = 2;
239
0
  print_columns(&list, colopts, &copts);
240
0
  string_list_clear(&list, 0);
241
0
}
242
243
static void list_commands_in_dir(struct cmdnames *cmds,
244
           const char *path,
245
           const char *prefix)
246
0
{
247
0
  DIR *dir = opendir(path);
248
0
  struct dirent *de;
249
0
  struct strbuf buf = STRBUF_INIT;
250
0
  int len;
251
252
0
  if (!dir)
253
0
    return;
254
0
  if (!prefix)
255
0
    prefix = "git-";
256
257
0
  strbuf_addf(&buf, "%s/", path);
258
0
  len = buf.len;
259
260
0
  while ((de = readdir(dir)) != NULL) {
261
0
    const char *ent;
262
0
    size_t entlen;
263
264
0
    if (!skip_prefix(de->d_name, prefix, &ent))
265
0
      continue;
266
267
0
    strbuf_setlen(&buf, len);
268
0
    strbuf_addstr(&buf, de->d_name);
269
0
    if (!is_executable(buf.buf))
270
0
      continue;
271
272
0
    entlen = strlen(ent);
273
0
    strip_suffix(ent, ".exe", &entlen);
274
275
0
    add_cmdname(cmds, ent, entlen);
276
0
  }
277
0
  closedir(dir);
278
0
  strbuf_release(&buf);
279
0
}
280
281
void load_command_list(const char *prefix,
282
    struct cmdnames *main_cmds,
283
    struct cmdnames *other_cmds)
284
0
{
285
0
  const char *env_path = getenv("PATH");
286
0
  const char *exec_path = git_exec_path();
287
288
0
  load_builtin_commands(prefix, main_cmds);
289
290
0
  if (exec_path) {
291
0
    list_commands_in_dir(main_cmds, exec_path, prefix);
292
0
    QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
293
0
    uniq(main_cmds);
294
0
  }
295
296
0
  if (env_path) {
297
0
    char *paths, *path, *colon;
298
0
    path = paths = xstrdup(env_path);
299
0
    while (1) {
300
0
      if ((colon = strchr(path, PATH_SEP)))
301
0
        *colon = 0;
302
0
      if (!exec_path || strcmp(path, exec_path))
303
0
        list_commands_in_dir(other_cmds, path, prefix);
304
305
0
      if (!colon)
306
0
        break;
307
0
      path = colon + 1;
308
0
    }
309
0
    free(paths);
310
311
0
    QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
312
0
    uniq(other_cmds);
313
0
  }
314
0
  exclude_cmds(other_cmds, main_cmds);
315
0
}
316
317
static int get_colopts(const char *var, const char *value,
318
           const struct config_context *ctx UNUSED, void *data)
319
0
{
320
0
  unsigned int *colopts = data;
321
322
0
  if (starts_with(var, "column."))
323
0
    return git_column_config(var, value, "help", colopts);
324
325
0
  return 0;
326
0
}
327
328
void list_commands(struct cmdnames *main_cmds, struct cmdnames *other_cmds)
329
0
{
330
0
  unsigned int colopts = 0;
331
0
  git_config(get_colopts, &colopts);
332
333
0
  if (main_cmds->cnt) {
334
0
    const char *exec_path = git_exec_path();
335
0
    printf_ln(_("available git commands in '%s'"), exec_path);
336
0
    putchar('\n');
337
0
    pretty_print_cmdnames(main_cmds, colopts);
338
0
    putchar('\n');
339
0
  }
340
341
0
  if (other_cmds->cnt) {
342
0
    puts(_("git commands available from elsewhere on your $PATH"));
343
0
    putchar('\n');
344
0
    pretty_print_cmdnames(other_cmds, colopts);
345
0
    putchar('\n');
346
0
  }
347
0
}
348
349
void list_common_cmds_help(void)
350
0
{
351
0
  puts(_("These are common Git commands used in various situations:"));
352
0
  putchar('\n');
353
0
  print_cmd_by_category(common_categories, NULL);
354
0
}
355
356
void list_all_main_cmds(struct string_list *list)
357
0
{
358
0
  struct cmdnames main_cmds, other_cmds;
359
0
  int i;
360
361
0
  memset(&main_cmds, 0, sizeof(main_cmds));
362
0
  memset(&other_cmds, 0, sizeof(other_cmds));
363
0
  load_command_list("git-", &main_cmds, &other_cmds);
364
365
0
  for (i = 0; i < main_cmds.cnt; i++)
366
0
    string_list_append(list, main_cmds.names[i]->name);
367
368
0
  cmdnames_release(&main_cmds);
369
0
  cmdnames_release(&other_cmds);
370
0
}
371
372
void list_all_other_cmds(struct string_list *list)
373
0
{
374
0
  struct cmdnames main_cmds, other_cmds;
375
0
  int i;
376
377
0
  memset(&main_cmds, 0, sizeof(main_cmds));
378
0
  memset(&other_cmds, 0, sizeof(other_cmds));
379
0
  load_command_list("git-", &main_cmds, &other_cmds);
380
381
0
  for (i = 0; i < other_cmds.cnt; i++)
382
0
    string_list_append(list, other_cmds.names[i]->name);
383
384
0
  cmdnames_release(&main_cmds);
385
0
  cmdnames_release(&other_cmds);
386
0
}
387
388
void list_cmds_by_category(struct string_list *list,
389
         const char *cat)
390
0
{
391
0
  int i, n = ARRAY_SIZE(command_list);
392
0
  uint32_t cat_id = 0;
393
394
0
  for (i = 0; category_names[i]; i++) {
395
0
    if (!strcmp(cat, category_names[i])) {
396
0
      cat_id = 1UL << i;
397
0
      break;
398
0
    }
399
0
  }
400
0
  if (!cat_id)
401
0
    die(_("unsupported command listing type '%s'"), cat);
402
403
0
  for (i = 0; i < n; i++) {
404
0
    struct cmdname_help *cmd = command_list + i;
405
406
0
    if (!(cmd->category & cat_id))
407
0
      continue;
408
0
    string_list_append(list, drop_prefix(cmd->name, cmd->category));
409
0
  }
410
0
}
411
412
void list_cmds_by_config(struct string_list *list)
413
0
{
414
0
  const char *cmd_list;
415
416
0
  if (git_config_get_string_tmp("completion.commands", &cmd_list))
417
0
    return;
418
419
0
  string_list_sort(list);
420
0
  string_list_remove_duplicates(list, 0);
421
422
0
  while (*cmd_list) {
423
0
    struct strbuf sb = STRBUF_INIT;
424
0
    const char *p = strchrnul(cmd_list, ' ');
425
426
0
    strbuf_add(&sb, cmd_list, p - cmd_list);
427
0
    if (sb.buf[0] == '-')
428
0
      string_list_remove(list, sb.buf + 1, 0);
429
0
    else
430
0
      string_list_insert(list, sb.buf);
431
0
    strbuf_release(&sb);
432
0
    while (*p == ' ')
433
0
      p++;
434
0
    cmd_list = p;
435
0
  }
436
0
}
437
438
void list_guides_help(void)
439
0
{
440
0
  struct category_description catdesc[] = {
441
0
    { CAT_guide, N_("The Git concept guides are:") },
442
0
    { 0, NULL }
443
0
  };
444
0
  print_cmd_by_category(catdesc, NULL);
445
0
  putchar('\n');
446
0
}
447
448
void list_user_interfaces_help(void)
449
0
{
450
0
  struct category_description catdesc[] = {
451
0
    { CAT_userinterfaces, N_("User-facing repository, command and file interfaces:") },
452
0
    { 0, NULL }
453
0
  };
454
0
  print_cmd_by_category(catdesc, NULL);
455
0
  putchar('\n');
456
0
}
457
458
void list_developer_interfaces_help(void)
459
0
{
460
0
  struct category_description catdesc[] = {
461
0
    { CAT_developerinterfaces, N_("File formats, protocols and other developer interfaces:") },
462
0
    { 0, NULL }
463
0
  };
464
0
  print_cmd_by_category(catdesc, NULL);
465
0
  putchar('\n');
466
0
}
467
468
static int get_alias(const char *var, const char *value,
469
         const struct config_context *ctx UNUSED, void *data)
470
0
{
471
0
  struct string_list *list = data;
472
473
0
  if (skip_prefix(var, "alias.", &var)) {
474
0
    if (!value)
475
0
      return config_error_nonbool(var);
476
0
    string_list_append(list, var)->util = xstrdup(value);
477
0
  }
478
479
0
  return 0;
480
0
}
481
482
static void list_all_cmds_help_external_commands(void)
483
0
{
484
0
  struct string_list others = STRING_LIST_INIT_DUP;
485
0
  int i;
486
487
0
  list_all_other_cmds(&others);
488
0
  if (others.nr)
489
0
    printf("\n%s\n", _("External commands"));
490
0
  for (i = 0; i < others.nr; i++)
491
0
    printf("   %s\n", others.items[i].string);
492
0
  string_list_clear(&others, 0);
493
0
}
494
495
static void list_all_cmds_help_aliases(int longest)
496
0
{
497
0
  struct string_list alias_list = STRING_LIST_INIT_DUP;
498
0
  struct cmdname_help *aliases;
499
0
  int i;
500
501
0
  git_config(get_alias, &alias_list);
502
0
  string_list_sort(&alias_list);
503
504
0
  for (i = 0; i < alias_list.nr; i++) {
505
0
    size_t len = strlen(alias_list.items[i].string);
506
0
    if (longest < len)
507
0
      longest = len;
508
0
  }
509
510
0
  if (alias_list.nr) {
511
0
    printf("\n%s\n", _("Command aliases"));
512
0
    ALLOC_ARRAY(aliases, alias_list.nr + 1);
513
0
    for (i = 0; i < alias_list.nr; i++) {
514
0
      aliases[i].name = alias_list.items[i].string;
515
0
      aliases[i].help = alias_list.items[i].util;
516
0
      aliases[i].category = 1;
517
0
    }
518
0
    aliases[alias_list.nr].name = NULL;
519
0
    print_command_list(aliases, 1, longest);
520
0
    free(aliases);
521
0
  }
522
0
  string_list_clear(&alias_list, 1);
523
0
}
524
525
void list_all_cmds_help(int show_external_commands, int show_aliases)
526
0
{
527
0
  int longest;
528
529
0
  puts(_("See 'git help <command>' to read about a specific subcommand"));
530
0
  putchar('\n');
531
0
  print_cmd_by_category(main_categories, &longest);
532
533
0
  if (show_external_commands)
534
0
    list_all_cmds_help_external_commands();
535
0
  if (show_aliases)
536
0
    list_all_cmds_help_aliases(longest);
537
0
}
538
539
int is_in_cmdlist(struct cmdnames *c, const char *s)
540
0
{
541
0
  int i;
542
0
  for (i = 0; i < c->cnt; i++)
543
0
    if (!strcmp(s, c->names[i]->name))
544
0
      return 1;
545
0
  return 0;
546
0
}
547
548
static int autocorrect;
549
static struct cmdnames aliases;
550
551
0
#define AUTOCORRECT_PROMPT (-3)
552
0
#define AUTOCORRECT_NEVER (-2)
553
0
#define AUTOCORRECT_IMMEDIATELY (-1)
554
555
static int git_unknown_cmd_config(const char *var, const char *value,
556
          const struct config_context *ctx,
557
          void *cb UNUSED)
558
0
{
559
0
  const char *p;
560
561
0
  if (!strcmp(var, "help.autocorrect")) {
562
0
    if (!value)
563
0
      return config_error_nonbool(var);
564
0
    if (!strcmp(value, "never")) {
565
0
      autocorrect = AUTOCORRECT_NEVER;
566
0
    } else if (!strcmp(value, "immediate")) {
567
0
      autocorrect = AUTOCORRECT_IMMEDIATELY;
568
0
    } else if (!strcmp(value, "prompt")) {
569
0
      autocorrect = AUTOCORRECT_PROMPT;
570
0
    } else {
571
0
      int v = git_config_int(var, value, ctx->kvi);
572
0
      autocorrect = (v < 0)
573
0
        ? AUTOCORRECT_IMMEDIATELY : v;
574
0
    }
575
0
  }
576
  /* Also use aliases for command lookup */
577
0
  if (skip_prefix(var, "alias.", &p))
578
0
    add_cmdname(&aliases, p, strlen(p));
579
580
0
  return 0;
581
0
}
582
583
static int levenshtein_compare(const void *p1, const void *p2)
584
0
{
585
0
  const struct cmdname *const *c1 = p1, *const *c2 = p2;
586
0
  const char *s1 = (*c1)->name, *s2 = (*c2)->name;
587
0
  int l1 = (*c1)->len;
588
0
  int l2 = (*c2)->len;
589
0
  return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
590
0
}
591
592
static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
593
0
{
594
0
  int i;
595
0
  ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
596
597
0
  for (i = 0; i < old->cnt; i++)
598
0
    cmds->names[cmds->cnt++] = old->names[i];
599
0
  FREE_AND_NULL(old->names);
600
0
  old->cnt = 0;
601
0
}
602
603
/* An empirically derived magic number */
604
0
#define SIMILARITY_FLOOR 7
605
0
#define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
606
607
static const char bad_interpreter_advice[] =
608
  N_("'%s' appears to be a git command, but we were not\n"
609
  "able to execute it. Maybe git-%s is broken?");
610
611
const char *help_unknown_cmd(const char *cmd)
612
0
{
613
0
  int i, n, best_similarity = 0;
614
0
  struct cmdnames main_cmds, other_cmds;
615
0
  struct cmdname_help *common_cmds;
616
617
0
  memset(&main_cmds, 0, sizeof(main_cmds));
618
0
  memset(&other_cmds, 0, sizeof(other_cmds));
619
0
  memset(&aliases, 0, sizeof(aliases));
620
621
0
  read_early_config(git_unknown_cmd_config, NULL);
622
623
  /*
624
   * Disable autocorrection prompt in a non-interactive session
625
   */
626
0
  if ((autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
627
0
    autocorrect = AUTOCORRECT_NEVER;
628
629
0
  if (autocorrect == AUTOCORRECT_NEVER) {
630
0
    fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
631
0
    exit(1);
632
0
  }
633
634
0
  load_command_list("git-", &main_cmds, &other_cmds);
635
636
0
  add_cmd_list(&main_cmds, &aliases);
637
0
  add_cmd_list(&main_cmds, &other_cmds);
638
0
  QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
639
0
  uniq(&main_cmds);
640
641
0
  extract_cmds(&common_cmds, common_mask);
642
643
  /* This abuses cmdname->len for levenshtein distance */
644
0
  for (i = 0, n = 0; i < main_cmds.cnt; i++) {
645
0
    int cmp = 0; /* avoid compiler stupidity */
646
0
    const char *candidate = main_cmds.names[i]->name;
647
648
    /*
649
     * An exact match means we have the command, but
650
     * for some reason exec'ing it gave us ENOENT; probably
651
     * it's a bad interpreter in the #! line.
652
     */
653
0
    if (!strcmp(candidate, cmd))
654
0
      die(_(bad_interpreter_advice), cmd, cmd);
655
656
    /* Does the candidate appear in common_cmds list? */
657
0
    while (common_cmds[n].name &&
658
0
           (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
659
0
      n++;
660
0
    if (common_cmds[n].name && !cmp) {
661
      /* Yes, this is one of the common commands */
662
0
      n++; /* use the entry from common_cmds[] */
663
0
      if (starts_with(candidate, cmd)) {
664
        /* Give prefix match a very good score */
665
0
        main_cmds.names[i]->len = 0;
666
0
        continue;
667
0
      }
668
0
    }
669
670
0
    main_cmds.names[i]->len =
671
0
      levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
672
0
  }
673
0
  FREE_AND_NULL(common_cmds);
674
675
0
  QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
676
677
0
  if (!main_cmds.cnt)
678
0
    die(_("Uh oh. Your system reports no Git commands at all."));
679
680
  /* skip and count prefix matches */
681
0
  for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
682
0
    ; /* still counting */
683
684
0
  if (main_cmds.cnt <= n) {
685
    /* prefix matches with everything? that is too ambiguous */
686
0
    best_similarity = SIMILARITY_FLOOR + 1;
687
0
  } else {
688
    /* count all the most similar ones */
689
0
    for (best_similarity = main_cmds.names[n++]->len;
690
0
         (n < main_cmds.cnt &&
691
0
          best_similarity == main_cmds.names[n]->len);
692
0
         n++)
693
0
      ; /* still counting */
694
0
  }
695
0
  if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
696
0
    const char *assumed = main_cmds.names[0]->name;
697
0
    main_cmds.names[0] = NULL;
698
0
    cmdnames_release(&main_cmds);
699
0
    fprintf_ln(stderr,
700
0
         _("WARNING: You called a Git command named '%s', "
701
0
           "which does not exist."),
702
0
         cmd);
703
0
    if (autocorrect == AUTOCORRECT_IMMEDIATELY)
704
0
      fprintf_ln(stderr,
705
0
           _("Continuing under the assumption that "
706
0
             "you meant '%s'."),
707
0
           assumed);
708
0
    else if (autocorrect == AUTOCORRECT_PROMPT) {
709
0
      char *answer;
710
0
      struct strbuf msg = STRBUF_INIT;
711
0
      strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed);
712
0
      answer = git_prompt(msg.buf, PROMPT_ECHO);
713
0
      strbuf_release(&msg);
714
0
      if (!(starts_with(answer, "y") ||
715
0
            starts_with(answer, "Y")))
716
0
        exit(1);
717
0
    } else {
718
0
      fprintf_ln(stderr,
719
0
           _("Continuing in %0.1f seconds, "
720
0
             "assuming that you meant '%s'."),
721
0
           (float)autocorrect/10.0, assumed);
722
0
      sleep_millisec(autocorrect * 100);
723
0
    }
724
0
    return assumed;
725
0
  }
726
727
0
  fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
728
729
0
  if (SIMILAR_ENOUGH(best_similarity)) {
730
0
    fprintf_ln(stderr,
731
0
         Q_("\nThe most similar command is",
732
0
            "\nThe most similar commands are",
733
0
         n));
734
735
0
    for (i = 0; i < n; i++)
736
0
      fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
737
0
  }
738
739
0
  exit(1);
740
0
}
741
742
void get_version_info(struct strbuf *buf, int show_build_options)
743
0
{
744
  /*
745
   * The format of this string should be kept stable for compatibility
746
   * with external projects that rely on the output of "git version".
747
   *
748
   * Always show the version, even if other options are given.
749
   */
750
0
  strbuf_addf(buf, "git version %s\n", git_version_string);
751
752
0
  if (show_build_options) {
753
0
    strbuf_addf(buf, "cpu: %s\n", GIT_HOST_CPU);
754
0
    if (git_built_from_commit_string[0])
755
0
      strbuf_addf(buf, "built from commit: %s\n",
756
0
             git_built_from_commit_string);
757
0
    else
758
0
      strbuf_addstr(buf, "no commit associated with this build\n");
759
0
    strbuf_addf(buf, "sizeof-long: %d\n", (int)sizeof(long));
760
0
    strbuf_addf(buf, "sizeof-size_t: %d\n", (int)sizeof(size_t));
761
0
    strbuf_addf(buf, "shell-path: %s\n", SHELL_PATH);
762
    /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
763
764
0
    if (fsmonitor_ipc__is_supported())
765
0
      strbuf_addstr(buf, "feature: fsmonitor--daemon\n");
766
0
#if defined LIBCURL_VERSION
767
0
    strbuf_addf(buf, "libcurl: %s\n", LIBCURL_VERSION);
768
0
#endif
769
0
#if defined OPENSSL_VERSION_TEXT
770
0
    strbuf_addf(buf, "OpenSSL: %s\n", OPENSSL_VERSION_TEXT);
771
0
#endif
772
0
#if defined ZLIB_VERSION
773
0
    strbuf_addf(buf, "zlib: %s\n", ZLIB_VERSION);
774
0
#endif
775
0
  }
776
0
}
777
778
int cmd_version(int argc, const char **argv, const char *prefix)
779
0
{
780
0
  struct strbuf buf = STRBUF_INIT;
781
0
  int build_options = 0;
782
0
  const char * const usage[] = {
783
0
    N_("git version [--build-options]"),
784
0
    NULL
785
0
  };
786
0
  struct option options[] = {
787
0
    OPT_BOOL(0, "build-options", &build_options,
788
0
       "also print build options"),
789
0
    OPT_END()
790
0
  };
791
792
0
  argc = parse_options(argc, argv, prefix, options, usage, 0);
793
794
0
  get_version_info(&buf, build_options);
795
0
  printf("%s", buf.buf);
796
797
0
  strbuf_release(&buf);
798
799
0
  return 0;
800
0
}
801
802
struct similar_ref_cb {
803
  const char *base_ref;
804
  struct string_list *similar_refs;
805
};
806
807
static int append_similar_ref(const char *refname, const char *referent UNUSED,
808
            const struct object_id *oid UNUSED,
809
            int flags UNUSED, void *cb_data)
810
0
{
811
0
  struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
812
0
  char *branch = strrchr(refname, '/') + 1;
813
814
  /* A remote branch of the same name is deemed similar */
815
0
  if (starts_with(refname, "refs/remotes/") &&
816
0
      !strcmp(branch, cb->base_ref))
817
0
    string_list_append_nodup(cb->similar_refs,
818
0
           refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), refname, 1));
819
0
  return 0;
820
0
}
821
822
static struct string_list guess_refs(const char *ref)
823
0
{
824
0
  struct similar_ref_cb ref_cb;
825
0
  struct string_list similar_refs = STRING_LIST_INIT_DUP;
826
827
0
  ref_cb.base_ref = ref;
828
0
  ref_cb.similar_refs = &similar_refs;
829
0
  refs_for_each_ref(get_main_ref_store(the_repository),
830
0
        append_similar_ref, &ref_cb);
831
0
  return similar_refs;
832
0
}
833
834
NORETURN void help_unknown_ref(const char *ref, const char *cmd,
835
             const char *error)
836
0
{
837
0
  int i;
838
0
  struct string_list suggested_refs = guess_refs(ref);
839
840
0
  fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
841
842
0
  if (suggested_refs.nr > 0) {
843
0
    fprintf_ln(stderr,
844
0
         Q_("\nDid you mean this?",
845
0
            "\nDid you mean one of these?",
846
0
            suggested_refs.nr));
847
0
    for (i = 0; i < suggested_refs.nr; i++)
848
0
      fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
849
0
  }
850
851
0
  string_list_clear(&suggested_refs, 0);
852
0
  exit(1);
853
0
}