cherry-pick/revert: transparently refresh index
A stat-dirty index is not a detail that ought to concern the operator of porcelain such as "git cherry-pick". Without this change, a cherry-pick after copying a worktree with rsync errors out with a misleading message. $ git cherry-pick build/top error: Your local changes to 'file.h' would be overwritten by merge. Aborting. Please, commit your changes or stash them before you can merge. Noticed-by: Pete Wyckoff <pw@padd.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Oct 31, 2010 at 14:59 UTC
f6ce1f25885f3db71935b201c5e3c858eb993f5c
2 files changed
+26
-2
builtin/revert.c
+16
-2
@@ -547,6 +547,21 @@ static void prepare_revs(struct rev_info *revs)
547
die("empty commit set passed");
548
}
549
550
+static void read_and_refresh_cache(const char *me)
551
+{
552
+ static struct lock_file index_lock;
553
+ int index_fd = hold_locked_index(&index_lock, 0);
554
+ if (read_index_preload(&the_index, NULL) < 0)
555
+ die("git %s: failed to read the index", me);
556
+ refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL);
557
+ if (the_index.cache_changed) {
558
+ if (write_index(&the_index, index_fd) ||
559
+ commit_locked_index(&index_lock))
560
+ die("git %s: failed to refresh the index", me);
561
+ }
562
+ rollback_lock_file(&index_lock);
563
+}
564
+
565
static int revert_or_cherry_pick(int argc, const char **argv)
566
{
567
struct rev_info revs;
@@ -567,8 +582,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
582
die("cherry-pick --ff cannot be used with --edit");
583
}
584
570
- if (read_cache() < 0)
571
- die("git %s: failed to read the index", me);
585
+ read_and_refresh_cache(me);
586
587
prepare_revs(&revs);
588
t/t3501-revert-cherry-pick.sh
+10
@@ -81,6 +81,16 @@ test_expect_success 'revert after renaming branch' '
81
82
'
83
84
+test_expect_success 'cherry-pick on stat-dirty working tree' '
85
+ git clone . copy &&
86
+ (
87
+ cd copy &&
88
+ git checkout initial &&
89
+ test-chmtime +40 oops &&
90
+ git cherry-pick added
91
+ )
92
+'
93
+
94
test_expect_success 'revert forbidden on dirty working tree' '
95
96
echo content >extra_file &&