@matheusbsbs / matheus-bagliano / commits / 41a7aa588f
Fork 0

Fix per-directory exclude handing for "git add"

In "dir_struct", each exclusion element in the exclusion stack records a base string (pointer to the beginning with length) so that we can tell where it came from, but this pointer is just pointing at the parameter that is given by the caller to the push_exclude_per_directory() function. While read_directory_recursive() runs, calls to excluded() makes use the data in the exclusion elements, including this base string. The caller of read_directory_recursive() is not supposed to free the buffer it gave to push_exclude_per_directory() earlier, until it returns. The test case Bruce Stephens gave in the mailing list discussion was simplified and added to the t3700 test. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Nov 16, 2007 at 01:15 UTC 41a7aa588f449d3b139480b102f510abc5ef7f85
2 files changed +28 -2
dir.c
+4 -2
@@ -654,6 +654,7 @@ static void free_simplify(struct path_simplify *simplify)
654 int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
655 {
656 struct path_simplify *simplify = create_simplify(pathspec);
657 + char *pp = NULL;
658
659 /*
660 * Make sure to do the per-directory exclude for all the
@@ -661,7 +662,8 @@ int read_directory(struct dir_struct *dir, const char *path, const char *base, i
662 */
663 if (baselen) {
664 if (dir->exclude_per_dir) {
664 - char *p, *pp = xmalloc(baselen+1);
665 + char *p;
666 + pp = xmalloc(baselen+1);
667 memcpy(pp, base, baselen+1);
668 p = pp;
669 while (1) {
@@ -677,12 +679,12 @@ int read_directory(struct dir_struct *dir, const char *path, const char *base, i
679 else
680 p = pp + baselen;
681 }
680 - free(pp);
682 }
683 }
684
685 read_directory_recursive(dir, path, base, baselen, 0, simplify);
686 free_simplify(simplify);
687 + free(pp);
688 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
689 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
690 return dir->nr;
t/t3700-add.sh
+24
@@ -104,9 +104,33 @@ test_expect_success 'add ignored ones with -f' '
104 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
105 '
106
107 +test_expect_success 'add ignored ones with -f' '
108 + rm -f .git/index &&
109 + git add -f d.?? &&
110 + git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
111 +'
112 +
113 +test_expect_success '.gitignore with subdirectory' '
114 +
115 + rm -f .git/index &&
116 + mkdir -p sub/dir &&
117 + echo "!dir/a.*" >sub/.gitignore &&
118 + >sub/a.ig &&
119 + >sub/dir/a.ig &&
120 + git add sub/dir &&
121 + git ls-files --error-unmatch sub/dir/a.ig &&
122 + rm -f .git/index &&
123 + (
124 + cd sub/dir &&
125 + git add .
126 + ) &&
127 + git ls-files --error-unmatch sub/dir/a.ig
128 +'
129 +
130 mkdir 1 1/2 1/3
131 touch 1/2/a 1/3/b 1/2/c
132 test_expect_success 'check correct prefix detection' '
133 + rm -f .git/index &&
134 git add 1/2/a 1/3/b 1/2/c
135 '
136