git4idea: added support for the future -v option for git clone (untested)
[fedora-idea.git] / plugins / git4idea / src / git4idea / config / GitVersion.java
blob8d92c905ae5d9d7d40092acbd0df04744801e105
1 /*
2 * Copyright 2000-2008 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package git4idea.config;
18 import org.jetbrains.annotations.NonNls;
20 import java.text.MessageFormat;
21 import java.text.ParseException;
22 import java.util.Locale;
24 /**
25 * The version of the git. Note that the version number ignores build and commit hash.
27 public final class GitVersion implements Comparable<GitVersion> {
28 /**
29 * The format of the "git version"
31 @NonNls private static MessageFormat FORMAT =
32 new MessageFormat("git version {0,number,integer}.{1,number,integer}.{2,number,integer}.{3,number,integer}", Locale.US);
33 /**
34 * Invalid version number
36 public static final GitVersion INVALID = new GitVersion(0, 0, 0, 0);
37 /**
38 * The minimal supported version
40 public static final GitVersion MIN = new GitVersion(1, 5, 4, 3);
42 /**
43 * Major version nubmer
45 private final int myMajor;
46 /**
47 * Minor version number
49 private final int myMinor;
50 /**
51 * Revision number
53 private final int myRevision;
54 /**
55 * Patch level
57 private final int myPatchLevel;
59 /**
60 * A constructor from fields
62 * @param major a major number
63 * @param minor a minor number
64 * @param revision a revision
65 * @param patchLevel a patch level
67 public GitVersion(int major, int minor, int revision, int patchLevel) {
68 myMajor = major;
69 myMinor = minor;
70 myRevision = revision;
71 myPatchLevel = patchLevel;
74 /**
75 * Parse output of "git version" command
77 * @param version a a version number
78 * @return a git version
80 public static GitVersion parse(String version) {
81 try {
82 Object[] parsed = FORMAT.parse(version);
83 int major = ((Long)parsed[0]).intValue();
84 int minor = ((Long)parsed[1]).intValue();
85 int revision = ((Long)parsed[2]).intValue();
86 int patchLevel = ((Long)parsed[3]).intValue();
87 return new GitVersion(major, minor, revision, patchLevel);
89 catch (ParseException e) {
90 throw new IllegalArgumentException("Unsupported format of git --version output: " + version);
94 /**
95 * @return true if the version is supported by the plugin
97 public boolean isSupported() {
98 return compareTo(MIN) >= 0;
102 * {@inheritDoc}
104 @Override
105 public boolean equals(final Object obj) {
106 return obj instanceof GitVersion && compareTo((GitVersion)obj) == 0;
110 * {@inheritDoc}
112 @Override
113 public int hashCode() {
114 return ((myMajor * 17 + myMinor) * 17 + myRevision) * 17 + myPatchLevel;
118 * {@inheritDoc}
120 public int compareTo(final GitVersion o) {
121 int d = myMajor - o.myMajor;
122 if (d != 0) {
123 return d;
125 d = myMinor - o.myMinor;
126 if (d != 0) {
127 return d;
129 d = myRevision - o.myRevision;
130 if (d != 0) {
131 return d;
133 return myPatchLevel - o.myPatchLevel;
137 * {@inheritDoc}
139 @Override
140 public String toString() {
141 //noinspection ConcatenationWithEmptyString
142 return "" + myMajor + "." + myMinor + "." + myRevision + "." + myPatchLevel;
146 * Compare version number
148 * @param gitVersion a git revsion to compare wiht
149 * @return the comparison result
151 public boolean isLessOrEqual(final GitVersion gitVersion) {
152 return gitVersion != null && compareTo(gitVersion) <= 0;