Skip to content

Commit f6ce1f2

Browse files
jrngitster
authored andcommitted
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 <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent af77aee commit f6ce1f2

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

builtin/revert.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,21 @@ static void prepare_revs(struct rev_info *revs)
547547
die("empty commit set passed");
548548
}
549549

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+
550565
static int revert_or_cherry_pick(int argc, const char **argv)
551566
{
552567
struct rev_info revs;
@@ -567,8 +582,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
567582
die("cherry-pick --ff cannot be used with --edit");
568583
}
569584

570-
if (read_cache() < 0)
571-
die("git %s: failed to read the index", me);
585+
read_and_refresh_cache(me);
572586

573587
prepare_revs(&revs);
574588

t/t3501-revert-cherry-pick.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ test_expect_success 'revert after renaming branch' '
8181
8282
'
8383

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+
8494
test_expect_success 'revert forbidden on dirty working tree' '
8595
8696
echo content >extra_file &&

0 commit comments

Comments
 (0)