1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jgit.lib;
14
15 import org.eclipse.jgit.lib.Config.SectionParser;
16 import org.eclipse.jgit.util.SystemReader;
17
18
19
20
21 public class UserConfig {
22
23 public static final Config.SectionParser<UserConfig> KEY = UserConfig::new;
24
25 private String authorName;
26
27 private String authorEmail;
28
29 private String committerName;
30
31 private String committerEmail;
32
33 private boolean isAuthorNameImplicit;
34
35 private boolean isAuthorEmailImplicit;
36
37 private boolean isCommitterNameImplicit;
38
39 private boolean isCommitterEmailImplicit;
40
41 private UserConfig(Config rc) {
42 authorName = getNameInternal(rc, Constants.GIT_AUTHOR_NAME_KEY);
43 if (authorName == null) {
44 authorName = getDefaultUserName();
45 isAuthorNameImplicit = true;
46 }
47 authorEmail = getEmailInternal(rc, Constants.GIT_AUTHOR_EMAIL_KEY);
48 if (authorEmail == null) {
49 authorEmail = getDefaultEmail();
50 isAuthorEmailImplicit = true;
51 }
52
53 committerName = getNameInternal(rc, Constants.GIT_COMMITTER_NAME_KEY);
54 if (committerName == null) {
55 committerName = getDefaultUserName();
56 isCommitterNameImplicit = true;
57 }
58 committerEmail = getEmailInternal(rc, Constants.GIT_COMMITTER_EMAIL_KEY);
59 if (committerEmail == null) {
60 committerEmail = getDefaultEmail();
61 isCommitterEmailImplicit = true;
62 }
63 }
64
65
66
67
68
69
70
71
72 public String getAuthorName() {
73 return authorName;
74 }
75
76
77
78
79
80
81
82
83
84 public String getCommitterName() {
85 return committerName;
86 }
87
88
89
90
91
92
93
94
95 public String getAuthorEmail() {
96 return authorEmail;
97 }
98
99
100
101
102
103
104
105
106 public String getCommitterEmail() {
107 return committerEmail;
108 }
109
110
111
112
113
114
115
116
117
118 public boolean isAuthorNameImplicit() {
119 return isAuthorNameImplicit;
120 }
121
122
123
124
125
126
127
128
129
130 public boolean isAuthorEmailImplicit() {
131 return isAuthorEmailImplicit;
132 }
133
134
135
136
137
138
139
140
141
142 public boolean isCommitterNameImplicit() {
143 return isCommitterNameImplicit;
144 }
145
146
147
148
149
150
151
152
153
154 public boolean isCommitterEmailImplicit() {
155 return isCommitterEmailImplicit;
156 }
157
158 private static String getNameInternal(Config rc, String envKey) {
159
160 String username = system().getenv(envKey);
161
162 if (username == null) {
163
164
165 username = rc.getString("user", null, "name");
166 }
167
168 return stripInvalidCharacters(username);
169 }
170
171
172
173
174
175 private static String getDefaultUserName() {
176
177 String username = system().getProperty(Constants.OS_USER_NAME_KEY);
178 if (username == null)
179 username = Constants.UNKNOWN_USER_DEFAULT;
180 return username;
181 }
182
183 private static String getEmailInternal(Config rc, String envKey) {
184
185 String email = system().getenv(envKey);
186
187 if (email == null) {
188
189 email = rc.getString("user", null, "email");
190 }
191
192 return stripInvalidCharacters(email);
193 }
194
195 private static String stripInvalidCharacters(String s) {
196 return s == null ? null : s.replaceAll("<|>|\n", "");
197 }
198
199
200
201
202
203 private static String getDefaultEmail() {
204
205 String username = getDefaultUserName();
206 return username + "@" + system().getHostname();
207 }
208
209 private static SystemReader system() {
210 return SystemReader.getInstance();
211 }
212 }