pull: make code more similar to the shell script again

When converting the pull command to a builtin, the require_clean_work_tree() function was renamed and the pull-specific parts hard-coded. This makes it impossible to reuse the code, so let's modify the code to make it more similar to the original shell script again. Note: when the hint "Please commit or stash them" was introduced first, Git did not have the convention of continuing error messages in lower case, but now we do have that convention, therefore we reintroduce this hint down-cased, obeying said convention. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Oct 7, 2016 at 18:08 UTC ea63b393ec76484690733d6f589c9e67fedbaa78
1 file changed +20 -10
builtin/pull.c
+20 -10
@@ -365,10 +365,11 @@ static int has_uncommitted_changes(void)
365 * If the work tree has unstaged or uncommitted changes, dies with the
366 * appropriate message.
367 */
368 -static void die_on_unclean_work_tree(void)
368 +static int require_clean_work_tree(const char *action, const char *hint,
369 + int gently)
370 {
371 struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file));
371 - int do_die = 0;
372 + int err = 0;
373
374 hold_locked_index(lock_file, 0);
375 refresh_cache(REFRESH_QUIET);
@@ -376,20 +377,28 @@ static void die_on_unclean_work_tree(void)
377 rollback_lock_file(lock_file);
378
379 if (has_unstaged_changes()) {
379 - error(_("Cannot pull with rebase: You have unstaged changes."));
380 - do_die = 1;
380 + /* TRANSLATORS: the action is e.g. "pull with rebase" */
381 + error(_("Cannot %s: You have unstaged changes."), _(action));
382 + err = 1;
383 }
384
385 if (has_uncommitted_changes()) {
384 - if (do_die)
386 + if (err)
387 error(_("Additionally, your index contains uncommitted changes."));
388 else
387 - error(_("Cannot pull with rebase: Your index contains uncommitted changes."));
388 - do_die = 1;
389 + error(_("Cannot %s: Your index contains uncommitted changes."),
390 + _(action));
391 + err = 1;
392 }
393
391 - if (do_die)
392 - exit(1);
394 + if (err) {
395 + if (hint)
396 + error("%s", hint);
397 + if (!gently)
398 + exit(128);
399 + }
400 +
401 + return err;
402 }
403
404 /**
@@ -875,7 +884,8 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
884 die(_("Updating an unborn branch with changes added to the index."));
885
886 if (!autostash)
878 - die_on_unclean_work_tree();
887 + require_clean_work_tree(N_("pull with rebase"),
888 + _("please commit or stash them."), 0);
889
890 if (get_rebase_fork_point(rebase_fork_point, repo, *refspecs))
891 hashclr(rebase_fork_point);