|
14 | 14 | #include "remote.h"
|
15 | 15 | #include "dir.h"
|
16 | 16 | #include "refs.h"
|
| 17 | +#include "revision.h" |
| 18 | +#include "lockfile.h" |
17 | 19 |
|
18 | 20 | enum rebase_type {
|
19 | 21 | REBASE_INVALID = -1,
|
@@ -295,6 +297,73 @@ static enum rebase_type config_get_rebase(void)
|
295 | 297 | return REBASE_FALSE;
|
296 | 298 | }
|
297 | 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 | + |
298 | 367 | /**
|
299 | 368 | * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
|
300 | 369 | * into merge_heads.
|
@@ -751,9 +820,15 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
|
751 | 820 | if (get_sha1("HEAD", orig_head))
|
752 | 821 | hashclr(orig_head);
|
753 | 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 | + |
755 | 829 | if (get_rebase_fork_point(rebase_fork_point, repo, *refspecs))
|
756 | 830 | hashclr(rebase_fork_point);
|
| 831 | + } |
757 | 832 |
|
758 | 833 | if (run_fetch(repo, refspecs))
|
759 | 834 | return 1;
|
|
0 commit comments