Skip to content

Commit 8944969

Browse files
pyokagangitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 81dbd76 commit 8944969

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

builtin/pull.c

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "remote.h"
1515
#include "dir.h"
1616
#include "refs.h"
17+
#include "revision.h"
18+
#include "lockfile.h"
1719

1820
enum rebase_type {
1921
REBASE_INVALID = -1,
@@ -295,6 +297,73 @@ static enum rebase_type config_get_rebase(void)
295297
return REBASE_FALSE;
296298
}
297299

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+
298367
/**
299368
* Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
300369
* into merge_heads.
@@ -751,9 +820,15 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
751820
if (get_sha1("HEAD", orig_head))
752821
hashclr(orig_head);
753822

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+
755829
if (get_rebase_fork_point(rebase_fork_point, repo, *refspecs))
756830
hashclr(rebase_fork_point);
831+
}
757832

758833
if (run_fetch(repo, refspecs))
759834
return 1;

0 commit comments

Comments
 (0)