Merge branch 'stable-2.11' into stable-2.12

* stable-2.11:
  Organize imports
  Change gitiles:gitiles target visibility to public

Change-Id: Id485f7edb255bf0cc426c44f24572196d6c4c181
diff --git a/.buckconfig b/.buckconfig
new file mode 100644
index 0000000..b606c45
--- /dev/null
+++ b/.buckconfig
@@ -0,0 +1,14 @@
+[alias]
+  gitiles = //:gitiles
+  plugin = //:gitiles
+
+[java]
+  src_roots = java, resources
+
+[project]
+  ignore = .git
+
+[cache]
+  mode = dir
+  dir = buck-out/cache
+
diff --git a/.gitignore b/.gitignore
index 80d6257..e034f3b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,10 @@
+/bucklets
 /target
+/.buckversion
 /.classpath
 /.project
 /.settings/org.maven.ide.eclipse.prefs
 /.settings/org.eclipse.m2e.core.prefs
+/.buckd
+/.watchmanconfig
+/buck-out
diff --git a/BUCK b/BUCK
index c5af8bd..a26293c 100644
--- a/BUCK
+++ b/BUCK
@@ -1,9 +1,10 @@
-include_defs('//lib/maven.defs')
+include_defs('//bucklets/gerrit_plugin.bucklet')
+include_defs('//bucklets/maven_jar.bucklet')
 
 genrule(
   name = 'gitiles',
   cmd = ' && '.join([
-    'cp $(location :gitiles_base) $OUT',
+    'cp $(location :gitiles__base) $OUT',
     'unzip -qd $TMP $(location :gitiles-servlet) "com/google/gitiles/static/*"',
     'cd $TMP/com/google/gitiles',
     'mv static +static',
@@ -11,14 +12,10 @@
   ]),
   visibility = ['PUBLIC'],
   out = 'gitiles.jar',
-  deps = [
-    ':gitiles-servlet',
-    ':gitiles_base',
-  ],
 )
 
 gerrit_plugin(
-  name = 'gitiles_base',
+  name = 'gitiles',
   srcs = glob(['src/main/java/**/*.java']),
   deps = [
     ':gitiles-servlet',
@@ -38,7 +35,6 @@
     'Gerrit-PluginName: gitiles',
     'Gerrit-Module: com.googlesource.gerrit.plugins.gitiles.Module',
     'Gerrit-HttpModule: com.googlesource.gerrit.plugins.gitiles.HttpModule',
-    'Gerrit-InitStep: com.googlesource.gerrit.plugins.gitiles.InitGitiles',
 
     # Gitiles uses /repo to access a repo, so the default plugin layout would
     # disallow repos named "static" or "Documentation". Paths starting with +
@@ -47,12 +43,18 @@
     'Gerrit-HttpDocumentationPrefix: +Documentation',
   ],
   visibility = [],
+  target_suffix = '__base'
+)
+
+java_library(
+  name = 'classpath',
+  deps = [':gitiles__plugin'],
 )
 
 maven_jar(
   name = 'gitiles-servlet',
-  id = 'com.google.gitiles:gitiles-servlet:0.1-6',
-  sha1 = '233fbe1315205861ac1adc3d77a3d5037f97fbaa',
+  id = 'com.google.gitiles:gitiles-servlet:0.1-7',
+  sha1 = '5166c0adbf7c63e28d894dcda65cea051e07043f',
   deps = [':prettify'],
   license = 'Apache2.0',
   repository = GERRIT,
@@ -77,14 +79,13 @@
   visibility = [],
 )
 
-# soy must match closure-template version used in Gitiles
+# soy version must match version used in Gitiles
 maven_jar(
   name = 'soy',
-  id = 'com.google.template:soy:e74fcfa284a1e31d42ac93e53cb84a71f638c70b',
-  sha1 = '1c75a007218f29d6124c46c8b18f4158cba4839c',
+  id = 'com.google.template:soy:2015-04-10',
+  sha1 = 'f2a260c0eafbc5756ccec546efd2ffd5b0a583cb',
   deps = [':icu4j'],
   license = 'Apache2.0',
-  repository = GERRIT,
   visibility = [],
 )
 
