advice.c 5.93 KB
Newer Older
1
#include "cache.h"
2
#include "config.h"
Ryan Dammrose's avatar
Ryan Dammrose committed
3
#include "color.h"
4
#include "help.h"
5

6
int advice_push_update_rejected = 1;
7 8
int advice_push_non_ff_current = 1;
int advice_push_non_ff_matching = 1;
9
int advice_push_already_exists = 1;
10 11
int advice_push_fetch_first = 1;
int advice_push_needs_force = 1;
12
int advice_push_unqualified_ref_name = 1;
13
int advice_status_hints = 1;
14
int advice_status_u_option = 1;
15
int advice_commit_before_merge = 1;
16
int advice_reset_quiet_warning = 1;
17
int advice_resolve_conflict = 1;
18
int advice_implicit_identity = 1;
19
int advice_detached_head = 1;
20
int advice_set_upstream_failure = 1;
21
int advice_object_name_warning = 1;
22
int advice_amworkdir = 1;
23
int advice_rm_hints = 1;
24
int advice_add_embedded_repo = 1;
25
int advice_ignored_hook = 1;
26
int advice_waiting_for_editor = 1;
27
int advice_graft_file_deprecated = 1;
28
int advice_checkout_ambiguous_remote_branch_name = 1;
29

Ryan Dammrose's avatar
Ryan Dammrose committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
static int advice_use_color = -1;
static char advice_colors[][COLOR_MAXLEN] = {
	GIT_COLOR_RESET,
	GIT_COLOR_YELLOW,	/* HINT */
};

enum color_advice {
	ADVICE_COLOR_RESET = 0,
	ADVICE_COLOR_HINT = 1,
};

static int parse_advise_color_slot(const char *slot)
{
	if (!strcasecmp(slot, "reset"))
		return ADVICE_COLOR_RESET;
	if (!strcasecmp(slot, "hint"))
		return ADVICE_COLOR_HINT;
	return -1;
}

static const char *advise_get_color(enum color_advice ix)
{
	if (want_color_stderr(advice_use_color))
		return advice_colors[ix];
	return "";
}

57 58 59 60
static struct {
	const char *name;
	int *preference;
} advice_config[] = {
61 62 63 64 65 66
	{ "pushUpdateRejected", &advice_push_update_rejected },
	{ "pushNonFFCurrent", &advice_push_non_ff_current },
	{ "pushNonFFMatching", &advice_push_non_ff_matching },
	{ "pushAlreadyExists", &advice_push_already_exists },
	{ "pushFetchFirst", &advice_push_fetch_first },
	{ "pushNeedsForce", &advice_push_needs_force },
67
	{ "pushUnqualifiedRefName", &advice_push_unqualified_ref_name },
68 69 70
	{ "statusHints", &advice_status_hints },
	{ "statusUoption", &advice_status_u_option },
	{ "commitBeforeMerge", &advice_commit_before_merge },
71
	{ "resetQuiet", &advice_reset_quiet_warning },
72 73 74 75 76
	{ "resolveConflict", &advice_resolve_conflict },
	{ "implicitIdentity", &advice_implicit_identity },
	{ "detachedHead", &advice_detached_head },
	{ "setupStreamFailure", &advice_set_upstream_failure },
	{ "objectNameWarning", &advice_object_name_warning },
77
	{ "amWorkDir", &advice_amworkdir },
78 79 80 81 82
	{ "rmHints", &advice_rm_hints },
	{ "addEmbeddedRepo", &advice_add_embedded_repo },
	{ "ignoredHook", &advice_ignored_hook },
	{ "waitingForEditor", &advice_waiting_for_editor },
	{ "graftFileDeprecated", &advice_graft_file_deprecated },
83
	{ "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
84 85

	/* make this an alias for backward compatibility */
86
	{ "pushNonFastForward", &advice_push_update_rejected }
87 88
};

89 90
void advise(const char *advice, ...)
{
91
	struct strbuf buf = STRBUF_INIT;
92
	va_list params;
93
	const char *cp, *np;
94 95

	va_start(params, advice);
96
	strbuf_vaddf(&buf, advice, params);
97
	va_end(params);
98 99 100

	for (cp = buf.buf; *cp; cp = np) {
		np = strchrnul(cp, '\n');
Ryan Dammrose's avatar
Ryan Dammrose committed
101 102 103 104
		fprintf(stderr,	_("%shint: %.*s%s\n"),
			advise_get_color(ADVICE_COLOR_HINT),
			(int)(np - cp), cp,
			advise_get_color(ADVICE_COLOR_RESET));
105 106 107 108
		if (*np)
			np++;
	}
	strbuf_release(&buf);
109 110
}

111 112
int git_default_advice_config(const char *var, const char *value)
{
Ryan Dammrose's avatar
Ryan Dammrose committed
113
	const char *k, *slot_name;
114 115
	int i;

Ryan Dammrose's avatar
Ryan Dammrose committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129
	if (!strcmp(var, "color.advice")) {
		advice_use_color = git_config_colorbool(var, value);
		return 0;
	}

	if (skip_prefix(var, "color.advice.", &slot_name)) {
		int slot = parse_advise_color_slot(slot_name);
		if (slot < 0)
			return 0;
		if (!value)
			return config_error_nonbool(var);
		return color_parse(value, advice_colors[slot]);
	}

130 131 132
	if (!skip_prefix(var, "advice.", &k))
		return 0;

133
	for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
134
		if (strcasecmp(k, advice_config[i].name))
135 136 137 138 139 140 141
			continue;
		*advice_config[i].preference = git_config_bool(var, value);
		return 0;
	}

	return 0;
}
142

