blob: e1e55ce141cd49697f09cbf90ff9c320ed6a95bb [file] [log] [blame]
Dave Borowitz14ce8282012-12-20 14:08:25 -08001// 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
15package com.google.gitiles;
16
17import com.google.common.base.Throwables;
18import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
Dave Borowitz3b744b12016-08-19 16:11:10 -040020import java.io.IOException;
Dave Borowitz3b744b12016-08-19 16:11:10 -040021import java.util.concurrent.ExecutionException;
Dave Borowitz14ce8282012-12-20 14:08:25 -080022import org.eclipse.jgit.lib.Constants;
23import org.eclipse.jgit.lib.ObjectId;
24import org.eclipse.jgit.lib.PersonIdent;
25import org.eclipse.jgit.revwalk.RevCommit;
26import org.eclipse.jgit.revwalk.RevObject;
27import org.eclipse.jgit.revwalk.RevTag;
28import org.eclipse.jgit.revwalk.RevWalk;
29
Dave Borowitz14ce8282012-12-20 14:08:25 -080030/**
31 * Cache of the time associated with Git objects.
Dave Borowitz40255d52016-08-19 16:16:22 -040032 *
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 Borowitz14ce8282012-12-20 14:08:25 -080036 */
37public class TimeCache {
Dave Borowitzd71f3762014-04-18 11:05:20 -070038 public static CacheBuilder<Object, Object> defaultBuilder() {
Dave Borowitz14ce8282012-12-20 14:08:25 -080039 return CacheBuilder.newBuilder().maximumSize(10 << 10);
40 }
41
42 private final Cache<ObjectId, Long> cache;
43
44 public TimeCache() {
Dave Borowitzd71f3762014-04-18 11:05:20 -070045 this(defaultBuilder());
Dave Borowitz14ce8282012-12-20 14:08:25 -080046 }
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 Nienhuysc0200f62016-05-02 17:34:51 +020058 return cache.get(
59 id,
Dave Borowitz2b159702016-10-04 10:06:58 -040060 () -> {
61 RevObject o = walk.parseAny(id);
62 while (o instanceof RevTag) {
Jonathan Nieder9e3b1b72019-03-07 14:38:28 -080063 walk.parseBody(o);
64
Dave Borowitz2b159702016-10-04 10:06:58 -040065 RevTag tag = (RevTag) o;
66 PersonIdent ident = tag.getTaggerIdent();
67 if (ident != null) {
68 return ident.getWhen().getTime() / 1000;
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020069 }
Dave Borowitz2b159702016-10-04 10:06:58 -040070 o = tag.getObject();
71 walk.parseHeaders(o);
Dave Borowitz14ce8282012-12-20 14:08:25 -080072 }
Dave Borowitz2b159702016-10-04 10:06:58 -040073 if (o.getType() == Constants.OBJ_COMMIT) {
74 return Long.valueOf(((RevCommit) o).getCommitTime());
75 }
76 return Long.MIN_VALUE;
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020077 });
Dave Borowitz14ce8282012-12-20 14:08:25 -080078 } catch (ExecutionException e) {
David Pursehouse33b67e92017-03-13 21:19:21 +090079 Throwables.throwIfInstanceOf(e.getCause(), IOException.class);
Dave Borowitz14ce8282012-12-20 14:08:25 -080080 throw new IOException(e);
81 }
82 }
83}