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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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.internal.JGitText;
  52. import org.eclipse.jgit.util.SystemReader;
  53. import org.eclipse.jgit.util.time.ProposedTimestamp;
  54. /**
  55. * A combination of a person identity and time in Git.
  56. *
  57. * Git combines Name + email + time + time zone to specify who wrote or
  58. * committed something.
  59. */
  60. public class PersonIdent implements Serializable {
  61. private static final long serialVersionUID = 1L;
  62. /**
  63. * @param tzOffset
  64. * timezone offset as in {@link #getTimeZoneOffset()}.
  65. * @return time zone object for the given offset.
  66. * @since 4.1
  67. */
  68. public static TimeZone getTimeZone(int tzOffset) {
  69. StringBuilder tzId = new StringBuilder(8);
  70. tzId.append("GMT"); //$NON-NLS-1$
  71. appendTimezone(tzId, tzOffset);
  72. return TimeZone.getTimeZone(tzId.toString());
  73. }
  74. /**
  75. * Format a timezone offset.
  76. *
  77. * @param r
  78. * string builder to append to.
  79. * @param offset
  80. * timezone offset as in {@link #getTimeZoneOffset()}.
  81. * @since 4.1
  82. */
  83. public static void appendTimezone(StringBuilder r, int offset) {
  84. final char sign;
  85. final int offsetHours;
  86. final int offsetMins;
  87. if (offset < 0) {
  88. sign = '-';
  89. offset = -offset;
  90. } else {
  91. sign = '+';
  92. }
  93. offsetHours = offset / 60;
  94. offsetMins = offset % 60;
  95. r.append(sign);
  96. if (offsetHours < 10) {
  97. r.append('0');
  98. }
  99. r.append(offsetHours);
  100. if (offsetMins < 10) {
  101. r.append('0');
  102. }
  103. r.append(offsetMins);
  104. }
  105. /**
  106. * Sanitize the given string for use in an identity and append to output.
  107. * <p>
  108. * Trims whitespace from both ends and special characters {@code \n < >} that
  109. * interfere with parsing; appends all other characters to the output.
  110. * Analogous to the C git function {@code strbuf_addstr_without_crud}.
  111. *
  112. * @param r
  113. * string builder to append to.
  114. * @param str
  115. * input string.
  116. * @since 4.4
  117. */
  118. public static void appendSanitized(StringBuilder r, String str) {
  119. // Trim any whitespace less than \u0020 as in String#trim().
  120. int i = 0;
  121. while (i < str.length() && str.charAt(i) <= ' ') {
  122. i++;
  123. }
  124. int end = str.length();
  125. while (end > i && str.charAt(end - 1) <= ' ') {
  126. end--;
  127. }
  128. for (; i < end; i++) {
  129. char c = str.charAt(i);
  130. switch (c) {
  131. case '\n':
  132. case '<':
  133. case '>':
  134. continue;
  135. default:
  136. r.append(c);
  137. break;
  138. }
  139. }
  140. }
  141. private final String name;
  142. private final String emailAddress;
  143. private final long when;
  144. private final int tzOffset;
  145. /**
  146. * Creates new PersonIdent from config info in repository, with current time.
  147. * This new PersonIdent gets the info from the default committer as available
  148. * from the configuration.
  149. *
  150. * @param repo
  151. */
  152. public PersonIdent(final Repository repo) {
  153. this(repo.getConfig().get(UserConfig.KEY));
  154. }
  155. /**
  156. * Copy a {@link PersonIdent}.
  157. *
  158. * @param pi
  159. * Original {@link PersonIdent}
  160. */
  161. public PersonIdent(final PersonIdent pi) {
  162. this(pi.getName(), pi.getEmailAddress());
  163. }
  164. /**
  165. * Construct a new {@link PersonIdent} with current time.
  166. *
  167. * @param aName
  168. * @param aEmailAddress
  169. */
  170. public PersonIdent(final String aName, final String aEmailAddress) {
  171. this(aName, aEmailAddress, SystemReader.getInstance().getCurrentTime());
  172. }
  173. /**
  174. * Construct a new {@link PersonIdent} with current time.
  175. *
  176. * @param aName
  177. * @param aEmailAddress
  178. * @param when
  179. */
  180. public PersonIdent(String aName, String aEmailAddress,
  181. ProposedTimestamp when) {
  182. this(aName, aEmailAddress, when.millis());
  183. }
  184. /**
  185. * Copy a PersonIdent, but alter the clone's time stamp
  186. *
  187. * @param pi
  188. * original {@link PersonIdent}
  189. * @param when
  190. * local time
  191. * @param tz
  192. * time zone
  193. */
  194. public PersonIdent(final PersonIdent pi, final Date when, final TimeZone tz) {
  195. this(pi.getName(), pi.getEmailAddress(), when, tz);
  196. }
  197. /**
  198. * Copy a {@link PersonIdent}, but alter the clone's time stamp
  199. *
  200. * @param pi
  201. * original {@link PersonIdent}
  202. * @param aWhen
  203. * local time
  204. */
  205. public PersonIdent(final PersonIdent pi, final Date aWhen) {
  206. this(pi.getName(), pi.getEmailAddress(), aWhen.getTime(), pi.tzOffset);
  207. }
  208. /**
  209. * Construct a PersonIdent from simple data
  210. *
  211. * @param aName
  212. * @param aEmailAddress
  213. * @param aWhen
  214. * local time stamp
  215. * @param aTZ
  216. * time zone
  217. */
  218. public PersonIdent(final String aName, final String aEmailAddress,
  219. final Date aWhen, final TimeZone aTZ) {
  220. this(aName, aEmailAddress, aWhen.getTime(), aTZ.getOffset(aWhen
  221. .getTime()) / (60 * 1000));
  222. }
  223. /**
  224. * Copy a PersonIdent, but alter the clone's time stamp
  225. *
  226. * @param pi
  227. * original {@link PersonIdent}
  228. * @param aWhen
  229. * local time stamp
  230. * @param aTZ
  231. * time zone
  232. */
  233. public PersonIdent(final PersonIdent pi, final long aWhen, final int aTZ) {
  234. this(pi.getName(), pi.getEmailAddress(), aWhen, aTZ);
  235. }
  236. private PersonIdent(final String aName, final String aEmailAddress,
  237. long when) {
  238. this(aName, aEmailAddress, when, SystemReader.getInstance()
  239. .getTimezone(when));
  240. }
  241. private PersonIdent(final UserConfig config) {
  242. this(config.getCommitterName(), config.getCommitterEmail());
  243. }
  244. /**
  245. * Construct a {@link PersonIdent}.
  246. * <p>
  247. * Whitespace in the name and email is preserved for the lifetime of this
  248. * object, but are trimmed by {@link #toExternalString()}. This means that
  249. * parsing the result of {@link #toExternalString()} may not return an
  250. * equivalent instance.
  251. *
  252. * @param aName
  253. * @param aEmailAddress
  254. * @param aWhen
  255. * local time stamp
  256. * @param aTZ
  257. * time zone
  258. */
  259. public PersonIdent(final String aName, final String aEmailAddress,
  260. final long aWhen, final int aTZ) {
  261. if (aName == null)
  262. throw new IllegalArgumentException(
  263. JGitText.get().personIdentNameNonNull);
  264. if (aEmailAddress == null)
  265. throw new IllegalArgumentException(
  266. JGitText.get().personIdentEmailNonNull);
  267. name = aName;
  268. emailAddress = aEmailAddress;
  269. when = aWhen;
  270. tzOffset = aTZ;
  271. }
  272. /**
  273. * @return Name of person
  274. */
  275. public String getName() {
  276. return name;
  277. }
  278. /**
  279. * @return email address of person
  280. */
  281. public String getEmailAddress() {
  282. return emailAddress;
  283. }
  284. /**
  285. * @return timestamp
  286. */
  287. public Date getWhen() {
  288. return new Date(when);
  289. }
  290. /**
  291. * @return this person's declared time zone; null if time zone is unknown.
  292. */
  293. public TimeZone getTimeZone() {
  294. return getTimeZone(tzOffset);
  295. }
  296. /**
  297. * @return this person's declared time zone as minutes east of UTC. If the
  298. * timezone is to the west of UTC it is negative.
  299. */
  300. public int getTimeZoneOffset() {
  301. return tzOffset;
  302. }
  303. /**
  304. * Hashcode is based only on the email address and timestamp.
  305. */
  306. public int hashCode() {
  307. int hc = getEmailAddress().hashCode();
  308. hc *= 31;
  309. hc += (int) (when / 1000L);
  310. return hc;
  311. }
  312. public boolean equals(final Object o) {
  313. if (o instanceof PersonIdent) {
  314. final PersonIdent p = (PersonIdent) o;
  315. return getName().equals(p.getName())
  316. && getEmailAddress().equals(p.getEmailAddress())
  317. && when / 1000L == p.when / 1000L;
  318. }
  319. return false;
  320. }
  321. /**
  322. * Format for Git storage.
  323. *
  324. * @return a string in the git author format
  325. */
  326. public String toExternalString() {
  327. final StringBuilder r = new StringBuilder();
  328. appendSanitized(r, getName());
  329. r.append(" <"); //$NON-NLS-1$
  330. appendSanitized(r, getEmailAddress());
  331. r.append("> "); //$NON-NLS-1$
  332. r.append(when / 1000);
  333. r.append(' ');
  334. appendTimezone(r, tzOffset);
  335. return r.toString();
  336. }
  337. @SuppressWarnings("nls")
  338. public String toString() {
  339. final StringBuilder r = new StringBuilder();
  340. final SimpleDateFormat dtfmt;
  341. dtfmt = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy Z", Locale.US);
  342. dtfmt.setTimeZone(getTimeZone());
  343. r.append("PersonIdent[");
  344. r.append(getName());
  345. r.append(", ");
  346. r.append(getEmailAddress());
  347. r.append(", ");
  348. r.append(dtfmt.format(Long.valueOf(when)));
  349. r.append("]");
  350. return r.toString();
  351. }
  352. }