pull --rebase: exit early when the working directory is dirty

Re-implement the behavior introduced by f9189cf (pull --rebase: exit early when the working directory is dirty, 2008-05-21). 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 8944969c20e80f13965905ecd2ccccac36a34831
1 file changed +76 -1
builtin/pull.c
+76 -1
@@ -14,6 +14,8 @@
14 #include "remote.h"
15 #include "dir.h"
16 #include "refs.h"
17 +#include "revision.h"
18 +#include "lockfile.h"
19
20 enum rebase_type {
21 REBASE_INVALID = -1,
@@ -295,6 +297,73 @@ static enum rebase_type config_get_rebase(void)
297 return REBASE_FALSE;
298 }
299
300 +/**
301 + * Returns 1 if there are unstaged changes, 0 otherwise.
302 + */
303 +static int has_unstaged_changes(const char *prefix)
304 +{
305 + struct rev_info rev_info;
306 + int result;
307 +
308 + init_revisions(&rev_info, prefix);
309 + DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
310 + DIFF_OPT_SET(&rev_info.diffopt, QUICK);
311 + diff_setup_done(&rev_info.diffopt);
312 + result = run_diff_files(&rev_info, 0);
313 + return diff_result_code(&rev_info.diffopt, result);
314 +}
315 +
316 +/**
317 + * Returns 1 if there are uncommitted changes, 0 otherwise.
318 + */
319 +static int has_uncommitted_changes(const char *prefix)
320 +{
321 + struct rev_info rev_info;
322 + int result;
323 +
324 + if (is_cache_unborn())
325 + return 0;
326 +
327 + init_revisions(&rev_info, prefix);
328 + DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
329 + DIFF_OPT_SET(&rev_info.diffopt, QUICK);
330 + add_head_to_pending(&rev_info);
331 + diff_setup_done(&rev_info.diffopt);
332 + result = run_diff_index(&rev_info, 1);
333 + return diff_result_code(&rev_info.diffopt, result);
334 +}
335 +
336 +/**
337 + * If the work tree has unstaged or uncommitted changes, dies with the
338 + * appropriate message.
339 + */
340 +static void die_on_unclean_work_tree(const char *prefix)
341 +{
342 + struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file));
343 + int do_die = 0;
344 +
345 + hold_locked_index(lock_file, 0);
346 + refresh_cache(REFRESH_QUIET);
347 + update_index_if_able(&the_index, lock_file);
348 + rollback_lock_file(lock_file);
349 +
350 + if (has_unstaged_changes(prefix)) {
351 + error(_("Cannot pull with rebase: You have unstaged changes."));
352 + do_die = 1;
353 + }
354 +
355 + if (has_uncommitted_changes(prefix)) {
356 + if (do_die)
357 + error(_("Additionally, your index contains uncommitted changes."));
358 + else
359 + error(_("Cannot pull with rebase: Your index contains uncommitted changes."));
360 + do_die = 1;
361 + }
362 +
363 + if (do_die)
364 + exit(1);
365 +}
366 +
367 /**
368 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
369 * into merge_heads.
@@ -751,9 +820,15 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
820 if (get_sha1("HEAD", orig_head))
821 hashclr(orig_head);
822
754 - if (opt_rebase)
823 + if (opt_rebase) {
824 + if (is_null_sha1(orig_head) && !is_cache_unborn())
825 + die(_("Updating an unborn branch with changes added to the index."));
826 +
827 + die_on_unclean_work_tree(prefix);
828 +
829 if (get_rebase_fork_point(rebase_fork_point, repo, *refspecs))
830 hashclr(rebase_fork_point);
831 + }
832
833 if (run_fetch(repo, refspecs))
834 return 1;