1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm.provider.git.util;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.maven.scm.providers.gitlib.settings.Settings;
28 import org.apache.maven.scm.providers.gitlib.settings.io.xpp3.GitXpp3Reader;
29 import org.codehaus.plexus.util.ReaderFactory;
30 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
31
32
33
34
35 public class GitUtil {
36 protected static final String GIT_SETTINGS_FILENAME = "git-settings.xml";
37
38 public static final File DEFAULT_SETTINGS_DIRECTORY = new File(System.getProperty("user.home"), ".scm");
39
40
41
42
43 public static final String PASSWORD_PLACE_HOLDER_WITH_DELIMITERS = ":********@";
44
45 private static final Pattern PASSWORD_IN_URL_PATTERN = Pattern.compile("^.*(:[^/].*@).*$");
46
47 private static File settingsDirectory = DEFAULT_SETTINGS_DIRECTORY;
48
49 private static Settings settings;
50
51 private GitUtil() {
52
53 }
54
55 public static Settings getSettings() {
56 if (settings == null) {
57 settings = readSettings();
58 }
59 return settings;
60 }
61
62 public static Settings readSettings() {
63 File settingsFile = getSettingsFile();
64
65 if (settingsFile.exists()) {
66 GitXpp3Reader reader = new GitXpp3Reader();
67 try {
68 return reader.read(ReaderFactory.newXmlReader(settingsFile));
69 } catch (IOException e) {
70
71 } catch (XmlPullParserException e) {
72 String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage();
73
74 System.err.println(message);
75 }
76 }
77
78 return new Settings();
79 }
80
81 public static void setSettingsDirectory(File directory) {
82 settingsDirectory = directory;
83 settings = readSettings();
84 }
85
86 public static File getSettingsFile() {
87 return new File(settingsDirectory, GIT_SETTINGS_FILENAME);
88 }
89
90
91
92
93
94
95
96
97
98 public static String maskPasswordInUrl(String urlWithCredentials) {
99 String output = urlWithCredentials;
100 final Matcher passwordMatcher = PASSWORD_IN_URL_PATTERN.matcher(output);
101 if (passwordMatcher.find()) {
102
103 final String clearPasswordWithDelimiters = passwordMatcher.group(1);
104
105 output = StringUtils.replace(output, clearPasswordWithDelimiters, PASSWORD_PLACE_HOLDER_WITH_DELIMITERS);
106 }
107 return output;
108 }
109 }