grep: stop looking at random places for .gitattributes
grep searches for .gitattributes using "name" field in struct grep_source but that field is not real on-disk path name. For example, "grep pattern rev" fills the field with "rev:path", and Git looks for .gitattributes in the (non-existent but exploitable) path "rev:path" instead of "path". This patch passes real paths down to grep_source_load_driver() when: - grep on work tree - grep on the index - grep a commit (or a tag if it points to a commit) so that these cases look up .gitattributes at proper paths. .gitattributes lookup is disabled in all other cases. Initial-work-by: Jeff King <peff@peff.net> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Oct 12, 2012 at 17:49 UTC
55c61688ea1e41f4a8c26f957bf1bc43cd39ed97
4 files changed
+51
-17
builtin/grep.c
+18
-13
@@ -86,7 +86,7 @@ static pthread_cond_t cond_result;
86
static int skip_first_line;
87
88
static void add_work(struct grep_opt *opt, enum grep_source_type type,
89
- const char *name, const void *id)
89
+ const char *name, const char *path, const void *id)
90
{
91
grep_lock();
92
@@ -94,7 +94,7 @@ static void add_work(struct grep_opt *opt, enum grep_source_type type,
94
pthread_cond_wait(&cond_write, &grep_mutex);
95
}
96
97
- grep_source_init(&todo[todo_end].source, type, name, id);
97
+ grep_source_init(&todo[todo_end].source, type, name, path, id);
98
if (opt->binary != GREP_BINARY_TEXT)
99
grep_source_load_driver(&todo[todo_end].source);
100
todo[todo_end].done = 0;
@@ -319,7 +319,8 @@ static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type
319
}
320
321
static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
322
- const char *filename, int tree_name_len)
322
+ const char *filename, int tree_name_len,
323
+ const char *path)
324
{
325
struct strbuf pathbuf = STRBUF_INIT;
326
@@ -333,7 +334,7 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
334
335
#ifndef NO_PTHREADS
336
if (use_threads) {
336
- add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, sha1);
337
+ add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, path, sha1);
338
strbuf_release(&pathbuf);
339
return 0;
340
} else
@@ -342,7 +343,7 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
343
struct grep_source gs;
344
int hit;
345
345
- grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, sha1);
346
+ grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, path, sha1);
347
strbuf_release(&pathbuf);
348
hit = grep_source(opt, &gs);
349
@@ -362,7 +363,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
363
364
#ifndef NO_PTHREADS
365
if (use_threads) {
365
- add_work(opt, GREP_SOURCE_FILE, buf.buf, filename);
366
+ add_work(opt, GREP_SOURCE_FILE, buf.buf, filename, filename);
367
strbuf_release(&buf);
368
return 0;
369
} else
@@ -371,7 +372,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
372
struct grep_source gs;
373
int hit;
374
374
- grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename);
375
+ grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename, filename);
376
strbuf_release(&buf);
377
hit = grep_source(opt, &gs);
378
@@ -427,7 +428,7 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
428
if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) {
429
if (ce_stage(ce))
430
continue;
430
- hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
431
+ hit |= grep_sha1(opt, ce->sha1, ce->name, 0, ce->name);
432
}
433
else
434
hit |= grep_file(opt, ce->name);
@@ -445,7 +446,8 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
446
}
447
448
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
448
- struct tree_desc *tree, struct strbuf *base, int tn_len)
449
+ struct tree_desc *tree, struct strbuf *base, int tn_len,
450
+ int check_attr)
451
{
452
int hit = 0;
453
enum interesting match = entry_not_interesting;
@@ -466,7 +468,8 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
468
strbuf_add(base, entry.path, te_len);
469
470
if (S_ISREG(entry.mode)) {
469
- hit |= grep_sha1(opt, entry.sha1, base->buf, tn_len);
471
+ hit |= grep_sha1(opt, entry.sha1, base->buf, tn_len,
472
+ check_attr ? base->buf + tn_len : NULL);
473
}
474
else if (S_ISDIR(entry.mode)) {
475
enum object_type type;
@@ -481,7 +484,8 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
484
485
strbuf_addch(base, '/');
486
init_tree_desc(&sub, data, size);
484
- hit |= grep_tree(opt, pathspec, &sub, base, tn_len);
487
+ hit |= grep_tree(opt, pathspec, &sub, base, tn_len,
488
+ check_attr);
489
free(data);
490
}
491
strbuf_setlen(base, old_baselen);
@@ -496,7 +500,7 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
500
struct object *obj, const char *name)
501
{
502
if (obj->type == OBJ_BLOB)
499
- return grep_sha1(opt, obj->sha1, name, 0);
503
+ return grep_sha1(opt, obj->sha1, name, 0, NULL);
504
if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
505
struct tree_desc tree;
506
void *data;
@@ -519,7 +523,8 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
523
strbuf_addch(&base, ':');
524
}
525
init_tree_desc(&tree, data, size);
522
- hit = grep_tree(opt, pathspec, &tree, &base, base.len);
526
+ hit = grep_tree(opt, pathspec, &tree, &base, base.len,
527
+ obj->type == OBJ_COMMIT);
528
strbuf_release(&base);
529
free(data);
530
return hit;
grep.c
+8
-3
@@ -1363,7 +1363,7 @@ int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1363
struct grep_source gs;
1364
int r;
1365
1366
- grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL);
1366
+ grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL, NULL);
1367
gs.buf = buf;
1368
gs.size = size;
1369
@@ -1374,10 +1374,12 @@ int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1374
}
1375
1376
void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1377
- const char *name, const void *identifier)
1377
+ const char *name, const char *path,
1378
+ const void *identifier)
1379
{
1380
gs->type = type;
1381
gs->name = name ? xstrdup(name) : NULL;
1382
+ gs->path = path ? xstrdup(path) : NULL;
1383
gs->buf = NULL;
1384
gs->size = 0;
1385
gs->driver = NULL;
@@ -1399,6 +1401,8 @@ void grep_source_clear(struct grep_source *gs)
1401
{
1402
free(gs->name);
1403
gs->name = NULL;
1404
+ free(gs->path);
1405
+ gs->path = NULL;
1406
free(gs->identifier);
1407
gs->identifier = NULL;
1408
grep_source_clear_data(gs);
@@ -1491,7 +1495,8 @@ void grep_source_load_driver(struct grep_source *gs)
1495
return;
1496
1497
grep_attr_lock();
1494
- gs->driver = userdiff_find_by_path(gs->name);
1498
+ if (gs->path)
1499
+ gs->driver = userdiff_find_by_path(gs->path);
1500
if (!gs->driver)
1501
gs->driver = userdiff_find_by_name("default");
1502
grep_attr_unlock();
grep.h
+3
-1
@@ -144,11 +144,13 @@ struct grep_source {
144
char *buf;
145
unsigned long size;
146
147
+ char *path; /* for attribute lookups */
148
struct userdiff_driver *driver;
149
};
150
151
void grep_source_init(struct grep_source *gs, enum grep_source_type type,
151
- const char *name, const void *identifier);
152
+ const char *name, const char *path,
153
+ const void *identifier);
154
void grep_source_clear_data(struct grep_source *gs);
155
void grep_source_clear(struct grep_source *gs);
156
void grep_source_load_driver(struct grep_source *gs);
t/t7008-grep-binary.sh
+22
@@ -111,6 +111,28 @@ test_expect_success 'grep respects binary diff attribute' '
111
test_cmp expect actual
112
'
113
114
+test_expect_success 'grep --cached respects binary diff attribute' '
115
+ git grep --cached text t >actual &&
116
+ test_cmp expect actual
117
+'
118
+
119
+test_expect_success 'grep --cached respects binary diff attribute (2)' '
120
+ git add .gitattributes &&
121
+ rm .gitattributes &&
122
+ git grep --cached text t >actual &&
123
+ test_when_finished "git rm --cached .gitattributes" &&
124
+ test_when_finished "git checkout .gitattributes" &&
125
+ test_cmp expect actual
126
+'
127
+
128
+test_expect_success 'grep revision respects binary diff attribute' '
129
+ git commit -m new &&
130
+ echo "Binary file HEAD:t matches" >expect &&
131
+ git grep text HEAD -- t >actual &&
132
+ test_when_finished "git reset HEAD^" &&
133
+ test_cmp expect actual
134
+'
135
+
136
test_expect_success 'grep respects not-binary diff attribute' '
137
echo binQary | q_to_nul >b &&
138
git add b &&