Fix leading slash behavior in attrs/ignores We were not following the git behavior for leading slashes in path names when matching git ignores and git attribute file patterns. This should fix issue #638.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
diff --git a/src/attr.c b/src/attr.c
index 3e3a7e7..120d127 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -23,10 +23,11 @@ int git_attr_get(
*value = NULL;
- if ((error = git_attr_path__init(
- &path, pathname, git_repository_workdir(repo))) < 0 ||
- (error = collect_attr_files(repo, pathname, &files)) < 0)
- return error;
+ if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
+ return -1;
+
+ if ((error = collect_attr_files(repo, pathname, &files)) < 0)
+ goto cleanup;
attr.name = name;
attr.name_hash = git_attr_file__name_hash(name);
@@ -38,13 +39,14 @@ int git_attr_get(
if (pos >= 0) {
*value = ((git_attr_assignment *)git_vector_get(
&rule->assigns, pos))->value;
- goto found;
+ goto cleanup;
}
}
}
-found:
+cleanup:
git_vector_free(&files);
+ git_attr_path__free(&path);
return error;
}
@@ -70,10 +72,11 @@ int git_attr_get_many(
memset((void *)values, 0, sizeof(const char *) * num_attr);
- if ((error = git_attr_path__init(
- &path, pathname, git_repository_workdir(repo))) < 0 ||
- (error = collect_attr_files(repo, pathname, &files)) < 0)
- return error;
+ if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
+ return -1;
+
+ if ((error = collect_attr_files(repo, pathname, &files)) < 0)
+ goto cleanup;
info = git__calloc(num_attr, sizeof(attr_get_many_info));
GITERR_CHECK_ALLOC(info);
@@ -108,6 +111,7 @@ int git_attr_get_many(
cleanup:
git_vector_free(&files);
+ git_attr_path__free(&path);
git__free(info);
return error;
@@ -128,10 +132,11 @@ int git_attr_foreach(
git_attr_assignment *assign;
git_strmap *seen = NULL;
- if ((error = git_attr_path__init(
- &path, pathname, git_repository_workdir(repo))) < 0 ||
- (error = collect_attr_files(repo, pathname, &files)) < 0)
- return error;
+ if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
+ return -1;
+
+ if ((error = collect_attr_files(repo, pathname, &files)) < 0)
+ goto cleanup;
seen = git_strmap_alloc();
GITERR_CHECK_ALLOC(seen);
@@ -158,6 +163,7 @@ int git_attr_foreach(
cleanup:
git_strmap_free(seen);
git_vector_free(&files);
+ git_attr_path__free(&path);
return error;
}
diff --git a/src/attr_file.c b/src/attr_file.c
index e34053f..650b58f 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -251,27 +251,50 @@ git_attr_assignment *git_attr_rule__lookup_assignment(
int git_attr_path__init(
git_attr_path *info, const char *path, const char *base)
{
- assert(info && path);
- info->path = path;
- info->basename = strrchr(path, '/');
- if (info->basename)
- info->basename++;
- if (!info->basename || !*info->basename)
- info->basename = path;
+ /* build full path as best we can */
+ git_buf_init(&info->full, 0);
if (base != NULL && git_path_root(path) < 0) {
- git_buf full_path = GIT_BUF_INIT;
- if (git_buf_joinpath(&full_path, base, 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->is_dir = (int)git_path_isdir(full_path.ptr);
- git_buf_free(&full_path);
- return 0;
+ info->path = info->full.ptr;
}
- info->is_dir = (int)git_path_isdir(path);
+
+ /* 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)
+ info->basename++;
+ if (!info->basename || !*info->basename)
+ info->basename = info->path;
+
+ info->is_dir = (int)git_path_isdir(info->full.ptr);
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):
@@ -353,6 +376,8 @@ int git_attr_fnmatch__parse(
if (*scan == '/') {
spec->flags = spec->flags | GIT_ATTR_FNMATCH_FULLPATH;
slash_count++;
+ if (pattern == scan)
+ pattern++;
}
/* remember if we see an unescaped wildcard in pattern */
else if ((*scan == '*' || *scan == '.' || *scan == '[') &&
diff --git a/src/attr_file.h b/src/attr_file.h
index 6775341..10851bc 100644
--- a/src/attr_file.h
+++ b/src/attr_file.h
@@ -10,6 +10,7 @@
#include "git2/attr.h"
#include "vector.h"
#include "pool.h"
+#include "buffer.h"
#define GIT_ATTR_FILE ".gitattributes"
#define GIT_ATTR_FILE_INREPO "info/attributes"
@@ -54,9 +55,10 @@ typedef struct {
} git_attr_file;
typedef struct {
+ git_buf full;
const char *path;
const char *basename;
- int is_dir;
+ int is_dir;
} git_attr_path;
/*
@@ -114,6 +116,8 @@ extern git_attr_assignment *git_attr_rule__lookup_assignment(
extern int git_attr_path__init(
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(
git_repository *repo, /* needed to expand macros */
git_pool *pool,
diff --git a/src/ignore.c b/src/ignore.c
index 165754b..20b96c6 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -176,20 +176,23 @@ int git_ignore__lookup(git_ignores *ignores, const char *pathname, int *ignored)
/* first process builtins - success means path was found */
if (ignore_lookup_in_rules(
&ignores->ign_internal->rules, &path, ignored))
- return 0;
+ goto cleanup;
/* next process files in the path */
git_vector_foreach(&ignores->ign_path, i, file) {
if (ignore_lookup_in_rules(&file->rules, &path, ignored))
- return 0;
+ goto cleanup;
}
/* last process global ignores */
git_vector_foreach(&ignores->ign_global, i, file) {
if (ignore_lookup_in_rules(&file->rules, &path, ignored))
- return 0;
+ goto cleanup;
}
*ignored = 0;
+
+cleanup:
+ git_attr_path__free(&path);
return 0;
}
diff --git a/tests-clar/attr/lookup.c b/tests-clar/attr/lookup.c
index accd617..81a4a55 100644
--- a/tests-clar/attr/lookup.c
+++ b/tests-clar/attr/lookup.c
@@ -25,6 +25,7 @@ void test_attr_lookup__simple(void)
cl_git_pass(git_attr_file__lookup_one(file,&path,"missing",&value));
cl_assert(!value);
+ git_attr_path__free(&path);
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);
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 },
/* path match */
{ "pat3file", "attr3", EXPECT_UNDEFINED, NULL },
- { "/pat3dir/pat3file", "attr3", EXPECT_UNDEFINED, NULL },
+ { "/pat3dir/pat3file", "attr3", EXPECT_TRUE, NULL },
{ "pat3dir/pat3file", "attr3", EXPECT_TRUE, NULL },
/* pattern* match */
{ "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/extra/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 */
{ "pat7a12z", "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);
git_attr_file__free(file);
+ git_attr_path__free(&path);
}
void test_attr_lookup__assign_variants(void)
diff --git a/tests-clar/status/ignore.c b/tests-clar/status/ignore.c
index 5d94007..94f8c3d 100644
--- a/tests-clar/status/ignore.c
+++ b/tests-clar/status/ignore.c
@@ -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, ".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);
+}
+