Merge pull request #646 from arrbee/ignore-pat-leading-slash

Ignore pat leading slash
This commit is contained in:
Vicent Martí 2012-04-26 12:08:43 -07:00
commit 631ba94e0e
6 changed files with 103 additions and 33 deletions

View File

@ -23,10 +23,11 @@ int git_attr_get(
*value = NULL; *value = NULL;
if ((error = git_attr_path__init( if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
&path, pathname, git_repository_workdir(repo))) < 0 || return -1;
(error = collect_attr_files(repo, pathname, &files)) < 0)
return error; if ((error = collect_attr_files(repo, pathname, &files)) < 0)
goto cleanup;
attr.name = name; attr.name = name;
attr.name_hash = git_attr_file__name_hash(name); attr.name_hash = git_attr_file__name_hash(name);
@ -38,13 +39,14 @@ int git_attr_get(
if (pos >= 0) { if (pos >= 0) {
*value = ((git_attr_assignment *)git_vector_get( *value = ((git_attr_assignment *)git_vector_get(
&rule->assigns, pos))->value; &rule->assigns, pos))->value;
goto found; goto cleanup;
} }
} }
} }
found: cleanup:
git_vector_free(&files); git_vector_free(&files);
git_attr_path__free(&path);
return error; return error;
} }
@ -70,10 +72,11 @@ int git_attr_get_many(
memset((void *)values, 0, sizeof(const char *) * num_attr); memset((void *)values, 0, sizeof(const char *) * num_attr);
if ((error = git_attr_path__init( if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
&path, pathname, git_repository_workdir(repo))) < 0 || return -1;
(error = collect_attr_files(repo, pathname, &files)) < 0)
return error; if ((error = collect_attr_files(repo, pathname, &files)) < 0)
goto cleanup;
info = git__calloc(num_attr, sizeof(attr_get_many_info)); info = git__calloc(num_attr, sizeof(attr_get_many_info));
GITERR_CHECK_ALLOC(info); GITERR_CHECK_ALLOC(info);
@ -108,6 +111,7 @@ int git_attr_get_many(
cleanup: cleanup:
git_vector_free(&files); git_vector_free(&files);
git_attr_path__free(&path);
git__free(info); git__free(info);
return error; return error;
@ -128,10 +132,11 @@ int git_attr_foreach(
git_attr_assignment *assign; git_attr_assignment *assign;
git_strmap *seen = NULL; git_strmap *seen = NULL;
if ((error = git_attr_path__init( if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
&path, pathname, git_repository_workdir(repo))) < 0 || return -1;
(error = collect_attr_files(repo, pathname, &files)) < 0)
return error; if ((error = collect_attr_files(repo, pathname, &files)) < 0)
goto cleanup;
seen = git_strmap_alloc(); seen = git_strmap_alloc();
GITERR_CHECK_ALLOC(seen); GITERR_CHECK_ALLOC(seen);
@ -158,6 +163,7 @@ int git_attr_foreach(
cleanup: cleanup:
git_strmap_free(seen); git_strmap_free(seen);
git_vector_free(&files); git_vector_free(&files);
git_attr_path__free(&path);
return error; return error;
} }

View File

@ -251,27 +251,50 @@ git_attr_assignment *git_attr_rule__lookup_assignment(
int git_attr_path__init( int git_attr_path__init(
git_attr_path *info, const char *path, const char *base) git_attr_path *info, const char *path, const char *base)
{ {
assert(info && path); /* build full path as best we can */
info->path = path; git_buf_init(&info->full, 0);
info->basename = strrchr(path, '/');
if (base != NULL && git_path_root(path) < 0) {
if (git_buf_joinpath(&info->full, base, path) < 0)
return -1;
info->path = info->full.ptr + strlen(base);
} else {
if (git_buf_sets(&info->full, path) < 0)
return -1;
info->path = info->full.ptr;
}
/* remove trailing slashes */
while (info->full.size > 0) {
if (info->full.ptr[info->full.size - 1] != '/')
break;
info->full.size--;
}
info->full.ptr[info->full.size] = '\0';
/* skip leading slashes in path */
while (*info->path == '/')
info->path++;
/* find trailing basename component */
info->basename = strrchr(info->path, '/');
if (info->basename) if (info->basename)
info->basename++; info->basename++;
if (!info->basename || !*info->basename) if (!info->basename || !*info->basename)
info->basename = path; info->basename = info->path;
if (base != NULL && git_path_root(path) < 0) { info->is_dir = (int)git_path_isdir(info->full.ptr);
git_buf full_path = GIT_BUF_INIT;
if (git_buf_joinpath(&full_path, base, path) < 0)
return -1;
info->is_dir = (int)git_path_isdir(full_path.ptr);
git_buf_free(&full_path);
return 0;
}
info->is_dir = (int)git_path_isdir(path);
return 0; return 0;
} }
void git_attr_path__free(git_attr_path *info)
{
git_buf_free(&info->full);
info->path = NULL;
info->basename = NULL;
}
/* /*
* From gitattributes(5): * From gitattributes(5):
@ -353,6 +376,8 @@ int git_attr_fnmatch__parse(
if (*scan == '/') { if (*scan == '/') {
spec->flags = spec->flags | GIT_ATTR_FNMATCH_FULLPATH; spec->flags = spec->flags | GIT_ATTR_FNMATCH_FULLPATH;
slash_count++; slash_count++;
if (pattern == scan)
pattern++;
} }
/* remember if we see an unescaped wildcard in pattern */ /* remember if we see an unescaped wildcard in pattern */
else if ((*scan == '*' || *scan == '.' || *scan == '[') && else if ((*scan == '*' || *scan == '.' || *scan == '[') &&

View File

@ -10,6 +10,7 @@
#include "git2/attr.h" #include "git2/attr.h"
#include "vector.h" #include "vector.h"
#include "pool.h" #include "pool.h"
#include "buffer.h"
#define GIT_ATTR_FILE ".gitattributes" #define GIT_ATTR_FILE ".gitattributes"
#define GIT_ATTR_FILE_INREPO "info/attributes" #define GIT_ATTR_FILE_INREPO "info/attributes"
@ -54,9 +55,10 @@ typedef struct {
} git_attr_file; } git_attr_file;
typedef struct { typedef struct {
git_buf full;
const char *path; const char *path;
const char *basename; const char *basename;
int is_dir; int is_dir;
} git_attr_path; } git_attr_path;
/* /*
@ -114,6 +116,8 @@ extern git_attr_assignment *git_attr_rule__lookup_assignment(
extern int git_attr_path__init( extern int git_attr_path__init(
git_attr_path *info, const char *path, const char *base); git_attr_path *info, const char *path, const char *base);
extern void git_attr_path__free(git_attr_path *info);
extern int git_attr_assignment__parse( extern int git_attr_assignment__parse(
git_repository *repo, /* needed to expand macros */ git_repository *repo, /* needed to expand macros */
git_pool *pool, git_pool *pool,

View File

@ -176,20 +176,23 @@ int git_ignore__lookup(git_ignores *ignores, const char *pathname, int *ignored)
/* first process builtins - success means path was found */ /* first process builtins - success means path was found */
if (ignore_lookup_in_rules( if (ignore_lookup_in_rules(
&ignores->ign_internal->rules, &path, ignored)) &ignores->ign_internal->rules, &path, ignored))
return 0; goto cleanup;
/* next process files in the path */ /* next process files in the path */
git_vector_foreach(&ignores->ign_path, i, file) { git_vector_foreach(&ignores->ign_path, i, file) {
if (ignore_lookup_in_rules(&file->rules, &path, ignored)) if (ignore_lookup_in_rules(&file->rules, &path, ignored))
return 0; goto cleanup;
} }
/* last process global ignores */ /* last process global ignores */
git_vector_foreach(&ignores->ign_global, i, file) { git_vector_foreach(&ignores->ign_global, i, file) {
if (ignore_lookup_in_rules(&file->rules, &path, ignored)) if (ignore_lookup_in_rules(&file->rules, &path, ignored))
return 0; goto cleanup;
} }
*ignored = 0; *ignored = 0;
cleanup:
git_attr_path__free(&path);
return 0; return 0;
} }

View File

@ -25,6 +25,7 @@ void test_attr_lookup__simple(void)
cl_git_pass(git_attr_file__lookup_one(file,&path,"missing",&value)); cl_git_pass(git_attr_file__lookup_one(file,&path,"missing",&value));
cl_assert(!value); cl_assert(!value);
git_attr_path__free(&path);
git_attr_file__free(file); git_attr_file__free(file);
} }
@ -45,6 +46,8 @@ static void run_test_cases(git_attr_file *file, struct attr_expected *cases, int
cl_git_pass(error); cl_git_pass(error);
attr_check_expected(c->expected, c->expected_str, value); attr_check_expected(c->expected, c->expected_str, value);
git_attr_path__free(&path);
} }
} }
@ -83,7 +86,7 @@ void test_attr_lookup__match_variants(void)
{ "/not/pat2/yousee", "attr2", EXPECT_UNDEFINED, NULL }, { "/not/pat2/yousee", "attr2", EXPECT_UNDEFINED, NULL },
/* path match */ /* path match */
{ "pat3file", "attr3", EXPECT_UNDEFINED, NULL }, { "pat3file", "attr3", EXPECT_UNDEFINED, NULL },
{ "/pat3dir/pat3file", "attr3", EXPECT_UNDEFINED, NULL }, { "/pat3dir/pat3file", "attr3", EXPECT_TRUE, NULL },
{ "pat3dir/pat3file", "attr3", EXPECT_TRUE, NULL }, { "pat3dir/pat3file", "attr3", EXPECT_TRUE, NULL },
/* pattern* match */ /* pattern* match */
{ "pat4.txt", "attr4", EXPECT_TRUE, NULL }, { "pat4.txt", "attr4", EXPECT_TRUE, NULL },
@ -101,7 +104,7 @@ void test_attr_lookup__match_variants(void)
{ "pat6/pat6/.pat6", "attr6", EXPECT_TRUE, NULL }, { "pat6/pat6/.pat6", "attr6", EXPECT_TRUE, NULL },
{ "pat6/pat6/extra/foobar.pat6", "attr6", EXPECT_UNDEFINED, NULL }, { "pat6/pat6/extra/foobar.pat6", "attr6", EXPECT_UNDEFINED, NULL },
{ "/prefix/pat6/pat6/foobar.pat6", "attr6", EXPECT_UNDEFINED, NULL }, { "/prefix/pat6/pat6/foobar.pat6", "attr6", EXPECT_UNDEFINED, NULL },
{ "/pat6/pat6/foobar.pat6", "attr6", EXPECT_UNDEFINED, NULL }, { "/pat6/pat6/foobar.pat6", "attr6", EXPECT_TRUE, NULL },
/* complex pattern */ /* complex pattern */
{ "pat7a12z", "attr7", EXPECT_TRUE, NULL }, { "pat7a12z", "attr7", EXPECT_TRUE, NULL },
{ "pat7e__x", "attr7", EXPECT_TRUE, NULL }, { "pat7e__x", "attr7", EXPECT_TRUE, NULL },
@ -139,6 +142,7 @@ void test_attr_lookup__match_variants(void)
run_test_cases(file, dir_cases, 1); run_test_cases(file, dir_cases, 1);
git_attr_file__free(file); git_attr_file__free(file);
git_attr_path__free(&path);
} }
void test_attr_lookup__assign_variants(void) void test_attr_lookup__assign_variants(void)

