Skip to content

Commit

Permalink
Merge branch 'jc/checkout-from-tree-keep-local-changes' into maint
Browse files Browse the repository at this point in the history
* jc/checkout-from-tree-keep-local-changes:
  checkout $tree $path: do not clobber local changes in $path not in $tree
  • Loading branch information
gitster committed Oct 21, 2011
2 parents 634b29d + 0a1283b commit df68408
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
6 changes: 5 additions & 1 deletion builtin/checkout.c
Expand Up @@ -71,7 +71,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;
Expand Down Expand Up @@ -228,6 +228,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);
}

Expand Down Expand Up @@ -266,6 +268,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);
Expand Down
42 changes: 42 additions & 0 deletions t/t2022-checkout-paths.sh
@@ -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

0 comments on commit df68408

Please sign in to comment.