16
16
17
17
package com .palantir .gradle .gitversion ;
18
18
19
+ import com .google .common .annotations .VisibleForTesting ;
19
20
import com .google .common .base .Preconditions ;
20
21
import com .google .common .base .Splitter ;
21
22
import java .io .BufferedReader ;
25
26
import java .nio .charset .StandardCharsets ;
26
27
import java .util .ArrayList ;
27
28
import java .util .Arrays ;
29
+ import java .util .HashMap ;
28
30
import java .util .HashSet ;
29
31
import java .util .List ;
32
+ import java .util .Map ;
30
33
import java .util .Set ;
31
34
import org .slf4j .Logger ;
32
35
import org .slf4j .LoggerFactory ;
33
36
34
37
/**
35
38
* Mimics git describe by using rev-list to support versions of git < 1.8.4.
36
39
*/
37
- class NativeGitDescribe implements GitDescribe {
38
- private static final Logger log = LoggerFactory .getLogger (NativeGitDescribe .class );
40
+ class Git {
41
+ private static final Logger log = LoggerFactory .getLogger (Git .class );
39
42
40
43
private static final Splitter LINE_SPLITTER =
41
44
Splitter .on (System .getProperty ("line.separator" )).omitEmptyStrings ();
42
45
private static final Splitter WORD_SPLITTER = Splitter .on (" " ).omitEmptyStrings ();
43
46
44
47
private final File directory ;
45
48
46
- NativeGitDescribe (File directory ) {
49
+ Git (File directory ) {
50
+ this (directory , false );
51
+ }
52
+
53
+ @ VisibleForTesting
54
+ Git (File directory , boolean testing ) {
55
+ if (!gitCommandExists ()) {
56
+ throw new RuntimeException ("Git not found in project" );
57
+ }
47
58
this .directory = directory ;
59
+ if (testing && !checkIfUserIsSet ()) {
60
+ setGitUser ();
61
+ }
48
62
}
49
63
50
64
private String runGitCmd (String ... commands ) throws IOException , InterruptedException {
65
+ return runGitCmd (new HashMap <>(), commands );
66
+ }
67
+
68
+ private String runGitCmd (Map <String , String > envvars , String ... commands ) throws IOException , InterruptedException {
51
69
List <String > cmdInput = new ArrayList <>();
52
70
cmdInput .add ("git" );
53
71
cmdInput .addAll (Arrays .asList (commands ));
54
72
ProcessBuilder pb = new ProcessBuilder (cmdInput );
73
+ Map <String , String > environment = pb .environment ();
74
+ environment .putAll (envvars );
55
75
pb .directory (directory );
56
76
pb .redirectErrorStream (true );
57
77
@@ -74,12 +94,77 @@ private String runGitCmd(String... commands) throws IOException, InterruptedExce
74
94
return builder .toString ().trim ();
75
95
}
76
96
77
- @ Override
78
- public String describe (String prefix ) {
79
- if (!gitCommandExists ()) {
97
+ public String runGitCommand (Map <String , String > envvar , String ... command ) {
98
+ try {
99
+ return runGitCmd (envvar , command );
100
+ } catch (IOException | InterruptedException | RuntimeException e ) {
101
+ log .debug ("Native git command {} failed.\n " , command , e );
102
+ return null ;
103
+ }
104
+ }
105
+
106
+ public String runGitCommand (String ... command ) {
107
+ return runGitCommand (new HashMap <>(), command );
108
+ }
109
+
110
+ private boolean checkIfUserIsSet () {
111
+ try {
112
+ String userEmail = runGitCmd ("config" , "user.email" );
113
+ if (userEmail .isEmpty ()) {
114
+ return false ;
115
+ }
116
+ return true ;
117
+ } catch (IOException | InterruptedException | RuntimeException e ) {
118
+ log .debug ("Native git config user.email failed" , e );
119
+ return false ;
120
+ }
121
+ }
122
+
123
+ private void setGitUser () {
124
+ try {
125
+ runGitCommand (
"config" ,
"--global" ,
"user.email" ,
"[email protected] " );
126
+ runGitCommand ("config" , "--global" , "user.name" , "name" );
127
+ } catch (RuntimeException e ) {
128
+ log .debug ("Native git set user failed" , e );
129
+ }
130
+ }
131
+
132
+ public String getCurrentBranch () {
133
+ try {
134
+ String branch = runGitCmd ("branch" , "--show-current" );
135
+ if (branch .isEmpty ()) {
136
+ return null ;
137
+ }
138
+ return branch ;
139
+ } catch (IOException | InterruptedException | RuntimeException e ) {
140
+ log .debug ("Native git branch --show-current failed" , e );
141
+ return null ;
142
+ }
143
+ }
144
+
145
+ public String getCurrentHeadFullHash () {
146
+ try {
147
+ return runGitCmd ("rev-parse" , "HEAD" );
148
+ } catch (IOException | InterruptedException | RuntimeException e ) {
149
+ log .debug ("Native git rev-parse HEAD failed" , e );
150
+ return null ;
151
+ }
152
+ }
153
+
154
+ public Boolean isClean () {
155
+ try {
156
+ String result = runGitCmd ("status" , "--porcelain" );
157
+ if (result .isEmpty ()) {
158
+ return true ;
159
+ }
160
+ return false ;
161
+ } catch (IOException | InterruptedException | RuntimeException e ) {
162
+ log .debug ("Native git status --porcelain failed" , e );
80
163
return null ;
81
164
}
165
+ }
82
166
167
+ public String describe (String prefix ) {
83
168
try {
84
169
// Get SHAs of all tags, we only need to search for these later on
85
170
Set <String > tagRefs = new HashSet <>();
0 commit comments