git

Форк
0
/
help.c 
853 строки · 21.2 Кб
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
{
55
	const char *new_name;
56
	const char *prefix;
57

58
	switch (category) {
59
	case CAT_guide:
60
	case CAT_userinterfaces:
61
	case CAT_developerinterfaces:
62
		prefix = "git";
63
		break;
64
	default:
65
		prefix = "git-";
66
		break;
67
	}
68
	if (skip_prefix(name, prefix, &new_name))
69
		return new_name;
70

71
	return name;
72
}
73

74
static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask)
75
{
76
	int i, nr = 0;
77
	struct cmdname_help *cmds;
78

79
	if (ARRAY_SIZE(command_list) == 0)
80
		BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
81

82
	ALLOC_ARRAY(cmds, ARRAY_SIZE(command_list) + 1);
83

84
	for (i = 0; i < ARRAY_SIZE(command_list); i++) {
85
		const struct cmdname_help *cmd = command_list + i;
86

87
		if (!(cmd->category & mask))
88
			continue;
89

90
		cmds[nr] = *cmd;
91
		cmds[nr].name = drop_prefix(cmd->name, cmd->category);
92

93
		nr++;
94
	}
95
	cmds[nr].name = NULL;
96
	*p_cmds = cmds;
97
}
98

99
static void print_command_list(const struct cmdname_help *cmds,
100
			       uint32_t mask, int longest)
101
{
102
	int i;
103

104
	for (i = 0; cmds[i].name; i++) {
105
		if (cmds[i].category & mask) {
106
			size_t len = strlen(cmds[i].name);
107
			printf("   %s   ", cmds[i].name);
108
			if (longest > len)
109
				mput_char(' ', longest - len);
110
			puts(_(cmds[i].help));
111
		}
112
	}
113
}
114

115
static int cmd_name_cmp(const void *elem1, const void *elem2)
116
{
117
	const struct cmdname_help *e1 = elem1;
118
	const struct cmdname_help *e2 = elem2;
119

120
	return strcmp(e1->name, e2->name);
121
}
122

123
static void print_cmd_by_category(const struct category_description *catdesc,
124
				  int *longest_p)
125
{
126
	struct cmdname_help *cmds;
127
	int longest = 0;
128
	int i, nr = 0;
129
	uint32_t mask = 0;
130

131
	for (i = 0; catdesc[i].desc; i++)
132
		mask |= catdesc[i].category;
133

134
	extract_cmds(&cmds, mask);
135

136
	for (i = 0; cmds[i].name; i++, nr++) {
137
		if (longest < strlen(cmds[i].name))
138
			longest = strlen(cmds[i].name);
139
	}
140
	QSORT(cmds, nr, cmd_name_cmp);
141

142
	for (i = 0; catdesc[i].desc; i++) {
143
		uint32_t mask = catdesc[i].category;
144
		const char *desc = catdesc[i].desc;
145

146
		if (i)
147
			putchar('\n');
148
		puts(_(desc));
149
		print_command_list(cmds, mask, longest);
150
	}
151
	free(cmds);
152
	if (longest_p)
153
		*longest_p = longest;
154
}
155

156
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
157
{
158
	struct cmdname *ent;
159
	FLEX_ALLOC_MEM(ent, name, name, len);
160
	ent->len = len;
161

162
	ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
163
	cmds->names[cmds->cnt++] = ent;
164
}
165

166
void cmdnames_release(struct cmdnames *cmds)
167
{
168
	int i;
169
	for (i = 0; i < cmds->cnt; ++i)
170
		free(cmds->names[i]);
171
	free(cmds->names);
172
	cmds->cnt = 0;
173
	cmds->alloc = 0;
174
}
175

176
static int cmdname_compare(const void *a_, const void *b_)
177
{
178
	struct cmdname *a = *(struct cmdname **)a_;
179
	struct cmdname *b = *(struct cmdname **)b_;
180
	return strcmp(a->name, b->name);
181
}
182

183
static void uniq(struct cmdnames *cmds)
184
{
185
	int i, j;
186

187
	if (!cmds->cnt)
188
		return;
189

190
	for (i = j = 1; i < cmds->cnt; i++) {
191
		if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
192
			free(cmds->names[i]);
193
		else
194
			cmds->names[j++] = cmds->names[i];
195
	}
196

197
	cmds->cnt = j;
198
}
199

