Skip to content

Commit

Permalink
Fix for literal pattern containing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
iizukanao committed Jul 31, 2014
1 parent 7b24ee5 commit 8a71359
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/ignore.c
Expand Up @@ -70,6 +70,8 @@ void cleanup_ignore(ignores *ig) {
void add_ignore_pattern(ignores *ig, const char *pattern) {
int i;
int pattern_len;
char *tmp_pattern;
int tmp_pattern_len;

/* Strip off the leading dot so that matches are more likely. */
if (strncmp(pattern, "./", 2) == 0) {
Expand Down Expand Up @@ -104,14 +106,25 @@ void add_ignore_pattern(ignores *ig, const char *pattern) {
/* a balanced binary tree is best for performance, but I'm lazy */
ig->names_len++;
ig->names = ag_realloc(ig->names, ig->names_len * sizeof(char *));

/* Prepend '/' if the pattern contains '/' but doesn't start with '/' */
if ((pattern[0] != '/') && (strchr(pattern, '/') != NULL)) {
ag_asprintf(&tmp_pattern, "/%s", pattern);
tmp_pattern_len = pattern_len + 1;
} else {
tmp_pattern = ag_strndup(pattern, pattern_len);
tmp_pattern_len = pattern_len;
}

for (i = ig->names_len - 1; i > 0; i--) {
if (strcmp(pattern, ig->names[i - 1]) > 0) {
if (strcmp(tmp_pattern, ig->names[i - 1]) > 0) {
break;
}
ig->names[i] = ig->names[i - 1];
}
ig->names[i] = ag_strndup(pattern, pattern_len);
log_debug("added literal ignore pattern %s", pattern);
ig->names[i] = ag_strndup(tmp_pattern, tmp_pattern_len);
log_debug("added literal ignore pattern %s", tmp_pattern);
free(tmp_pattern);
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/contain_slash.t
@@ -0,0 +1,23 @@
Setup:

$ . $TESTDIR/setup.sh
$ mkdir -p ./a/b/c ./d/e/f ./g/a/b ./h/d/e
$ echo 'whatever1' > ./a/b/c/foo.yml
$ echo 'whatever2' > ./d/e/f/foo.yml
$ echo 'whatever3' > ./g/a/b/foo.yml
$ echo 'whatever4' > ./h/d/e/foo.yml
$ echo 'a/b\n/d/e' > ./.gitignore

Ignore files in ./a/b or ./d/e

$ ag whatever . | sort
g/a/b/foo.yml:1:whatever3
h/d/e/foo.yml:1:whatever4

Dont ignore anything (unrestricted search):

$ ag -u whatever . | sort
a/b/c/foo.yml:1:whatever1
d/e/f/foo.yml:1:whatever2
g/a/b/foo.yml:1:whatever3
h/d/e/foo.yml:1:whatever4

0 comments on commit 8a71359

Please sign in to comment.