diff --git a/lib/gerrit/BUCK b/lib/gerrit/BUCK
new file mode 100644
index 0000000..88ee169
--- /dev/null
+++ b/lib/gerrit/BUCK
@@ -0,0 +1,13 @@
+include_defs('//bucklets/maven_jar.bucklet')
+
+VER = '2.12'
+REPO = MAVEN_CENTRAL
+
+maven_jar(
+  name = 'plugin-api',
+  id = 'com.google.gerrit:gerrit-plugin-api:' + VER,
+  sha1 = '8ce1f6e65078bbcf03a1758f96b3ebca19b7fe3c',
+  attach_source = False,
+  repository = REPO,
+  license = 'Apache2.0',
+)
diff --git a/lib/jgit/BUCK b/lib/jgit/BUCK
new file mode 100644
index 0000000..678c0a3
--- /dev/null
+++ b/lib/jgit/BUCK
@@ -0,0 +1,41 @@
+include_defs('//bucklets/maven_jar.bucklet')
+
+REPO = GERRIT  # Leave here even if set to MAVEN_CENTRAL.
+VERS = '4.0.1.201506240215-r.94-g39dc898'
+
+maven_jar(
+  name = 'jgit',
+  id = 'org.eclipse.jgit:org.eclipse.jgit:' + VERS,
+  bin_sha1 = 'e076b3f71daaadd27e9cfc810778968deed04038',
+  src_sha1 = '255a8c836c1a85da9ffc371ba3e59e63c2bc1dee',
+  license = 'jgit',
+  repository = REPO,
+  unsign = True,
+  deps = [':ewah'],
+  exclude = [
+    'META-INF/eclipse.inf',
+    'about.html',
+    'plugin.properties',
+  ],
+)
+
+maven_jar(
+  name = 'jgit-servlet',
+  id = 'org.eclipse.jgit:org.eclipse.jgit.http.server:' + VERS,
+  sha1 = '4d4346164f89593a82670780fd041be358ab76c9',
+  license = 'jgit',
+  repository = REPO,
+  deps = [':jgit'],
+  unsign = True,
+  exclude = [
+    'about.html',
+    'plugin.properties',
+  ],
+)
+
+maven_jar(
+  name = 'ewah',
+  id = 'com.googlecode.javaewah:JavaEWAH:0.7.9',
+  sha1 = 'eceaf316a8faf0e794296ebe158ae110c7d72a5a',
+  license = 'Apache2.0',
+)
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gitiles/GitilesWeblinks.java b/src/main/java/com/googlesource/gerrit/plugins/gitiles/GitilesWeblinks.java
new file mode 100644
index 0000000..0b9dd20
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/gitiles/GitilesWeblinks.java
@@ -0,0 +1,68 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.gitiles;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.common.WebLinkInfo;
+import com.google.gerrit.extensions.webui.BranchWebLink;
+import com.google.gerrit.extensions.webui.FileHistoryWebLink;
+import com.google.gerrit.extensions.webui.FileWebLink;
+import com.google.gerrit.extensions.webui.PatchSetWebLink;
+import com.google.gerrit.extensions.webui.ProjectWebLink;
+
+import com.google.inject.Inject;
+
+public class GitilesWeblinks implements BranchWebLink, FileWebLink,
+    PatchSetWebLink, ProjectWebLink, FileHistoryWebLink {
+  private final String name;
+  private final String baseUrl;
+
+  @Inject
+  public GitilesWeblinks(@PluginName String pluginName) {
+    name = pluginName;
+    baseUrl = "plugins/" + pluginName;
+  }
+
+  @Override
+  public WebLinkInfo getProjectWeblink(String projectName) {
+    return new WebLinkInfo(name, null, String.format("%s/%s", baseUrl,
+        projectName), Target.BLANK);
+  }
+
+  @Override
+  public WebLinkInfo getPatchSetWebLink(String projectName, String commit) {
+    return new WebLinkInfo(name, null, String.format("%s/%s/+/%s", baseUrl,
+        projectName, commit), Target.BLANK);
+  }
+
+  @Override
+  public WebLinkInfo getFileWebLink(String projectName, String revision,
+      String fileName) {
+    return new WebLinkInfo(name, null, String.format("%s/%s/+/%s/%s", baseUrl,
+        projectName, revision, fileName), Target.BLANK);
+  }
+
+  @Override
+  public WebLinkInfo getBranchWebLink(String projectName, String branchName) {
+    return new WebLinkInfo(name, null, String.format("%s/%s/+/%s", baseUrl,
+        projectName, branchName), Target.BLANK);
+  }
+
+  @Override
+  public WebLinkInfo getFileHistoryWebLink(String projectName, String revision, String fileName) {
+    return new WebLinkInfo(name, null, String.format("%s/%s/+log/%s/%s", baseUrl,
+        projectName, revision, fileName), Target.BLANK);
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gitiles/HttpModule.java b/src/main/java/com/googlesource/gerrit/plugins/gitiles/HttpModule.java
index f4cbbd7..c1163cf 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gitiles/HttpModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gitiles/HttpModule.java
@@ -123,7 +123,7 @@
   @Provides
   @Named("gitiles")
   Config getConfig(SitePaths site) throws IOException, ConfigInvalidException {
-    File cfgFile = new File(site.etc_dir, "gitiles.config");
+    File cfgFile = site.etc_dir.resolve("gitiles.config").toFile();
     FileBasedConfig cfg = new FileBasedConfig(cfgFile, FS.DETECTED);
     if (cfg.getFile().exists()) {
       cfg.load();
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gitiles/InitGitiles.java b/src/main/java/com/googlesource/gerrit/plugins/gitiles/InitGitiles.java
deleted file mode 100644
index 7b79b56..0000000
--- a/src/main/java/com/googlesource/gerrit/plugins/gitiles/InitGitiles.java
+++ /dev/null
@@ -1,83 +0,0 @@
-// Copyright (C) 2013 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.googlesource.gerrit.plugins.gitiles;
-
-import com.google.gerrit.pgm.init.api.ConsoleUI;
-import com.google.gerrit.pgm.init.api.InitFlags;
-import com.google.gerrit.pgm.init.api.InitStep;
-import com.google.gerrit.pgm.init.api.Section;
-import com.google.gerrit.server.config.SitePaths;
-import com.google.inject.Inject;
-
-import org.eclipse.jgit.storage.file.FileBasedConfig;
-
-import java.io.File;
-import java.io.IOException;
-
-class InitGitiles implements InitStep {
-  private final ConsoleUI ui;
-  private final Section gitweb;
-  private final FileBasedConfig cfg;
-  private final File gitilesConfig;
-
-  @Inject
-  InitGitiles(InitFlags flags, ConsoleUI ui, Section.Factory sections,
-      SitePaths sitePaths) {
-    this.ui = ui;
-    this.gitweb = sections.get("gitweb", null);
-    this.cfg = flags.cfg;
-    this.gitilesConfig = new File(sitePaths.etc_dir, "gitiles.config");
-  }
-
-  @Override
-  public void run() throws IOException {
-    ui.header("Gitiles");
-    if (!confirm()) {
-      return;
-    }
-
-    gitweb.set("type", "custom");
-    gitweb.set("linkname", "gitiles");
-    gitweb.unset("cgi");
-    gitweb.unset("pathSeparator");
-    gitweb.set("url", "plugins/gitiles/");
-    gitweb.set("revision", "${project}/+/${commit}");
-    gitweb.set("project", "${project}");
-    gitweb.set("branch", "${project}/+/${branch}");
-    gitweb.set("filehistory", "${project}/+log/${branch}/${file}");
-    gitweb.set("file", "${project}/+/${commit}/${file}");
-    gitweb.set("roottree", "${project}/+/${commit}/");
-
-    // Configuration is mainly done in code. Create an empty config file so the
-    // user knows where to put additional configuration.
-    gitilesConfig.createNewFile();
-  }
-
-  @Override
-  public void postRun() {
-    // Do nothing.
-  }
-
-  private boolean confirm() {
-    if (!cfg.getSections().contains("gitweb")) {
-      return ui.yesno(true, "Configure Gitiles source browser");
-    }
-    if ("custom".equalsIgnoreCase(cfg.getString("gitweb", null, "type"))
-        && "gitiles".equalsIgnoreCase(cfg.getString("gitweb", null, "linkname"))) {
-      return ui.yesno(false, "Restore default Gitiles config");
-    }
-    return ui.yesno(false, "Override existing gitweb config with Gitiles");
-  }
-}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gitiles/Module.java b/src/main/java/com/googlesource/gerrit/plugins/gitiles/Module.java
index 62ae26e..82ded08 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gitiles/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gitiles/Module.java
@@ -16,6 +16,12 @@
 
 import com.google.gerrit.common.Nullable;
 import com.google.gerrit.extensions.events.LifecycleListener;
+import com.google.gerrit.extensions.registration.DynamicSet;
+import com.google.gerrit.extensions.webui.BranchWebLink;
+import com.google.gerrit.extensions.webui.FileHistoryWebLink;
+import com.google.gerrit.extensions.webui.FileWebLink;
+import com.google.gerrit.extensions.webui.PatchSetWebLink;
+import com.google.gerrit.extensions.webui.ProjectWebLink;
 import com.google.gerrit.lifecycle.LifecycleModule;
 import com.google.gerrit.server.config.CanonicalWebUrl;
 import com.google.gerrit.server.config.GerritServerConfig;
@@ -43,6 +49,11 @@
 class Module extends LifecycleModule {
   @Override
   protected void configure() {
+    DynamicSet.bind(binder(), BranchWebLink.class).to(GitilesWeblinks.class);
+    DynamicSet.bind(binder(), FileHistoryWebLink.class).to(GitilesWeblinks.class);
+    DynamicSet.bind(binder(), FileWebLink.class).to(GitilesWeblinks.class);
+    DynamicSet.bind(binder(), PatchSetWebLink.class).to(GitilesWeblinks.class);
+    DynamicSet.bind(binder(), ProjectWebLink.class).to(GitilesWeblinks.class);
     bind(GitilesAccess.Factory.class).to(GerritGitilesAccess.Factory.class);
     bind(new TypeLiteral<RepositoryResolver<HttpServletRequest>>() {}).to(Resolver.class);
     listener().to(Lifecycle.class);
diff --git a/src/main/resources/+Documentation/build.md b/src/main/resources/+Documentation/build.md
new file mode 100644
index 0000000..452fc34
--- /dev/null
+++ b/src/main/resources/+Documentation/build.md
@@ -0,0 +1,74 @@
+Build
+=====
+
+This plugin can be built with Buck.
+
+Buck
+----
+
+Two build modes are supported: Standalone and in Gerrit tree.
+The standalone build mode is recommended, as this mode doesn't require
+the Gerrit tree to exist locally.
+
+
+### Build standalone
+
+Clone bucklets library:
+
+```
+  git clone https://gerrit.googlesource.com/bucklets
+
+```
+and link it to gitiles plugin directory:
+
+```
+  cd gitiles && ln -s ../bucklets .
+```
+
+Add link to the .buckversion file:
+
+```
+  cd gitiles && ln -s bucklets/buckversion .buckversion
+```
+
+Add link to the .watchmanconfig file:
+
+```
+  cd gitiles && ln -s bucklets/watchmanconfig .watchmanconfig
+```
+
+To build the plugin, issue the following command:
+
+```
+  buck build plugin
+```
+
+The output is created in
+
+```
+  buck-out/gen/gitiles.jar
+```
+
+### Build in Gerrit tree
+
+Clone or link this plugin to the plugins directory of Gerrit's source
+tree, and issue the command:
+
+```
+  buck build plugins/gitiles
+```
+
+The output is created in
+
+```
+  buck-out/gen/plugins/gitiles/gitiles.jar
+```
+
+This project can be imported into the Eclipse IDE:
+
+```
+  ./tools/eclipse/project.py
+```
+
+How to build the Gerrit Plugin API is described in the [Gerrit
+documentation](../../../Documentation/dev-buck.html#_extension_and_plugin_api_jar_files).
diff --git a/src/main/resources/+Documentation/config.md b/src/main/resources/+Documentation/config.md
index 673eae8..befac64 100644
--- a/src/main/resources/+Documentation/config.md
+++ b/src/main/resources/+Documentation/config.md
@@ -4,13 +4,16 @@
 The Gitiles plugin for Gerrit allows you to list and browse all your Gerrit
 repositories using [Gitiles](http://code.google.com/p/gitiles).
 
-Once you have installed the plugin, run `java -jar gerrit.war init -d my-site`
-to configure Gitiles as your source browser for Gerrit, replacing gitweb. The
-plugin points Gitiles back at the running Gerrit server for things like
+The plugin points Gitiles back at the running Gerrit server for things like
 Change-Id links, so you can be up and running with no additional configuration.
 If necessary, though, you can put additional Gitiles configuration in
 `etc/gitiles.config`.
 
+Gitiles now provides its own links through the weblinks extensions in Gerrit.
+If you upgrade from an older version of the Gitiles plugin, that uses the gitweb
+configuration in 'etc/gerrit.config', you will have to remove this section or
+the links will be in duplicate.
+
 Access Controls
 ---------------