Commit 6d6020defcb17ec0c4836db428f148ebaedabe65

Carlos Martín Nieto 2015-09-08T18:34:51

Merge pull request #3353 from ethomson/wrongcase_add index: canonicalize directory case when adding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1f3c3ed..6ade3e3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -31,6 +31,11 @@ v0.23 + 1
   with which to implement the transactional/atomic semantics for the
   configuration backend.
 
+* `git_index_add` will now use the case as provided by the caller on
+  case insensitive systems.  Previous versions would keep the case as
+  it existed in the index.  This does not affect the higher-level
+  `git_index_add_bypath` or `git_index_add_frombuffer` functions.
+
 v0.23
 ------
 
diff --git a/src/index.c b/src/index.c
index 20a6c93..2e89347 100644
--- a/src/index.c
+++ b/src/index.c
@@ -977,16 +977,27 @@ static int index_entry_reuc_init(git_index_reuc_entry **reuc_out,
 	return 0;
 }
 
-static void index_entry_cpy(git_index_entry *tgt, const git_index_entry *src)
+static void index_entry_cpy(
+	git_index_entry *tgt,
+	git_index *index,
+	const git_index_entry *src,
+	bool update_path)
 {
 	const char *tgt_path = tgt->path;
 	memcpy(tgt, src, sizeof(*tgt));
-	tgt->path = tgt_path; /* reset to existing path data */
+
+	/* keep the existing path buffer, but update the path to the one
+	 * given by the caller, if we trust it.
+	 */
+	tgt->path = tgt_path;
+
+	if (index->ignore_case && update_path)
+		memcpy((char *)tgt->path, src->path, strlen(tgt->path));
 }
 
 static int index_entry_dup(
 	git_index_entry **out,
-	git_repository *repo,
+	git_index *index,
 	const git_index_entry *src)
 {
 	git_index_entry *entry;
@@ -996,10 +1007,10 @@ static int index_entry_dup(
 		return 0;
 	}
 
-	if (index_entry_create(&entry, repo, src->path) < 0)
+	if (index_entry_create(&entry, INDEX_OWNER(index), src->path) < 0)
 		return -1;
 
-	index_entry_cpy(entry, src);
+	index_entry_cpy(entry, index, src, false);
 	*out = entry;
 	return 0;
 }
@@ -1102,6 +1113,74 @@ static int check_file_directory_collision(git_index *index,
 	return 0;
 }
 
