Skip to content

Commit

Permalink
Merge branch 'cb/leading-path-removal'
Browse files Browse the repository at this point in the history
* cb/leading-path-removal:
  use persistent memory for rejected paths
  do not overwrite files in leading path
  lstat_cache: optionally return match_len
  add function check_ok_to_remove()
  t7607: add leading-path tests
  t7607: use test-lib functions and check MERGE_HEAD

Conflicts:
	t/t7607-merge-overwrite.sh
  • Loading branch information
Junio C Hamano committed Nov 30, 2010
2 parents 5acb623 + 7980872 commit 208247a
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 135 deletions.
2 changes: 1 addition & 1 deletion cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ struct cache_def {

extern int has_symlink_leading_path(const char *name, int len);
extern int threaded_has_symlink_leading_path(struct cache_def *, const char *, int);
extern int has_symlink_or_noent_leading_path(const char *name, int len);
extern int check_leading_path(const char *name, int len);
extern int has_dirs_only_path(const char *name, int len, int prefix_len);
extern void schedule_dir_for_removal(const char *name, int len);
extern void remove_scheduled_dirs(void);
Expand Down
64 changes: 43 additions & 21 deletions symlinks.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ static inline void reset_lstat_cache(struct cache_def *cache)
* of the prefix, where the cache should use the stat() function
* instead of the lstat() function to test each path component.
*/
static int lstat_cache(struct cache_def *cache, const char *name, int len,
int track_flags, int prefix_len_stat_func)
static int lstat_cache_matchlen(struct cache_def *cache,
const char *name, int len,
int *ret_flags, int track_flags,
int prefix_len_stat_func)
{
int match_len, last_slash, last_slash_dir, previous_slash;
int match_flags, ret_flags, save_flags, max_len, ret;
int save_flags, max_len, ret;
struct stat st;

if (cache->track_flags != track_flags ||
Expand All @@ -90,13 +92,13 @@ static int lstat_cache(struct cache_def *cache, const char *name, int len,
match_len = last_slash =
longest_path_match(name, len, cache->path, cache->len,
&previous_slash);
match_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK);
*ret_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK);

if (!(track_flags & FL_FULLPATH) && match_len == len)
match_len = last_slash = previous_slash;

if (match_flags && match_len == cache->len)
return match_flags;
if (*ret_flags && match_len == cache->len)
return match_len;
/*
* If we now have match_len > 0, we would know that
* the matched part will always be a directory.
Expand All @@ -105,16 +107,16 @@ static int lstat_cache(struct cache_def *cache, const char *name, int len,
* a substring of the cache on a path component basis,
* we can return immediately.
*/
match_flags = track_flags & FL_DIR;
if (match_flags && len == match_len)
return match_flags;
*ret_flags = track_flags & FL_DIR;
if (*ret_flags && len == match_len)
return match_len;
}

/*
* Okay, no match from the cache so far, so now we have to
* check the rest of the path components.
*/
ret_flags = FL_DIR;
*ret_flags = FL_DIR;
last_slash_dir = last_slash;
max_len = len < PATH_MAX ? len : PATH_MAX;
while (match_len < max_len) {
Expand All @@ -133,16 +135,16 @@ static int lstat_cache(struct cache_def *cache, const char *name, int len,
ret = lstat(cache->path, &st);

if (ret) {
ret_flags = FL_LSTATERR;
*ret_flags = FL_LSTATERR;
if (errno == ENOENT)
ret_flags |= FL_NOENT;
*ret_flags |= FL_NOENT;
} else if (S_ISDIR(st.st_mode)) {
last_slash_dir = last_slash;
continue;
} else if (S_ISLNK(st.st_mode)) {
ret_flags = FL_SYMLINK;
*ret_flags = FL_SYMLINK;
} else {
ret_flags = FL_ERR;
*ret_flags = FL_ERR;
}
break;
}
Expand All @@ -152,7 +154,7 @@ static int lstat_cache(struct cache_def *cache, const char *name, int len,
* path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached
* for the moment!
*/
save_flags = ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
save_flags = *ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) {
cache->path[last_slash] = '\0';
cache->len = last_slash;
Expand All @@ -176,7 +178,16 @@ static int lstat_cache(struct cache_def *cache, const char *name, int len,
} else {
reset_lstat_cache(cache);
}
return ret_flags;
return match_len;
}

static int lstat_cache(struct cache_def *cache, const char *name, int len,
int track_flags, int prefix_len_stat_func)
{
int flags;
(void)lstat_cache_matchlen(cache, name, len, &flags, track_flags,
prefix_len_stat_func);
return flags;
}

#define USE_ONLY_LSTAT 0
Expand All @@ -198,15 +209,26 @@ int has_symlink_leading_path(const char *name, int len)
}

/*
* Return non-zero if path 'name' has a leading symlink component or
* Return zero if path 'name' has a leading symlink component or
* if some leading path component does not exists.
*
* Return -1 if leading path exists and is a directory.
*
* Return path length if leading path exists and is neither a
* directory nor a symlink.
*/
int has_symlink_or_noent_leading_path(const char *name, int len)
int check_leading_path(const char *name, int len)
{
struct cache_def *cache = &default_cache; /* FIXME */
return lstat_cache(cache, name, len,
FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT) &
(FL_SYMLINK|FL_NOENT);
int flags;
int match_len = lstat_cache_matchlen(cache, name, len, &flags,
FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT);
if (flags & (FL_SYMLINK|FL_NOENT))
return 0;
else if (flags & FL_DIR)
return -1;
else
return match_len;
}

/*
Expand Down
102 changes: 79 additions & 23 deletions t/t7607-merge-overwrite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,54 @@ Do not overwrite changes.'
. ./test-lib.sh

test_expect_success 'setup' '
echo c0 > c0.c &&
git add c0.c &&
git commit -m c0 &&
git tag c0 &&
echo c1 > c1.c &&
git add c1.c &&
git commit -m c1 &&
git tag c1 &&
test_commit c0 c0.c &&
test_commit c1 c1.c &&
test_commit c1a c1.c "c1 a" &&
git reset --hard c0 &&
echo c2 > c2.c &&
git add c2.c &&
git commit -m c2 &&
git tag c2 &&
git reset --hard c1 &&
echo "c1 a" > c1.c &&
git add c1.c &&
git commit -m "c1 a" &&
git tag c1a &&
test_commit c2 c2.c &&
git reset --hard c0 &&
mkdir sub &&
echo "sub/f" > sub/f &&
mkdir sub2 &&
echo "sub2/f" > sub2/f &&
git add sub/f sub2/f &&
git commit -m sub &&
git tag sub &&
echo "VERY IMPORTANT CHANGES" > important
'

test_expect_success 'will not overwrite untracked file' '
git reset --hard c1 &&
cat important > c2.c &&
cp important c2.c &&
test_must_fail git merge c2 &&
test_path_is_missing .git/MERGE_HEAD &&
test_cmp important c2.c
'

test_expect_success 'will overwrite tracked file' '
git reset --hard c1 &&
cp important c2.c &&
git add c2.c &&
git commit -m important &&
git checkout c2
'

test_expect_success 'will not overwrite new file' '
git reset --hard c1 &&
cat important > c2.c &&
cp important c2.c &&
git add c2.c &&
test_must_fail git merge c2 &&
test_path_is_missing .git/MERGE_HEAD &&
test_cmp important c2.c
'

test_expect_success 'will not overwrite staged changes' '
git reset --hard c1 &&
cat important > c2.c &&
cp important c2.c &&
git add c2.c &&
rm c2.c &&
test_must_fail git merge c2 &&
test_path_is_missing .git/MERGE_HEAD &&
git checkout c2.c &&
test_cmp important c2.c
'
Expand All @@ -57,7 +63,7 @@ test_expect_success 'will not overwrite removed file' '
git reset --hard c1 &&
git rm c1.c &&
git commit -m "rm c1.c" &&
cat important > c1.c &&
cp important c1.c &&
test_must_fail git merge c1a &&
test_cmp important c1.c
'
Expand All @@ -66,24 +72,74 @@ test_expect_success 'will not overwrite re-added file' '
git reset --hard c1 &&
git rm c1.c &&
git commit -m "rm c1.c" &&
cat important > c1.c &&
cp important c1.c &&
git add c1.c &&
test_must_fail git merge c1a &&
test_path_is_missing .git/MERGE_HEAD &&
test_cmp important c1.c
'

test_expect_success 'will not overwrite removed file with staged changes' '
git reset --hard c1 &&
git rm c1.c &&
git commit -m "rm c1.c" &&
cat important > c1.c &&
cp important c1.c &&
git add c1.c &&
rm c1.c &&
test_must_fail git merge c1a &&
test_path_is_missing .git/MERGE_HEAD &&
git checkout c1.c &&
test_cmp important c1.c
'

test_expect_success 'will not overwrite untracked subtree' '
git reset --hard c0 &&
rm -rf sub &&
mkdir -p sub/f &&
cp important sub/f/important &&
test_must_fail git merge sub &&
test_path_is_missing .git/MERGE_HEAD &&
test_cmp important sub/f/important
'

cat >expect <<\EOF
error: The following untracked working tree files would be overwritten by merge:
sub
sub2
Please move or remove them before you can merge.
EOF

test_expect_success 'will not overwrite untracked file in leading path' '
git reset --hard c0 &&
rm -rf sub &&
cp important sub &&
cp important sub2 &&
test_must_fail git merge sub 2>out &&
test_cmp out expect &&
test_path_is_missing .git/MERGE_HEAD &&
test_cmp important sub &&
test_cmp important sub2 &&
rm -f sub sub2
'

test_expect_failure SYMLINKS 'will not overwrite untracked symlink in leading path' '
git reset --hard c0 &&
rm -rf sub &&
mkdir sub2 &&
ln -s sub2 sub &&
test_must_fail git merge sub &&
test_path_is_missing .git/MERGE_HEAD
'

test_expect_success SYMLINKS 'will not be confused by symlink in leading path' '
git reset --hard c0 &&
rm -rf sub &&
ln -s sub2 sub &&
git add sub &&
git commit -m ln &&
git checkout sub
'

cat >expect <<\EOF
error: Untracked working tree file 'c0.c' would be overwritten by merge.
fatal: read-tree failed
Expand Down
16 changes: 8 additions & 8 deletions t/t7609-merge-co-error-msgs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ test_expect_success 'setup' '

cat >expect <<\EOF
error: The following untracked working tree files would be overwritten by merge:
two
three
four
five
four
three
two
Please move or remove them before you can merge.
EOF

Expand All @@ -49,9 +49,9 @@ test_expect_success 'untracked files overwritten by merge (fast and non-fast for

cat >expect <<\EOF
error: Your local changes to the following files would be overwritten by merge:
two
three
four
three
two
Please, commit your changes or stash them before you can merge.
error: The following untracked working tree files would be overwritten by merge:
five
Expand All @@ -68,8 +68,8 @@ test_expect_success 'untracked files or local changes ovewritten by merge' '

cat >expect <<\EOF
error: Your local changes to the following files would be overwritten by checkout:
rep/two
rep/one
rep/two
Please, commit your changes or stash them before you can switch branches.
EOF

Expand All @@ -89,8 +89,8 @@ test_expect_success 'cannot switch branches because of local changes' '

cat >expect <<\EOF
error: Your local changes to the following files would be overwritten by checkout:
rep/two
rep/one
rep/two
Please, commit your changes or stash them before you can switch branches.
EOF

Expand All @@ -102,8 +102,8 @@ test_expect_success 'not uptodate file porcelain checkout error' '

cat >expect <<\EOF
error: Updating the following directories would lose untracked files in it:
rep2
rep
rep2
EOF

Expand Down
Loading

0 comments on commit 208247a

Please sign in to comment.