Skip to content

Commit e561810

Browse files
amravgitster
authored andcommitted
help: add help_unknown_ref()
When the user gives an unknown string to a command that expects to get a ref, we could be more helpful than just saying "that's not a ref" and die. Add helper function help_unknown_ref() to take care of displaying an error message along with a list of suggested refs the user might have meant. An interaction with "git merge" might go like this: $ git merge foo merge: foo - not something we can merge Did you mean one of these? origin/foo upstream/foo Signed-off-by: Vikrant Varma <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9b79519 commit e561810

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

help.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "string-list.h"
88
#include "column.h"
99
#include "version.h"
10+
#include "refs.h"
1011

1112
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
1213
{
@@ -404,3 +405,52 @@ int cmd_version(int argc, const char **argv, const char *prefix)
404405
printf("git version %s\n", git_version_string);
405406
return 0;
406407
}
408+
409+
struct similar_ref_cb {
410+
const char *base_ref;
411+
struct string_list *similar_refs;
412+
};
413+
414+
static int append_similar_ref(const char *refname, const unsigned char *sha1,
415+
int flags, void *cb_data)
416+
{
417+
struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
418+
char *branch = strrchr(refname, '/') + 1;
419+
/* A remote branch of the same name is deemed similar */
420+
if (!prefixcmp(refname, "refs/remotes/") &&
421+
!strcmp(branch, cb->base_ref))
422+
string_list_append(cb->similar_refs,
423+
refname + strlen("refs/remotes/"));
424+
return 0;
425+
}
426+
427+
static struct string_list guess_refs(const char *ref)
428+
{
429+
struct similar_ref_cb ref_cb;
430+
struct string_list similar_refs = STRING_LIST_INIT_NODUP;
431+
432+
ref_cb.base_ref = ref;
433+
ref_cb.similar_refs = &similar_refs;
434+
for_each_ref(append_similar_ref, &ref_cb);
435+
return similar_refs;
436+
}
437+
438+
void help_unknown_ref(const char *ref, const char *cmd, const char *error)
439+
{
440+
int i;
441+
struct string_list suggested_refs = guess_refs(ref);
442+
443+
fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
444+
445+
if (suggested_refs.nr > 0) {
446+
fprintf_ln(stderr,
447+
Q_("\nDid you mean this?",
448+
"\nDid you mean one of these?",
449+
suggested_refs.nr));
450+
for (i = 0; i < suggested_refs.nr; i++)
451+
fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
452+
}
453+
454+
string_list_clear(&suggested_refs, 0);
455+
exit(1);
456+
}

help.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ extern void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes);
2727
extern int is_in_cmdlist(struct cmdnames *cmds, const char *name);
2828
extern void list_commands(unsigned int colopts, struct cmdnames *main_cmds, struct cmdnames *other_cmds);
2929

30+
/*
31+
* call this to die(), when it is suspected that the user mistyped a
32+
* ref to the command, to give suggested "correct" refs.
33+
*/
34+
extern void help_unknown_ref(const char *ref, const char *cmd, const char *error);
3035
#endif /* HELP_H */

0 commit comments

Comments
 (0)