mirror of
https://github.com/git/git
synced 2024-08-24 18:26:02 +00:00
checkout $tree $path: do not clobber local changes in $path not in $tree
Checking paths out of a tree is (currently) defined to do: - Grab the paths from the named tree that match the given pathspec, and add them to the index; - Check out the contents from the index for paths that match the pathspec to the working tree; and while at it - If the given pathspec did not match anything, suspect a typo from the command line and error out without updating the index nor the working tree. Suppose that the branch you are working on has dir/myfile, and the "other" branch has dir/other but not dir/myfile. Further imagine that you have either modified or removed dir/myfile in your working tree, but you have not run "git add dir/myfile" or "git rm dir/myfile" to tell Git about your local change. Running $ git checkout other dir would add dir/other to the index with the contents taken out of the "other" branch, and check out the paths from the index that match the pathspec "dir", namely, "dir/other" and "dir/myfile", overwriting your local changes to "dir/myfile", even though "other" branch does not even know about that file. Fix it by updating the working tree only with the index entries that was read from the "other" tree. Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
7ed863a85a
commit
0a1283bc39
|
@ -70,7 +70,7 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,
|
|||
hashcpy(ce->sha1, sha1);
|
||||
memcpy(ce->name, base, baselen);
|
||||
memcpy(ce->name + baselen, pathname, len - baselen);
|
||||
ce->ce_flags = create_ce_flags(len, 0);
|
||||
ce->ce_flags = create_ce_flags(len, 0) | CE_UPDATE;
|
||||
ce->ce_mode = create_ce_mode(mode);
|
||||
add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
|
||||
return 0;
|
||||
|
@ -222,6 +222,8 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
|
|||
|
||||
for (pos = 0; pos < active_nr; pos++) {
|
||||
struct cache_entry *ce = active_cache[pos];
|
||||
if (source_tree && !(ce->ce_flags & CE_UPDATE))
|
||||
continue;
|
||||
match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, ps_matched);
|
||||
}
|
||||
|
||||
|
@ -260,6 +262,8 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
|
|||
state.refresh_cache = 1;
|
||||
for (pos = 0; pos < active_nr; pos++) {
|
||||
struct cache_entry *ce = active_cache[pos];
|
||||
if (source_tree && !(ce->ce_flags & CE_UPDATE))
|
||||
continue;
|
||||
if (match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, NULL)) {
|
||||
if (!ce_stage(ce)) {
|
||||
errs |= checkout_entry(ce, &state, NULL);
|
||||
|
|
42
t/t2022-checkout-paths.sh
Executable file
42
t/t2022-checkout-paths.sh
Executable file
|
@ -0,0 +1,42 @@
|
|||
#!/bin/sh
|
||||
|
||||
test_description='checkout $tree -- $paths'
|
||||
. ./test-lib.sh
|
||||
|
||||
test_expect_success setup '
|
||||
mkdir dir &&
|
||||
>dir/master &&
|
||||
echo common >dir/common &&
|
||||
git add dir/master dir/common &&
|
||||
test_tick && git commit -m "master has dir/master" &&
|
||||
git checkout -b next &&
|
||||
git mv dir/master dir/next0 &&
|
||||
echo next >dir/next1 &&
|
||||
git add dir &&
|
||||
test_tick && git commit -m "next has dir/next but not dir/master"
|
||||
'
|
||||
|
||||
test_expect_success 'checking out paths out of a tree does not clobber unrelated paths' '
|
||||
git checkout next &&
|
||||
git reset --hard &&
|
||||
rm dir/next0 &&
|
||||
cat dir/common >expect.common &&
|
||||
echo modified >expect.next1 &&
|
||||
cat expect.next1 >dir/next1 &&
|
||||
echo untracked >expect.next2 &&
|
||||
cat expect.next2 >dir/next2 &&
|
||||
|
||||
git checkout master dir &&
|
||||
|
||||
test_cmp expect.common dir/common &&
|
||||
test_path_is_file dir/master &&
|
||||
git diff --exit-code master dir/master &&
|
||||
|
||||
test_path_is_missing dir/next0 &&
|
||||
test_cmp expect.next1 dir/next1 &&
|
||||
test_path_is_file dir/next2 &&
|
||||
test_must_fail git ls-files --error-unmatch dir/next2 &&
|
||||
test_cmp expect.next2 dir/next2
|
||||
'
|
||||
|
||||
test_done
|
Loading…
Reference in a new issue