Add global configuration for LFS file locks storage

Introduce global plugin (lfs.config) configuration parameter for LFS
locks directory storage:
  [locks]
    directory = [directory]

By default it points to $GERRIT_SITE/data/lfs/lfs_locks (existing
behavior is preserved).

Change-Id: I47b6b17f9e4008a77c2d44c62390cdfebb70f328
Signed-off-by: Jacek Centkowski <jcentkowski@collab.net>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksPathProvider.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksPathProvider.java
new file mode 100644
index 0000000..be9c037
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksPathProvider.java
@@ -0,0 +1,42 @@
+// Copyright (C) 2017 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.lfs.locks;
+
+import com.google.common.base.MoreObjects;
+import com.google.gerrit.extensions.annotations.PluginData;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+import com.googlesource.gerrit.plugins.lfs.LfsConfigurationFactory;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+@Singleton
+class LfsLocksPathProvider implements Provider<String> {
+  private final String path;
+
+  @Inject
+  LfsLocksPathProvider(LfsConfigurationFactory configFactory, @PluginData Path defaultDataDir) {
+    String dataDir = configFactory.getGlobalConfig().getString("locks", null, "directory");
+    this.path =
+        MoreObjects.firstNonNull(
+            dataDir, Paths.get(defaultDataDir.toString(), "lfs_locks").toString());
+  }
+
+  @Override
+  public String get() {
+    return path;
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java
index 934ee00..346bdad 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java
@@ -16,7 +16,6 @@
 
 import com.google.common.cache.Cache;
 import com.google.common.cache.CacheBuilder;
-import com.google.gerrit.extensions.annotations.PluginData;
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.gerrit.server.CurrentUser;
 import com.google.gson.FieldNamingPolicy;
@@ -62,10 +61,10 @@
 
   @Inject
   LfsProjectLocks(
-      PathToLockId toLockId, @PluginData Path defaultDataDir, @Assisted Project.NameKey project) {
+      PathToLockId toLockId, LfsLocksPathProvider locksPath, @Assisted Project.NameKey project) {
     this.toLockId = toLockId;
     this.project = project.get();
-    this.locksPath = Paths.get(defaultDataDir.toString(), "lfs_locks", this.project);
+    this.locksPath = Paths.get(locksPath.get(), this.project);
     this.locks = CacheBuilder.newBuilder().build();
   }
 
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 53bad5a..d692dd5 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -93,6 +93,16 @@
 The following options can be configured in `$GERRIT_SITE/etc/@PLUGIN@.config`
 and `$GERRIT_SITE/etc/@PLUGIN@.secure.config.`
 
+### Section `locks`
+
+locks.directory
+: The directory in which to store Git LFS file locks.
+[Git LFS File Locking API](https://github.com/git-lfs/git-lfs/blob/master/docs/api/locking.md)
+specifies that certain path can be locked by user. It prevents from accidental file overwrite
+by different user and costly (manual in most cases) binary file merge. Gerrit uses file based
+locks that are stored under `directory`.
+: Default is `$GERRIT_SITE/data/@PLUGIN@/lfs_locks`.
+
 ### Section `auth`
 
 auth.sshExpirationSeconds