Add archive download link to tree page

Change-Id: I8ddd2ad5116ac736e52a7eb9ba0d6d99da643078
diff --git a/gitiles-servlet/src/main/java/com/google/gitiles/PathServlet.java b/gitiles-servlet/src/main/java/com/google/gitiles/PathServlet.java
index da212aa..0dbf793 100644
--- a/gitiles-servlet/src/main/java/com/google/gitiles/PathServlet.java
+++ b/gitiles-servlet/src/main/java/com/google/gitiles/PathServlet.java
@@ -293,7 +293,9 @@
         "title", !view.getPathPart().isEmpty() ? view.getPathPart() : "/",
         "breadcrumbs", view.getBreadcrumbs(hasSingleTree),
         "type", FileType.TREE.toString(),
-        "data", new TreeSoyData(rw, view).toSoyData(id, tw)));
+        "data", new TreeSoyData(rw, view)
+            .setArchiveFormat(archiveFormat)
+            .toSoyData(id, tw)));
   }
 
   private CanonicalTreeParser getOnlyChildSubtree(RevWalk rw, ObjectId id, byte[] prefix)
diff --git a/gitiles-servlet/src/main/java/com/google/gitiles/TreeSoyData.java b/gitiles-servlet/src/main/java/com/google/gitiles/TreeSoyData.java
index 6c7ec06..b7a9b28 100644
--- a/gitiles-servlet/src/main/java/com/google/gitiles/TreeSoyData.java
+++ b/gitiles-servlet/src/main/java/com/google/gitiles/TreeSoyData.java
@@ -70,12 +70,18 @@
 
   private final RevWalk rw;
   private final GitilesView view;
+  private ArchiveFormat archiveFormat;
 
   public TreeSoyData(RevWalk rw, GitilesView view) {
     this.rw = rw;
     this.view = view;
   }
 
+  public TreeSoyData setArchiveFormat(ArchiveFormat archiveFormat) {
+    this.archiveFormat = archiveFormat;
+    return this;
+  }
+
   public Map<String, Object> toSoyData(ObjectId treeId, TreeWalk tw) throws MissingObjectException,
          IOException {
     List<Object> entries = Lists.newArrayList();
@@ -126,6 +132,11 @@
     if (view.getType() == GitilesView.Type.PATH
         && view.getRevision().getPeeledType() == OBJ_COMMIT) {
       data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
+      data.put("archiveUrl", GitilesView.archive()
+          .copyFrom(view)
+          .setExtension(archiveFormat.getDefaultSuffix())
+          .toUrl());
+      data.put("archiveType", archiveFormat.getShortName());
     }
 
     return data;
diff --git a/gitiles-servlet/src/main/resources/com/google/gitiles/templates/ObjectDetail.soy b/gitiles-servlet/src/main/resources/com/google/gitiles/templates/ObjectDetail.soy
index 73ac298..fea0534 100644
--- a/gitiles-servlet/src/main/resources/com/google/gitiles/templates/ObjectDetail.soy
+++ b/gitiles-servlet/src/main/resources/com/google/gitiles/templates/ObjectDetail.soy
@@ -142,6 +142,8 @@
  *
  * @param sha SHA of this path's tree.
  * @param? logUrl optional URL to a log for 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:
  *     type: entry type, matching one of the constant names defined in
  *         org.eclipse.jgit.lib.FileMode.
@@ -155,6 +157,9 @@
 <div class="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 $archiveUrl}
+    <span class="download-link">[<a href="{$archiveUrl}">{$archiveType}</a>]</span>
+  {/if}
 </div>
 
 {if length($entries)}