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

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