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> and others
5    *
6    * This program and the accompanying materials are made available under the
7    * terms of the Eclipse Distribution License v. 1.0 which is available at
8    * https://www.eclipse.org/org/documents/edl-v10.php.
9    *
10   * SPDX-License-Identifier: BSD-3-Clause
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   * The standard "user" configuration parameters.
20   */
21  public class UserConfig {
22  	/** Key for {@link Config#get(SectionParser)}. */
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  	 * Get the author name as defined in the git variables and configurations.
67  	 *
68  	 * @return the author name as defined in the git variables and
69  	 *         configurations. If no name could be found, try to use the system
70  	 *         user name instead.
71  	 */
72  	public String getAuthorName() {
73  		return authorName;
74  	}
75  
76  	/**
77  	 * Get the committer name as defined in the git variables and
78  	 * configurations.
79  	 *
80  	 * @return the committer name as defined in the git variables and
81  	 *         configurations. If no name could be found, try to use the system
82  	 *         user name instead.
83  	 */
84  	public String getCommitterName() {
85  		return committerName;
86  	}
87  
88  	/**
89  	 * Get the author email as defined in git variables and configurations.
90  	 *
91  	 * @return the author email as defined in git variables and configurations.
92  	 *         If no email could be found, try to propose one default with the
93  	 *         user name and the host name.
94  	 */
95  	public String getAuthorEmail() {
96  		return authorEmail;
97  	}
98  
99  	/**
100 	 * Get the committer email as defined in git variables and configurations.
101 	 *
102 	 * @return the committer email as defined in git variables and
103 	 *         configurations. If no email could be found, try to propose one
104 	 *         default with the user name and the host name.
105 	 */
106 	public String getCommitterEmail() {
107 		return committerEmail;
108 	}
109 
110 	/**
111 	 * Whether the author name was not explicitly configured but constructed
112 	 * from information the system has about the logged on user
113 	 *
114 	 * @return true if the author name was not explicitly configured but
115 	 *         constructed from information the system has about the logged on
116 	 *         user
117 	 */
118 	public boolean isAuthorNameImplicit() {
119 		return isAuthorNameImplicit;
120 	}
121 
122 	/**
123 	 * Whether the author email was not explicitly configured but constructed
124 	 * from information the system has about the logged on user
125 	 *
126 	 * @return true if the author email was not explicitly configured but
127 	 *         constructed from information the system has about the logged on
128 	 *         user
129 	 */
130 	public boolean isAuthorEmailImplicit() {
131 		return isAuthorEmailImplicit;
132 	}
133 
134 	/**
135 	 * Whether the committer name was not explicitly configured but constructed
136 	 * from information the system has about the logged on user
137 	 *
138 	 * @return true if the committer name was not explicitly configured but
139 	 *         constructed from information the system has about the logged on
140 	 *         user
141 	 */
142 	public boolean isCommitterNameImplicit() {
143 		return isCommitterNameImplicit;
144 	}
145 
146 	/**
147 	 * Whether the author email was not explicitly configured but constructed
148 	 * from information the system has about the logged on user
149 	 *
150 	 * @return true if the author email was not explicitly configured but
151 	 *         constructed from information the system has about the logged on
152 	 *         user
153 	 */
154 	public boolean isCommitterEmailImplicit() {
155 		return isCommitterEmailImplicit;
156 	}
157 
158 	private static String getNameInternal(Config rc, String envKey) {
159 		// try to get the user name for the system property GIT_XXX_NAME
160 		String username = system().getenv(envKey);
161 
162 		if (username == null) {
163 			// try to get the user name from the local and global
164 			// configurations.
165 			username = rc.getString("user", null, "name"); //$NON-NLS-1$ //$NON-NLS-2$
166 		}
167 
168 		return stripInvalidCharacters(username);
169 	}
170 
171 	/**
172 	 * @return try to get user name of the logged on user from the operating
173 	 *         system
174 	 */
175 	private static String getDefaultUserName() {
176 		// get the system user name
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 		// try to get the email for the system property GIT_XXX_EMAIL
185 		String email = system().getenv(envKey);
186 
187 		if (email == null) {
188 			// try to get the email from the local and global configurations.
189 			email = rc.getString("user", null, "email"); //$NON-NLS-1$ //$NON-NLS-2$
190 		}
191 
192 		return stripInvalidCharacters(email);
193 	}
194 
195 	private static String stripInvalidCharacters(String s) {
196 		return s == null ? null : s.replaceAll("<|>|\n", ""); //$NON-NLS-1$//$NON-NLS-2$
197 	}
198 
199 	/**
200 	 * @return try to construct email for logged on user using system
201 	 *         information
202 	 */
203 	private static String getDefaultEmail() {
204 		// try to construct an email
205 		String username = getDefaultUserName();
206 		return username + "@" + system().getHostname(); //$NON-NLS-1$
207 	}
208 
209 	private static SystemReader system() {
210 		return SystemReader.getInstance();
211 	}
212 }