pull: error on no merge candidates

Commit a8c9bef (pull: improve advice for unconfigured error case, 2009-10-05) fully established the current advices given by git-pull for the different cases where git-fetch will not have anything marked for merge: 1. We fetched from a specific remote, and a refspec was given, but it ended up not fetching anything. This is usually because the user provided a wildcard refspec which had no matches on the remote end. 2. We fetched from a non-default remote, but didn't specify a branch to merge. We can't use the configured one because it applies to the default remote, and thus the user must specify the branches to merge. 3. We fetched from the branch's or repo's default remote, but: a. We are not on a branch, so there will never be a configured branch to merge with. b. We are on a branch, but there is no configured branch to merge with. 4. We fetched from the branch's or repo's default remote, but the configured branch to merge didn't get fetched (either it doesn't exist, or wasn't part of the configured fetch refspec) Re-implement the above behavior by implementing get_merge_heads() to parse the heads in FETCH_HEAD for merging, and implementing die_no_merge_candidates(), which will be called when FETCH_HEAD has no heads for merging. Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Paul Tan <pyokagan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Paul Tan committed Jun 18, 2015 at 18:54 UTC 44c175c7a46b3a0446e046bdaa566adb9e57d89d
1 file changed +113
builtin/pull.c
+113
@@ -10,6 +10,8 @@
10 #include "parse-options.h"
11 #include "exec_cmd.h"
12 #include "run-command.h"
13 +#include "sha1-array.h"
14 +#include "remote.h"
15
16 static const char * const pull_usage[] = {
17 N_("git pull [options] [<repository> [<refspec>...]]"),
@@ -164,6 +166,111 @@ static void argv_push_force(struct argv_array *arr)
166 argv_array_push(arr, "-f");
167 }
168
169 +/**
170 + * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
171 + * into merge_heads.
172 + */
173 +static void get_merge_heads(struct sha1_array *merge_heads)
174 +{
175 + const char *filename = git_path("FETCH_HEAD");
176 + FILE *fp;
177 + struct strbuf sb = STRBUF_INIT;
178 + unsigned char sha1[GIT_SHA1_RAWSZ];
179 +
180 + if (!(fp = fopen(filename, "r")))
181 + die_errno(_("could not open '%s' for reading"), filename);
182 + while (strbuf_getline(&sb, fp, '\n') != EOF) {
183 + if (get_sha1_hex(sb.buf, sha1))
184 + continue; /* invalid line: does not start with SHA1 */
185 + if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t"))
186 + continue; /* ref is not-for-merge */
187 + sha1_array_append(merge_heads, sha1);
188 + }
189 + fclose(fp);
190 + strbuf_release(&sb);
191 +}
192 +
193 +/**
194 + * Used by die_no_merge_candidates() as a for_each_remote() callback to
195 + * retrieve the name of the remote if the repository only has one remote.
196 + */
197 +static int get_only_remote(struct remote *remote, void *cb_data)
198 +{
199 + const char **remote_name = cb_data;
200 +
201 + if (*remote_name)
202 + return -1;
203 +
204 + *remote_name = remote->name;
205 + return 0;
206 +}
207 +
208 +/**
209 + * Dies with the appropriate reason for why there are no merge candidates:
210 + *
211 + * 1. We fetched from a specific remote, and a refspec was given, but it ended
212 + * up not fetching anything. This is usually because the user provided a
213 + * wildcard refspec which had no matches on the remote end.
214 + *
215 + * 2. We fetched from a non-default remote, but didn't specify a branch to
216 + * merge. We can't use the configured one because it applies to the default
217 + * remote, thus the user must specify the branches to merge.
218 + *
219 + * 3. We fetched from the branch's or repo's default remote, but:
220 + *
221 + * a. We are not on a branch, so there will never be a configured branch to
222 + * merge with.
223 + *
224 + * b. We are on a branch, but there is no configured branch to merge with.
225 + *
226 + * 4. We fetched from the branch's or repo's default remote, but the configured
227 + * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't
228 + * part of the configured fetch refspec.)
229 + */
230 +static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs)
231 +{
232 + struct branch *curr_branch = branch_get("HEAD");
233 + const char *remote = curr_branch ? curr_branch->remote_name : NULL;
234 +
235 + if (*refspecs) {
236 + fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched."));
237 + fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n"
238 + "matches on the remote end."));
239 + } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) {
240 + fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n"
241 + "a branch. Because this is not the default configured remote\n"
242 + "for your current branch, you must specify a branch on the command line."),
243 + repo);
244 + } else if (!curr_branch) {
245 + fprintf_ln(stderr, _("You are not currently on a branch."));
246 + fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
247 + fprintf_ln(stderr, _("See git-pull(1) for details."));
248 + fprintf(stderr, "\n");
249 + fprintf_ln(stderr, " git pull <remote> <branch>");
250 + fprintf(stderr, "\n");
251 + } else if (!curr_branch->merge_nr) {
252 + const char *remote_name = NULL;
253 +
254 + if (for_each_remote(get_only_remote, &remote_name) || !remote_name)
255 + remote_name = "<remote>";
256 +
257 + fprintf_ln(stderr, _("There is no tracking information for the current branch."));
258 + fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
259 + fprintf_ln(stderr, _("See git-pull(1) for details."));
260 + fprintf(stderr, "\n");
261 + fprintf_ln(stderr, " git pull <remote> <branch>");
262 + fprintf(stderr, "\n");
263 + fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:\n"
264 + "\n"
265 + " git branch --set-upstream-to=%s/<branch> %s\n"),
266 + remote_name, curr_branch->name);
267 + } else
268 + fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n"
269 + "from the remote, but no such ref was fetched."),
270 + *curr_branch->merge_name);
271 + exit(1);
272 +}
273 +
274 /**
275 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
276 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
@@ -277,6 +384,7 @@ static int run_merge(void)
384 int cmd_pull(int argc, const char **argv, const char *prefix)
385 {
386 const char *repo, **refspecs;
387 + struct sha1_array merge_heads = SHA1_ARRAY_INIT;
388
389 if (!getenv("_GIT_USE_BUILTIN_PULL")) {
390 const char *path = mkpath("%s/git-pull", git_exec_path());
@@ -295,5 +403,10 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
403 if (opt_dry_run)
404 return 0;
405
406 + get_merge_heads(&merge_heads);
407 +
408 + if (!merge_heads.nr)
409 + die_no_merge_candidates(repo, refspecs);
410 +
411 return run_merge();
412 }