1 /*
2 * Copyright (C) 2009, Google Inc.
3 * Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.com>
4 * Copyright (C) 2011, Matthias Sohn <matthias.sohn@sap.com>
5 * and other copyright owners as documented in the project's IP log.
6 *
7 * This program and the accompanying materials are made available
8 * under the terms of the Eclipse Distribution License v1.0 which
9 * accompanies this distribution, is reproduced below, and is
10 * available at http://www.eclipse.org/org/documents/edl-v10.php
11 *
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
16 * conditions are met:
17 *
18 * - Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials provided
24 * with the distribution.
25 *
26 * - Neither the name of the Eclipse Foundation, Inc. nor the
27 * names of its contributors may be used to endorse or promote
28 * products derived from this software without specific prior
29 * written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 */
45
46 package org.eclipse.jgit.lib;
47
48 import org.eclipse.jgit.lib.Config.SectionParser;
49 import org.eclipse.jgit.util.SystemReader;
50
51 /** The standard "user" configuration parameters. */
52 public class UserConfig {
53 /** Key for {@link Config#get(SectionParser)}. */
54 public static final Config.SectionParser<UserConfig> KEY = new SectionParser<UserConfig>() {
55 public UserConfig parse(final Config cfg) {
56 return new UserConfig(cfg);
57 }
58 };
59
60 private String authorName;
61
62 private String authorEmail;
63
64 private String committerName;
65
66 private String committerEmail;
67
68 private boolean isAuthorNameImplicit;
69
70 private boolean isAuthorEmailImplicit;
71
72 private boolean isCommitterNameImplicit;
73
74 private boolean isCommitterEmailImplicit;
75
76 private UserConfig(final Config rc) {
77 authorName = getNameInternal(rc, Constants.GIT_AUTHOR_NAME_KEY);
78 if (authorName == null) {
79 authorName = getDefaultUserName();
80 isAuthorNameImplicit = true;
81 }
82 authorEmail = getEmailInternal(rc, Constants.GIT_AUTHOR_EMAIL_KEY);
83 if (authorEmail == null) {
84 authorEmail = getDefaultEmail();
85 isAuthorEmailImplicit = true;
86 }
87
88 committerName = getNameInternal(rc, Constants.GIT_COMMITTER_NAME_KEY);
89 if (committerName == null) {
90 committerName = getDefaultUserName();
91 isCommitterNameImplicit = true;
92 }
93 committerEmail = getEmailInternal(rc, Constants.GIT_COMMITTER_EMAIL_KEY);
94 if (committerEmail == null) {
95 committerEmail = getDefaultEmail();
96 isCommitterEmailImplicit = true;
97 }
98 }
99
100 /**
101 * @return the author name as defined in the git variables and
102 * configurations. If no name could be found, try to use the system
103 * user name instead.
104 */
105 public String getAuthorName() {
106 return authorName;
107 }
108
109 /**
110 * @return the committer name as defined in the git variables and
111 * configurations. If no name could be found, try to use the system
112 * user name instead.
113 */
114 public String getCommitterName() {
115 return committerName;
116 }
117
118 /**
119 * @return the author email as defined in git variables and
120 * configurations. If no email could be found, try to
121 * propose one default with the user name and the
122 * host name.
123 */
124 public String getAuthorEmail() {
125 return authorEmail;
126 }
127
128 /**
129 * @return the committer email as defined in git variables and
130 * configurations. If no email could be found, try to
131 * propose one default with the user name and the
132 * host name.
133 */
134 public String getCommitterEmail() {
135 return committerEmail;
136 }
137
138 /**
139 * @return true if the author name was not explicitly configured but
140 * constructed from information the system has about the logged on
141 * user
142 */
143 public boolean isAuthorNameImplicit() {
144 return isAuthorNameImplicit;
145 }
146
147 /**
148 * @return true if the author email was not explicitly configured but
149 * constructed from information the system has about the logged on
150 * user
151 */
152 public boolean isAuthorEmailImplicit() {
153 return isAuthorEmailImplicit;
154 }
155
156 /**
157 * @return true if the committer name was not explicitly configured but
158 * constructed from information the system has about the logged on
159 * user
160 */
161 public boolean isCommitterNameImplicit() {
162 return isCommitterNameImplicit;
163 }
164
165 /**
166 * @return true if the author email was not explicitly configured but
167 * constructed from information the system has about the logged on
168 * user
169 */
170 public boolean isCommitterEmailImplicit() {
171 return isCommitterEmailImplicit;
172 }
173
174 private static String getNameInternal(Config rc, String envKey) {
175 // try to get the user name for the system property GIT_XXX_NAME
176 String username = system().getenv(envKey);
177
178 if (username == null) {
179 // try to get the user name from the local and global
180 // configurations.
181 username = rc.getString("user", null, "name"); //$NON-NLS-1$ //$NON-NLS-2$
182 }
183
184 return stripInvalidCharacters(username);
185 }
186
187 /**
188 * @return try to get user name of the logged on user from the operating
189 * system
190 */
191 private static String getDefaultUserName() {
192 // get the system user name
193 String username = system().getProperty(Constants.OS_USER_NAME_KEY);
194 if (username == null)
195 username = Constants.UNKNOWN_USER_DEFAULT;
196 return username;
197 }
198
199 private static String getEmailInternal(Config rc, String envKey) {
200 // try to get the email for the system property GIT_XXX_EMAIL
201 String email = system().getenv(envKey);
202
203 if (email == null) {
204 // try to get the email from the local and global configurations.
205 email = rc.getString("user", null, "email"); //$NON-NLS-1$ //$NON-NLS-2$
206 }
207
208 return stripInvalidCharacters(email);
209 }
210
211 private static String stripInvalidCharacters(String s) {
212 return s == null ? null : s.replaceAll("<|>|\n", ""); //$NON-NLS-1$//$NON-NLS-2$
213 }
214
215 /**
216 * @return try to construct email for logged on user using system
217 * information
218 */
219 private static String getDefaultEmail() {
220 // try to construct an email
221 String username = getDefaultUserName();
222 return username + "@" + system().getHostname(); //$NON-NLS-1$
223 }
224
225 private static SystemReader system() {
226 return SystemReader.getInstance();
227 }
228 }