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

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