Skip to content

Commit 55c6168

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 40701ad commit 55c6168

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

builtin/grep.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ static pthread_cond_t cond_result;
8686
static int skip_first_line;
8787

8888
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)
9090
{
9191
grep_lock();
9292

9393
while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) {
9494
pthread_cond_wait(&cond_write, &grep_mutex);
9595
}
9696

97-
grep_source_init(&todo[todo_end].source, type, name, id);
97+
grep_source_init(&todo[todo_end].source, type, name, path, id);
9898
if (opt->binary != GREP_BINARY_TEXT)
9999
grep_source_load_driver(&todo[todo_end].source);
100100
todo[todo_end].done = 0;
@@ -319,7 +319,8 @@ static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type
319319
}
320320

321321
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)
323324
{
324325
struct strbuf pathbuf = STRBUF_INIT;
325326

@@ -333,7 +334,7 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
333334

334335
#ifndef NO_PTHREADS
335336
if (use_threads) {
336-
add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, sha1);
337+
add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, path, sha1);
337338
strbuf_release(&pathbuf);
338339
return 0;
339340
} else
@@ -342,7 +343,7 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
342343
struct grep_source gs;
343344
int hit;
344345

345-
grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, sha1);
346+
grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, path, sha1);
346347
strbuf_release(&pathbuf);
347348
hit = grep_source(opt, &gs);
348349

@@ -362,7 +363,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
362363

363364
#ifndef NO_PTHREADS
364365
if (use_threads) {
365-
add_work(opt, GREP_SOURCE_FILE, buf.buf, filename);
366+
add_work(opt, GREP_SOURCE_FILE, buf.buf, filename, filename);
366367
strbuf_release(&buf);
367368
return 0;
368369
} else
@@ -371,7 +372,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
371372
struct grep_source gs;
372373
int hit;
373374

374-
grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename);
375+
grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename, filename);
375376
strbuf_release(&buf);
376377
hit = grep_source(opt, &gs);
377378

@@ -427,7 +428,7 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
427428
if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) {
428429
if (ce_stage(ce))
429430
continue;
430-
hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
431+
hit |= grep_sha1(opt, ce->sha1, ce->name, 0, ce->name);
431432
}
432433
else
433434
hit |= grep_file(opt, ce->name);
@@ -445,7 +446,8 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
445446
}
446447

447448
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)
449451
{
450452
int hit = 0;
451453
enum interesting match = entry_not_interesting;
@@ -466,7 +468,8 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
466468
strbuf_add(base, entry.path, te_len);
467469

468470
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);
470473
}
471474
else if (S_ISDIR(entry.mode)) {
472475
enum object_type type;
@@ -481,7 +484,8 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
481484

482485
strbuf_addch(base, '/');
483486
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);
485489
free(data);
486490
}
487491
strbuf_setlen(base, old_baselen);
@@ -496,7 +500,7 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
496500
struct object *obj, const char *name)
497501
{
498502
if (obj->type == OBJ_BLOB)
499-
return grep_sha1(opt, obj->sha1, name, 0);
503+
return grep_sha1(opt, obj->sha1, name, 0, NULL);
500504
if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
501505
struct tree_desc tree;
502506
void *data;
@@ -519,7 +523,8 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
519523
strbuf_addch(&base, ':');
520524
}
521525
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);
523528
strbuf_release(&base);
524529
free(data);
525530
return hit;

grep.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,7 @@ int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
13631363
struct grep_source gs;
13641364
int r;
13651365

1366-
grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL);
1366+
grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL, NULL);
13671367
gs.buf = buf;
13681368
gs.size = size;
13691369

@@ -1374,10 +1374,12 @@ int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
13741374
}
13751375

13761376
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)
13781379
{
13791380
gs->type = type;
13801381
gs->name = name ? xstrdup(name) : NULL;
1382+
gs->path = path ? xstrdup(path) : NULL;
13811383
gs->buf = NULL;
13821384
gs->size = 0;
13831385
gs->driver = NULL;
@@ -1399,6 +1401,8 @@ void grep_source_clear(struct grep_source *gs)
13991401
{
14001402
free(gs->name);
14011403
gs->name = NULL;
1404+
free(gs->path);
1405+
gs->path = NULL;
14021406
free(gs->identifier);
14031407
gs->identifier = NULL;
14041408
grep_source_clear_data(gs);
@@ -1491,7 +1495,8 @@ void grep_source_load_driver(struct grep_source *gs)
14911495
return;
14921496

14931497
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);
14951500
if (!gs->driver)
14961501
gs->driver = userdiff_find_by_name("default");
14971502
grep_attr_unlock();

grep.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,13 @@ struct grep_source {
144144
char *buf;
145145
unsigned long size;
146146

147+
char *path; /* for attribute lookups */
147148
struct userdiff_driver *driver;
148149
};
149150

150151
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);
152154
void grep_source_clear_data(struct grep_source *gs);
153155
void grep_source_clear(struct grep_source *gs);
154156
void grep_source_load_driver(struct grep_source *gs);

t/t7008-grep-binary.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,28 @@ test_expect_success 'grep respects binary diff attribute' '
111111
test_cmp expect actual
112112
'
113113

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+
114136
test_expect_success 'grep respects not-binary diff attribute' '
115137
echo binQary | q_to_nul >b &&
116138
git add b &&

0 commit comments

Comments
 (0)