All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gerrit.acceptance.GitClientVersion Maven / Gradle / Ivy

There is a newer version: 3.11.0
Show newest version
// Copyright (C) 2019 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.acceptance;

import static java.util.stream.Collectors.joining;

import com.google.common.base.Splitter;
import java.util.List;
import java.util.stream.IntStream;

/** Class to parse and represent version of git-core client */
public class GitClientVersion implements Comparable {
  private final int v[];

  /**
   * Constructor to represent instance for minimum supported git-core version
   *
   * @param parts version passed as single digits
   */
  public GitClientVersion(int... parts) {
    this.v = parts;
  }

  /**
   * Parse the git-core version as returned by git version command
   *
   * @param version String returned by git version command
   */
  public GitClientVersion(String version) {
    // "git version x.y.z", at Google "git version x.y.z.gXXXXXXXXXX-goog"
    List parts = Splitter.on(".").splitToList(Splitter.on(" ").splitToList(version).get(2));
    int numParts = Math.min(parts.size(), 3); // ignore Google-specific part of the version
    v = new int[numParts];
    for (int i = 0; i < numParts; i++) {
      v[i] = Integer.valueOf(parts.get(i));
    }
  }

  @Override
  public int compareTo(GitClientVersion o) {
    int m = Math.max(v.length, o.v.length);
    for (int i = 0; i < m; i++) {
      int l = i < v.length ? v[i] : 0;
      int r = i < o.v.length ? o.v[i] : 0;
      if (l != r) {
        return l < r ? -1 : 1;
      }
    }
    return 0;
  }

  @Override
  public String toString() {
    return IntStream.of(v).mapToObj(String::valueOf).collect(joining("."));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy