Skip to content

Commit 1197c22

Browse files
committed
Merge branch 'vv/help-unknown-ref'
Detect "git merge foo" that might have meant "git merge origin/foo" and give an error message that is more specific than "foo is not something we can merge". * vv/help-unknown-ref: merge: use help_unknown_ref() help: add help_unknown_ref()
2 parents 41aaccd + f3f8af0 commit 1197c22

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

builtin/merge.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,8 @@ static struct commit_list *collect_parents(struct commit *head_commit,
10541054
for (i = 0; i < argc; i++) {
10551055
struct commit *commit = get_merge_parent(argv[i]);
10561056
if (!commit)
1057-
die(_("%s - not something we can merge"), argv[i]);
1057+
help_unknown_ref(argv[i], "merge",
1058+
"not something we can merge");
10581059
remotes = &commit_list_insert(commit, remotes)->next;
10591060
}
10601061
*remotes = NULL;

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)