200
void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
201
{
202
	int ci, cj, ei;
203
	int cmp;
204

205
	ci = cj = ei = 0;
206
	while (ci < cmds->cnt && ei < excludes->cnt) {
207
		cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
208
		if (cmp < 0)
209
			cmds->names[cj++] = cmds->names[ci++];
210
		else if (cmp == 0) {
211
			ei++;
212
			free(cmds->names[ci++]);
213
		} else if (cmp > 0)
214
			ei++;
215
	}
216

217
	while (ci < cmds->cnt)
218
		cmds->names[cj++] = cmds->names[ci++];
219

220
	cmds->cnt = cj;
221
}
222

223
static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
224
{
225
	struct string_list list = STRING_LIST_INIT_NODUP;
226
	struct column_options copts;
227
	int i;
228

229
	for (i = 0; i < cmds->cnt; i++)
230
		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
	colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
236
	memset(&copts, 0, sizeof(copts));
237
	copts.indent = "  ";
238
	copts.padding = 2;
239
	print_columns(&list, colopts, &copts);
240
	string_list_clear(&list, 0);
241
}
242

243
static void list_commands_in_dir(struct cmdnames *cmds,
244
					 const char *path,
245
					 const char *prefix)
246
{
247
	DIR *dir = opendir(path);
248
	struct dirent *de;
249
	struct strbuf buf = STRBUF_INIT;
250
	int len;
251

252
	if (!dir)
253
		return;
254
	if (!prefix)
255
		prefix = "git-";
256

257
	strbuf_addf(&buf, "%s/", path);
258
	len = buf.len;
259

260
	while ((de = readdir(dir)) != NULL) {
261
		const char *ent;
262
		size_t entlen;
263

264
		if (!skip_prefix(de->d_name, prefix, &ent))
265
			continue;
266

267
		strbuf_setlen(&buf, len);
268
		strbuf_addstr(&buf, de->d_name);
269
		if (!is_executable(buf.buf))
270
			continue;
271

272
		entlen = strlen(ent);
273
		strip_suffix(ent, ".exe", &entlen);
274

275
		add_cmdname(cmds, ent, entlen);
276
	}
277
	closedir(dir);
278
	strbuf_release(&buf);
279
}
280

281
void load_command_list(const char *prefix,
282
		struct cmdnames *main_cmds,
283
		struct cmdnames *other_cmds)
284
{
285
	const char *env_path = getenv("PATH");
286
	const char *exec_path = git_exec_path();
287

288
	load_builtin_commands(prefix, main_cmds);
289

290
	if (exec_path) {
291
		list_commands_in_dir(main_cmds, exec_path, prefix);
292
		QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
293
		uniq(main_cmds);
294
	}
295

296
	if (env_path) {
297
		char *paths, *path, *colon;
298
		path = paths = xstrdup(env_path);
299
		while (1) {
300
			if ((colon = strchr(path, PATH_SEP)))
301
				*colon = 0;
302
			if (!exec_path || strcmp(path, exec_path))
303
				list_commands_in_dir(other_cmds, path, prefix);
304

305
			if (!colon)
306
				break;
307
			path = colon + 1;
308
		}
309
		free(paths);
310

311
		QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
312
		uniq(other_cmds);
313
	}
314
	exclude_cmds(other_cmds, main_cmds);
315
}
316

317
static int get_colopts(const char *var, const char *value,
318
		       const struct config_context *ctx UNUSED, void *data)
319
{
320
	unsigned int *colopts = data;
321

322
	if (starts_with(var, "column."))
323
		return git_column_config(var, value, "help", colopts);
324

325
	return 0;
326
}
327

328
void list_commands(struct cmdnames *main_cmds, struct cmdnames *other_cmds)
329
{
330
	unsigned int colopts = 0;
331
	git_config(get_colopts, &colopts);
332

333
	if (main_cmds->cnt) {
334
		const char *exec_path = git_exec_path();
335
		printf_ln(_("available git commands in '%s'"), exec_path);
336
		putchar('\n');
337
		pretty_print_cmdnames(main_cmds, colopts);
338
		putchar('\n');
339
	}
340

341
	if (other_cmds->cnt) {
342
		puts(_("git commands available from elsewhere on your $PATH"));
343
		putchar('\n');
344
		pretty_print_cmdnames(other_cmds, colopts);
345
		putchar('\n');
346
	}
347
}
348

