@matheusbsbs / matheus-bagliano / commits / 0d042fecf2
Fork 0

git-grep: show pathnames relative to the current directory

By default, the command shows pathnames relative to the current directory. Use --full-name (the same flag to do so in ls-files) if you want to see the full pathname relative to the project root. This makes it very pleasant to run in Emacs compilation (or "grep-find") buffer. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Aug 11, 2006 at 00:44 UTC 0d042fecf2f4130717d198f9cc2d792e72370bda
3 files changed +85 -13
Documentation/git-grep.txt
+7 -1
@@ -11,7 +11,7 @@ SYNOPSIS
11 [verse]
12 'git-grep' [--cached]
13 [-a | --text] [-I] [-i | --ignore-case] [-w | --word-regexp]
14 - [-v | --invert-match]
14 + [-v | --invert-match] [--full-name]
15 [-E | --extended-regexp] [-G | --basic-regexp] [-F | --fixed-strings]
16 [-n] [-l | --files-with-matches] [-L | --files-without-match]
17 [-c | --count]
@@ -47,6 +47,12 @@ OPTIONS
47 -v | --invert-match::
48 Select non-matching lines.
49
50 +--full-name::
51 + When run from a subdirectory, the command usually
52 + outputs paths relative to the current directory. This
53 + option forces paths to be output relative to the project
54 + top directory.
55 +
56 -E | --extended-regexp | -G | --basic-regexp::
57 Use POSIX extended/basic regexp for patterns. Default
58 is to use basic regexp.
builtin-grep.c
+49 -10
@@ -123,6 +123,7 @@ struct grep_opt {
123 struct grep_pat *pattern_list;
124 struct grep_pat **pattern_tail;
125 struct grep_expr *pattern_expression;
126 + int prefix_length;
127 regex_t regexp;
128 unsigned linenum:1;
129 unsigned invert:1;
@@ -136,6 +137,7 @@ struct grep_opt {
137 #define GREP_BINARY_TEXT 2
138 unsigned binary:2;
139 unsigned extended:1;
140 + unsigned relative:1;
141 int regflags;
142 unsigned pre_context;
143 unsigned post_context;
@@ -632,19 +634,40 @@ static int grep_buffer(struct grep_opt *opt, const char *name,
634 return !!last_hit;
635 }
636
635 -static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name)
637 +static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len)
638 {
639 unsigned long size;
640 char *data;
641 char type[20];
642 + char *to_free = NULL;
643 int hit;
644 +
645 data = read_sha1_file(sha1, type, &size);
646 if (!data) {
647 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
648 return 0;
649 }
650 + if (opt->relative && opt->prefix_length) {
651 + static char name_buf[PATH_MAX];
652 + char *cp;
653 + int name_len = strlen(name) - opt->prefix_length + 1;
654 +
655 + if (!tree_name_len)
656 + name += opt->prefix_length;
657 + else {
658 + if (ARRAY_SIZE(name_buf) <= name_len)
659 + cp = to_free = xmalloc(name_len);
660 + else
661 + cp = name_buf;
662 + memcpy(cp, name, tree_name_len);
663 + strcpy(cp + tree_name_len,
664 + name + tree_name_len + opt->prefix_length);
665 + name = cp;
666 + }
667 + }
668 hit = grep_buffer(opt, name, data, size);
669 free(data);
670 + free(to_free);
671 return hit;
672 }
673
@@ -674,6 +697,8 @@ static int grep_file(struct grep_opt *opt, const char *filename)
697 return 0;
698 }
699 close(i);
700 + if (opt->relative && opt->prefix_length)
701 + filename += opt->prefix_length;
702 i = grep_buffer(opt, filename, data, st.st_size);
703 free(data);
704 return i;
@@ -720,7 +745,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
745 char *argptr = randarg;
746 struct grep_pat *p;
747
723 - if (opt->extended)
748 + if (opt->extended || (opt->relative && opt->prefix_length))
749 return -1;
750 len = nr = 0;
751 push_arg("grep");
@@ -845,7 +870,7 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
870 if (!pathspec_matches(paths, ce->name))
871 continue;
872 if (cached)
848 - hit |= grep_sha1(opt, ce->sha1, ce->name);
873 + hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
874 else
875 hit |= grep_file(opt, ce->name);
876 }
@@ -860,11 +885,12 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
885 int hit = 0;
886 struct name_entry entry;
887 char *down;
863 - char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
888 + int tn_len = strlen(tree_name);
889 + char *path_buf = xmalloc(PATH_MAX + tn_len + 100);
890
865 - if (tree_name[0]) {
866 - int offset = sprintf(path_buf, "%s:", tree_name);
867 - down = path_buf + offset;
891 + if (tn_len) {
892 + tn_len = sprintf(path_buf, "%s:", tree_name);
893 + down = path_buf + tn_len;
894 strcat(down, base);
895 }
896 else {
@@ -886,7 +912,7 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
912 if (!pathspec_matches(paths, down))
913 ;
914 else if (S_ISREG(entry.mode))
889 - hit |= grep_sha1(opt, entry.sha1, path_buf);
915 + hit |= grep_sha1(opt, entry.sha1, path_buf, tn_len);
916 else if (S_ISDIR(entry.mode)) {
917 char type[20];
918 struct tree_desc sub;
@@ -907,7 +933,7 @@ static int grep_object(struct grep_opt *opt, const char **paths,
933 struct object *obj, const char *name)
934 {
935 if (obj->type == OBJ_BLOB)
910 - return grep_sha1(opt, obj->sha1, name);
936 + return grep_sha1(opt, obj->sha1, name, 0);
937 if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
938 struct tree_desc tree;
939 void *data;
@@ -945,6 +971,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
971 int i;
972
973 memset(&opt, 0, sizeof(opt));
974 + opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
975 + opt.relative = 1;
976 opt.pattern_tail = &opt.pattern_list;
977 opt.regflags = REG_NEWLINE;
978
@@ -1118,6 +1146,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1146 }
1147 die(emsg_missing_argument, arg);
1148 }
1149 + if (!strcmp("--full-name", arg)) {
1150 + opt.relative = 0;
1151 + continue;
1152 + }
1153 if (!strcmp("--", arg)) {
1154 /* later processing wants to have this at argv[1] */
1155 argv--;
@@ -1176,8 +1208,15 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1208 verify_filename(prefix, argv[j]);
1209 }
1210
1179 - if (i < argc)
1211 + if (i < argc) {
1212 paths = get_pathspec(prefix, argv + i);
1213 + if (opt.prefix_length && opt.relative) {
1214 + /* Make sure we do not get outside of paths */
1215 + for (i = 0; paths[i]; i++)
1216 + if (strncmp(prefix, paths[i], opt.prefix_length))
1217 + die("git-grep: cannot generate relative filenames containing '..'");
1218 + }
1219 + }
1220 else if (prefix) {
1221 paths = xcalloc(2, sizeof(const char *));
1222 paths[0] = prefix;
t/t7002-grep.sh
+29 -2
@@ -3,7 +3,7 @@
3 # Copyright (c) 2006 Junio C Hamano
4 #
5
6 -test_description='git grep -w
6 +test_description='git grep various.
7 '
8
9 . ./test-lib.sh
@@ -19,7 +19,9 @@ test_expect_success setup '
19 echo x x xx x >x &&
20 echo y yy >y &&
21 echo zzz > z &&
22 - git add file x y z &&
22 + mkdir t &&
23 + echo test >t/t &&
24 + git add file x y z t/t &&
25 git commit -m initial
26 '
27
@@ -80,6 +82,31 @@ do
82 diff expected actual
83 fi
84 '
85 +
86 + test_expect_success "grep $L (t-1)" '
87 + echo "${HC}t/t:1:test" >expected &&
88 + git grep -n -e test $H >actual &&
89 + diff expected actual
90 + '
91 +
92 + test_expect_success "grep $L (t-2)" '
93 + echo "${HC}t:1:test" >expected &&
94 + (
95 + cd t &&
96 + git grep -n -e test $H
97 + ) >actual &&
98 + diff expected actual
99 + '
100 +
101 + test_expect_success "grep $L (t-3)" '
102 + echo "${HC}t/t:1:test" >expected &&
103 + (
104 + cd t &&
105 + git grep --full-name -n -e test $H
106 + ) >actual &&
107 + diff expected actual
108 + '
109 +
110 done
111
112 test_done