do not overwrite files in leading path
If the work tree contains an untracked file x, and unpack-trees wants to checkout a path x/*, the file x is removed unconditionally. Instead, apply the same checks that are normally used for untracked files, and abort if the file cannot be removed. Signed-off-by: Clemens Buchacher <drizzd@aon.at> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Clemens Buchacher committed
Oct 9, 2010 at 15:53 UTC
b1735b1ab7743700064af528a998cdc952c8b9a1
4 files changed
+31
-10
cache.h
+1
-1
@@ -859,7 +859,7 @@ struct cache_def {
859
860
extern int has_symlink_leading_path(const char *name, int len);
861
extern int threaded_has_symlink_leading_path(struct cache_def *, const char *, int);
862
-extern int has_symlink_or_noent_leading_path(const char *name, int len);
862
+extern int check_leading_path(const char *name, int len);
863
extern int has_dirs_only_path(const char *name, int len, int prefix_len);
864
extern void schedule_dir_for_removal(const char *name, int len);
865
extern void remove_scheduled_dirs(void);
symlinks.c
+16
-5
@@ -209,15 +209,26 @@ int has_symlink_leading_path(const char *name, int len)
209
}
210
211
/*
212
- * Return non-zero if path 'name' has a leading symlink component or
212
+ * Return zero if path 'name' has a leading symlink component or
213
* if some leading path component does not exists.
214
+ *
215
+ * Return -1 if leading path exists and is a directory.
216
+ *
217
+ * Return path length if leading path exists and is neither a
218
+ * directory nor a symlink.
219
*/
215
-int has_symlink_or_noent_leading_path(const char *name, int len)
220
+int check_leading_path(const char *name, int len)
221
{
222
struct cache_def *cache = &default_cache; /* FIXME */
218
- return lstat_cache(cache, name, len,
219
- FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT) &
220
- (FL_SYMLINK|FL_NOENT);
223
+ int flags;
224
+ int match_len = lstat_cache_matchlen(cache, name, len, &flags,
225
+ FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT);
226
+ if (flags & (FL_SYMLINK|FL_NOENT))
227
+ return 0;
228
+ else if (flags & FL_DIR)
229
+ return -1;
230
+ else
231
+ return match_len;
232
}
233
234
/*
t/t7607-merge-overwrite.sh
+1
-1
@@ -100,7 +100,7 @@ test_expect_success 'will not overwrite untracked subtree' '
100
test_cmp important sub/f/important
101
'
102
103
-test_expect_failure 'will not overwrite untracked file in leading path' '
103
+test_expect_success 'will not overwrite untracked file in leading path' '
104
git reset --hard c0 &&
105
rm -rf sub &&
106
cp important sub &&
unpack-trees.c
+13
-3
@@ -182,7 +182,7 @@ static void display_error_msgs(struct unpack_trees_options *o)
182
*/
183
static void unlink_entry(struct cache_entry *ce)
184
{
185
- if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
185
+ if (!check_leading_path(ce->name, ce_namelen(ce)))
186
return;
187
if (remove_or_warn(ce->ce_mode, ce->name))
188
return;
@@ -1194,18 +1194,28 @@ static int verify_absent_1(struct cache_entry *ce,
1194
enum unpack_trees_error_types error_type,
1195
struct unpack_trees_options *o)
1196
{
1197
+ int len;
1198
struct stat st;
1199
1200
if (o->index_only || o->reset || !o->update)
1201
return 0;
1202
1202
- if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
1203
+ len = check_leading_path(ce->name, ce_namelen(ce));
1204
+ if (!len)
1205
return 0;
1206
+ else if (len > 0) {
1207
+ char path[PATH_MAX + 1];
1208
+ memcpy(path, ce->name, len);
1209
+ path[len] = 0;
1210
+ lstat(path, &st);
1211
1205
- if (!lstat(ce->name, &st))
1212
+ return check_ok_to_remove(path, len, DT_UNKNOWN, NULL, &st,
1213
+ error_type, o);
1214
+ } else if (!lstat(ce->name, &st))
1215
return check_ok_to_remove(ce->name, ce_namelen(ce),
1216
ce_to_dtype(ce), ce, &st,
1217
error_type, o);
1218
+
1219
return 0;
1220
}
1221