349
void list_common_cmds_help(void)
350
{
351
	puts(_("These are common Git commands used in various situations:"));
352
	putchar('\n');
353
	print_cmd_by_category(common_categories, NULL);
354
}
355

356
void list_all_main_cmds(struct string_list *list)
357
{
358
	struct cmdnames main_cmds, other_cmds;
359
	int i;
360

361
	memset(&main_cmds, 0, sizeof(main_cmds));
362
	memset(&other_cmds, 0, sizeof(other_cmds));
363
	load_command_list("git-", &main_cmds, &other_cmds);
364

365
	for (i = 0; i < main_cmds.cnt; i++)
366
		string_list_append(list, main_cmds.names[i]->name);
367

368
	cmdnames_release(&main_cmds);
369
	cmdnames_release(&other_cmds);
370
}
371

372
void list_all_other_cmds(struct string_list *list)
373
{
374
	struct cmdnames main_cmds, other_cmds;
375
	int i;
376

377
	memset(&main_cmds, 0, sizeof(main_cmds));
378
	memset(&other_cmds, 0, sizeof(other_cmds));
379
	load_command_list("git-", &main_cmds, &other_cmds);
380

381
	for (i = 0; i < other_cmds.cnt; i++)
382
		string_list_append(list, other_cmds.names[i]->name);
383

384
	cmdnames_release(&main_cmds);
385
	cmdnames_release(&other_cmds);
386
}
387

388
void list_cmds_by_category(struct string_list *list,
389
			   const char *cat)
390
{
391
	int i, n = ARRAY_SIZE(command_list);
392
	uint32_t cat_id = 0;
393

394
	for (i = 0; category_names[i]; i++) {
395
		if (!strcmp(cat, category_names[i])) {
396
			cat_id = 1UL << i;
397
			break;
398
		}
399
	}
400
	if (!cat_id)
401
		die(_("unsupported command listing type '%s'"), cat);
402

403
	for (i = 0; i < n; i++) {
404
		struct cmdname_help *cmd = command_list + i;
405

406
		if (!(cmd->category & cat_id))
407
			continue;
408
		string_list_append(list, drop_prefix(cmd->name, cmd->category));
409
	}
410
}
411

412
void list_cmds_by_config(struct string_list *list)
413
{
414
	const char *cmd_list;
415

416
	if (git_config_get_string_tmp("completion.commands", &cmd_list))
417
		return;
418

419
	string_list_sort(list);
420
	string_list_remove_duplicates(list, 0);
421

422
	while (*cmd_list) {
423
		struct strbuf sb = STRBUF_INIT;
424
		const char *p = strchrnul(cmd_list, ' ');
425

426
		strbuf_add(&sb, cmd_list, p - cmd_list);
427
		if (sb.buf[0] == '-')
428
			string_list_remove(list, sb.buf + 1, 0);
429
		else
430
			string_list_insert(list, sb.buf);
431
		strbuf_release(&sb);
432
		while (*p == ' ')
433
			p++;
434
		cmd_list = p;
435
	}
436
}
437

438
void list_guides_help(void)
439
{
440
	struct category_description catdesc[] = {
441
		{ CAT_guide, N_("The Git concept guides are:") },
442
		{ 0, NULL }
443
	};
444
	print_cmd_by_category(catdesc, NULL);
445
	putchar('\n');
446
}
447

448
void list_user_interfaces_help(void)
449
{
450
	struct category_description catdesc[] = {
451
		{ CAT_userinterfaces, N_("User-facing repository, command and file interfaces:") },
452
		{ 0, NULL }
453
	};
454
	print_cmd_by_category(catdesc, NULL);
455
	putchar('\n');
456
}
457

458
void list_developer_interfaces_help(void)
459
{
460
	struct category_description catdesc[] = {
461
		{ CAT_developerinterfaces, N_("File formats, protocols and other developer interfaces:") },
462
		{ 0, NULL }
463
	};
464
	print_cmd_by_category(catdesc, NULL);
465
	putchar('\n');
466
}
467

