17
17
18
18
package pl .project13 .maven .git ;
19
19
20
+ import org .apache .http .client .utils .URIBuilder ;
20
21
import org .jetbrains .annotations .NotNull ;
21
22
import pl .project13 .maven .git .log .LoggerBridge ;
22
23
import pl .project13 .maven .git .util .PropertyManager ;
23
24
24
- import java .io .IOException ;
25
+ import java .net .URI ;
26
+ import java .net .URISyntaxException ;
25
27
import java .util .Map ;
26
28
import java .util .Properties ;
27
29
import java .util .TimeZone ;
28
30
import java .text .SimpleDateFormat ;
31
+ import java .util .regex .Pattern ;
29
32
30
33
import static com .google .common .base .Strings .isNullOrEmpty ;
31
34
@@ -228,4 +231,48 @@ protected void put(@NotNull Properties properties, String key, String value) {
228
231
log .info ("{} {}" , keyWithPrefix , value );
229
232
PropertyManager .putWithoutPrefix (properties , keyWithPrefix , value );
230
233
}
234
+
235
+ /**
236
+ * Regex to check for SCP-style SSH+GIT connection strings such as '[email protected] '
237
+ */
238
+ static final Pattern GIT_SCP_FORMAT = Pattern .compile ("^([a-zA-Z0-9_.+-])+@(.*)" );
239
+ /**
240
+ * If the git remote value is a URI and contains a user info component, strip the password from it if it exists.
241
+ *
242
+ * @param gitRemoteString The value of the git remote
243
+ * @return
244
+ * @throws GitCommitIdExecutionException
245
+ */
246
+ protected static String stripCredentialsFromOriginUrl (String gitRemoteString ) throws GitCommitIdExecutionException {
247
+
248
+ // The URL might be null if the repo hasn't set a remote
249
+ if (gitRemoteString == null ) {
250
+ return gitRemoteString ;
251
+ }
252
+
253
+ // Remotes using ssh connection strings in the 'git@github' format aren't
254
+ // proper URIs and won't parse . Plus since you should be using SSH keys,
255
+ // credentials like are not in the URL.
256
+ if (GIT_SCP_FORMAT .matcher (gitRemoteString ).matches ()) {
257
+ return gitRemoteString ;
258
+ }
259
+ // At this point, we should have a properly formatted URL
260
+ try {
261
+ URI original = new URI (gitRemoteString );
262
+ String userInfoString = original .getUserInfo ();
263
+ if (null == userInfoString ) {
264
+ return gitRemoteString ;
265
+ }
266
+ URIBuilder b = new URIBuilder (gitRemoteString );
267
+ String [] userInfo = userInfoString .split (":" );
268
+ // Build a new URL from the original URL, but nulling out the password
269
+ // component of the userinfo. We keep the username so that ssh uris such
270
+ // ssh://[email protected] will retain 'git@'.
271
+ b .setUserInfo (userInfo [0 ]);
272
+ return b .build ().toString ();
273
+
274
+ } catch (URISyntaxException e ) {
275
+ throw new GitCommitIdExecutionException (e );
276
+ }
277
+ }
231
278
}
0 commit comments