Skip to content

Commit

Permalink
cherry-pick/revert: transparently refresh index
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jonathan Nieder authored and Junio C Hamano committed Nov 10, 2010
1 parent af77aee commit f6ce1f2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
18 changes: 16 additions & 2 deletions builtin/revert.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,21 @@ static void prepare_revs(struct rev_info *revs)
die("empty commit set passed");
}

static void read_and_refresh_cache(const char *me)
{
static struct lock_file index_lock;
int index_fd = hold_locked_index(&index_lock, 0);
if (read_index_preload(&the_index, NULL) < 0)
die("git %s: failed to read the index", me);
refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL);
if (the_index.cache_changed) {
if (write_index(&the_index, index_fd) ||
commit_locked_index(&index_lock))
die("git %s: failed to refresh the index", me);
}
rollback_lock_file(&index_lock);
}

static int revert_or_cherry_pick(int argc, const char **argv)
{
struct rev_info revs;
Expand All @@ -567,8 +582,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
die("cherry-pick --ff cannot be used with --edit");
}

if (read_cache() < 0)
die("git %s: failed to read the index", me);
read_and_refresh_cache(me);

prepare_revs(&revs);

Expand Down
10 changes: 10 additions & 0 deletions t/t3501-revert-cherry-pick.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ test_expect_success 'revert after renaming branch' '
'

test_expect_success 'cherry-pick on stat-dirty working tree' '
git clone . copy &&
(
cd copy &&
git checkout initial &&
test-chmtime +40 oops &&
git cherry-pick added
)
'

test_expect_success 'revert forbidden on dirty working tree' '
echo content >extra_file &&
Expand Down

0 comments on commit f6ce1f2

Please sign in to comment.