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.1KB

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