View File

@ -50,3 +50,31 @@ void test_status_ignore__0(void)
cl_assert(git_attr_cache__is_cached(g_repo, ".git/info/exclude")); cl_assert(git_attr_cache__is_cached(g_repo, ".git/info/exclude"));
cl_assert(git_attr_cache__is_cached(g_repo, ".gitignore")); cl_assert(git_attr_cache__is_cached(g_repo, ".gitignore"));
} }
void test_status_ignore__1(void)
{
int ignored;
cl_git_rewritefile("attr/.gitignore", "/*.txt\n/dir/\n");
git_attr_cache_flush(g_repo);
cl_git_pass(git_status_should_ignore(g_repo, "root_test4.txt", &ignored));
cl_assert(ignored);
cl_git_pass(git_status_should_ignore(g_repo, "sub/subdir_test2.txt", &ignored));
cl_assert(!ignored);
cl_git_pass(git_status_should_ignore(g_repo, "dir", &ignored));
cl_assert(ignored);
cl_git_pass(git_status_should_ignore(g_repo, "dir/", &ignored));
cl_assert(ignored);
cl_git_pass(git_status_should_ignore(g_repo, "sub/dir", &ignored));
cl_assert(!ignored);
cl_git_pass(git_status_should_ignore(g_repo, "sub/dir/", &ignored));
cl_assert(!ignored);
}