468
static int get_alias(const char *var, const char *value,
469
		     const struct config_context *ctx UNUSED, void *data)
470
{
471
	struct string_list *list = data;
472

473
	if (skip_prefix(var, "alias.", &var)) {
474
		if (!value)
475
			return config_error_nonbool(var);
476
		string_list_append(list, var)->util = xstrdup(value);
477
	}
478

479
	return 0;
480
}
481

482
static void list_all_cmds_help_external_commands(void)
483
{
484
	struct string_list others = STRING_LIST_INIT_DUP;
485
	int i;
486

487
	list_all_other_cmds(&others);
488
	if (others.nr)
489
		printf("\n%s\n", _("External commands"));
490
	for (i = 0; i < others.nr; i++)
491
		printf("   %s\n", others.items[i].string);
492
	string_list_clear(&others, 0);
493
}
494

495
static void list_all_cmds_help_aliases(int longest)
496
{
497
	struct string_list alias_list = STRING_LIST_INIT_DUP;
498
	struct cmdname_help *aliases;
499
	int i;
500

501
	git_config(get_alias, &alias_list);
502
	string_list_sort(&alias_list);
503

504
	for (i = 0; i < alias_list.nr; i++) {
505
		size_t len = strlen(alias_list.items[i].string);
506
		if (longest < len)
507
			longest = len;
508
	}
509

510
	if (alias_list.nr) {
511
		printf("\n%s\n", _("Command aliases"));
512
		ALLOC_ARRAY(aliases, alias_list.nr + 1);
513
		for (i = 0; i < alias_list.nr; i++) {
514
			aliases[i].name = alias_list.items[i].string;
515
			aliases[i].help = alias_list.items[i].util;
516
			aliases[i].category = 1;
517
		}
518
		aliases[alias_list.nr].name = NULL;
519
		print_command_list(aliases, 1, longest);
520
		free(aliases);
521
	}
522
	string_list_clear(&alias_list, 1);
523
}
524

525
void list_all_cmds_help(int show_external_commands, int show_aliases)
526
{
527
	int longest;
528

529
	puts(_("See 'git help <command>' to read about a specific subcommand"));
530
	putchar('\n');
531
	print_cmd_by_category(main_categories, &longest);
532

533
	if (show_external_commands)
534
		list_all_cmds_help_external_commands();
535
	if (show_aliases)
536
		list_all_cmds_help_aliases(longest);
537
}
538

539
int is_in_cmdlist(struct cmdnames *c, const char *s)
540
{
541
	int i;
542
	for (i = 0; i < c->cnt; i++)
543
		if (!strcmp(s, c->names[i]->name))
544
			return 1;
545
	return 0;
546
}
547

548
static int autocorrect;
549
static struct cmdnames aliases;
550

551
#define AUTOCORRECT_PROMPT (-3)
552
#define AUTOCORRECT_NEVER (-2)
553
#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
{
559
	const char *p;
560

561
	if (!strcmp(var, "help.autocorrect")) {
562
		if (!value)
563
			return config_error_nonbool(var);
564
		if (!strcmp(value, "never")) {
565
			autocorrect = AUTOCORRECT_NEVER;
566
		} else if (!strcmp(value, "immediate")) {
567
			autocorrect = AUTOCORRECT_IMMEDIATELY;
568
		} else if (!strcmp(value, "prompt")) {
569
			autocorrect = AUTOCORRECT_PROMPT;
570
		} else {
571
			int v = git_config_int(var, value, ctx->kvi);
572
			autocorrect = (v < 0)
573
				? AUTOCORRECT_IMMEDIATELY : v;
574
		}
575
	}
576
	/* Also use aliases for command lookup */
577
	if (skip_prefix(var, "alias.", &p))
578
		add_cmdname(&aliases, p, strlen(p));
579

580
	return 0;
581
}
582

583
static int levenshtein_compare(const void *p1, const void *p2)
584
{
585
	const struct cmdname *const *c1 = p1, *const *c2 = p2;
586
	const char *s1 = (*c1)->name, *s2 = (*c2)->name;
587
	int l1 = (*c1)->len;
588
	int l2 = (*c2)->len;
589
	return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
590
}
591

