summaryrefslogtreecommitdiffstats
path: root/mgrapp/src/com/google/codereview/manager/RepositoryCache.java
blob: bc106b2290945f700f6aefb3a8226583f4761364 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2008 Google Inc.
//
// 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.codereview.manager;

import com.google.codereview.util.GitMetaUtil;

import org.spearce.jgit.lib.Repository;

import java.io.File;
import java.io.IOException;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/** Cache of active Git repositories being used by the manager. */
public class RepositoryCache {
  private static final Pattern REPO_NAME =
      Pattern.compile("^[A-Za-z][A-Za-z0-9/_-]+$");

  private final File base;

  private final Map<String, Reference<Repository>> cache;

  /**
   * Create a new cache to manage a specific base directory (and below).
   * 
   * @param basedir top level directory that contains all repositories.
   */
  public RepositoryCache(final File basedir) {
    base = basedir;
    cache = new HashMap<String, Reference<Repository>>();
  }

  /**
   * @return the base directory which contains all known repositories.
   */
  public File getBaseDirectory() {
    return base;
  }

  /**
   * Get (or open) a repository by name.
   * 
   * @param name the repository name, relative to the base directory supplied
   *        when the cache was created.
   * @return the cached Repository instance.
   * @throws InvalidRepositoryException the name does not denote an existing
   *         repository, or the name cannot be read as a repository.
   */
  public synchronized Repository get(String name)
      throws InvalidRepositoryException {
    if (name.endsWith(".git")) {
      name = name.substring(0, name.length() - 4);
    }

    if (!REPO_NAME.matcher(name).matches()) {
      throw new InvalidRepositoryException(name);
    }

    final Reference<Repository> ref = cache.get(name);
    Repository db = ref != null ? ref.get() : null;
    if (db == null) {
      try {
        db = GitMetaUtil.open(new File(base, name));
        if (db == null) {
          throw new InvalidRepositoryException(name);
        }
      } catch (IOException err) {
        throw new InvalidRepositoryException(name, err);
      }
      cache.put(name, new SoftReference<Repository>(db));
    }
    return db;
  }
}