Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  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 java.io.Serializable;
  47. import java.text.SimpleDateFormat;
  48. import java.util.Date;
  49. import java.util.Locale;
  50. import java.util.TimeZone;
  51. import org.eclipse.jgit.util.SystemReader;
  52. /**
  53. * A combination of a person identity and time in Git.
  54. *
  55. * Git combines Name + email + time + time zone to specify who wrote or
  56. * committed something.
  57. */
  58. public class PersonIdent implements Serializable {
  59. private static final long serialVersionUID = 1L;
  60. private final String name;
  61. private final String emailAddress;
  62. private final long when;
  63. private final int tzOffset;
  64. /**
  65. * Creates new PersonIdent from config info in repository, with current time.
  66. * This new PersonIdent gets the info from the default committer as available
  67. * from the configuration.
  68. *
  69. * @param repo
  70. */
  71. public PersonIdent(final Repository repo) {
  72. this(repo.getConfig().get(UserConfig.KEY));
  73. }
  74. /**
  75. * Copy a {@link PersonIdent}.
  76. *
  77. * @param pi
  78. * Original {@link PersonIdent}
  79. */
  80. public PersonIdent(final PersonIdent pi) {
  81. this(pi.getName(), pi.getEmailAddress());
  82. }
  83. /**
  84. * Construct a new {@link PersonIdent} with current time.
  85. *
  86. * @param aName
  87. * @param aEmailAddress
  88. */
  89. public PersonIdent(final String aName, final String aEmailAddress) {
  90. this(aName, aEmailAddress, SystemReader.getInstance().getCurrentTime());
  91. }
  92. /**
  93. * Copy a PersonIdent, but alter the clone's time stamp
  94. *
  95. * @param pi
  96. * original {@link PersonIdent}
  97. * @param when
  98. * local time
  99. * @param tz
  100. * time zone
  101. */
  102. public PersonIdent(final PersonIdent pi, final Date when, final TimeZone tz) {
  103. this(pi.getName(), pi.getEmailAddress(), when, tz);
  104. }
  105. /**
  106. * Copy a {@link PersonIdent}, but alter the clone's time stamp
  107. *
  108. * @param pi
  109. * original {@link PersonIdent}
  110. * @param aWhen
  111. * local time
  112. */
  113. public PersonIdent(final PersonIdent pi, final Date aWhen) {
  114. this(pi.getName(), pi.getEmailAddress(), aWhen.getTime(), pi.tzOffset);
  115. }
  116. /**
  117. * Construct a PersonIdent from simple data
  118. *
  119. * @param aName
  120. * @param aEmailAddress
  121. * @param aWhen
  122. * local time stamp
  123. * @param aTZ
  124. * time zone
  125. */
  126. public PersonIdent(final String aName, final String aEmailAddress,
  127. final Date aWhen, final TimeZone aTZ) {
  128. this(aName, aEmailAddress, aWhen.getTime(), aTZ.getOffset(aWhen
  129. .getTime()) / (60 * 1000));
  130. }
  131. /**
  132. * Copy a PersonIdent, but alter the clone's time stamp
  133. *
  134. * @param pi
  135. * original {@link PersonIdent}
  136. * @param aWhen
  137. * local time stamp
  138. * @param aTZ
  139. * time zone
  140. */
  141. public PersonIdent(final PersonIdent pi, final long aWhen, final int aTZ) {
  142. this(pi.getName(), pi.getEmailAddress(), aWhen, aTZ);
  143. }
  144. private PersonIdent(final String aName, final String aEmailAddress,
  145. long when) {
  146. this(aName, aEmailAddress, when, SystemReader.getInstance()
  147. .getTimezone(when));
  148. }
  149. private PersonIdent(final UserConfig config) {
  150. this(config.getCommitterName(), config.getCommitterEmail());
  151. }
  152. /**
  153. * Construct a {@link PersonIdent}
  154. *
  155. * @param aName
  156. * @param aEmailAddress
  157. * @param aWhen
  158. * local time stamp
  159. * @param aTZ
  160. * time zone
  161. */
  162. public PersonIdent(final String aName, final String aEmailAddress,
  163. final long aWhen, final int aTZ) {
  164. if (aName == null)
  165. throw new IllegalArgumentException(
  166. "Name of PersonIdent must not be null.");
  167. if (aEmailAddress == null)
  168. throw new IllegalArgumentException(
  169. "E-mail address of PersonIdent must not be null.");
  170. name = aName;
  171. emailAddress = aEmailAddress;
  172. when = aWhen;
  173. tzOffset = aTZ;
  174. }
  175. /**
  176. * @return Name of person
  177. */
  178. public String getName() {
  179. return name;
  180. }
  181. /**
  182. * @return email address of person
  183. */
  184. public String getEmailAddress() {
  185. return emailAddress;
  186. }
  187. /**
  188. * @return timestamp
  189. */
  190. public Date getWhen() {
  191. return new Date(when);
  192. }
  193. /**
  194. * @return this person's declared time zone; null if time zone is unknown.
  195. */
  196. public TimeZone getTimeZone() {
  197. StringBuilder tzId = new StringBuilder(8);
  198. tzId.append("GMT"); //$NON-NLS-1$
  199. appendTimezone(tzId);
  200. return TimeZone.getTimeZone(tzId.toString());
  201. }
  202. /**
  203. * @return this person's declared time zone as minutes east of UTC. If the
  204. * timezone is to the west of UTC it is negative.
  205. */
  206. public int getTimeZoneOffset() {
  207. return tzOffset;
  208. }
  209. public int hashCode() {
  210. int hc = getEmailAddress().hashCode();
  211. hc *= 31;
  212. hc += (int) (when / 1000L);
  213. return hc;
  214. }
  215. public boolean equals(final Object o) {
  216. if (o instanceof PersonIdent) {
  217. final PersonIdent p = (PersonIdent) o;
  218. return getName().equals(p.getName())
  219. && getEmailAddress().equals(p.getEmailAddress())
  220. && when / 1000L == p.when / 1000L;
  221. }
  222. return false;
  223. }
  224. /**
  225. * Format for Git storage.
  226. *
  227. * @return a string in the git author format
  228. */
  229. public String toExternalString() {
  230. final StringBuilder r = new StringBuilder();
  231. r.append(getName().trim());
  232. r.append(" <"); //$NON-NLS-1$
  233. r.append(getEmailAddress().trim());
  234. r.append("> "); //$NON-NLS-1$
  235. r.append(when / 1000);
  236. r.append(' ');
  237. appendTimezone(r);
  238. return r.toString();
  239. }
  240. private void appendTimezone(final StringBuilder r) {
  241. int offset = tzOffset;
  242. final char sign;
  243. final int offsetHours;
  244. final int offsetMins;
  245. if (offset < 0) {
  246. sign = '-';
  247. offset = -offset;
  248. } else {
  249. sign = '+';
  250. }
  251. offsetHours = offset / 60;
  252. offsetMins = offset % 60;
  253. r.append(sign);
  254. if (offsetHours < 10) {
  255. r.append('0');
  256. }
  257. r.append(offsetHours);
  258. if (offsetMins < 10) {
  259. r.append('0');
  260. }
  261. r.append(offsetMins);
  262. }
  263. @SuppressWarnings("nls")
  264. public String toString() {
  265. final StringBuilder r = new StringBuilder();
  266. final SimpleDateFormat dtfmt;
  267. dtfmt = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy Z", Locale.US);
  268. dtfmt.setTimeZone(getTimeZone());
  269. r.append("PersonIdent[");
  270. r.append(getName());
  271. r.append(", ");
  272. r.append(getEmailAddress());
  273. r.append(", ");
  274. r.append(dtfmt.format(Long.valueOf(when)));
  275. r.append("]");
  276. return r.toString();
  277. }
  278. }