Line data Source code
1 : // Copyright (C) 2019 The Android Open Source Project 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 : 15 : package com.google.gerrit.acceptance; 16 : 17 : import static java.util.stream.Collectors.joining; 18 : 19 : import com.google.common.base.Splitter; 20 : import java.util.List; 21 : import java.util.stream.IntStream; 22 : 23 : /** Class to parse and represent version of git-core client */ 24 : public class GitClientVersion implements Comparable<GitClientVersion> { 25 : private final int v[]; 26 : 27 : /** 28 : * Constructor to represent instance for minimum supported git-core version 29 : * 30 : * @param parts version passed as single digits 31 : */ 32 1 : public GitClientVersion(int... parts) { 33 1 : this.v = parts; 34 1 : } 35 : 36 : /** 37 : * Parse the git-core version as returned by git version command 38 : * 39 : * @param version String returned by git version command 40 : */ 41 1 : public GitClientVersion(String version) { 42 : // "git version x.y.z", at Google "git version x.y.z.gXXXXXXXXXX-goog" 43 1 : List<String> parts = Splitter.on(".").splitToList(Splitter.on(" ").splitToList(version).get(2)); 44 1 : int numParts = Math.min(parts.size(), 3); // ignore Google-specific part of the version 45 1 : v = new int[numParts]; 46 1 : for (int i = 0; i < numParts; i++) { 47 1 : v[i] = Integer.valueOf(parts.get(i)); 48 : } 49 1 : } 50 : 51 : @Override 52 : public int compareTo(GitClientVersion o) { 53 1 : int m = Math.max(v.length, o.v.length); 54 1 : for (int i = 0; i < m; i++) { 55 1 : int l = i < v.length ? v[i] : 0; 56 1 : int r = i < o.v.length ? o.v[i] : 0; 57 1 : if (l != r) { 58 1 : return l < r ? -1 : 1; 59 : } 60 : } 61 0 : return 0; 62 : } 63 : 64 : @Override 65 : public String toString() { 66 0 : return IntStream.of(v).mapToObj(String::valueOf).collect(joining(".")); 67 : } 68 : }