TreeSoyData: link tree pages to content search

Add a search link to repository tree pages. The link opens the +grep
HTML view at the current revision and preserves the current directory,
so searches from a subtree remain limited to that path.

Issue: 376381593
Change-Id: Ie1015b890b34edf23cf938651ca1edbe7e17cb72
Signed-off-by: Devansh Varshney <devansh@libreoffice.org>
diff --git a/java/com/google/gitiles/TreeSoyData.java b/java/com/google/gitiles/TreeSoyData.java
index 4e2a579..3544735 100644
--- a/java/com/google/gitiles/TreeSoyData.java
+++ b/java/com/google/gitiles/TreeSoyData.java
@@ -165,6 +165,7 @@
     if (view.getType() == GitilesView.Type.PATH
         && view.getRevision().getPeeledType() == OBJ_COMMIT) {
       data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
+      data.put("grepUrl", GitilesView.grep().copyFrom(view).toUrl());
       data.put(
           "archiveUrl",
           GitilesView.archive()
diff --git a/javatests/com/google/gitiles/PathServletTest.java b/javatests/com/google/gitiles/PathServletTest.java
index 18f8310..70124f1 100644
--- a/javatests/com/google/gitiles/PathServletTest.java
+++ b/javatests/com/google/gitiles/PathServletTest.java
@@ -54,6 +54,28 @@
   }
 
   @Test
+  public void rootTreeHtmlHasGrepLink() throws Exception {
+    repo.branch("master").commit().add("foo", "contents").create();
+
+    FakeHttpServletResponse res =
+        buildResponse("/repo/+/master/", "format=html", SC_OK);
+
+    assertThat(res.getActualBodyString())
+        .contains("href=\"/b/repo/+grep/master/\"");
+  }
+
+  @Test
+  public void subTreeHtmlHasPathScopedGrepLink() throws Exception {
+    repo.branch("master").commit().add("src/a.txt", "contents").create();
+
+    FakeHttpServletResponse res =
+        buildResponse("/repo/+/master/src", "format=html", SC_OK);
+
+    assertThat(res.getActualBodyString())
+        .contains("href=\"/b/repo/+grep/master/src\"");
+  }
+
+  @Test
   public void subTreeHtml() throws Exception {
     repo.branch("master")
         .commit()
diff --git a/resources/com/google/gitiles/templates/ObjectDetail.soy b/resources/com/google/gitiles/templates/ObjectDetail.soy
index 4fc83d8..41399ac 100644
--- a/resources/com/google/gitiles/templates/ObjectDetail.soy
+++ b/resources/com/google/gitiles/templates/ObjectDetail.soy
@@ -179,6 +179,7 @@
 {template treeDetail}
   {@param sha: ?}  /** SHA of this path's tree. */
   {@param? logUrl: ?}  /** optional URL to a log for this path. */
+  {@param? grepUrl: ?}  /** optional URL to search file contents under this path. */
   {@param? archiveUrl: ?}  /** optional URL to a download link of this tree as an archive. */
   {@param? archiveType: ?}  /** type of the archive to download, if archiveUrl is set. */
   {@param entries: ?}  /** list of entries with the following keys:
@@ -194,6 +195,7 @@
   <div class="u-sha1 u-monospace TreeDetail-sha1">
     {msg desc="SHA-1 for the path's tree"}tree: {$sha}{/msg}
     {if $logUrl}{sp}[<a href="{$logUrl}">{msg desc="history for a path"}path history{/msg}</a>]{/if}
+    {if $grepUrl}{sp}[<a href="{$grepUrl}">{msg desc="search file contents under a path"}search{/msg}</a>]{/if}
     {if $archiveUrl}
       {sp}<span>[<a href="{$archiveUrl}">{$archiveType}</a>]</span>
     {/if}