@matheusbsbs / matheus-bagliano / commits / 4d06f8ac43
Fork 0

Fix 'git add' with .gitignore

When '*.ig' is ignored, and you have two files f.ig and d.ig/foo in the working tree, $ git add . correctly ignored f.ig but failed to ignore d.ig/foo. This was caused by a thinko in an earlier commit 4888c534, when we tried to allow adding otherwise ignored files. After reverting that commit, this takes a much simpler approach. When we have an unmatched pathspec that talks about an existing pathname, we know it is an ignored path the user tried to add, so we include it in the set of paths directory walker returned. This does not let you say "git add -f D" on an ignored directory D and add everything under D. People can submit a patch to further allow it if they want to, but I think it is a saner behaviour to require explicit paths to be spelled out in such a case. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 29, 2006 at 11:01 UTC 4d06f8ac4389ed8bcd2d03b2dcab4b7e2a858402
4 files changed +69 -29
builtin-add.c
+27 -25
@@ -26,18 +26,9 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p
26 i = dir->nr;
27 while (--i >= 0) {
28 struct dir_entry *entry = *src++;
29 - int how = match_pathspec(pathspec, entry->name, entry->len,
30 - prefix, seen);
31 - /*
32 - * ignored entries can be added with exact match,
33 - * but not with glob nor recursive.
34 - */
35 - if (!how ||
36 - (entry->ignored_entry && how != MATCHED_EXACTLY)) {
37 - free(entry);
38 - continue;
39 - }
40 - *dst++ = entry;
29 + if (match_pathspec(pathspec, entry->name, entry->len,
30 + prefix, seen))
31 + *dst++ = entry;
32 }
33 dir->nr = dst - dir->entries;
34
@@ -47,10 +38,20 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p
38 if (seen[i])
39 continue;
40
50 - /* Existing file? We must have ignored it */
41 match = pathspec[i];
52 - if (!match[0] || !lstat(match, &st))
42 + if (!match[0])
43 continue;
44 +
45 + /* Existing file? We must have ignored it */
46 + if (!lstat(match, &st)) {
47 + struct dir_entry *ent;
48 +
49 + ent = dir_add_name(dir, match, strlen(match));
50 + ent->ignored = 1;
51 + if (S_ISDIR(st.st_mode))
52 + ent->ignored_dir = 1;
53 + continue;
54 + }
55 die("pathspec '%s' did not match any files", match);
56 }
57 }
@@ -62,8 +63,6 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec)
63
64 /* Set up the default git porcelain excludes */
65 memset(dir, 0, sizeof(*dir));
65 - if (pathspec)
66 - dir->show_both = 1;
66 dir->exclude_per_dir = ".gitignore";
67 path = git_path("info/exclude");
68 if (!access(path, R_OK))
@@ -154,7 +153,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
153 if (show_only) {
154 const char *sep = "", *eof = "";
155 for (i = 0; i < dir.nr; i++) {
157 - if (!ignored_too && dir.entries[i]->ignored_entry)
156 + if (!ignored_too && dir.entries[i]->ignored)
157 continue;
158 printf("%s%s", sep, dir.entries[i]->name);
159 sep = " ";
@@ -168,16 +167,19 @@ int cmd_add(int argc, const char **argv, const char *prefix)
167 die("index file corrupt");
168
169 if (!ignored_too) {
171 - int has_ignored = -1;
172 - for (i = 0; has_ignored < 0 && i < dir.nr; i++)
173 - if (dir.entries[i]->ignored_entry)
174 - has_ignored = i;
175 - if (0 <= has_ignored) {
170 + int has_ignored = 0;
171 + for (i = 0; i < dir.nr; i++)
172 + if (dir.entries[i]->ignored)
173 + has_ignored = 1;
174 + if (has_ignored) {
175 fprintf(stderr, ignore_warning);
177 - for (i = has_ignored; i < dir.nr; i++) {
178 - if (!dir.entries[i]->ignored_entry)
176 + for (i = 0; i < dir.nr; i++) {
177 + if (!dir.entries[i]->ignored)
178 continue;
180 - fprintf(stderr, "%s\n", dir.entries[i]->name);
179 + fprintf(stderr, "%s", dir.entries[i]->name);
180 + if (dir.entries[i]->ignored_dir)
181 + fprintf(stderr, " (directory)");
182 + fputc('\n', stderr);
183 }
184 fprintf(stderr,
185 "Use -f if you really want to add them.\n");
dir.c
+5 -3
@@ -260,12 +260,12 @@ int excluded(struct dir_struct *dir, const char *pathname)
260 return 0;
261 }
262
263 -static void add_name(struct dir_struct *dir, const char *pathname, int len)
263 +struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
264 {
265 struct dir_entry *ent;
266
267 if (cache_name_pos(pathname, len) >= 0)
268 - return;
268 + return NULL;
269
270 if (dir->nr == dir->alloc) {
271 int alloc = alloc_nr(dir->alloc);
@@ -273,10 +273,12 @@ static void add_name(struct dir_struct *dir, const char *pathname, int len)
273 dir->entries = xrealloc(dir->entries, alloc*sizeof(ent));
274 }
275 ent = xmalloc(sizeof(*ent) + len + 1);
276 + ent->ignored = ent->ignored_dir = 0;
277 ent->len = len;
278 memcpy(ent->name, pathname, len);
279 ent->name[len] = 0;
280 dir->entries[dir->nr++] = ent;
281 + return ent;
282 }
283
284 static int dir_exists(const char *dirname, int len)
@@ -364,7 +366,7 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
366 if (check_only)
367 goto exit_early;
368 else
367 - add_name(dir, fullname, baselen + len);
369 + dir_add_name(dir, fullname, baselen + len);
370 }
371 exit_early:
372 closedir(fdir);
dir.h
+4 -1
@@ -13,7 +13,9 @@
13
14
15 struct dir_entry {
16 - int len;
16 + unsigned int ignored : 1;
17 + unsigned int ignored_dir : 1;
18 + unsigned int len : 30;
19 char name[FLEX_ARRAY]; /* more */
20 };
21
@@ -55,5 +57,6 @@ extern void add_excludes_from_file(struct dir_struct *, const char *fname);
57 extern void add_exclude(const char *string, const char *base,
58 int baselen, struct exclude_list *which);
59 extern int file_exists(const char *);
60 +extern struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len);
61
62 #endif
t/t3700-add.sh
+33
@@ -51,4 +51,37 @@ test_expect_success \
51 *) echo fail; git-ls-files --stage xfoo3; (exit 1);;
52 esac'
53
54 +test_expect_success '.gitignore test setup' '
55 + echo "*.ig" >.gitignore &&
56 + mkdir c.if d.ig &&
57 + >a.ig && >b.if &&
58 + >c.if/c.if && >c.if/c.ig &&
59 + >d.ig/d.if && >d.ig/d.ig
60 +'
61 +
62 +test_expect_success '.gitignore is honored' '
63 + git-add . &&
64 + ! git-ls-files | grep "\\.ig"
65 +'
66 +
67 +test_expect_success 'error out when attempting to add ignored ones without -f' '
68 + ! git-add a.?? &&
69 + ! git-ls-files | grep "\\.ig"
70 +'
71 +
72 +test_expect_success 'error out when attempting to add ignored ones without -f' '
73 + ! git-add d.?? &&
74 + ! git-ls-files | grep "\\.ig"
75 +'
76 +
77 +test_expect_success 'add ignored ones with -f' '
78 + git-add -f a.?? &&
79 + git-ls-files --error-unmatch a.ig
80 +'
81 +
82 +test_expect_success 'add ignored ones with -f' '
83 + git-add -f d.??/* &&
84 + git-ls-files --error-unmatch d.ig/d.if d.ig/d.ig
85 +'
86 +
87 test_done