Merge branch 'stable-2.12' into stable-2.13

* stable-2.12:
  Fix standalone Eclipse project generation
  Calculate repository disk usage by adding size of git objects
  Remove source_under_test to comply with latest buck version

Change-Id: I07c7c515d05f4c6c4a9df2b5ba5af937b659e602
diff --git a/BUCK b/BUCK
index 289b610..4d527fb 100644
--- a/BUCK
+++ b/BUCK
@@ -13,23 +13,18 @@
   ]
 )
 
-# this is required for bucklets/tools/eclipse/project.py to work
-# not sure, if this does something useful in standalone context
+TEST_DEPS = GERRIT_PLUGIN_API + GERRIT_TESTS + [
+  ':quota__plugin'
+]
+
 java_library(
   name = 'classpath',
-  deps = [':quota__plugin'],
+  deps = TEST_DEPS,
 )
 
 java_test(
   name = 'quota_tests',
   srcs = glob(['src/test/java/**/*.java']),
   labels = ['quota'],
-  deps = GERRIT_PLUGIN_API + [
-    ':quota__plugin',
-    '//lib:junit',
-    '//lib/easymock:easymock',
-    '//lib/log:log4j',
-    '//lib/log:impl_log4j',
-  ],
-  source_under_test = [':quota__plugin'],
+  deps = TEST_DEPS,
 )
diff --git a/lib/BUCK b/lib/BUCK
deleted file mode 100644
index 6959cde..0000000
--- a/lib/BUCK
+++ /dev/null
@@ -1,19 +0,0 @@
-include_defs('//bucklets/maven_jar.bucklet')
-
-maven_jar(
-  name = 'junit',
-  id = 'junit:junit:4.11',
-  sha1 = '4e031bb61df09069aeb2bffb4019e7a5034a4ee0',
-  attach_source = False,
-  license = 'DO_NOT_DISTRIBUTE',
-  deps = [':hamcrest-core'],
-)
-
-maven_jar(
-  name = 'hamcrest-core',
-  id = 'org.hamcrest:hamcrest-core:1.3',
-  sha1 = '42a25dc3219429f0e5d060061f71acb49bf010a0',
-  attach_source = False,
-  license = 'DO_NOT_DISTRIBUTE',
-  visibility = ['//lib:junit'],
-)
diff --git a/lib/easymock/BUCK b/lib/easymock/BUCK
deleted file mode 100644
index 0a4eb4d..0000000
--- a/lib/easymock/BUCK
+++ /dev/null
@@ -1,31 +0,0 @@
-include_defs('//bucklets/maven_jar.bucklet')
-
-maven_jar(
-  name = 'easymock',
-  id = 'org.easymock:easymock:3.4',
-  sha1 = '9fdeea183a399f25c2469497612cad131e920fa3',
-  license = 'DO_NOT_DISTRIBUTE',
-  attach_source = False,
-  deps = [
-    ':cglib-2_2',
-    ':objenesis',
-  ],
-)
-
-maven_jar(
-  name = 'cglib-2_2',
-  id = 'cglib:cglib-nodep:2.2.2',
-  sha1 = '00d456bb230c70c0b95c76fb28e429d42f275941',
-  license = 'DO_NOT_DISTRIBUTE',
-  attach_source = False,
-)
-
-maven_jar(
-  name = 'objenesis',
-  id = 'org.objenesis:objenesis:1.2',
-  sha1 = 'bfcb0539a071a4c5a30690388903ac48c0667f2a',
-  license = 'DO_NOT_DISTRIBUTE',
-  visibility = ['//lib/powermock:powermock-reflect'],
-  attach_source = False,
-)
-
diff --git a/lib/gerrit/BUCK b/lib/gerrit/BUCK
index fefa172..4b48569 100644
--- a/lib/gerrit/BUCK
+++ b/lib/gerrit/BUCK
@@ -12,3 +12,11 @@
   license = 'Apache2.0',
 )
 