+static int canonicalize_directory_path(
+	git_index *index, git_index_entry *entry)
+{
+	const git_index_entry *match, *best = NULL;
+	char *search, *sep;
+	size_t pos, search_len, best_len;
+
+	if (!index->ignore_case)
+		return 0;
+
+	/* item already exists in the index, simply re-use the existing case */
+	if ((match = git_index_get_bypath(index, entry->path, 0)) != NULL) {
+		memcpy((char *)entry->path, match->path, strlen(entry->path));
+		return 0;
+	}
+
+	/* nothing to do */
+	if (strchr(entry->path, '/') == NULL)
+		return 0;
+
+	if ((search = git__strdup(entry->path)) == NULL)
+		return -1;
+
+	/* starting at the parent directory and descending to the root, find the
+	 * common parent directory.
+	 */
+	while (!best && (sep = strrchr(search, '/'))) {
+		sep[1] = '\0';
+
+		search_len = strlen(search);
+
+		git_vector_bsearch2(
+			&pos, &index->entries, index->entries_search_path, search);
+
+		while ((match = git_vector_get(&index->entries, pos))) {
+			if (GIT_IDXENTRY_STAGE(match) != 0) {
+				/* conflicts do not contribute to canonical paths */
+			} else if (memcmp(search, match->path, search_len) == 0) {
+				/* prefer an exact match to the input filename */
+				best = match;
+				best_len = search_len;
+				break;
+			} else if (strncasecmp(search, match->path, search_len) == 0) {
+				/* continue walking, there may be a path with an exact
+				 * (case sensitive) match later in the index, but use this
+				 * as the best match until that happens.
+				 */
+				if (!best) {
+					best = match;
+					best_len = search_len;
+				}
+			} else {
+				break;
+			}
+
+			pos++;
+		}
+
+		sep[0] = '\0';
+	}
+
+	if (best)
+		memcpy((char *)entry->path, best->path, best_len);
+
+	git__free(search);
+	return 0;
+}
+
 static int index_no_dups(void **old, void *new)
 {
 	const git_index_entry *entry = new;
@@ -1115,10 +1194,17 @@ static int index_no_dups(void **old, void *new)
  * it, then it will return an error **and also free the entry**.  When
  * it replaces an existing entry, it will update the entry_ptr with the
  * actual entry in the index (and free the passed in one).
+ * trust_path is whether we use the given path, or whether (on case
+ * insensitive systems only) we try to canonicalize the given path to
+ * be within an existing directory.
  * trust_mode is whether we trust the mode in entry_ptr.
  */
 static int index_insert(
-	git_index *index, git_index_entry **entry_ptr, int replace, bool trust_mode)
+	git_index *index,
+	git_index_entry **entry_ptr,
+	int replace,
+	bool trust_path,
+	bool trust_mode)
 {
 	int error = 0;
 	size_t path_length, position;
@@ -1156,8 +1242,14 @@ static int index_insert(
 			entry->mode = index_merge_mode(index, existing, entry->mode);
 	}
 
+	/* canonicalize the directory name */
+	if (!trust_path)
+		error = canonicalize_directory_path(index, entry);
+
 	/* look for tree / blob name collisions, removing conflicts if requested */
-	error = check_file_directory_collision(index, entry, position, replace);
+	if (!error)
+		error = check_file_directory_collision(index, entry, position, replace);
+
 	if (error < 0)
 		/* skip changes */;
 
@@ -1166,7 +1258,7 @@ static int index_insert(
 	 */
 	else if (existing) {
 		if (replace)
-			index_entry_cpy(existing, entry);
+			index_entry_cpy(existing, index, entry, trust_path);
 		index_entry_free(entry);
 		*entry_ptr = entry = existing;
 	}
@@ -1246,7 +1338,7 @@ int git_index_add_frombuffer(
 		return -1;
 	}
 
-	if (index_entry_dup(&entry, INDEX_OWNER(index), source_entry) < 0)
+	if (index_entry_dup(&entry, index, source_entry) < 0)
 		return -1;
 
 	error = git_blob_create_frombuffer(&id, INDEX_OWNER(index), buffer, len);
@@ -1258,7 +1350,7 @@ int git_index_add_frombuffer(
 	git_oid_cpy(&entry->id, &id);
 	entry->file_size = len;
 
-	if ((error = index_insert(index, &entry, 1, true)) < 0)
+	if ((error = index_insert(index, &entry, 1, true, true)) < 0)
 		return error;
 
 	/* Adding implies conflict was resolved, move conflict entries to REUC */
@@ -1317,7 +1409,7 @@ int git_index_add_bypath(git_index *index, const char *path)
 	assert(index && path);
 
 	if ((ret = index_entry_init(&entry, index, path)) == 0)
-		ret = index_insert(index, &entry, 1, false);
+		ret = index_insert(index, &entry, 1, false, false);
 
 	/* If we were given a directory, let's see if it's a submodule */
 	if (ret < 0 && ret != GIT_EDIRECTORY)
@@ -1343,7 +1435,7 @@ int git_index_add_bypath(git_index *index, const char *path)
 			if ((ret = add_repo_as_submodule(&entry, index, path)) < 0)
 				return ret;
 
-			if ((ret = index_insert(index, &entry, 1, false)) < 0)
+			if ((ret = index_insert(index, &entry, 1, false, false)) < 0)
 				return ret;
 		} else if (ret < 0) {
 			return ret;
@@ -1393,8 +1485,8 @@ int git_index_add(git_index *index, const git_index_entry *source_entry)
 		return -1;
 	}
 
-	if ((ret = index_entry_dup(&entry, INDEX_OWNER(index), source_entry)) < 0 ||
-		(ret = index_insert(index, &entry, 1, true)) < 0)
+	if ((ret = index_entry_dup(&entry, index, source_entry)) < 0 ||
+		(ret = index_insert(index, &entry, 1, true, true)) < 0)
 		return ret;
 
 	git_tree_cache_invalidate_path(index->tree, entry->path);
@@ -1543,9 +1635,9 @@ int git_index_conflict_add(git_index *index,
 
 	assert (index);
 
-	if ((ret = index_entry_dup(&entries[0], INDEX_OWNER(index), ancestor_entry)) < 0 ||
-		(ret = index_entry_dup(&entries[1], INDEX_OWNER(index), our_entry)) < 0 ||
-		(ret = index_entry_dup(&entries[2], INDEX_OWNER(index), their_entry)) < 0)
+	if ((ret = index_entry_dup(&entries[0], index, ancestor_entry)) < 0 ||
+		(ret = index_entry_dup(&entries[1], index, our_entry)) < 0 ||
+		(ret = index_entry_dup(&entries[2], index, their_entry)) < 0)
 		goto on_error;
 
 	/* Validate entries */
@@ -1579,7 +1671,7 @@ int git_index_conflict_add(git_index *index,
 		/* Make sure stage is correct */
 		GIT_IDXENTRY_STAGE_SET(entries[i], i + 1);
 
-		if ((ret = index_insert(index, &entries[i], 0, true)) < 0)
+		if ((ret = index_insert(index, &entries[i], 0, true, true)) < 0)
 			goto on_error;
 
 		entries[i] = NULL; /* don't free if later entry fails */
@@ -2153,7 +2245,7 @@ static size_t read_entry(
 
 	entry.path = (char *)path_ptr;
 
-	if (index_entry_dup(out, INDEX_OWNER(index), &entry) < 0)
+	if (index_entry_dup(out, index, &entry) < 0)
 		return 0;
 
 	return entry_size;
@@ -2670,7 +2762,7 @@ static int read_tree_cb(
 		entry->mode == old_entry->mode &&
 		git_oid_equal(&entry->id, &old_entry->id))
 	{
-		index_entry_cpy(entry, old_entry);
+		index_entry_cpy(entry, data->index, old_entry, false);
 		entry->flags_extended = 0;
 	}
 
@@ -2802,7 +2894,7 @@ int git_index_read_index(
 		if (diff < 0) {
 			git_vector_insert(&remove_entries, (git_index_entry *)old_entry);
 		} else if (diff > 0) {
-			if ((error = index_entry_dup(&entry, git_index_owner(index), new_entry)) < 0)
+			if ((error = index_entry_dup(&entry, index, new_entry)) < 0)
 				goto done;
 
 			git_vector_insert(&new_entries, entry);
@@ -2813,7 +2905,7 @@ int git_index_read_index(
 			if (git_oid_equal(&old_entry->id, &new_entry->id)) {
 				git_vector_insert(&new_entries, (git_index_entry *)old_entry);
 			} else {
-				if ((error = index_entry_dup(&entry, git_index_owner(index), new_entry)) < 0)
+				if ((error = index_entry_dup(&entry, index, new_entry)) < 0)
 					goto done;
 
 				git_vector_insert(&new_entries, entry);
diff --git a/tests/index/bypath.c b/tests/index/bypath.c
index b607e17..b152b09 100644
--- a/tests/index/bypath.c
+++ b/tests/index/bypath.c
@@ -72,3 +72,171 @@ void test_index_bypath__add_hidden(void)
 	cl_assert_equal_i(GIT_FILEMODE_BLOB, entry->mode);
 #endif
 }
+
+void test_index_bypath__add_keeps_existing_case(void)
+{
+	const git_index_entry *entry;
+
+	if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
+		clar__skip();
+
+	cl_git_mkfile("submod2/just_a_dir/file1.txt", "This is a file");
+	cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/file1.txt"));
+
+	cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/file1.txt", 0));
+	cl_assert_equal_s("just_a_dir/file1.txt", entry->path);
+
+	cl_git_rewritefile("submod2/just_a_dir/file1.txt", "Updated!");
+	cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/FILE1.txt"));
+
+	cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/FILE1.txt", 0));
+	cl_assert_equal_s("just_a_dir/file1.txt", entry->path);
+}
+
+void test_index_bypath__add_honors_existing_case(void)
+{
+	const git_index_entry *entry;
+
+	if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
+		clar__skip();
+
+	cl_git_mkfile("submod2/just_a_dir/file1.txt", "This is a file");
+	cl_git_mkfile("submod2/just_a_dir/file2.txt", "This is another file");
+	cl_git_mkfile("submod2/just_a_dir/file3.txt", "This is another file");
+	cl_git_mkfile("submod2/just_a_dir/file4.txt", "And another file");
+
+	cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/File1.txt"));
+	cl_git_pass(git_index_add_bypath(g_idx, "JUST_A_DIR/file2.txt"));
+	cl_git_pass(git_index_add_bypath(g_idx, "Just_A_Dir/FILE3.txt"));
+
+	cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/File1.txt", 0));
+	cl_assert_equal_s("just_a_dir/File1.txt", entry->path);
+
+	cl_assert(entry = git_index_get_bypath(g_idx, "JUST_A_DIR/file2.txt", 0));
+	cl_assert_equal_s("just_a_dir/file2.txt", entry->path);
+
+	cl_assert(entry = git_index_get_bypath(g_idx, "Just_A_Dir/FILE3.txt", 0));
+	cl_assert_equal_s("just_a_dir/FILE3.txt", entry->path);
+
+	cl_git_rewritefile("submod2/just_a_dir/file3.txt", "Rewritten");
+	cl_git_pass(git_index_add_bypath(g_idx, "Just_A_Dir/file3.txt"));
+
+	cl_assert(entry = git_index_get_bypath(g_idx, "Just_A_Dir/file3.txt", 0));
+	cl_assert_equal_s("just_a_dir/FILE3.txt", entry->path);
+}
+
+void test_index_bypath__add_honors_existing_case_2(void)
+{
+	git_index_entry dummy = { { 0 } };
+	const git_index_entry *entry;
+
+	if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
+		clar__skip();
+
+	dummy.mode = GIT_FILEMODE_BLOB;
+
+	/* note that `git_index_add` does no checking to canonical directories */
+	dummy.path = "Just_a_dir/file0.txt";
+	cl_git_pass(git_index_add(g_idx, &dummy));
+
+	dummy.path = "just_a_dir/fileA.txt";
+	cl_git_pass(git_index_add(g_idx, &dummy));
+
+	dummy.path = "Just_A_Dir/fileB.txt";
+	cl_git_pass(git_index_add(g_idx, &dummy));
+
+	dummy.path = "JUST_A_DIR/fileC.txt";
+	cl_git_pass(git_index_add(g_idx, &dummy));
+
+	dummy.path = "just_A_dir/fileD.txt";
+	cl_git_pass(git_index_add(g_idx, &dummy));
+
+	dummy.path = "JUST_a_DIR/fileE.txt";
+	cl_git_pass(git_index_add(g_idx, &dummy));
+
+	cl_git_mkfile("submod2/just_a_dir/file1.txt", "This is a file");
+	cl_git_mkfile("submod2/just_a_dir/file2.txt", "This is another file");
+	cl_git_mkfile("submod2/just_a_dir/file3.txt", "This is another file");
+	cl_git_mkfile("submod2/just_a_dir/file4.txt", "And another file");
+
+	cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/File1.txt"));
+	cl_git_pass(git_index_add_bypath(g_idx, "JUST_A_DIR/file2.txt"));
+	cl_git_pass(git_index_add_bypath(g_idx, "Just_A_Dir/FILE3.txt"));
+	cl_git_pass(git_index_add_bypath(g_idx, "JusT_A_DIR/FILE4.txt"));
+
+	cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/File1.txt", 0));
+	cl_assert_equal_s("just_a_dir/File1.txt", entry->path);
+
+	cl_assert(entry = git_index_get_bypath(g_idx, "JUST_A_DIR/file2.txt", 0));
+	cl_assert_equal_s("JUST_A_DIR/file2.txt", entry->path);
+
+	cl_assert(entry = git_index_get_bypath(g_idx, "Just_A_Dir/FILE3.txt", 0));
+	cl_assert_equal_s("Just_A_Dir/FILE3.txt", entry->path);
+
+	cl_git_rewritefile("submod2/just_a_dir/file3.txt", "Rewritten");
+	cl_git_pass(git_index_add_bypath(g_idx, "Just_A_Dir/file3.txt"));
+
+	cl_assert(entry = git_index_get_bypath(g_idx, "Just_A_Dir/file3.txt", 0));
+	cl_assert_equal_s("Just_A_Dir/FILE3.txt", entry->path);
+}
+
+void test_index_bypath__add_honors_existing_case_3(void)
+{
+	git_index_entry dummy = { { 0 } };
+	const git_index_entry *entry;
+
+	if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
+		clar__skip();
+
+	dummy.mode = GIT_FILEMODE_BLOB;
+
+	dummy.path = "just_a_dir/filea.txt";
+	cl_git_pass(git_index_add(g_idx, &dummy));
+
+	dummy.path = "Just_A_Dir/fileB.txt";
+	cl_git_pass(git_index_add(g_idx, &dummy));
+
+	dummy.path = "just_A_DIR/FILEC.txt";
+	cl_git_pass(git_index_add(g_idx, &dummy));
+
+	dummy.path = "Just_a_DIR/FileD.txt";
+	cl_git_pass(git_index_add(g_idx, &dummy));
+
+	cl_git_mkfile("submod2/JuSt_A_DiR/fILEE.txt", "This is a file");
+
+	cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/fILEE.txt"));
+
+	cl_assert(entry = git_index_get_bypath(g_idx, "JUST_A_DIR/fILEE.txt", 0));
+	cl_assert_equal_s("just_a_dir/fILEE.txt", entry->path);
+}
+
+void test_index_bypath__add_honors_existing_case_4(void)
+{
+	git_index_entry dummy = { { 0 } };
+	const git_index_entry *entry;
+
+	if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
+		clar__skip();
+
+	dummy.mode = GIT_FILEMODE_BLOB;
+
+	dummy.path = "just_a_dir/a/b/c/d/e/file1.txt";
+	cl_git_pass(git_index_add(g_idx, &dummy));
+
+	dummy.path = "just_a_dir/a/B/C/D/E/file2.txt";
+	cl_git_pass(git_index_add(g_idx, &dummy));
+
+	cl_must_pass(p_mkdir("submod2/just_a_dir/a", 0777));
+	cl_must_pass(p_mkdir("submod2/just_a_dir/a/b", 0777));
+	cl_must_pass(p_mkdir("submod2/just_a_dir/a/b/z", 0777));
+	cl_must_pass(p_mkdir("submod2/just_a_dir/a/b/z/y", 0777));
+	cl_must_pass(p_mkdir("submod2/just_a_dir/a/b/z/y/x", 0777));
+
+	cl_git_mkfile("submod2/just_a_dir/a/b/z/y/x/FOO.txt", "This is a file");
+
+	cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/A/b/Z/y/X/foo.txt"));
+
+	cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/A/b/Z/y/X/foo.txt", 0));
+	cl_assert_equal_s("just_a_dir/a/b/Z/y/X/foo.txt", entry->path);
+}
+
diff --git a/tests/index/rename.c b/tests/index/rename.c
index dd3cfa7..ebaa9b7 100644
--- a/tests/index/rename.c
+++ b/tests/index/rename.c
@@ -48,3 +48,34 @@ void test_index_rename__single_file(void)
 
 	cl_fixture_cleanup("rename");
 }
+
+void test_index_rename__casechanging(void)
+{
+	git_repository *repo;
+	git_index *index;
+	const git_index_entry *entry;
+	git_index_entry new = {{0}};
+
+	p_mkdir("rename", 0700);
+
+	cl_git_pass(git_repository_init(&repo, "./rename", 0));
+	cl_git_pass(git_repository_index(&index, repo));
+
+	cl_git_mkfile("./rename/lame.name.txt", "new_file\n");
+
+	cl_git_pass(git_index_add_bypath(index, "lame.name.txt"));
+	cl_assert_equal_i(1, git_index_entrycount(index));
+	cl_assert((entry = git_index_get_bypath(index, "lame.name.txt", 0)));
+
+	memcpy(&new, entry, sizeof(git_index_entry));
+	new.path = "LAME.name.TXT";
+
+	cl_git_pass(git_index_add(index, &new));
+	cl_assert((entry = git_index_get_bypath(index, "LAME.name.TXT", 0)));
+
+	if (cl_repo_get_bool(repo, "core.ignorecase"))
+		cl_assert_equal_i(1, git_index_entrycount(index));
+	else
+		cl_assert_equal_i(2, git_index_entrycount(index));
+}
+