Blame: extract Region to its own file Change-Id: Id7bcdf400dc0ca76bb48b9314862bdb339a8a9d8
diff --git a/gitiles-servlet/src/main/java/com/google/gitiles/blame/BlameCache.java b/gitiles-servlet/src/main/java/com/google/gitiles/blame/BlameCache.java index cf5e477..530227d 100644 --- a/gitiles-servlet/src/main/java/com/google/gitiles/blame/BlameCache.java +++ b/gitiles-servlet/src/main/java/com/google/gitiles/blame/BlameCache.java
@@ -14,73 +14,12 @@ package com.google.gitiles.blame; -import com.google.common.base.Objects; - -import org.eclipse.jgit.blame.BlameResult; import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.revwalk.RevCommit; import java.io.IOException; import java.util.List; public interface BlameCache { - public static class Region { - private final String sourcePath; - private final ObjectId sourceCommit; - private final PersonIdent sourceAuthor; - private int count; - - public Region(BlameResult blame, int start) { - this.sourcePath = blame.getSourcePath(start); - RevCommit c = blame.getSourceCommit(start); - if (c != null) { - this.sourceCommit = c.copy(); - this.sourceAuthor = c.getAuthorIdent(); - } else { - // Unknown source commit due to JGit bug. - this.sourceCommit = null; - this.sourceAuthor = null; - } - this.count = 1; - } - - public Region(String sourcePath, ObjectId sourceCommit, PersonIdent sourceAuthor, int count) { - this.sourcePath = sourcePath; - this.sourceCommit = sourceCommit; - this.sourceAuthor = sourceAuthor; - this.count = count; - } - - public int getCount() { - return count; - } - - public String getSourcePath() { - return sourcePath; - } - - public ObjectId getSourceCommit() { - return sourceCommit; - } - - public PersonIdent getSourceAuthor() { - return sourceAuthor; - } - - public boolean growFrom(BlameResult blame, int i) { - // Don't compare line numbers, so we collapse regions from the same source - // but with deleted lines into one. - if (Objects.equal(blame.getSourcePath(i), sourcePath) - && Objects.equal(blame.getSourceCommit(i), sourceCommit)) { - count++; - return true; - } else { - return false; - } - } - } - public List<Region> get(Repository repo, ObjectId commitId, String path) throws IOException; }
diff --git a/gitiles-servlet/src/main/java/com/google/gitiles/blame/BlameCacheImpl.java b/gitiles-servlet/src/main/java/com/google/gitiles/blame/BlameCacheImpl.java index ea10d73..0f0f662 100644 --- a/gitiles-servlet/src/main/java/com/google/gitiles/blame/BlameCacheImpl.java +++ b/gitiles-servlet/src/main/java/com/google/gitiles/blame/BlameCacheImpl.java
@@ -35,9 +35,9 @@ /** Guava implementation of BlameCache, weighted by number of blame regions. */ public class BlameCacheImpl implements BlameCache { public static CacheBuilder<Key, List<Region>> newBuilder() { - return CacheBuilder.newBuilder().weigher(new Weigher<Key, List<BlameCache.Region>>() { + return CacheBuilder.newBuilder().weigher(new Weigher<Key, List<Region>>() { @Override - public int weigh(Key key, List<BlameCache.Region> value) { + public int weigh(Key key, List<Region> value) { return value.size(); } }).maximumWeight(10 << 10); @@ -89,16 +89,16 @@ } public BlameCacheImpl(CacheBuilder<Key, List<Region>> builder) { - this.cache = builder.build(new CacheLoader<Key, List<BlameCache.Region>>() { + this.cache = builder.build(new CacheLoader<Key, List<Region>>() { @Override - public List<BlameCache.Region> load(Key key) throws IOException { + public List<Region> load(Key key) throws IOException { return loadBlame(key); } }); } @Override - public List<BlameCache.Region> get(Repository repo, ObjectId commitId, String path) + public List<Region> get(Repository repo, ObjectId commitId, String path) throws IOException { try { return cache.get(new Key(repo, commitId, path)); @@ -107,7 +107,7 @@ } } - private List<BlameCache.Region> loadBlame(Key key) throws IOException { + private List<Region> loadBlame(Key key) throws IOException { try { BlameGenerator gen = new BlameGenerator(key.repo, key.path); BlameResult blame; @@ -123,10 +123,10 @@ int lineCount = blame.getResultContents().size(); blame.discardResultContents(); - List<BlameCache.Region> regions = Lists.newArrayList(); + List<Region> regions = Lists.newArrayList(); for (int i = 0; i < lineCount; i++) { if (regions.isEmpty() || !regions.get(regions.size() - 1).growFrom(blame, i)) { - regions.add(new BlameCache.Region(blame, i)); + regions.add(new Region(blame, i)); } } return Collections.unmodifiableList(regions);
diff --git a/gitiles-servlet/src/main/java/com/google/gitiles/blame/BlameServlet.java b/gitiles-servlet/src/main/java/com/google/gitiles/blame/BlameServlet.java index b50b2fb..3d88ffc 100644 --- a/gitiles-servlet/src/main/java/com/google/gitiles/blame/BlameServlet.java +++ b/gitiles-servlet/src/main/java/com/google/gitiles/blame/BlameServlet.java
@@ -77,7 +77,7 @@ String title = "Blame - " + view.getPathPart(); Map<String, ?> blobData = new BlobSoyData(rw, view).toSoyData(view.getPathPart(), blobId); if (blobData.get("data") != null) { - List<BlameCache.Region> regions = cache.get(repo, commit, view.getPathPart()); + List<Region> regions = cache.get(repo, commit, view.getPathPart()); if (regions.isEmpty()) { res.setStatus(SC_NOT_FOUND); return; @@ -116,10 +116,10 @@ } private static List<Map<String, ?>> toSoyData(GitilesView view, ObjectReader reader, - List<BlameCache.Region> regions, GitDateFormatter df) throws IOException { + List<Region> regions, GitDateFormatter df) throws IOException { Map<ObjectId, String> abbrevShas = Maps.newHashMap(); List<Map<String, ?>> result = Lists.newArrayListWithCapacity(regions.size()); - for (BlameCache.Region r : regions) { + for (Region r : regions) { if (r.getSourceCommit() == null) { // JGit bug may fail to blame some regions. We should fix this // upstream, but handle it for now.
diff --git a/gitiles-servlet/src/main/java/com/google/gitiles/blame/Region.java b/gitiles-servlet/src/main/java/com/google/gitiles/blame/Region.java new file mode 100644 index 0000000..8b3fd41 --- /dev/null +++ b/gitiles-servlet/src/main/java/com/google/gitiles/blame/Region.java
@@ -0,0 +1,79 @@ +// Copyright (C) 2014 Google Inc. All Rights Reserved. +// +// 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.google.gitiles.blame; + +import com.google.common.base.Objects; + +import org.eclipse.jgit.blame.BlameResult; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.PersonIdent; +import org.eclipse.jgit.revwalk.RevCommit; + +/** Region of the blame of a file. */ +public class Region { + private final String sourcePath; + private final ObjectId sourceCommit; + private final PersonIdent sourceAuthor; + private int count; + + public Region(BlameResult blame, int start) { + this.sourcePath = blame.getSourcePath(start); + RevCommit c = blame.getSourceCommit(start); + if (c != null) { + this.sourceCommit = c.copy(); + this.sourceAuthor = c.getAuthorIdent(); + } else { + // Unknown source commit due to JGit bug. + this.sourceCommit = null; + this.sourceAuthor = null; + } + this.count = 1; + } + + public Region(String sourcePath, ObjectId sourceCommit, PersonIdent sourceAuthor, int count) { + this.sourcePath = sourcePath; + this.sourceCommit = sourceCommit; + this.sourceAuthor = sourceAuthor; + this.count = count; + } + + public int getCount() { + return count; + } + + public String getSourcePath() { + return sourcePath; + } + + public ObjectId getSourceCommit() { + return sourceCommit; + } + + public PersonIdent getSourceAuthor() { + return sourceAuthor; + } + + public boolean growFrom(BlameResult blame, int i) { + // Don't compare line numbers, so we collapse regions from the same source + // but with deleted lines into one. + if (Objects.equal(blame.getSourcePath(i), sourcePath) + && Objects.equal(blame.getSourceCommit(i), sourceCommit)) { + count++; + return true; + } else { + return false; + } + } +}