summaryrefslogtreecommitdiffstats
path: root/gerrit-server/src/main/java/com/google/gerrit/server/project/RefPattern.java
blob: 72face2fb633ab930a5c2020e396b2f419de2f16 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (C) 2016 The Android Open Source Project
//
// 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.server.project;

import com.google.common.base.Throwables;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.gerrit.common.data.AccessSection;
import com.google.gerrit.common.data.RefConfigSection;
import com.google.gerrit.common.errors.InvalidNameException;
import dk.brics.automaton.RegExp;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.eclipse.jgit.lib.Repository;

public class RefPattern {
  public static final String USERID_SHARDED = "shardeduserid";
  public static final String USERNAME = "username";

  private static final LoadingCache<String, String> exampleCache =
      CacheBuilder.newBuilder()
          .maximumSize(4000)
          .build(
              new CacheLoader<String, String>() {
                @Override
                public String load(String refPattern) {
                  return example(refPattern);
                }
              });

  public static String shortestExample(String refPattern) {
    if (isRE(refPattern)) {
      try {
        return exampleCache.get(refPattern);
      } catch (ExecutionException e) {
        Throwables.throwIfUnchecked(e.getCause());
        throw new RuntimeException(e);
      }
    } else if (refPattern.endsWith("/*")) {
      return refPattern.substring(0, refPattern.length() - 1) + '1';
    } else {
      return refPattern;
    }
  }

  static String example(String refPattern) {
    // Since Brics will substitute dot [.] with \0 when generating
    // shortest example, any usage of dot will fail in
    // Repository.isValidRefName() if not combined with star [*].
    // To get around this, we substitute the \0 with an arbitrary
    // accepted character.
    return toRegExp(refPattern).toAutomaton().getShortestExample(true).replace('\0', '-');
  }

  public static boolean isRE(String refPattern) {
    return refPattern.startsWith(AccessSection.REGEX_PREFIX);
  }

  public static RegExp toRegExp(String refPattern) {
    if (isRE(refPattern)) {
      refPattern = refPattern.substring(1);
    }
    return new RegExp(refPattern, RegExp.NONE);
  }

  public static void validate(String refPattern) throws InvalidNameException {
    if (refPattern.startsWith(RefConfigSection.REGEX_PREFIX)) {
      if (!Repository.isValidRefName(shortestExample(refPattern))) {
        throw new InvalidNameException(refPattern);
      }
    } else if (refPattern.equals(RefConfigSection.ALL)) {
      // This is a special case we have to allow, it fails below.
    } else if (refPattern.endsWith("/*")) {
      String prefix = refPattern.substring(0, refPattern.length() - 2);
      if (!Repository.isValidRefName(prefix)) {
        throw new InvalidNameException(refPattern);
      }
    } else if (!Repository.isValidRefName(refPattern)) {
      throw new InvalidNameException(refPattern);
    }
    validateRegExp(refPattern);
  }

  public static void validateRegExp(String refPattern) throws InvalidNameException {
    try {
      refPattern = refPattern.replace("${" + USERID_SHARDED + "}", "");
      refPattern = refPattern.replace("${" + USERNAME + "}", "");
      Pattern.compile(refPattern);
    } catch (PatternSyntaxException e) {
      throw new InvalidNameException(refPattern + " " + e.getMessage());
    }
  }
}