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.

IpLogGenerator.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.iplog;
  44. import java.io.BufferedReader;
  45. import java.io.File;
  46. import java.io.FileReader;
  47. import java.io.IOException;
  48. import java.io.OutputStream;
  49. import java.text.MessageFormat;
  50. import java.text.ParseException;
  51. import java.text.SimpleDateFormat;
  52. import java.util.ArrayList;
  53. import java.util.Collection;
  54. import java.util.Collections;
  55. import java.util.Comparator;
  56. import java.util.Date;
  57. import java.util.HashMap;
  58. import java.util.HashSet;
  59. import java.util.List;
  60. import java.util.Map;
  61. import java.util.Set;
  62. import java.util.TreeMap;
  63. import java.util.TreeSet;
  64. import javax.xml.parsers.DocumentBuilderFactory;
  65. import javax.xml.parsers.ParserConfigurationException;
  66. import javax.xml.transform.OutputKeys;
  67. import javax.xml.transform.Transformer;
  68. import javax.xml.transform.TransformerConfigurationException;
  69. import javax.xml.transform.TransformerException;
  70. import javax.xml.transform.TransformerFactory;
  71. import javax.xml.transform.dom.DOMSource;
  72. import javax.xml.transform.stream.StreamResult;
  73. import org.eclipse.jgit.diff.Edit;
  74. import org.eclipse.jgit.diff.EditList;
  75. import org.eclipse.jgit.diff.MyersDiff;
  76. import org.eclipse.jgit.diff.RawText;
  77. import org.eclipse.jgit.errors.ConfigInvalidException;
  78. import org.eclipse.jgit.errors.MissingObjectException;
  79. import org.eclipse.jgit.iplog.Committer.ActiveRange;
  80. import org.eclipse.jgit.lib.BlobBasedConfig;
  81. import org.eclipse.jgit.lib.Constants;
  82. import org.eclipse.jgit.lib.MutableObjectId;
  83. import org.eclipse.jgit.lib.ObjectLoader;
  84. import org.eclipse.jgit.lib.PersonIdent;
  85. import org.eclipse.jgit.lib.Repository;
  86. import org.eclipse.jgit.lib.WindowCursor;
  87. import org.eclipse.jgit.revwalk.FooterKey;
  88. import org.eclipse.jgit.revwalk.RevCommit;
  89. import org.eclipse.jgit.revwalk.RevTree;
  90. import org.eclipse.jgit.revwalk.RevWalk;
  91. import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
  92. import org.eclipse.jgit.treewalk.TreeWalk;
  93. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  94. import org.eclipse.jgit.util.RawParseUtils;
  95. import org.w3c.dom.Document;
  96. import org.w3c.dom.Element;
  97. /**
  98. * Creates an Eclipse IP log in XML format.
  99. *
  100. * @see <a href="http://www.eclipse.org/projects/xml/iplog.xsd">IP log XSD</a>
  101. */
  102. public class IpLogGenerator {
  103. private static final String IPLOG_NS = "http://www.eclipse.org/projects/xml/iplog";
  104. private static final String IPLOG_PFX = "iplog:";
  105. private static final String INDENT = "{http://xml.apache.org/xslt}indent-amount";
  106. private static final FooterKey BUG = new FooterKey("Bug");
  107. /** Projects indexed by their ID string, e.g. {@code technology.jgit}. */
  108. private final Map<String, Project> projects = new TreeMap<String, Project>();
  109. /** Known committers, indexed by their foundation ID. */
  110. private final Map<String, Committer> committersById = new HashMap<String, Committer>();
  111. /** Known committers, indexed by their email address. */
  112. private final Map<String, Committer> committersByEmail = new HashMap<String, Committer>();
  113. /** Discovered contributors. */
  114. private final Map<String, Contributor> contributorsByName = new HashMap<String, Contributor>();
  115. /** All known CQs matching the projects we care about. */
  116. private final Set<CQ> cqs = new HashSet<CQ>();
  117. /** Root commits which were scanned to gather project data. */
  118. private final Set<RevCommit> commits = new HashSet<RevCommit>();
  119. /** The meta file we loaded to bootstrap our definitions. */
  120. private IpLogMeta meta;
  121. private String characterEncoding = "UTF-8";
  122. private Repository db;
  123. private RevWalk rw;
  124. private NameConflictTreeWalk tw;
  125. private final WindowCursor curs = new WindowCursor();
  126. private final MutableObjectId idbuf = new MutableObjectId();
  127. private Document doc;
  128. /** Create an empty generator. */
  129. public IpLogGenerator() {
  130. // Do nothing.
  131. }
  132. /**
  133. * Set the character encoding used to write the output file.
  134. *
  135. * @param encodingName
  136. * the character set encoding name.
  137. */
  138. public void setCharacterEncoding(String encodingName) {
  139. characterEncoding = encodingName;
  140. }
  141. /**
  142. * Scan a Git repository's history to compute the changes within it.
  143. *
  144. * @param repo
  145. * the repository to scan.
  146. * @param startCommit
  147. * commit the IP log is needed for.
  148. * @param version
  149. * symbolic label for the version.
  150. * @throws IOException
  151. * the repository cannot be read.
  152. * @throws ConfigInvalidException
  153. * the {@code .eclipse_iplog} file present at the top level of
  154. * {@code startId} is not a valid configuration file.
  155. */
  156. public void scan(Repository repo, RevCommit startCommit, String version)
  157. throws IOException, ConfigInvalidException {
  158. try {
  159. db = repo;
  160. rw = new RevWalk(db);
  161. tw = new NameConflictTreeWalk(db);
  162. RevCommit c = rw.parseCommit(startCommit);
  163. loadEclipseIpLog(version, c);
  164. loadCommitters(repo);
  165. scanProjectCommits(meta.getProjects().get(0), c);
  166. commits.add(c);
  167. } finally {
  168. WindowCursor.release(curs);
  169. db = null;
  170. rw = null;
  171. tw = null;
  172. }
  173. }
  174. private void loadEclipseIpLog(String version, RevCommit commit)
  175. throws IOException, ConfigInvalidException {
  176. TreeWalk log = TreeWalk.forPath(db, IpLogMeta.IPLOG_CONFIG_FILE, commit
  177. .getTree());
  178. if (log == null)
  179. return;
  180. meta = new IpLogMeta();
  181. try {
  182. meta.loadFrom(new BlobBasedConfig(null, db, log.getObjectId(0)));
  183. } catch (ConfigInvalidException e) {
  184. throw new ConfigInvalidException(MessageFormat.format(IpLogText.get().configurationFileInCommitIsInvalid
  185. , log.getPathString(), commit.name()), e);
  186. }
  187. if (meta.getProjects().isEmpty()) {
  188. throw new ConfigInvalidException(MessageFormat.format(IpLogText.get().configurationFileInCommitHasNoProjectsDeclared
  189. , log.getPathString(), commit.name()));
  190. }
  191. for (Project p : meta.getProjects()) {
  192. p.setVersion(version);
  193. projects.put(p.getName(), p);
  194. }
  195. cqs.addAll(meta.getCQs());
  196. }
  197. private void loadCommitters(Repository repo) throws IOException {
  198. SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  199. File list = new File(repo.getDirectory(), "gerrit_committers");
  200. BufferedReader br = new BufferedReader(new FileReader(list));
  201. String line;
  202. while ((line = br.readLine()) != null) {
  203. String[] field = line.trim().split(" *\\| *");
  204. String user = field[1];
  205. String name = field[2];
  206. String email = field[3];
  207. Date begin = parseDate(dt, field[4]);
  208. Date end = parseDate(dt, field[5]);
  209. if (user.startsWith("username:"))
  210. user = user.substring("username:".length());
  211. Committer who = committersById.get(user);
  212. if (who == null) {
  213. who = new Committer(user);
  214. int sp = name.indexOf(' ');
  215. if (0 < sp) {
  216. who.setFirstName(name.substring(0, sp).trim());
  217. who.setLastName(name.substring(sp + 1).trim());
  218. } else {
  219. who.setFirstName(name);
  220. who.setLastName(null);
  221. }
  222. committersById.put(who.getID(), who);
  223. }
  224. who.addEmailAddress(email);
  225. who.addActiveRange(new ActiveRange(begin, end));
  226. committersByEmail.put(email, who);
  227. }
  228. }
  229. private Date parseDate(SimpleDateFormat dt, String value)
  230. throws IOException {
  231. if ("NULL".equals(value) || "".equals(value) || value == null)
  232. return null;
  233. int dot = value.indexOf('.');
  234. if (0 < dot)
  235. value = value.substring(0, dot);
  236. try {
  237. return dt.parse(value);
  238. } catch (ParseException e) {
  239. IOException err = new IOException(MessageFormat.format(IpLogText.get().invalidDate, value));
  240. err.initCause(e);
  241. throw err;
  242. }
  243. }
  244. private void scanProjectCommits(Project proj, RevCommit start)
  245. throws IOException {
  246. rw.reset();
  247. rw.markStart(start);
  248. RevCommit commit;
  249. while ((commit = rw.next()) != null) {
  250. if (proj.isSkippedCommit(commit)) {
  251. continue;
  252. }
  253. final PersonIdent author = commit.getAuthorIdent();
  254. final Date when = author.getWhen();
  255. Committer who = committersByEmail.get(author.getEmailAddress());
  256. if (who != null && who.inRange(when)) {
  257. // Commit was written by the committer while they were
  258. // an active committer on the project.
  259. //
  260. who.setHasCommits(true);
  261. continue;
  262. }
  263. // Commit from a non-committer contributor.
  264. //
  265. final int cnt = commit.getParentCount();
  266. if (2 <= cnt) {
  267. // Avoid a pointless merge attributed to a non-committer.
  268. // Skip this commit if every file matches at least one
  269. // of the parent commits exactly, if so then the blame
  270. // for code in that file can be fully passed onto that
  271. // parent and this non-committer isn't responsible.
  272. //
  273. tw.setFilter(TreeFilter.ANY_DIFF);
  274. tw.setRecursive(true);
  275. RevTree[] trees = new RevTree[1 + cnt];
  276. trees[0] = commit.getTree();
  277. for (int i = 0; i < cnt; i++)
  278. trees[i + 1] = commit.getParent(i).getTree();
  279. tw.reset(trees);
  280. boolean matchAll = true;
  281. while (tw.next()) {
  282. boolean matchOne = false;
  283. for (int i = 1; i <= cnt; i++) {
  284. if (tw.getRawMode(0) == tw.getRawMode(i)
  285. && tw.idEqual(0, i)) {
  286. matchOne = true;
  287. break;
  288. }
  289. }
  290. if (!matchOne) {
  291. matchAll = false;
  292. break;
  293. }
  294. }
  295. if (matchAll)
  296. continue;
  297. }
  298. Contributor contributor = contributorsByName.get(author.getName());
  299. if (contributor == null) {
  300. String id = author.getEmailAddress();
  301. String name = author.getName();
  302. contributor = new Contributor(id, name);
  303. contributorsByName.put(name, contributor);
  304. }
  305. String id = commit.name();
  306. String subj = commit.getShortMessage();
  307. SingleContribution item = new SingleContribution(id, when, subj);
  308. List<String> bugs = commit.getFooterLines(BUG);
  309. if (1 == bugs.size()) {
  310. item.setBugID(bugs.get(0));
  311. } else if (2 <= bugs.size()) {
  312. StringBuilder tmp = new StringBuilder();
  313. for (String bug : bugs) {
  314. if (tmp.length() > 0)
  315. tmp.append(",");
  316. tmp.append(bug);
  317. }
  318. item.setBugID(tmp.toString());
  319. }
  320. if (2 <= cnt) {
  321. item.setSize("(merge)");
  322. contributor.add(item);
  323. continue;
  324. }
  325. int addedLines = 0;
  326. if (1 == cnt) {
  327. final RevCommit parent = commit.getParent(0);
  328. tw.setFilter(TreeFilter.ANY_DIFF);
  329. tw.setRecursive(true);
  330. tw.reset(new RevTree[] { parent.getTree(), commit.getTree() });
  331. while (tw.next()) {
  332. if (tw.getFileMode(1).getObjectType() != Constants.OBJ_BLOB)
  333. continue;
  334. byte[] oldImage;
  335. if (tw.getFileMode(0).getObjectType() == Constants.OBJ_BLOB)
  336. oldImage = openBlob(0);
  337. else
  338. oldImage = new byte[0];
  339. EditList edits = new MyersDiff(new RawText(oldImage),
  340. new RawText(openBlob(1))).getEdits();
  341. for (Edit e : edits)
  342. addedLines += e.getEndB() - e.getBeginB();
  343. }
  344. } else { // no parents, everything is an addition
  345. tw.setFilter(TreeFilter.ALL);
  346. tw.setRecursive(true);
  347. tw.reset(commit.getTree());
  348. while (tw.next()) {
  349. if (tw.getFileMode(0).getObjectType() == Constants.OBJ_BLOB) {
  350. byte[] buf = openBlob(0);
  351. for (int ptr = 0; ptr < buf.length;) {
  352. ptr = RawParseUtils.nextLF(buf, ptr);
  353. addedLines++;
  354. }
  355. }
  356. }
  357. }
  358. if (addedLines < 0)
  359. throw new IOException(MessageFormat.format(IpLogText.get().incorrectlyScanned, commit.name()));
  360. if (1 == addedLines)
  361. item.setSize("+1 line");
  362. else
  363. item.setSize("+" + addedLines + " lines");
  364. contributor.add(item);
  365. }
  366. }
  367. private byte[] openBlob(int side) throws IOException {
  368. tw.getObjectId(idbuf, side);
  369. ObjectLoader ldr = db.openObject(curs, idbuf);
  370. if (ldr == null)
  371. throw new MissingObjectException(idbuf.copy(), Constants.OBJ_BLOB);
  372. return ldr.getCachedBytes();
  373. }
  374. /**
  375. * Dump the scanned information into an XML file.
  376. *
  377. * @param out
  378. * the file stream to write to. The caller is responsible for
  379. * closing the stream upon completion.
  380. * @throws IOException
  381. * the stream cannot be written.
  382. */
  383. public void writeTo(OutputStream out) throws IOException {
  384. try {
  385. TransformerFactory factory = TransformerFactory.newInstance();
  386. Transformer s = factory.newTransformer();
  387. s.setOutputProperty(OutputKeys.ENCODING, characterEncoding);
  388. s.setOutputProperty(OutputKeys.METHOD, "xml");
  389. s.setOutputProperty(OutputKeys.INDENT, "yes");
  390. s.setOutputProperty(INDENT, "2");
  391. s.transform(new DOMSource(toXML()), new StreamResult(out));
  392. } catch (ParserConfigurationException e) {
  393. IOException err = new IOException(IpLogText.get().cannotSerializeXML);
  394. err.initCause(e);
  395. throw err;
  396. } catch (TransformerConfigurationException e) {
  397. IOException err = new IOException(IpLogText.get().cannotSerializeXML);
  398. err.initCause(e);
  399. throw err;
  400. } catch (TransformerException e) {
  401. IOException err = new IOException(IpLogText.get().cannotSerializeXML);
  402. err.initCause(e);
  403. throw err;
  404. }
  405. }
  406. private Document toXML() throws ParserConfigurationException {
  407. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  408. factory.setNamespaceAware(true);
  409. doc = factory.newDocumentBuilder().newDocument();
  410. Element root = createElement("iplog");
  411. doc.appendChild(root);
  412. if (projects.size() == 1) {
  413. Project soleProject = projects.values().iterator().next();
  414. root.setAttribute("name", soleProject.getID());
  415. }
  416. Set<String> licenses = new TreeSet<String>();
  417. for (Project project : sort(projects, Project.COMPARATOR)) {
  418. root.appendChild(createProject(project));
  419. licenses.addAll(project.getLicenses());
  420. }
  421. for (RevCommit c : sort(commits))
  422. root.appendChild(createCommitMeta(c));
  423. for (String name : sort(licenses))
  424. root.appendChild(createLicense(name));
  425. if (!cqs.isEmpty())
  426. appendBlankLine(root);
  427. for (CQ cq : sort(cqs, CQ.COMPARATOR))
  428. root.appendChild(createCQ(cq));
  429. if (!committersByEmail.isEmpty())
  430. appendBlankLine(root);
  431. for (Committer committer : sort(committersById, Committer.COMPARATOR))
  432. root.appendChild(createCommitter(committer));
  433. for (Contributor c : sort(contributorsByName, Contributor.COMPARATOR)) {
  434. appendBlankLine(root);
  435. root.appendChild(createContributor(c));
  436. }
  437. return doc;
  438. }
  439. private void appendBlankLine(Element root) {
  440. root.appendChild(doc.createTextNode("\n\n "));
  441. }
  442. private Element createProject(Project p) {
  443. Element project = createElement("project");
  444. required(project, "id", p.getID());
  445. required(project, "name", p.getName());
  446. optional(project, "comments", p.getComments());
  447. optional(project, "version", p.getVersion());
  448. return project;
  449. }
  450. private Element createCommitMeta(RevCommit c) {
  451. Element meta = createElement("meta");
  452. required(meta, "key", "git-commit");
  453. required(meta, "value", c.name());
  454. return meta;
  455. }
  456. private Element createLicense(String name) {
  457. Element license = createElement("license");
  458. required(license, "id", name);
  459. optional(license, "description", null);
  460. optional(license, "comments", null);
  461. return license;
  462. }
  463. private Element createCQ(CQ cq) {
  464. Element r = createElement("cq");
  465. required(r, "id", Long.toString(cq.getID()));
  466. required(r, "description", cq.getDescription());
  467. optional(r, "license", cq.getLicense());
  468. optional(r, "use", cq.getUse());
  469. optional(r, "state", cq.getState());
  470. optional(r, "comments", cq.getComments());
  471. return r;
  472. }
  473. private Element createCommitter(Committer who) {
  474. Element r = createElement("committer");
  475. required(r, "id", who.getID());
  476. required(r, "firstName", who.getFirstName());
  477. required(r, "lastName", who.getLastName());
  478. optional(r, "affiliation", who.getAffiliation());
  479. required(r, "active", Boolean.toString(who.isActive()));
  480. required(r, "hasCommits", Boolean.toString(who.hasCommits()));
  481. optional(r, "comments", who.getComments());
  482. return r;
  483. }
  484. private Element createContributor(Contributor c) {
  485. Element r = createElement("contributor");
  486. required(r, "id", c.getID());
  487. required(r, "name", c.getName());
  488. for (SingleContribution s : sort(c.getContributions(),
  489. SingleContribution.COMPARATOR))
  490. r.appendChild(createContribution(s));
  491. return r;
  492. }
  493. private Element createContribution(SingleContribution s) {
  494. Element r = createElement("bug");
  495. required(r, "id", s.getID());
  496. optional(r, "bug-id", s.getBugID());
  497. required(r, "size", s.getSize());
  498. required(r, "type", "A"); // assume attachment type
  499. required(r, "created", format(s.getCreated()));
  500. required(r, "summary", s.getSummary());
  501. return r;
  502. }
  503. private String format(Date created) {
  504. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(created);
  505. }
  506. private Element createElement(String name) {
  507. return doc.createElementNS(IPLOG_NS, IPLOG_PFX + name);
  508. }
  509. private void required(Element r, String name, String value) {
  510. if (value == null)
  511. value = "";
  512. r.setAttribute(name, value);
  513. }
  514. private void optional(Element r, String name, String value) {
  515. if (value != null && value.length() > 0)
  516. r.setAttribute(name, value);
  517. }
  518. private static <T, Q extends Comparator<T>> Iterable<T> sort(
  519. Collection<T> objs, Q cmp) {
  520. ArrayList<T> sorted = new ArrayList<T>(objs);
  521. Collections.sort(sorted, cmp);
  522. return sorted;
  523. }
  524. private static <T, Q extends Comparator<T>> Iterable<T> sort(
  525. Map<?, T> objs, Q cmp) {
  526. return sort(objs.values(), cmp);
  527. }
  528. @SuppressWarnings("unchecked")
  529. private static <T extends Comparable> Iterable<T> sort(Collection<T> objs) {
  530. ArrayList<T> sorted = new ArrayList<T>(objs);
  531. Collections.sort(sorted);
  532. return sorted;
  533. }
  534. }