return 0;
}
+ static int is_empty_line(const char *bol, const char *eol)
+ {
+ while (bol < eol && isspace(*bol))
+ bol++;
+ return bol == eol;
+ }
+
static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
{
char *bol;
+ char *peek_bol = NULL;
unsigned long left;
unsigned lno = 1;
unsigned last_hit = 0;
show_function = 1;
goto next_line;
}
- if (show_function && match_funcname(opt, gs, bol, eol))
- show_function = 0;
+ if (show_function && (!peek_bol || peek_bol < bol)) {
+ unsigned long peek_left = left;
+ char *peek_eol = eol;
+
+ /*
+ * Trailing empty lines are not interesting.
+ * Peek past them to see if they belong to the
+ * body of the current function.
+ */
+ peek_bol = bol;
+ while (is_empty_line(peek_bol, peek_eol)) {
+ peek_bol = peek_eol + 1;
+ peek_eol = end_of_line(peek_bol, &peek_left);
+ }
+
+ if (match_funcname(opt, gs, peek_bol, peek_eol))
+ show_function = 0;
+ }
if (show_function ||
(last_hit && lno <= last_hit + opt->post_context)) {
/* If the last hit is within the post context,
if (lstat(filename, &st) < 0) {
err_ret:
if (errno != ENOENT)
- error(_("'%s': %s"), filename, strerror(errno));
+ error_errno(_("failed to stat '%s'"), filename);
return -1;
}
if (!S_ISREG(st.st_mode))
goto err_ret;
data = xmallocz(size);
if (st.st_size != read_in_full(i, data, size)) {
- error(_("'%s': short read %s"), filename, strerror(errno));
+ error_errno(_("'%s': short read"), filename);
close(i);
free(data);
return -1;
. ./test-lib.sh
cat >hello.c <<EOF
+ #include <assert.h>
#include <stdio.h>
+
int main(int argc, const char **argv)
{
printf("Hello world.\n");
cat >expected <<EOF
hello.c-#include <stdio.h>
+ hello.c-
hello.c=int main(int argc, const char **argv)
hello.c-{
hello.c- printf("Hello world.\n");
test_cmp expected actual
'
+ cat >expected <<EOF
+ hello.c-#include <assert.h>
+ hello.c:#include <stdio.h>
+ EOF
+
+ test_expect_success 'grep -W shows no trailing empty lines' '
+ git grep -W stdio >actual &&
+ test_cmp expected actual
+ '
+
cat >expected <<EOF
hello.c= printf("Hello world.\n");
hello.c: return 0;
)
'
+test_expect_success 'grep --no-index descends into repos, but not .git' '
+ rm -fr non &&
+ mkdir -p non/git &&
+ (
+ GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd non/git &&
+
+ echo magic >file &&
+ git init repo &&
+ (
+ cd repo &&
+ echo magic >file &&
+ git add file &&
+ git commit -m foo &&
+ echo magic >.git/file
+ ) &&
+
+ cat >expect <<-\EOF &&
+ file
+ repo/file
+ EOF
+ git grep -l --no-index magic >actual &&
+ test_cmp expect actual
+ )
+'
+
test_expect_success 'setup double-dash tests' '
cat >double-dash <<EOF &&
--
cat >expected <<EOF
<BOLD;GREEN>hello.c<RESET>
- 2:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
- 6: /* <BLACK;BYELLOW>char<RESET> ?? */
+ 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
+ 8: /* <BLACK;BYELLOW>char<RESET> ?? */
<BOLD;GREEN>hello_world<RESET>
3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
'
cat >expected <<EOF
- hello.c-#include <stdio.h>
+ hello.c-
hello.c=int main(int argc, const char **argv)
hello.c-{
hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");