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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. package org.eclipse.jgit.lib;
  13. import org.eclipse.jgit.lib.Config.SectionParser;
  14. import org.eclipse.jgit.util.SystemReader;
  15. /**
  16. * The standard "user" configuration parameters.
  17. */
  18. public class UserConfig {
  19. /** Key for {@link Config#get(SectionParser)}. */
  20. public static final Config.SectionParser<UserConfig> KEY = UserConfig::new;
  21. private String authorName;
  22. private String authorEmail;
  23. private String committerName;
  24. private String committerEmail;
  25. private boolean isAuthorNameImplicit;
  26. private boolean isAuthorEmailImplicit;
  27. private boolean isCommitterNameImplicit;
  28. private boolean isCommitterEmailImplicit;
  29. private UserConfig(Config rc) {
  30. authorName = getNameInternal(rc, Constants.GIT_AUTHOR_NAME_KEY);
  31. if (authorName == null) {
  32. authorName = getDefaultUserName();
  33. isAuthorNameImplicit = true;
  34. }
  35. authorEmail = getEmailInternal(rc, Constants.GIT_AUTHOR_EMAIL_KEY);
  36. if (authorEmail == null) {
  37. authorEmail = getDefaultEmail();
  38. isAuthorEmailImplicit = true;
  39. }
  40. committerName = getNameInternal(rc, Constants.GIT_COMMITTER_NAME_KEY);
  41. if (committerName == null) {
  42. committerName = getDefaultUserName();
  43. isCommitterNameImplicit = true;
  44. }
  45. committerEmail = getEmailInternal(rc, Constants.GIT_COMMITTER_EMAIL_KEY);
  46. if (committerEmail == null) {
  47. committerEmail = getDefaultEmail();
  48. isCommitterEmailImplicit = true;
  49. }
  50. }
  51. /**
  52. * Get the author name as defined in the git variables and configurations.
  53. *
  54. * @return the author name as defined in the git variables and
  55. * configurations. If no name could be found, try to use the system
  56. * user name instead.
  57. */
  58. public String getAuthorName() {
  59. return authorName;
  60. }
  61. /**
  62. * Get the committer name as defined in the git variables and
  63. * configurations.
  64. *
  65. * @return the committer name as defined in the git variables and
  66. * configurations. If no name could be found, try to use the system
  67. * user name instead.
  68. */
  69. public String getCommitterName() {
  70. return committerName;
  71. }
  72. /**
  73. * Get the author email as defined in git variables and configurations.
  74. *
  75. * @return the author email as defined in git variables and configurations.
  76. * If no email could be found, try to propose one default with the
  77. * user name and the host name.
  78. */
  79. public String getAuthorEmail() {
  80. return authorEmail;
  81. }
  82. /**
  83. * Get the committer email as defined in git variables and configurations.
  84. *
  85. * @return the committer email as defined in git variables and
  86. * configurations. If no email could be found, try to propose one
  87. * default with the user name and the host name.
  88. */
  89. public String getCommitterEmail() {
  90. return committerEmail;
  91. }
  92. /**
  93. * Whether the author name was not explicitly configured but constructed
  94. * from information the system has about the logged on user
  95. *
  96. * @return true if the author name was not explicitly configured but
  97. * constructed from information the system has about the logged on
  98. * user
  99. */
  100. public boolean isAuthorNameImplicit() {
  101. return isAuthorNameImplicit;
  102. }
  103. /**
  104. * Whether the author email was not explicitly configured but constructed
  105. * from information the system has about the logged on user
  106. *
  107. * @return true if the author email was not explicitly configured but
  108. * constructed from information the system has about the logged on
  109. * user
  110. */
  111. public boolean isAuthorEmailImplicit() {
  112. return isAuthorEmailImplicit;
  113. }
  114. /**
  115. * Whether the committer name was not explicitly configured but constructed
  116. * from information the system has about the logged on user
  117. *
  118. * @return true if the committer name was not explicitly configured but
  119. * constructed from information the system has about the logged on
  120. * user
  121. */
  122. public boolean isCommitterNameImplicit() {
  123. return isCommitterNameImplicit;
  124. }
  125. /**
  126. * Whether the author email was not explicitly configured but constructed
  127. * from information the system has about the logged on user
  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 isCommitterEmailImplicit() {
  134. return isCommitterEmailImplicit;
  135. }
  136. private static String getNameInternal(Config rc, String envKey) {
  137. // try to get the user name for the system property GIT_XXX_NAME
  138. String username = system().getenv(envKey);
  139. if (username == null) {
  140. // try to get the user name from the local and global
  141. // configurations.
  142. username = rc.getString("user", null, "name"); //$NON-NLS-1$ //$NON-NLS-2$
  143. }
  144. return stripInvalidCharacters(username);
  145. }
  146. /**
  147. * @return try to get user name of the logged on user from the operating
  148. * system
  149. */
  150. private static String getDefaultUserName() {
  151. // get the system user name
  152. String username = system().getProperty(Constants.OS_USER_NAME_KEY);
  153. if (username == null)
  154. username = Constants.UNKNOWN_USER_DEFAULT;
  155. return username;
  156. }
  157. private static String getEmailInternal(Config rc, String envKey) {
  158. // try to get the email for the system property GIT_XXX_EMAIL
  159. String email = system().getenv(envKey);
  160. if (email == null) {
  161. // try to get the email from the local and global configurations.
  162. email = rc.getString("user", null, "email"); //$NON-NLS-1$ //$NON-NLS-2$
  163. }
  164. return stripInvalidCharacters(email);
  165. }
  166. private static String stripInvalidCharacters(String s) {
  167. return s == null ? null : s.replaceAll("<|>|\n", ""); //$NON-NLS-1$//$NON-NLS-2$
  168. }
  169. /**
  170. * @return try to construct email for logged on user using system
  171. * information
  172. */
  173. private static String getDefaultEmail() {
  174. // try to construct an email
  175. String username = getDefaultUserName();
  176. return username + "@" + system().getHostname(); //$NON-NLS-1$
  177. }
  178. private static SystemReader system() {
  179. return SystemReader.getInstance();
  180. }
  181. }