11package pl .project13 .maven .git ;
22
3- import java .io .ByteArrayInputStream ;
4- import java .io .File ;
5- import java .io .IOException ;
6- import java .io .InputStream ;
7- import java .text .ParseException ;
8- import java .text .SimpleDateFormat ;
9- import java .util .Date ;
10- import java .util .LinkedHashMap ;
11- import java .util .Locale ;
12- import java .util .Map ;
13- import org .jetbrains .annotations .NotNull ;
14- import org .jetbrains .annotations .Nullable ;
3+ import com .google .common .base .Splitter ;
154import org .apache .maven .plugin .MojoExecutionException ;
5+ import org .jetbrains .annotations .NotNull ;
166import pl .project13 .maven .git .log .LoggerBridge ;
17- import pl . project13 . maven . git . log . MavenLoggerBridge ;
7+
188import java .io .*;
199
2010
@@ -40,12 +30,12 @@ public static NativeGitProvider on(@NotNull File dotGitDirectory) {
4030 return new NativeGitProvider (dotGitDirectory );
4131 }
4232
43- NativeGitProvider (@ NotNull File dotGitDirectory ){
33+ NativeGitProvider (@ NotNull File dotGitDirectory ) {
4434 this .dotGitDirectory = dotGitDirectory ;
4535 }
4636
4737
48- @ NotNull
38+ @ NotNull
4939 public NativeGitProvider withLoggerBridge (LoggerBridge bridge ) {
5040 super .loggerBridge = bridge ;
5141 return this ;
@@ -73,36 +63,36 @@ public NativeGitProvider setDateFormat(String dateFormat) {
7363 return this ;
7464 }
7565
76- public NativeGitProvider setGitDescribe (GitDescribeConfig gitDescribe ){
66+ public NativeGitProvider setGitDescribe (GitDescribeConfig gitDescribe ) {
7767 super .gitDescribe = gitDescribe ;
7868 return this ;
7969 }
8070
8171 @ Override
82- protected void init () throws MojoExecutionException {
83- try {
72+ protected void init () throws MojoExecutionException {
73+ try {
8474 canonical = dotGitDirectory .getCanonicalFile ();
8575 } catch (Exception ex ) {
8676 throw new MojoExecutionException ("Passed a invalid directory, not a GIT repository: " + dotGitDirectory , ex );
8777 }
8878 }
8979
9080 @ Override
91- protected String getBuildAuthorName (){
81+ protected String getBuildAuthorName () {
9282 return tryToRunGitCommand (canonical , "log -1 --pretty=format:\" %an\" " );
9383 }
9484
9585 @ Override
96- protected String getBuildAuthorEmail (){
86+ protected String getBuildAuthorEmail () {
9787 return tryToRunGitCommand (canonical , "log -1 --pretty=format:\" %ae\" " );
9888 }
9989
10090 @ Override
101- protected void prepareGitToExtractMoreDetailedReproInformation () throws MojoExecutionException {
91+ protected void prepareGitToExtractMoreDetailedReproInformation () throws MojoExecutionException {
10292 }
10393
10494 @ Override
105- protected String getBranchName () throws IOException {
95+ protected String getBranchName () throws IOException {
10696 return getBranch (canonical );
10797 }
10898
@@ -115,115 +105,118 @@ private String getBranch(File canonical) {
115105 }
116106
117107 @ Override
118- protected String getGitDescribe () throws MojoExecutionException {
108+ protected String getGitDescribe () throws MojoExecutionException {
119109 String argumentsForGitDescribe = getArgumentsForGitDescribe (super .gitDescribe );
120110 String gitDescribe = tryToRunGitCommand (canonical , "describe " + argumentsForGitDescribe );
121111 return gitDescribe ;
122112 }
123113
124- private String getArgumentsForGitDescribe (GitDescribeConfig gitDescribe ){
125- if (gitDescribe != null ){
114+ private String getArgumentsForGitDescribe (GitDescribeConfig gitDescribe ) {
115+ if (gitDescribe != null ) {
126116 return getArgumentsForGitDescribeAndDescibeNotNull (gitDescribe );
127- }else {
117+ } else {
128118 return "" ;
129119 }
130120 }
131-
132- private String getArgumentsForGitDescribeAndDescibeNotNull (GitDescribeConfig gitDescribe ){
121+
122+ private String getArgumentsForGitDescribeAndDescibeNotNull (GitDescribeConfig gitDescribe ) {
133123 StringBuilder argumentsForGitDescribe = new StringBuilder ();
134124
135- if (gitDescribe .isAlways ()){
125+ if (gitDescribe .isAlways ()) {
136126 argumentsForGitDescribe .append ("--always " );
137127 }
138128
139129 String dirtyMark = gitDescribe .getDirty ();
140- if (dirtyMark != null && !dirtyMark .isEmpty ()){
130+ if (dirtyMark != null && !dirtyMark .isEmpty ()) {
141131 // Option: --dirty[=<mark>]
142132 // TODO: Code Injection? Or does the CliRunner escape Arguments?
143133 argumentsForGitDescribe .append ("--dirty=" + dirtyMark + " " );
144134 }
145135
146136 argumentsForGitDescribe .append ("--abbrev=" + gitDescribe .getAbbrev () + " " );
147137
148- if (gitDescribe .getTags ()){
138+ if (gitDescribe .getTags ()) {
149139 argumentsForGitDescribe .append ("--tags " );
150140 }
151141
152- if (gitDescribe .getForceLongFormat ()){
142+ if (gitDescribe .getForceLongFormat ()) {
153143 argumentsForGitDescribe .append ("--long " );
154144 }
155145 return argumentsForGitDescribe .toString ();
156146 }
157147
158148 @ Override
159- protected String getCommitId (){
149+ protected String getCommitId () {
160150 return tryToRunGitCommand (canonical , "rev-parse HEAD" );
161151 }
162152
163153 @ Override
164- protected String getAbbrevCommitId () throws MojoExecutionException {
154+ protected String getAbbrevCommitId () throws MojoExecutionException {
165155 // we could run: tryToRunGitCommand(canonical, "rev-parse --short="+abbrevLength+" HEAD");
166156 // but minimum length for --short is 4, our abbrevLength could be 2
167157 String commitId = getCommitId ();
168158 String abbrevCommitId = "" ;
169159
170- if (commitId != null && !commitId .isEmpty ()){
160+ if (commitId != null && !commitId .isEmpty ()) {
171161 abbrevCommitId = commitId .substring (0 , abbrevLength );
172162 }
173163
174164 return abbrevCommitId ;
175165 }
176166
177167 @ Override
178- protected String getCommitAuthorName (){
168+ protected String getCommitAuthorName () {
179169 return tryToRunGitCommand (canonical , "log -1 --pretty=format:\" %cn\" " );
180170 }
181171
182172 @ Override
183- protected String getCommitAuthorEmail (){
173+ protected String getCommitAuthorEmail () {
184174 return tryToRunGitCommand (canonical , "log -1 --pretty=format:\" %ce\" " );
185175 }
186176
187177 @ Override
188- protected String getCommitMessageFull (){
189- return tryToRunGitCommand (canonical , "log -1 --pretty=format:\" %B\" " );
178+ protected String getCommitMessageFull () {
179+ return tryToRunGitCommand (canonical , "log -1 --pretty=format:\" %B\" " );
190180 }
191181
192182 @ Override
193- protected String getCommitMessageShort (){
183+ protected String getCommitMessageShort () {
194184 return tryToRunGitCommand (canonical , "log -1 --pretty=format:\" %s\" " );
195185 }
196186
197187 @ Override
198- protected String getCommitTime (){
188+ protected String getCommitTime () {
199189 return tryToRunGitCommand (canonical , "log -1 --pretty=format:\" %ci\" " );
200190 }
201191
202192 @ Override
203- protected String getRemoteOriginUrl () throws MojoExecutionException {
193+ protected String getRemoteOriginUrl () throws MojoExecutionException {
204194 return getOriginRemote (canonical );
205195 }
206196
207197 @ Override
208- protected void finalCleanUp (){
198+ protected void finalCleanUp () {
209199 }
210200
211201 private String getOriginRemote (File directory ) throws MojoExecutionException {
212202 String remoteUrl = null ;
213- try {
203+ try {
214204 String remotes = runGitCommand (directory , "remote -v" );
215- for (String line : remotes .split ("\n " )) {
205+
206+ // welcome to text output parsing hell! - no `\n` is not enough
207+ for (String line : Splitter .onPattern ("\\ ((fetch|push)\\ )?" ).split (remotes )) {
216208 String trimmed = line .trim ();
209+
217210 if (trimmed .startsWith ("origin" )) {
218211 String [] splited = trimmed .split ("\\ s+" );
219- if (splited .length != REMOTE_COLS ) {
220- throw new MojoExecutionException ("Unsupported GIT output - verbose remote address: " + line );
212+ if (splited .length != REMOTE_COLS - 1 ) { // because (fetch/push) was trimmed
213+ throw new MojoExecutionException ("Unsupported GIT output ( verbose remote address): " + line );
221214 }
222- remoteUrl = splited [1 ];
215+ remoteUrl = splited [1 ];
223216 }
224217 }
225- }catch (Exception e ){
226- throw new MojoExecutionException ("Error " , e );
218+ } catch (Exception e ) {
219+ throw new MojoExecutionException ("Error while obtaining origin remote " , e );
227220 }
228221 return remoteUrl ;
229222 }
@@ -246,7 +239,7 @@ private String runGitCommand(File directory, String gitCommand) throws MojoExecu
246239
247240 String result = getRunner ().run (directory , command ).trim ();
248241 return result ;
249- }catch (IOException ex ) {
242+ } catch (IOException ex ) {
250243 throw new MojoExecutionException ("Could not run GIT command - GIT is not installed or not exists in system path? " + "Tried to run: 'git " + gitCommand + "'" , ex );
251244 }
252245 }
@@ -269,7 +262,7 @@ protected static class Runner implements CliRunner {
269262 @ Override
270263 public String run (File directory , String command ) throws IOException {
271264 String output = "" ;
272- try {
265+ try {
273266 ProcessBuilder builder = new ProcessBuilder (command .split ("\\ s" ));
274267 final Process proc = builder .directory (directory ).start ();
275268 proc .waitFor ();
@@ -282,7 +275,7 @@ public String run(File directory, String command) throws IOException {
282275 }
283276
284277 if (proc .exitValue () != 0 ) {
285- String message = String .format ("Git command exited with invalid status [%d]: `%s`" , proc .exitValue (),output );
278+ String message = String .format ("Git command exited with invalid status [%d]: `%s`" , proc .exitValue (), output );
286279 throw new IOException (message );
287280 }
288281 output = commandResult .toString ();
@@ -291,5 +284,5 @@ public String run(File directory, String command) throws IOException {
291284 }
292285 return output ;
293286 }
294- }
287+ }
295288}
0 commit comments