@matheusbsbs / matheus-bagliano / commits / 0e06cc8b82
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 0e06cc8b823144be16a9fc1f703126b68d20d3b5
2 files changed +28 -2
dir.c
+4 -2
@@ -626,6 +626,7 @@ static void free_simplify(struct path_simplify *simplify)
626 int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
627 {
628 struct path_simplify *simplify = create_simplify(pathspec);
629 + char *pp = NULL;
630
631 /*
632 * Make sure to do the per-directory exclude for all the
@@ -633,7 +634,8 @@ int read_directory(struct dir_struct *dir, const char *path, const char *base, i
634 */
635 if (baselen) {
636 if (dir->exclude_per_dir) {
636 - char *p, *pp = xmalloc(baselen+1);
637 + char *p;
638 + pp = xmalloc(baselen+1);
639 memcpy(pp, base, baselen+1);
640 p = pp;
641 while (1) {
@@ -649,12 +651,12 @@ int read_directory(struct dir_struct *dir, const char *path, const char *base, i
651 else
652 p = pp + baselen;
653 }
652 - free(pp);
654 }
655 }
656
657 read_directory_recursive(dir, path, base, baselen, 0, simplify);
658 free_simplify(simplify);
659 + free(pp);
660 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
661 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
662 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