592
static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
593
{
594
	int i;
595
	ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
596

597
	for (i = 0; i < old->cnt; i++)
598
		cmds->names[cmds->cnt++] = old->names[i];
599
	FREE_AND_NULL(old->names);
600
	old->cnt = 0;
601
}
602

603
/* An empirically derived magic number */
604
#define SIMILARITY_FLOOR 7
605
#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
{
613
	int i, n, best_similarity = 0;
614
	struct cmdnames main_cmds, other_cmds;
615
	struct cmdname_help *common_cmds;
616

617
	memset(&main_cmds, 0, sizeof(main_cmds));
618
	memset(&other_cmds, 0, sizeof(other_cmds));
619
	memset(&aliases, 0, sizeof(aliases));
620

621
	read_early_config(git_unknown_cmd_config, NULL);
622

623
	/*
624
	 * Disable autocorrection prompt in a non-interactive session
625
	 */
626
	if ((autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
627
		autocorrect = AUTOCORRECT_NEVER;
628

629
	if (autocorrect == AUTOCORRECT_NEVER) {
630
		fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
631
		exit(1);
632
	}
633

634
	load_command_list("git-", &main_cmds, &other_cmds);
635

636
	add_cmd_list(&main_cmds, &aliases);
637
	add_cmd_list(&main_cmds, &other_cmds);
638
	QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
639
	uniq(&main_cmds);
640

641
	extract_cmds(&common_cmds, common_mask);
642

643
	/* This abuses cmdname->len for levenshtein distance */
644
	for (i = 0, n = 0; i < main_cmds.cnt; i++) {
645
		int cmp = 0; /* avoid compiler stupidity */
646
		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
		if (!strcmp(candidate, cmd))
654
			die(_(bad_interpreter_advice), cmd, cmd);
655

656
		/* Does the candidate appear in common_cmds list? */
657
		while (common_cmds[n].name &&
658
		       (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
659
			n++;
660
		if (common_cmds[n].name && !cmp) {
661
			/* Yes, this is one of the common commands */
662
			n++; /* use the entry from common_cmds[] */
663
			if (starts_with(candidate, cmd)) {
664
				/* Give prefix match a very good score */
665
				main_cmds.names[i]->len = 0;
666
				continue;
667
			}
668
		}
669

670
		main_cmds.names[i]->len =
671
			levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
672
	}
673
	FREE_AND_NULL(common_cmds);
674

675
	QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
676

677
	if (!main_cmds.cnt)
678
		die(_("Uh oh. Your system reports no Git commands at all."));
679

680
	/* skip and count prefix matches */
681
	for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
682
		; /* still counting */
683

684
	if (main_cmds.cnt <= n) {
685
		/* prefix matches with everything? that is too ambiguous */
686
		best_similarity = SIMILARITY_FLOOR + 1;
687
	} else {
688
		/* count all the most similar ones */
689
		for (best_similarity = main_cmds.names[n++]->len;
690
		     (n < main_cmds.cnt &&
691
		      best_similarity == main_cmds.names[n]->len);
692
		     n++)
693
			; /* still counting */
694
	}
695
	if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
696
		const char *assumed = main_cmds.names[0]->name;
697
		main_cmds.names[0] = NULL;
698
		cmdnames_release(&main_cmds);
699
		fprintf_ln(stderr,
700
			   _("WARNING: You called a Git command named '%s', "
701
			     "which does not exist."),
702
			   cmd);
703
		if (autocorrect == AUTOCORRECT_IMMEDIATELY)
704
			fprintf_ln(stderr,
705
				   _("Continuing under the assumption that "
706
				     "you meant '%s'."),
707
				   assumed);
708
		else if (autocorrect == AUTOCORRECT_PROMPT) {
709
			char *answer;
710
			struct strbuf msg = STRBUF_INIT;
711
			strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed);
712
			answer = git_prompt(msg.buf, PROMPT_ECHO);
713
			strbuf_release(&msg);
714
			if (!(starts_with(answer, "y") ||
715
			      starts_with(answer, "Y")))
716
				exit(1);
717
		} else {
718
			fprintf_ln(stderr,
719
				   _("Continuing in %0.1f seconds, "
720
				     "assuming that you meant '%s'."),
721
				   (float)autocorrect/10.0, assumed);
722
			sleep_millisec(autocorrect * 100);
723
		}
724
		return assumed;
725
	}
726

727
	fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
728

729
	if (SIMILAR_ENOUGH(best_similarity)) {
730
		fprintf_ln(stderr,
731
			   Q_("\nThe most similar command is",
732
			      "\nThe most similar commands are",
733
			   n));
734

735
		for (i = 0; i < n; i++)
736
			fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
737
	}
738

739
	exit(1);
740
}
741

742
void get_version_info(struct strbuf *buf, int show_build_options)
743
{
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
	strbuf_addf(buf, "git version %s\n", git_version_string);
751

752
	if (show_build_options) {
753
		strbuf_addf(buf, "cpu: %s\n", GIT_HOST_CPU);
754
		if (git_built_from_commit_string[0])
755
			strbuf_addf(buf, "built from commit: %s\n",
756
			       git_built_from_commit_string);
757
		else
758
			strbuf_addstr(buf, "no commit associated with this build\n");
759
		strbuf_addf(buf, "sizeof-long: %d\n", (int)sizeof(long));
760
		strbuf_addf(buf, "sizeof-size_t: %d\n", (int)sizeof(size_t));
761
		strbuf_addf(buf, "shell-path: %s\n", SHELL_PATH);
762
		/* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
763

764
		if (fsmonitor_ipc__is_supported())
765
			strbuf_addstr(buf, "feature: fsmonitor--daemon\n");
766
#if defined LIBCURL_VERSION
767
		strbuf_addf(buf, "libcurl: %s\n", LIBCURL_VERSION);
768
#endif
769
#if defined OPENSSL_VERSION_TEXT
770
		strbuf_addf(buf, "OpenSSL: %s\n", OPENSSL_VERSION_TEXT);
771
#endif
772
#if defined ZLIB_VERSION
773
		strbuf_addf(buf, "zlib: %s\n", ZLIB_VERSION);
774
#endif
775
	}
776
}
777

778
int cmd_version(int argc, const char **argv, const char *prefix)
779
{
780
	struct strbuf buf = STRBUF_INIT;
781
	int build_options = 0;
782
	const char * const usage[] = {
783
		N_("git version [--build-options]"),
784
		NULL
785
	};
786
	struct option options[] = {
787
		OPT_BOOL(0, "build-options", &build_options,
788
			 "also print build options"),
789
		OPT_END()
790
	};
791

792
	argc = parse_options(argc, argv, prefix, options, usage, 0);
793

794
	get_version_info(&buf, build_options);
795
	printf("%s", buf.buf);
796

797
	strbuf_release(&buf);
798

799
	return 0;
800
}
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
{
811
	struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
812
	char *branch = strrchr(refname, '/') + 1;
813

814
	/* A remote branch of the same name is deemed similar */
815
	if (starts_with(refname, "refs/remotes/") &&
816
	    !strcmp(branch, cb->base_ref))
817
		string_list_append_nodup(cb->similar_refs,
818
					 refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), refname, 1));
819
	return 0;
820
}
821

822
static struct string_list guess_refs(const char *ref)
823
{
824
	struct similar_ref_cb ref_cb;
825
	struct string_list similar_refs = STRING_LIST_INIT_DUP;
826

827
	ref_cb.base_ref = ref;
828
	ref_cb.similar_refs = &similar_refs;
829
	refs_for_each_ref(get_main_ref_store(the_repository),
830
			  append_similar_ref, &ref_cb);
831
	return similar_refs;
832
}
833

834
NORETURN void help_unknown_ref(const char *ref, const char *cmd,
835
			       const char *error)
836
{
837
	int i;
838
	struct string_list suggested_refs = guess_refs(ref);
839

840
	fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
841

842
	if (suggested_refs.nr > 0) {
843
		fprintf_ln(stderr,
844
			   Q_("\nDid you mean this?",
845
			      "\nDid you mean one of these?",
846
			      suggested_refs.nr));
847
		for (i = 0; i < suggested_refs.nr; i++)
848
			fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
849
	}
850

851
	string_list_clear(&suggested_refs, 0);
852
	exit(1);
853
}
854

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.