選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

UserConfig.java 7.0KB

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