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

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