+maven_jar(
+  name = 'acceptance-framework',
+  id = 'com.google.gerrit:gerrit-acceptance-framework:' + VER,
+  sha1 ='76eceefa7e31c6945513c36c4d106066d1df3e63',
+  license = 'Apache2.0',
+  attach_source = False,
+  repository = REPO,
+)
diff --git a/lib/log/BUCK b/lib/log/BUCK
deleted file mode 100644
index be5cac5..0000000
--- a/lib/log/BUCK
+++ /dev/null
@@ -1,21 +0,0 @@
-include_defs('//bucklets/maven_jar.bucklet')
-
-VER = '1.7.7'
-
-maven_jar(
-  name = 'impl_log4j',
-  id = 'org.slf4j:slf4j-log4j12:' + VER,
-  sha1 = '58f588119ffd1702c77ccab6acb54bfb41bed8bd',
-  license = 'slf4j',
-  deps = [':log4j'],
-  attach_source = False,
-)
-
-maven_jar(
-  name = 'log4j',
-  id = 'log4j:log4j:1.2.17',
-  sha1 = '5af35056b4d257e4b64b9e8069c0746e8b08629f',
-  license = 'Apache2.0',
-  exclude = ['META-INF/LICENSE', 'META-INF/NOTICE'],
-  attach_source = False,
-)
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuota.java b/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuota.java
index e9a1973..4388cd9 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuota.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuota.java
@@ -17,8 +17,10 @@
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
 import com.google.common.collect.Ordering;
+import com.google.gerrit.extensions.annotations.PluginName;
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.gerrit.server.cache.CacheModule;
+import com.google.gerrit.server.config.PluginConfigFactory;
 import com.google.gerrit.server.git.GitRepositoryManager;
 import com.google.gerrit.server.git.ReceivePackInitializer;
 import com.google.gerrit.server.project.ProjectCache;
@@ -28,6 +30,9 @@
 import com.google.inject.name.Named;
 
 import org.apache.commons.lang.mutable.MutableLong;
+import org.eclipse.jgit.internal.storage.file.FileRepository;
+import org.eclipse.jgit.internal.storage.file.GC;
+import org.eclipse.jgit.internal.storage.file.GC.RepoStatistics;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.transport.PostReceiveHook;
 import org.eclipse.jgit.transport.ReceiveCommand;
@@ -145,15 +150,23 @@
   static class Loader extends CacheLoader<Project.NameKey, AtomicLong> {
 
     private final GitRepositoryManager gitManager;
+    private final boolean useGitObjectCount;
 
     @Inject
-    Loader(GitRepositoryManager gitManager) {
+    Loader(GitRepositoryManager gitManager,
+        PluginConfigFactory cfg,
+        @PluginName String pluginName) {
       this.gitManager = gitManager;
+      this.useGitObjectCount = cfg.getFromGerritConfig(pluginName)
+          .getBoolean("useGitObjectCount", false);
     }
 
     @Override
     public AtomicLong load(Project.NameKey project) throws IOException {
-      try (Repository git = gitManager.openRepository(project)){
+      try (Repository git = gitManager.openRepository(project)) {
+        if (useGitObjectCount) {
+          return new AtomicLong(getDiskUsageByGitObjectCount(git));
+        }
         return new AtomicLong(getDiskUsage(git.getDirectory()));
       }
     }
@@ -172,6 +185,12 @@
       });
       return size.longValue();
     }
+
+    private long getDiskUsageByGitObjectCount(Repository repo)
+        throws IOException {
+      RepoStatistics stats = new GC((FileRepository) repo).getStatistics();
+      return stats.sizeOfLooseObjects + stats.sizeOfPackedObjects;
+    }
   }
 
   @Override
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
index 7dd1927..e22de10 100644
--- a/src/main/resources/Documentation/build.md
+++ b/src/main/resources/Documentation/build.md
@@ -55,6 +55,12 @@
   buck test
 ```
 
+This project can be imported into the Eclipse IDE:
+
+```
+  ./bucklets/tools/eclipse.py
+```
+
 ### Build in Gerrit tree
 
 Clone or link this plugin to the plugins directory of Gerrit's source
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index ea254a7..31c32b9 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -119,6 +119,20 @@
     maxTotalSize = 20 m
 ```
 
+If one prefers computing a repository size by adding the size of the git objects,
+the following section should be added into the `gerrit.config` file:
+
+```
+  [plugin "quota"]
+        useGitObjectCount = true
+```
+
+<a id="useGitObjectCount">
+`plugin.quota.useGitObjectCount`
+: Use git object count. If true, *repoSize = looseObjectsSize +
+packedObjectsSize*, where *looseObjectsSize* and *packedObjectsSize* are given
+by JGit RepoStatistics. By default, false.
+
 Publication Schedule
 --------------------