View Javadoc
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  /**
52   * The standard "user" configuration parameters.
53   */
54  public class UserConfig {
55  	/** Key for {@link Config#get(SectionParser)}. */
56  	public static final Config.SectionParser<UserConfig> KEY = UserConfig::new;
57  
58  	private String authorName;
59  
60  	private String authorEmail;
61  
62  	private String committerName;
63  
64  	private String committerEmail;
65  
66  	private boolean isAuthorNameImplicit;
67  
68  	private boolean isAuthorEmailImplicit;
69  
70  	private boolean isCommitterNameImplicit;
71  
72  	private boolean isCommitterEmailImplicit;
73  
74  	private UserConfig(Config rc) {
75  		authorName = getNameInternal(rc, Constants.GIT_AUTHOR_NAME_KEY);
76  		if (authorName == null) {
77  			authorName = getDefaultUserName();
78  			isAuthorNameImplicit = true;
79  		}
80  		authorEmail = getEmailInternal(rc, Constants.GIT_AUTHOR_EMAIL_KEY);
81  		if (authorEmail == null) {
82  			authorEmail = getDefaultEmail();
83  			isAuthorEmailImplicit = true;
84  		}
85  
86  		committerName = getNameInternal(rc, Constants.GIT_COMMITTER_NAME_KEY);
87  		if (committerName == null) {
88  			committerName = getDefaultUserName();
89  			isCommitterNameImplicit = true;
90  		}
91  		committerEmail = getEmailInternal(rc, Constants.GIT_COMMITTER_EMAIL_KEY);
92  		if (committerEmail == null) {
93  			committerEmail = getDefaultEmail();
94  			isCommitterEmailImplicit = true;
95  		}
96  	}
97  
98  	/**
99  	 * Get the author name as defined in the git variables and configurations.
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 	 * Get the committer name as defined in the git variables and
111 	 * configurations.
112 	 *
113 	 * @return the committer name as defined in the git variables and
114 	 *         configurations. If no name could be found, try to use the system
115 	 *         user name instead.
116 	 */
117 	public String getCommitterName() {
118 		return committerName;
119 	}
120 
121 	/**
122 	 * Get the author email as defined in git variables and configurations.
123 	 *
124 	 * @return the author email as defined in git variables and configurations.
125 	 *         If no email could be found, try to propose one default with the
126 	 *         user name and the host name.
127 	 */
128 	public String getAuthorEmail() {
129 		return authorEmail;
130 	}
131 
132 	/**
133 	 * Get the committer email as defined in git variables and configurations.
134 	 *
135 	 * @return the committer email as defined in git variables and
136 	 *         configurations. If no email could be found, try to propose one
137 	 *         default with the user name and the host name.
138 	 */
139 	public String getCommitterEmail() {
140 		return committerEmail;
141 	}
142 
143 	/**
144 	 * Whether the author name was not explicitly configured but constructed
145 	 * from information the system has about the logged on user
146 	 *
147 	 * @return true if the author name was not explicitly configured but
148 	 *         constructed from information the system has about the logged on
149 	 *         user
150 	 */
151 	public boolean isAuthorNameImplicit() {
152 		return isAuthorNameImplicit;
153 	}
154 
155 	/**
156 	 * Whether the author email was not explicitly configured but constructed
157 	 * from information the system has about the logged on user
158 	 *
159 	 * @return true if the author email was not explicitly configured but
160 	 *         constructed from information the system has about the logged on
161 	 *         user
162 	 */
163 	public boolean isAuthorEmailImplicit() {
164 		return isAuthorEmailImplicit;
165 	}
166 
167 	/**
168 	 * Whether the committer name was not explicitly configured but constructed
169 	 * from information the system has about the logged on user
170 	 *
171 	 * @return true if the committer name was not explicitly configured but
172 	 *         constructed from information the system has about the logged on
173 	 *         user
174 	 */
175 	public boolean isCommitterNameImplicit() {
176 		return isCommitterNameImplicit;
177 	}
178 
179 	/**
180 	 * Whether the author email was not explicitly configured but constructed
181 	 * from information the system has about the logged on user
182 	 *
183 	 * @return true if the author email was not explicitly configured but
184 	 *         constructed from information the system has about the logged on
185 	 *         user
186 	 */
187 	public boolean isCommitterEmailImplicit() {
188 		return isCommitterEmailImplicit;
189 	}
190 
191 	private static String getNameInternal(Config rc, String envKey) {
192 		// try to get the user name for the system property GIT_XXX_NAME
193 		String username = system().getenv(envKey);
194 
195 		if (username == null) {
196 			// try to get the user name from the local and global
197 			// configurations.
198 			username = rc.getString("user", null, "name"); //$NON-NLS-1$ //$NON-NLS-2$
199 		}
200 
201 		return stripInvalidCharacters(username);
202 	}
203 
204 	/**
205 	 * @return try to get user name of the logged on user from the operating
206 	 *         system
207 	 */
208 	private static String getDefaultUserName() {
209 		// get the system user name
210 		String username = system().getProperty(Constants.OS_USER_NAME_KEY);
211 		if (username == null)
212 			username = Constants.UNKNOWN_USER_DEFAULT;
213 		return username;
214 	}
215 
216 	private static String getEmailInternal(Config rc, String envKey) {
217 		// try to get the email for the system property GIT_XXX_EMAIL
218 		String email = system().getenv(envKey);
219 
220 		if (email == null) {
221 			// try to get the email from the local and global configurations.
222 			email = rc.getString("user", null, "email"); //$NON-NLS-1$ //$NON-NLS-2$
223 		}
224 
225 		return stripInvalidCharacters(email);
226 	}
227 
228 	private static String stripInvalidCharacters(String s) {
229 		return s == null ? null : s.replaceAll("<|>|\n", ""); //$NON-NLS-1$//$NON-NLS-2$
230 	}
231 
232 	/**
233 	 * @return try to construct email for logged on user using system
234 	 *         information
235 	 */
236 	private static String getDefaultEmail() {
237 		// try to construct an email
238 		String username = getDefaultUserName();
239 		return username + "@" + system().getHostname(); //$NON-NLS-1$
240 	}
241 
242 	private static SystemReader system() {
243 		return SystemReader.getInstance();
244 	}
245 }