@matheusbsbs / matheus-bagliano / commits / 2031427167
Fork 0

git add: respect core.filemode with unmerged entries

When a merge left unmerged entries, git add failed to pick up the file mode from the index, when core.filemode == 0. If more than one unmerged entry is there, the order of stage preference is 2, 1, 3. Noticed by Johannes Sixt. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jun 29, 2007 at 18:32 UTC 20314271679e169f324c118c69c8d9e0399feec9
2 files changed +55 -1
read-cache.c
+29 -1
@@ -350,6 +350,34 @@ int remove_file_from_index(struct index_state *istate, const char *path)
350 return 0;
351 }
352
353 +static int compare_name(struct cache_entry *ce, const char *path, int namelen)
354 +{
355 + return namelen != ce_namelen(ce) || memcmp(path, ce->name, namelen);
356 +}
357 +
358 +static int index_name_pos_also_unmerged(struct index_state *istate,
359 + const char *path, int namelen)
360 +{
361 + int pos = index_name_pos(istate, path, namelen);
362 + struct cache_entry *ce;
363 +
364 + if (pos >= 0)
365 + return pos;
366 +
367 + /* maybe unmerged? */
368 + pos = -1 - pos;
369 + if (pos >= istate->cache_nr ||
370 + compare_name((ce = istate->cache[pos]), path, namelen))
371 + return -1;
372 +
373 + /* order of preference: stage 2, 1, 3 */
374 + if (ce_stage(ce) == 1 && pos + 1 < istate->cache_nr &&
375 + ce_stage((ce = istate->cache[pos + 1])) == 2 &&
376 + !compare_name(ce, path, namelen))
377 + pos++;
378 + return pos;
379 +}
380 +
381 int add_file_to_index(struct index_state *istate, const char *path, int verbose)
382 {
383 int size, namelen;
@@ -380,7 +408,7 @@ int add_file_to_index(struct index_state *istate, const char *path, int verbose)
408 * from it, otherwise assume unexecutable regular file.
409 */
410 struct cache_entry *ent;
383 - int pos = index_name_pos(istate, path, namelen);
411 + int pos = index_name_pos_also_unmerged(istate, path, namelen);
412
413 ent = (0 <= pos) ? istate->cache[pos] : NULL;
414 ce->ce_mode = ce_mode_from_stat(ent, st.st_mode);
t/t3700-add.sh
+26
@@ -110,4 +110,30 @@ test_expect_success 'check correct prefix detection' '
110 git add 1/2/a 1/3/b 1/2/c
111 '
112
113 +test_expect_success 'git add and filemode=0 with unmerged entries' '
114 + echo 1 > stage1 &&
115 + echo 2 > stage2 &&
116 + echo 3 > stage3 &&
117 + for s in 1 2 3
118 + do
119 + echo "100755 $(git hash-object -w stage$s) $s file"
120 + done | git update-index --index-info &&
121 + git config core.filemode 0 &&
122 + echo new > file &&
123 + git add file &&
124 + git ls-files --stage | grep "^100755 .* 0 file$"
125 +'
126 +
127 +test_expect_success 'git add and filemode=0 prefers stage 2 over stage 1' '
128 + git rm --cached -f file &&
129 + (
130 + echo "100644 $(git hash-object -w stage1) 1 file"
131 + echo "100755 $(git hash-object -w stage2) 2 file"
132 + ) | git update-index --index-info &&
133 + git config core.filemode 0 &&
134 + echo new > file &&
135 + git add file &&
136 + git ls-files --stage | grep "^100755 .* 0 file$"
137 +'
138 +
139 test_done