// 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.gerrit.git;

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;
  }
}

@@ -819,40 +819,8 @@
 Map;
-%0Aimport java.util.regex.Pattern;
 %0A%0A/*
@@ -919,104 +919,8 @@
 e %7B%0A
-  private static final Pattern REPO_NAME =%0A      Pattern.compile(%22%5E%5BA-Za-z%5D%5BA-Za-z0-9/_-%5D+$%22);%0A%0A
   pr
@@ -1990,111 +1990,8 @@
  %7D%0A%0A
-    if (!REPO_NAME.matcher(name).matches()) %7B%0A      throw new InvalidRepositoryException(name);%0A    %7D%0A%0A
     
@@ -2111,24 +2111,122 @@
  == null) %7B%0A
+      if (isUnreasonableName(name)) %7B%0A        throw new InvalidRepositoryException(name);%0A      %7D%0A
       try %7B%0A
@@ -2527,24 +2527,24 @@
 db));%0A    %7D%0A
-
     return d
@@ -2549,11 +2549,680 @@
  db;%0A  %7D
+%0A%0A  private boolean isUnreasonableName(final String name) %7B%0A    if (name.length() == 0) return true; // no empty paths%0A%0A    if (name.indexOf('%5C%5C') %3E= 0) return true; // no windows/dos stlye paths%0A    if (name.charAt(0) == '/') return true; // no absolute paths%0A    if (new File(name).isAbsolute()) return true; // no absolute paths%0A%0A    if (name.startsWith(%22../%22)) return true; // no %22l../etc/passwd%22%0A    if (name.contains(%22/../%22)) return true; // no %22foo/../etc/passwd%22%0A    if (name.contains(%22/./%22)) return true; // %22foo/./foo%22 is insane to ask%0A    if (name.contains(%22//%22)) return true; // windows UNC path can be %22//...%22%0A%0A    return false; // is a reasonable name%0A  %7D
 %0A%7D%0A

Allow . in a repository path name