4 #include "run-command.h"
5 #include "levenshtein.h"
7 #include "common-cmds.h"
8 #include "string-list.h"
13 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
16 FLEX_ALLOC_MEM(ent
, name
, name
, len
);
19 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
20 cmds
->names
[cmds
->cnt
++] = ent
;
23 static void clean_cmdnames(struct cmdnames
*cmds
)
26 for (i
= 0; i
< cmds
->cnt
; ++i
)
33 static int cmdname_compare(const void *a_
, const void *b_
)
35 struct cmdname
*a
= *(struct cmdname
**)a_
;
36 struct cmdname
*b
= *(struct cmdname
**)b_
;
37 return strcmp(a
->name
, b
->name
);
40 static void uniq(struct cmdnames
*cmds
)
47 for (i
= j
= 1; i
< cmds
->cnt
; i
++) {
48 if (!strcmp(cmds
->names
[i
]->name
, cmds
->names
[j
-1]->name
))
51 cmds
->names
[j
++] = cmds
->names
[i
];
57 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
63 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
64 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
66 cmds
->names
[cj
++] = cmds
->names
[ci
++];
69 free(cmds
->names
[ci
++]);
74 while (ci
< cmds
->cnt
)
75 cmds
->names
[cj
++] = cmds
->names
[ci
++];
80 static void pretty_print_cmdnames(struct cmdnames
*cmds
, unsigned int colopts
)
82 struct string_list list
= STRING_LIST_INIT_NODUP
;
83 struct column_options copts
;
86 for (i
= 0; i
< cmds
->cnt
; i
++)
87 string_list_append(&list
, cmds
->names
[i
]->name
);
89 * always enable column display, we only consult column.*
90 * about layout strategy and stuff
92 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
93 memset(&copts
, 0, sizeof(copts
));
96 print_columns(&list
, colopts
, &copts
);
97 string_list_clear(&list
, 0);
100 static void list_commands_in_dir(struct cmdnames
*cmds
,
104 DIR *dir
= opendir(path
);
106 struct strbuf buf
= STRBUF_INIT
;
114 strbuf_addf(&buf
, "%s/", path
);
117 while ((de
= readdir(dir
)) != NULL
) {
121 if (!skip_prefix(de
->d_name
, prefix
, &ent
))
124 strbuf_setlen(&buf
, len
);
125 strbuf_addstr(&buf
, de
->d_name
);
126 if (!is_executable(buf
.buf
))
129 entlen
= strlen(ent
);
130 strip_suffix(ent
, ".exe", &entlen
);
132 add_cmdname(cmds
, ent
, entlen
);
135 strbuf_release(&buf
);
138 void load_command_list(const char *prefix
,
139 struct cmdnames
*main_cmds
,
140 struct cmdnames
*other_cmds
)
142 const char *env_path
= getenv("PATH");
143 const char *exec_path
= git_exec_path();
146 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
147 QSORT(main_cmds
->names
, main_cmds
->cnt
, cmdname_compare
);
152 char *paths
, *path
, *colon
;
153 path
= paths
= xstrdup(env_path
);
155 if ((colon
= strchr(path
, PATH_SEP
)))
157 if (!exec_path
|| strcmp(path
, exec_path
))
158 list_commands_in_dir(other_cmds
, path
, prefix
);
166 QSORT(other_cmds
->names
, other_cmds
->cnt
, cmdname_compare
);
169 exclude_cmds(other_cmds
, main_cmds
);
172 void list_commands(unsigned int colopts
,
173 struct cmdnames
*main_cmds
, struct cmdnames
*other_cmds
)
175 if (main_cmds
->cnt
) {
176 const char *exec_path
= git_exec_path();
177 printf_ln(_("available git commands in '%s'"), exec_path
);
179 pretty_print_cmdnames(main_cmds
, colopts
);
183 if (other_cmds
->cnt
) {
184 printf_ln(_("git commands available from elsewhere on your $PATH"));
186 pretty_print_cmdnames(other_cmds
, colopts
);
191 static int cmd_group_cmp(const void *elem1
, const void *elem2
)
193 const struct cmdname_help
*e1
= elem1
;
194 const struct cmdname_help
*e2
= elem2
;
196 if (e1
->group
< e2
->group
)
198 if (e1
->group
> e2
->group
)
200 return strcmp(e1
->name
, e2
->name
);
203 void list_common_cmds_help(void)
206 int current_grp
= -1;
208 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
209 if (longest
< strlen(common_cmds
[i
].name
))
210 longest
= strlen(common_cmds
[i
].name
);
213 QSORT(common_cmds
, ARRAY_SIZE(common_cmds
), cmd_group_cmp
);
215 puts(_("These are common Git commands used in various situations:"));
217 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
218 if (common_cmds
[i
].group
!= current_grp
) {
219 printf("\n%s\n", _(common_cmd_groups
[common_cmds
[i
].group
]));
220 current_grp
= common_cmds
[i
].group
;
223 printf(" %s ", common_cmds
[i
].name
);
224 mput_char(' ', longest
- strlen(common_cmds
[i
].name
));
225 puts(_(common_cmds
[i
].help
));
229 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
232 for (i
= 0; i
< c
->cnt
; i
++)
233 if (!strcmp(s
, c
->names
[i
]->name
))
238 static int autocorrect
;
239 static struct cmdnames aliases
;
241 static int git_unknown_cmd_config(const char *var
, const char *value
, void *cb
)
245 if (!strcmp(var
, "help.autocorrect"))
246 autocorrect
= git_config_int(var
,value
);
247 /* Also use aliases for command lookup */
248 if (skip_prefix(var
, "alias.", &p
))
249 add_cmdname(&aliases
, p
, strlen(p
));
251 return git_default_config(var
, value
, cb
);
254 static int levenshtein_compare(const void *p1
, const void *p2
)
256 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
257 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
260 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
263 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
266 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
268 for (i
= 0; i
< old
->cnt
; i
++)
269 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
275 /* An empirically derived magic number */
276 #define SIMILARITY_FLOOR 7
277 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
279 static const char bad_interpreter_advice
[] =
280 N_("'%s' appears to be a git command, but we were not\n"
281 "able to execute it. Maybe git-%s is broken?");
283 const char *help_unknown_cmd(const char *cmd
)
285 int i
, n
, best_similarity
= 0;
286 struct cmdnames main_cmds
, other_cmds
;
288 memset(&main_cmds
, 0, sizeof(main_cmds
));
289 memset(&other_cmds
, 0, sizeof(other_cmds
));
290 memset(&aliases
, 0, sizeof(aliases
));
292 git_config(git_unknown_cmd_config
, NULL
);
294 load_command_list("git-", &main_cmds
, &other_cmds
);
296 add_cmd_list(&main_cmds
, &aliases
);
297 add_cmd_list(&main_cmds
, &other_cmds
);
298 QSORT(main_cmds
.names
, main_cmds
.cnt
, cmdname_compare
);
301 /* This abuses cmdname->len for levenshtein distance */
302 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
303 int cmp
= 0; /* avoid compiler stupidity */
304 const char *candidate
= main_cmds
.names
[i
]->name
;
307 * An exact match means we have the command, but
308 * for some reason exec'ing it gave us ENOENT; probably
309 * it's a bad interpreter in the #! line.
311 if (!strcmp(candidate
, cmd
))
312 die(_(bad_interpreter_advice
), cmd
, cmd
);
314 /* Does the candidate appear in common_cmds list? */
315 while (n
< ARRAY_SIZE(common_cmds
) &&
316 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
318 if ((n
< ARRAY_SIZE(common_cmds
)) && !cmp
) {
319 /* Yes, this is one of the common commands */
320 n
++; /* use the entry from common_cmds[] */
321 if (starts_with(candidate
, cmd
)) {
322 /* Give prefix match a very good score */
323 main_cmds
.names
[i
]->len
= 0;
328 main_cmds
.names
[i
]->len
=
329 levenshtein(cmd
, candidate
, 0, 2, 1, 3) + 1;
332 QSORT(main_cmds
.names
, main_cmds
.cnt
, levenshtein_compare
);
335 die(_("Uh oh. Your system reports no Git commands at all."));
337 /* skip and count prefix matches */
338 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
339 ; /* still counting */
341 if (main_cmds
.cnt
<= n
) {
342 /* prefix matches with everything? that is too ambiguous */
343 best_similarity
= SIMILARITY_FLOOR
+ 1;
345 /* count all the most similar ones */
346 for (best_similarity
= main_cmds
.names
[n
++]->len
;
347 (n
< main_cmds
.cnt
&&
348 best_similarity
== main_cmds
.names
[n
]->len
);
350 ; /* still counting */
352 if (autocorrect
&& n
== 1 && SIMILAR_ENOUGH(best_similarity
)) {
353 const char *assumed
= main_cmds
.names
[0]->name
;
354 main_cmds
.names
[0] = NULL
;
355 clean_cmdnames(&main_cmds
);
357 _("WARNING: You called a Git command named '%s', "
358 "which does not exist.\n"
359 "Continuing under the assumption that you meant '%s'"),
361 if (autocorrect
> 0) {
362 fprintf_ln(stderr
, _("in %0.1f seconds automatically..."),
363 (float)autocorrect
/10.0);
364 sleep_millisec(autocorrect
* 100);
369 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
371 if (SIMILAR_ENOUGH(best_similarity
)) {
373 Q_("\nThe most similar command is",
374 "\nThe most similar commands are",
377 for (i
= 0; i
< n
; i
++)
378 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
384 int cmd_version(int argc
, const char **argv
, const char *prefix
)
387 * The format of this string should be kept stable for compatibility
388 * with external projects that rely on the output of "git version".
390 printf("git version %s\n", git_version_string
);
392 if (!strcmp(*argv
, "--build-options")) {
393 printf("sizeof-long: %d\n", (int)sizeof(long));
394 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
400 struct similar_ref_cb
{
401 const char *base_ref
;
402 struct string_list
*similar_refs
;
405 static int append_similar_ref(const char *refname
, const struct object_id
*oid
,
406 int flags
, void *cb_data
)
408 struct similar_ref_cb
*cb
= (struct similar_ref_cb
*)(cb_data
);
409 char *branch
= strrchr(refname
, '/') + 1;
412 /* A remote branch of the same name is deemed similar */
413 if (skip_prefix(refname
, "refs/remotes/", &remote
) &&
414 !strcmp(branch
, cb
->base_ref
))
415 string_list_append(cb
->similar_refs
, remote
);
419 static struct string_list
guess_refs(const char *ref
)
421 struct similar_ref_cb ref_cb
;
422 struct string_list similar_refs
= STRING_LIST_INIT_NODUP
;
424 ref_cb
.base_ref
= ref
;
425 ref_cb
.similar_refs
= &similar_refs
;
426 for_each_ref(append_similar_ref
, &ref_cb
);
430 void help_unknown_ref(const char *ref
, const char *cmd
, const char *error
)
433 struct string_list suggested_refs
= guess_refs(ref
);
435 fprintf_ln(stderr
, _("%s: %s - %s"), cmd
, ref
, error
);
437 if (suggested_refs
.nr
> 0) {
439 Q_("\nDid you mean this?",
440 "\nDid you mean one of these?",
442 for (i
= 0; i
< suggested_refs
.nr
; i
++)
443 fprintf(stderr
, "\t%s\n", suggested_refs
.items
[i
].string
);
446 string_list_clear(&suggested_refs
, 0);