Skip to content

Commit afa15f3

Browse files
Michael J Grubergitster
authored andcommitted
grep: honor --textconv for the case rev:path
Make "grep" honor the "--textconv" option also for the object case, i.e. when used with an argument "rev:path". Signed-off-by: Michael J Gruber <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 335ec3b commit afa15f3

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

builtin/grep.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,10 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
458458
}
459459

460460
static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
461-
struct object *obj, const char *name)
461+
struct object *obj, const char *name, struct object_context *oc)
462462
{
463463
if (obj->type == OBJ_BLOB)
464-
return grep_sha1(opt, obj->sha1, name, 0, NULL);
464+
return grep_sha1(opt, obj->sha1, name, 0, oc ? oc->path : NULL);
465465
if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
466466
struct tree_desc tree;
467467
void *data;
@@ -503,7 +503,7 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
503503
for (i = 0; i < nr; i++) {
504504
struct object *real_obj;
505505
real_obj = deref_tag(list->objects[i].item, NULL, 0);
506-
if (grep_object(opt, pathspec, real_obj, list->objects[i].name)) {
506+
if (grep_object(opt, pathspec, real_obj, list->objects[i].name, list->objects[i].context)) {
507507
hit = 1;
508508
if (opt->status_only)
509509
break;
@@ -820,12 +820,13 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
820820
for (i = 0; i < argc; i++) {
821821
const char *arg = argv[i];
822822
unsigned char sha1[20];
823+
struct object_context oc;
823824
/* Is it a rev? */
824-
if (!get_sha1(arg, sha1)) {
825+
if (!get_sha1_with_context(arg, 0, sha1, &oc)) {
825826
struct object *object = parse_object_or_die(sha1, arg);
826827
if (!seen_dashdash)
827828
verify_non_filename(prefix, arg);
828-
add_object_array(object, arg, &list);
829+
add_object_array_with_context(object, arg, &list, xmemdupz(&oc, sizeof(struct object_context)));
829830
continue;
830831
}
831832
if (!strcmp(arg, "--")) {

object.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,7 @@ int object_list_contains(struct object_list *list, struct object *obj)
255255
return 0;
256256
}
257257

258-
void add_object_array(struct object *obj, const char *name, struct object_array *array)
259-
{
260-
add_object_array_with_mode(obj, name, array, S_IFINVALID);
261-
}
262-
263-
void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
258+
static void add_object_array_with_mode_context(struct object *obj, const char *name, struct object_array *array, unsigned mode, struct object_context *context)
264259
{
265260
unsigned nr = array->nr;
266261
unsigned alloc = array->alloc;
@@ -275,9 +270,28 @@ void add_object_array_with_mode(struct object *obj, const char *name, struct obj
275270
objects[nr].item = obj;
276271
objects[nr].name = name;
277272
objects[nr].mode = mode;
273+
objects[nr].context = context;
278274
array->nr = ++nr;
279275
}
280276

277+
void add_object_array(struct object *obj, const char *name, struct object_array *array)
278+
{
279+
add_object_array_with_mode(obj, name, array, S_IFINVALID);
280+
}
281+
282+
void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
283+
{
284+
add_object_array_with_mode_context(obj, name, array, mode, NULL);
285+
}
286+
287+
void add_object_array_with_context(struct object *obj, const char *name, struct object_array *array, struct object_context *context)
288+
{
289+
if (context)
290+
add_object_array_with_mode_context(obj, name, array, context->mode, context);
291+
else
292+
add_object_array_with_mode_context(obj, name, array, S_IFINVALID, context);
293+
}
294+
281295
void object_array_remove_duplicates(struct object_array *array)
282296
{
283297
unsigned int ref, src, dst;

object.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct object_array {
1313
struct object *item;
1414
const char *name;
1515
unsigned mode;
16+
struct object_context *context;
1617
} *objects;
1718
};
1819

@@ -85,6 +86,7 @@ int object_list_contains(struct object_list *list, struct object *obj);
8586
/* Object array handling .. */
8687
void add_object_array(struct object *obj, const char *name, struct object_array *array);
8788
void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode);
89+
void add_object_array_with_context(struct object *obj, const char *name, struct object_array *array, struct object_context *context);
8890
void object_array_remove_duplicates(struct object_array *);
8991

9092
void clear_object_flags(unsigned flags);

t/t7008-grep-binary.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,10 @@ test_expect_success 'grep --no-textconv does not honor textconv' '
170170
test_must_fail git grep --no-textconv Qfile
171171
'
172172

173-
test_expect_failure 'grep --textconv blob honors textconv' '
173+
test_expect_success 'grep --textconv blob honors textconv' '
174174
echo "HEAD:a:binaryQfile" >expect &&
175175
git grep --textconv Qfile HEAD:a >actual &&
176176
test_cmp expect actual
177177
'
178178

179-
test_expect_success 'grep --no-textconv blob does not honor textconv' '
180-
test_must_fail git grep --no-textconv Qfile HEAD:a
181-
'
182-
183179
test_done

0 commit comments

Comments
 (0)