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.

PersonIdent.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. final UserConfig config = repo.getConfig().get(UserConfig.KEY);
  73. name = config.getCommitterName();
  74. emailAddress = config.getCommitterEmail();
  75. when = SystemReader.getInstance().getCurrentTime();
  76. tzOffset = SystemReader.getInstance().getTimezone(when);
  77. }
  78. /**
  79. * Copy a {@link PersonIdent}.
  80. *
  81. * @param pi
  82. * Original {@link PersonIdent}
  83. */
  84. public PersonIdent(final PersonIdent pi) {
  85. this(pi.getName(), pi.getEmailAddress());
  86. }
  87. /**
  88. * Construct a new {@link PersonIdent} with current time.
  89. *
  90. * @param aName
  91. * @param aEmailAddress
  92. */
  93. public PersonIdent(final String aName, final String aEmailAddress) {
  94. name = aName;
  95. emailAddress = aEmailAddress;
  96. when = SystemReader.getInstance().getCurrentTime();
  97. tzOffset = SystemReader.getInstance().getTimezone(when);
  98. }
  99. /**
  100. * Copy a PersonIdent, but alter the clone's time stamp
  101. *
  102. * @param pi
  103. * original {@link PersonIdent}
  104. * @param when
  105. * local time
  106. * @param tz
  107. * time zone
  108. */
  109. public PersonIdent(final PersonIdent pi, final Date when, final TimeZone tz) {
  110. this(pi.getName(), pi.getEmailAddress(), when, tz);
  111. }
  112. /**
  113. * Copy a {@link PersonIdent}, but alter the clone's time stamp
  114. *
  115. * @param pi
  116. * original {@link PersonIdent}
  117. * @param aWhen
  118. * local time
  119. */
  120. public PersonIdent(final PersonIdent pi, final Date aWhen) {
  121. name = pi.getName();
  122. emailAddress = pi.getEmailAddress();
  123. when = aWhen.getTime();
  124. tzOffset = pi.tzOffset;
  125. }
  126. /**
  127. * Construct a PersonIdent from simple data
  128. *
  129. * @param aName
  130. * @param aEmailAddress
  131. * @param aWhen
  132. * local time stamp
  133. * @param aTZ
  134. * time zone
  135. */
  136. public PersonIdent(final String aName, final String aEmailAddress,
  137. final Date aWhen, final TimeZone aTZ) {
  138. name = aName;
  139. emailAddress = aEmailAddress;
  140. when = aWhen.getTime();
  141. tzOffset = aTZ.getOffset(when) / (60 * 1000);
  142. }
  143. /**
  144. * Construct a {@link PersonIdent}
  145. *
  146. * @param aName
  147. * @param aEmailAddress
  148. * @param aWhen
  149. * local time stamp
  150. * @param aTZ
  151. * time zone
  152. */
  153. public PersonIdent(final String aName, final String aEmailAddress,
  154. final long aWhen, final int aTZ) {
  155. name = aName;
  156. emailAddress = aEmailAddress;
  157. when = aWhen;
  158. tzOffset = aTZ;
  159. }
  160. /**
  161. * Copy a PersonIdent, but alter the clone's time stamp
  162. *
  163. * @param pi
  164. * original {@link PersonIdent}
  165. * @param aWhen
  166. * local time stamp
  167. * @param aTZ
  168. * time zone
  169. */
  170. public PersonIdent(final PersonIdent pi, final long aWhen, final int aTZ) {
  171. name = pi.getName();
  172. emailAddress = pi.getEmailAddress();
  173. when = aWhen;
  174. tzOffset = aTZ;
  175. }
  176. /**
  177. * @return Name of person
  178. */
  179. public String getName() {
  180. return name;
  181. }
  182. /**
  183. * @return email address of person
  184. */
  185. public String getEmailAddress() {
  186. return emailAddress;
  187. }
  188. /**
  189. * @return timestamp
  190. */
  191. public Date getWhen() {
  192. return new Date(when);
  193. }
  194. /**
  195. * @return this person's declared time zone; null if time zone is unknown.
  196. */
  197. public TimeZone getTimeZone() {
  198. StringBuilder tzId = new StringBuilder(8);
  199. tzId.append("GMT");
  200. appendTimezone(tzId);
  201. return TimeZone.getTimeZone(tzId.toString());
  202. }
  203. /**
  204. * @return this person's declared time zone as minutes east of UTC. If the
  205. * timezone is to the west of UTC it is negative.
  206. */
  207. public int getTimeZoneOffset() {
  208. return tzOffset;
  209. }
  210. public int hashCode() {
  211. int hc = getEmailAddress().hashCode();
  212. hc *= 31;
  213. hc += (int) (when / 1000L);
  214. return hc;
  215. }
  216. public boolean equals(final Object o) {
  217. if (o instanceof PersonIdent) {
  218. final PersonIdent p = (PersonIdent) o;
  219. return getName().equals(p.getName())
  220. && getEmailAddress().equals(p.getEmailAddress())
  221. && when / 1000L == p.when / 1000L;
  222. }
  223. return false;
  224. }
  225. /**
  226. * Format for Git storage.
  227. *
  228. * @return a string in the git author format
  229. */
  230. public String toExternalString() {
  231. final StringBuilder r = new StringBuilder();
  232. r.append(getName());
  233. r.append(" <");
  234. r.append(getEmailAddress());
  235. r.append("> ");
  236. r.append(when / 1000);
  237. r.append(' ');
  238. appendTimezone(r);
  239. return r.toString();
  240. }
  241. private void appendTimezone(final StringBuilder r) {
  242. int offset = tzOffset;
  243. final char sign;
  244. final int offsetHours;
  245. final int offsetMins;
  246. if (offset < 0) {
  247. sign = '-';
  248. offset = -offset;
  249. } else {
  250. sign = '+';
  251. }
  252. offsetHours = offset / 60;
  253. offsetMins = offset % 60;
  254. r.append(sign);
  255. if (offsetHours < 10) {
  256. r.append('0');
  257. }
  258. r.append(offsetHours);
  259. if (offsetMins < 10) {
  260. r.append('0');
  261. }
  262. r.append(offsetMins);
  263. }
  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. }