143 144 145 146 147 148 149 150
void list_config_advices(struct string_list *list, const char *prefix)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(advice_config); i++)
		list_config_item(list, prefix, advice_config[i].name);
}

151
int error_resolve_conflict(const char *me)
152
{
153 154 155 156 157 158 159 160 161 162 163 164 165 166
	if (!strcmp(me, "cherry-pick"))
		error(_("Cherry-picking is not possible because you have unmerged files."));
	else if (!strcmp(me, "commit"))
		error(_("Committing is not possible because you have unmerged files."));
	else if (!strcmp(me, "merge"))
		error(_("Merging is not possible because you have unmerged files."));
	else if (!strcmp(me, "pull"))
		error(_("Pulling is not possible because you have unmerged files."));
	else if (!strcmp(me, "revert"))
		error(_("Reverting is not possible because you have unmerged files."));
	else
		error(_("It is not possible to %s because you have unmerged files."),
			me);

167
	if (advice_resolve_conflict)
168 169 170 171
		/*
		 * Message used both when 'git commit' fails and when
		 * other commands doing a merge do.
		 */
172
		advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
173
			 "as appropriate to mark resolution and make a commit."));
174 175 176 177 178 179
	return -1;
}

void NORETURN die_resolve_conflict(const char *me)
{
	error_resolve_conflict(me);
180
	die(_("Exiting because of an unresolved conflict."));
181
}
182

183 184 185 186
void NORETURN die_conclude_merge(void)
{
	error(_("You have not concluded your merge (MERGE_HEAD exists)."));
	if (advice_resolve_conflict)
187
		advise(_("Please, commit your changes before merging."));
188 189 190
	die(_("Exiting because of unfinished merge."));
}

191 192
void detach_advice(const char *new_name)
{
193 194
	const char *fmt =
	_("Note: checking out '%s'.\n\n"
195 196 197 198 199
	"You are in 'detached HEAD' state. You can look around, make experimental\n"
	"changes and commit them, and you can discard any commits you make in this\n"
	"state without impacting any branches by performing another checkout.\n\n"
	"If you want to create a new branch to retain commits you create, you may\n"
	"do so (now or later) by using -b with the checkout command again. Example:\n\n"
200
	"  git checkout -b <new-branch-name>\n\n");
201 202 203

	fprintf(stderr, fmt, new_name);
}