Search in sources :

Example 1 with VersionElementType

use of com.google.copybara.git.LatestVersionSelector.VersionElementType in project copybara by google.

the class GitModule method versionSelector.

@SuppressWarnings("unused")
@StarlarkMethod(name = "latest_version", doc = "Customize what version of the available branches and tags to pick." + " By default it ignores the reference passed as parameter. Using `force:reference`" + " in the CLI will force to use that reference instead.", parameters = { @Param(name = "refspec_format", doc = "The format of the branch/tag", named = true, defaultValue = "\"refs/tags/${n0}.${n1}.${n2}\""), @Param(name = "refspec_groups", named = true, doc = "A set of named regexes that can be used to match part of the versions. Copybara" + " uses [re2](https://github.com/google/re2/wiki/Syntax) syntax. Use the" + " following nomenclature n0, n1, n2 for the version part (will use numeric" + " sorting) or s0, s1, s2 (alphabetic sorting). Note that there can be mixed" + " but the numbers cannot be repeated. In other words n0, s1, n2 is valid but" + " not n0, s0, n1. n0 has more priority than n1. If there are fields where" + " order is not important, use s(N+1) where N ist he latest sorted field." + " Example {\"n0\": \"[0-9]+\", \"s1\": \"[a-z]+\"}", defaultValue = "{'n0' : '[0-9]+', 'n1' : '[0-9]+', 'n2' : '[0-9]+'}") }, useStarlarkThread = true)
public LatestVersionSelector versionSelector(String refspec, Dict<?, ?> groups, // <String, String>
StarlarkThread thread) throws EvalException {
    Map<String, String> groupsMap = Dict.cast(groups, String.class, String.class, "refspec_groups");
    check(refspec.startsWith("refs/"), "Wrong value '%s'. Refspec has to" + " start with 'refs/'. For example 'refs/tags/${v0}.${v1}.${v2}'", refspec);
    TreeMap<Integer, VersionElementType> elements = new TreeMap<>();
    Pattern regexKey = Pattern.compile("([sn])([0-9])");
    for (String s : groupsMap.keySet()) {
        Matcher matcher = regexKey.matcher(s);
        check(matcher.matches(), "Incorrect key for refspec_group. Should be in the " + "format of n0, n1, etc. or s0, s1, etc. Value: %s", s);
        VersionElementType type = matcher.group(1).equals("s") ? ALPHABETIC : NUMERIC;
        int num = Integer.parseInt(matcher.group(2));
        check(!elements.containsKey(num) || elements.get(num) == type, "Cannot use same n in both s%s and n%s: %s", num, num, s);
        elements.put(num, type);
    }
    for (Integer num : elements.keySet()) {
        if (num > 0) {
            check(elements.containsKey(num - 1), "Cannot have s%s or n%s if s%s or n%s" + " doesn't exist", num, num, num - 1, num - 1);
        }
    }
    LatestVersionSelector versionPicker = new LatestVersionSelector(refspec, Replace.parsePatterns(groupsMap), elements, thread.getCallerLocation());
    ImmutableList<String> extraGroups = versionPicker.getUnmatchedGroups();
    check(extraGroups.isEmpty(), "Extra refspec_groups not used in pattern: %s", extraGroups);
    return versionPicker;
}
Also used : Pattern(com.google.re2j.Pattern) VersionElementType(com.google.copybara.git.LatestVersionSelector.VersionElementType) Matcher(com.google.re2j.Matcher) TreeMap(java.util.TreeMap) StarlarkMethod(net.starlark.java.annot.StarlarkMethod)

Aggregations

VersionElementType (com.google.copybara.git.LatestVersionSelector.VersionElementType)1 Matcher (com.google.re2j.Matcher)1 Pattern (com.google.re2j.Pattern)1 TreeMap (java.util.TreeMap)1 StarlarkMethod (net.starlark.java.annot.StarlarkMethod)1