| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 1 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.gitiles; |
| 16 | |
| 17 | import com.google.common.base.Throwables; |
| 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 20 | import java.io.IOException; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 21 | import java.util.concurrent.ExecutionException; |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 22 | import org.eclipse.jgit.lib.Constants; |
| 23 | import org.eclipse.jgit.lib.ObjectId; |
| 24 | import org.eclipse.jgit.lib.PersonIdent; |
| 25 | import org.eclipse.jgit.revwalk.RevCommit; |
| 26 | import org.eclipse.jgit.revwalk.RevObject; |
| 27 | import org.eclipse.jgit.revwalk.RevTag; |
| 28 | import org.eclipse.jgit.revwalk.RevWalk; |
| 29 | |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 30 | /** |
| 31 | * Cache of the time associated with Git objects. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 32 | * |
| 33 | * <p>Uses the time as stored in annotated tags if available, or else the commit time of the tagged |
| 34 | * commit. Non-commits are given {@link Long#MIN_VALUE}, rather than searching for occurrences in |
| 35 | * the entire repository. |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 36 | */ |
| 37 | public class TimeCache { |
| Dave Borowitz | d71f376 | 2014-04-18 11:05:20 -0700 | [diff] [blame] | 38 | public static CacheBuilder<Object, Object> defaultBuilder() { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 39 | return CacheBuilder.newBuilder().maximumSize(10 << 10); |
| 40 | } |
| 41 | |
| 42 | private final Cache<ObjectId, Long> cache; |
| 43 | |
| 44 | public TimeCache() { |
| Dave Borowitz | d71f376 | 2014-04-18 11:05:20 -0700 | [diff] [blame] | 45 | this(defaultBuilder()); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | public TimeCache(CacheBuilder<Object, Object> builder) { |
| 49 | this.cache = builder.build(); |
| 50 | } |
| 51 | |
| 52 | public Cache<?, ?> getCache() { |
| 53 | return cache; |
| 54 | } |
| 55 | |
| 56 | Long getTime(final RevWalk walk, final ObjectId id) throws IOException { |
| 57 | try { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 58 | return cache.get( |
| 59 | id, |
| Dave Borowitz | 2b15970 | 2016-10-04 10:06:58 -0400 | [diff] [blame] | 60 | () -> { |
| 61 | RevObject o = walk.parseAny(id); |
| 62 | while (o instanceof RevTag) { |
| Jonathan Nieder | 9e3b1b7 | 2019-03-07 14:38:28 -0800 | [diff] [blame] | 63 | walk.parseBody(o); |
| 64 | |
| Dave Borowitz | 2b15970 | 2016-10-04 10:06:58 -0400 | [diff] [blame] | 65 | RevTag tag = (RevTag) o; |
| 66 | PersonIdent ident = tag.getTaggerIdent(); |
| 67 | if (ident != null) { |
| 68 | return ident.getWhen().getTime() / 1000; |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 69 | } |
| Dave Borowitz | 2b15970 | 2016-10-04 10:06:58 -0400 | [diff] [blame] | 70 | o = tag.getObject(); |
| 71 | walk.parseHeaders(o); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 72 | } |
| Dave Borowitz | 2b15970 | 2016-10-04 10:06:58 -0400 | [diff] [blame] | 73 | if (o.getType() == Constants.OBJ_COMMIT) { |
| 74 | return Long.valueOf(((RevCommit) o).getCommitTime()); |
| 75 | } |
| 76 | return Long.MIN_VALUE; |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 77 | }); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 78 | } catch (ExecutionException e) { |
| David Pursehouse | 33b67e9 | 2017-03-13 21:19:21 +0900 | [diff] [blame] | 79 | Throwables.throwIfInstanceOf(e.getCause(), IOException.class); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 80 | throw new IOException(e); |
| 81 | } |
| 82 | } |
| 83 | } |