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
;
25 * The version of the git. Note that the version number ignores build and commit hash.
27 public final class GitVersion
implements Comparable
<GitVersion
> {
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
);
34 * Invalid version number
36 public static final GitVersion INVALID
= new GitVersion(0, 0, 0, 0);
38 * The minimal supported version
40 public static final GitVersion MIN
= new GitVersion(1, 5, 4, 3);
43 * Major version nubmer
45 private final int myMajor
;
47 * Minor version number
49 private final int myMinor
;
53 private final int myRevision
;
57 private final int myPatchLevel
;
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
) {
70 myRevision
= revision
;
71 myPatchLevel
= patchLevel
;
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
) {
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
);
95 * @return true if the version is supported by the plugin
97 public boolean isSupported() {
98 return compareTo(MIN
) >= 0;
105 public boolean equals(final Object obj
) {
106 return obj
instanceof GitVersion
&& compareTo((GitVersion
)obj
) == 0;
113 public int hashCode() {
114 return ((myMajor
* 17 + myMinor
) * 17 + myRevision
) * 17 + myPatchLevel
;
120 public int compareTo(final GitVersion o
) {
121 int d
= myMajor
- o
.myMajor
;
125 d
= myMinor
- o
.myMinor
;
129 d
= myRevision
- o
.myRevision
;
133 return myPatchLevel
- o
.myPatchLevel
;
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;