You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserConfig.java 7.8KB

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