# [PATCH 1/2] gitweb: add filename search function

2 messages from 2013-08-15 to 2013-09-03. Participants: Christopher Durkin, Junio C Hamano.
Thread: https://gitlist.dev/t/34697

## Christopher Durkin, 2013-08-15 03:02

Subject: [PATCH 1/2] gitweb: add filename search function
Message-ID: <CANzJMBWDgtjbNwPLjoKniwy5xCGKyz0yBFiMBfdGJMi5Q=W4+Q@mail.gmail.com>
URL: https://gitlist.dev/e/CANzJMBWDgtjbNwPLjoKniwy5xCGKyz0yBFiMBfdGJMi5Q%3DW4%2BQ%40mail.gmail.com

```
Add another item to the search drop-down (author/committer/etc)
to search for file names instead of file content. Output is similar
to the grep contents output, with each entry linking to the file.
---
 gitweb/gitweb.perl | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index f429f75..4a7b0a5 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -6360,6 +6360,65 @@ sub git_search_grep_body {
  print "</table>\n";
 }

+sub git_search_filenames {
+    my %co = @_;
+    local $/ = "\n";
+    my $match_limit = 1000;
+
+    open my $fd, "-|", git_cmd(), 'ls-tree', '--name-only',
'--full-name', '-r',
+      $co{'tree'} or die_error(500, "Open git-ls-tree failed");
+
+    git_header_html();
+
+    git_print_page_nav('','', $hash,$co{'tree'},$hash);
+    git_print_header_div('commit', esc_html($co{'title'}), $hash);
+
+    print "<table class=\"filename_search\">\n";
+    my $matches = 0;
+    my $alternate = 1;
+    my $file_href;
+
+    while (my $filename = <$fd>) {
+        chomp $filename;
+
+        if ($matches > $match_limit) {
+            print "<div class=\"diff nodifferences\">Too many
matches, listing trimmed</div>\n";
+            last;
+        }
+
+        if ($search_use_regexp) {
+            next unless ($filename =~ /$searchtext/);
+        } else {
+            next unless index($filename,$searchtext) >= 0;
+        }
+
+        $matches++;
+        $file_href = href(action=>"blob", hash_base=>$co{'id'},
+                          file_name=>$filename);
+
+ if ($alternate) {
+    print "<tr class=\"dark\">\n";
+ } else {
+    print "<tr class=\"light\">\n";
+ }
+ $alternate ^= 1;
+
+        print "<td class=\"list\">".
+            $cgi->a({-href => $file_href, -class => "list"},
esc_path($filename));
+ print "</td>\n";
+ print "</tr>\n";
+    }
+
+    if ($matches == 0) {
+        print "<div class=\"diff nodifferences\">No matches found</div>\n";
+    }
+    close $fd;
+
+    print "</table>\n";
+
+    git_footer_html();
+}
+
 ## ======================================================================
 ## ======================================================================
 ## actions
-- 
1.8.3.msysgit.0

```

## Junio C Hamano, 2013-09-03 20:07

Subject: Re: [PATCH 1/2] gitweb: add filename search function
Message-ID: <xmqqbo49r7mz.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqbo49r7mz.fsf%40gitster.dls.corp.google.com
In-Reply-To: <CANzJMBWDgtjbNwPLjoKniwy5xCGKyz0yBFiMBfdGJMi5Q=W4+Q@mail.gmail.com>

```
Christopher Durkin <cjdurkin@gmail.com> writes:

> Add another item to the search drop-down (author/committer/etc)
> to search for file names instead of file content. Output is similar
> to the grep contents output, with each entry linking to the file.
> ---

Missing sign-off.

>  gitweb/gitweb.perl | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 59 insertions(+)
>
> diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
> index f429f75..4a7b0a5 100755
> --- a/gitweb/gitweb.perl
> +++ b/gitweb/gitweb.perl
> @@ -6360,6 +6360,65 @@ sub git_search_grep_body {
>   print "</table>\n";

I think this file is indented with HT, not a single SP.

>  }
>
> +sub git_search_filenames {
> +    my %co = @_;

... nor four SP.  One level of indent is a single HT.

> +    local $/ = "\n";
> +    my $match_limit = 1000;
> +
> +    open my $fd, "-|", git_cmd(), 'ls-tree', '--name-only',
> '--full-name', '-r',

Line-wrapped broken patch that cannot be applied (I see there are a
few more similar ones below).

> +      $co{'tree'} or die_error(500, "Open git-ls-tree failed");
> +
> +    git_header_html();
> +
> +    git_print_page_nav('','', $hash,$co{'tree'},$hash);
> +    git_print_header_div('commit', esc_html($co{'title'}), $hash);
> +
> +    print "<table class=\"filename_search\">\n";
> +    my $matches = 0;
> +    my $alternate = 1;
> +    my $file_href;
> +
> +    while (my $filename = <$fd>) {
> +        chomp $filename;
> +
> +        if ($matches > $match_limit) {
> +            print "<div class=\"diff nodifferences\">Too many
> matches, listing trimmed</div>\n";
> +            last;
> +        }
> +
> +        if ($search_use_regexp) {
> +            next unless ($filename =~ /$searchtext/);
> +        } else {
> +            next unless index($filename,$searchtext) >= 0;
> +        }
> +
> +        $matches++;
> +        $file_href = href(action=>"blob", hash_base=>$co{'id'},
> +                          file_name=>$filename);
> +
> + if ($alternate) {
> +    print "<tr class=\"dark\">\n";
> + } else {
> +    print "<tr class=\"light\">\n";
> + }
> + $alternate ^= 1;
> +
> +        print "<td class=\"list\">".
> +            $cgi->a({-href => $file_href, -class => "list"},
> esc_path($filename));
> + print "</td>\n";
> + print "</tr>\n";
> +    }
> +
> +    if ($matches == 0) {
> +        print "<div class=\"diff nodifferences\">No matches found</div>\n";
> +    }
> +    close $fd;
> +
> +    print "</table>\n";
> +
> +    git_footer_html();
> +}
> +
>  ## ======================================================================
>  ## ======================================================================
>  ## actions

```
