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.

BranchGraphServlet.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  3. * Copyright 2013 gitblit.com.
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. package com.gitblit.servlet;
  20. import java.awt.BasicStroke;
  21. import java.awt.Color;
  22. import java.awt.Graphics;
  23. import java.awt.Graphics2D;
  24. import java.awt.RenderingHints;
  25. import java.awt.Stroke;
  26. import java.awt.image.BufferedImage;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.io.OutputStream;
  30. import java.io.Serializable;
  31. import java.util.ArrayList;
  32. import java.util.LinkedList;
  33. import java.util.List;
  34. import java.util.Set;
  35. import java.util.TreeSet;
  36. import javax.imageio.ImageIO;
  37. import com.google.inject.Inject;
  38. import com.google.inject.Singleton;
  39. import javax.servlet.ServletException;
  40. import javax.servlet.http.HttpServlet;
  41. import javax.servlet.http.HttpServletRequest;
  42. import javax.servlet.http.HttpServletResponse;
  43. import org.eclipse.jgit.lib.Ref;
  44. import org.eclipse.jgit.lib.Repository;
  45. import org.eclipse.jgit.revplot.AbstractPlotRenderer;
  46. import org.eclipse.jgit.revplot.PlotCommit;
  47. import org.eclipse.jgit.revplot.PlotCommitList;
  48. import org.eclipse.jgit.revplot.PlotLane;
  49. import org.eclipse.jgit.revplot.PlotWalk;
  50. import org.eclipse.jgit.revwalk.RevCommit;
  51. import com.gitblit.Constants;
  52. import com.gitblit.IStoredSettings;
  53. import com.gitblit.Keys;
  54. import com.gitblit.manager.IRepositoryManager;
  55. import com.gitblit.utils.JGitUtils;
  56. import com.gitblit.utils.StringUtils;
  57. /**
  58. * Handles requests for branch graphs
  59. *
  60. * @author James Moger
  61. *
  62. */
  63. @Singleton
  64. public class BranchGraphServlet extends HttpServlet {
  65. private static final long serialVersionUID = 1L;
  66. private static final int LANE_WIDTH = 14;
  67. // must match tr.commit css height
  68. private static final int ROW_HEIGHT = 24;
  69. private static final int RIGHT_PAD = 2;
  70. private final Stroke[] strokeCache;
  71. private IStoredSettings settings;
  72. private IRepositoryManager repositoryManager;
  73. @Inject
  74. public BranchGraphServlet(
  75. IStoredSettings settings,
  76. IRepositoryManager repositoryManager) {
  77. this.settings = settings;
  78. this.repositoryManager = repositoryManager;
  79. strokeCache = new Stroke[4];
  80. for (int i = 1; i < strokeCache.length; i++) {
  81. strokeCache[i] = new BasicStroke(i);
  82. }
  83. }
  84. /**
  85. * Returns an url to this servlet for the specified parameters.
  86. *
  87. * @param baseURL
  88. * @param repository
  89. * @param objectId
  90. * @param numberCommits
  91. * @return an url
  92. */
  93. public static String asLink(String baseURL, String repository, String objectId, int numberCommits) {
  94. if (baseURL.length() > 0 && baseURL.charAt(baseURL.length() - 1) == '/') {
  95. baseURL = baseURL.substring(0, baseURL.length() - 1);
  96. }
  97. return baseURL + Constants.BRANCH_GRAPH_PATH + "?r=" + repository
  98. + (objectId == null ? "" : ("&h=" + objectId))
  99. + (numberCommits > 0 ? ("&l=" + numberCommits) : "");
  100. }
  101. @Override
  102. protected long getLastModified(HttpServletRequest req) {
  103. String repository = req.getParameter("r");
  104. String objectId = req.getParameter("h");
  105. Repository r = null;
  106. try {
  107. r = repositoryManager.getRepository(repository);
  108. if (StringUtils.isEmpty(objectId)) {
  109. objectId = JGitUtils.getHEADRef(r);
  110. }
  111. RevCommit commit = JGitUtils.getCommit(r, objectId);
  112. return JGitUtils.getCommitDate(commit).getTime();
  113. } finally {
  114. if (r != null) {
  115. r.close();
  116. }
  117. }
  118. }
  119. @Override
  120. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  121. throws ServletException, IOException {
  122. InputStream is = null;
  123. Repository r = null;
  124. PlotWalk rw = null;
  125. try {
  126. String repository = request.getParameter("r");
  127. String objectId = request.getParameter("h");
  128. String length = request.getParameter("l");
  129. r = repositoryManager.getRepository(repository);
  130. rw = new PlotWalk(r);
  131. if (StringUtils.isEmpty(objectId)) {
  132. objectId = JGitUtils.getHEADRef(r);
  133. }
  134. rw.markStart(rw.lookupCommit(r.resolve(objectId)));
  135. // default to the items-per-page setting, unless specified
  136. int maxCommits = settings.getInteger(Keys.web.itemsPerPage, 50);
  137. int requestedCommits = maxCommits;
  138. if (!StringUtils.isEmpty(length)) {
  139. int l = Integer.parseInt(length);
  140. if (l > 0) {
  141. requestedCommits = l;
  142. }
  143. }
  144. // fetch the requested commits plus some extra so that the last
  145. // commit displayed *likely* has correct lane assignments
  146. CommitList commitList = new CommitList();
  147. commitList.source(rw);
  148. commitList.fillTo(2*Math.max(requestedCommits, maxCommits));
  149. // determine the appropriate width for the image
  150. int numLanes = 1;
  151. int numCommits = Math.min(requestedCommits, commitList.size());
  152. if (numCommits > 1) {
  153. // determine graph width
  154. Set<String> parents = new TreeSet<String>();
  155. for (int i = 0; i < commitList.size(); i++) {
  156. PlotCommit<Lane> commit = commitList.get(i);
  157. boolean checkLane = false;
  158. if (i < numCommits) {
  159. // commit in visible list
  160. checkLane = true;
  161. // remember parents
  162. for (RevCommit p : commit.getParents()) {
  163. parents.add(p.getName());
  164. }
  165. } else if (parents.contains(commit.getName())) {
  166. // commit outside visible list, but it is a parent of a
  167. // commit in the visible list so we need to know it's lane
  168. // assignment
  169. checkLane = true;
  170. }
  171. if (checkLane) {
  172. int pos = commit.getLane().getPosition();
  173. numLanes = Math.max(numLanes, pos + 1);
  174. }
  175. }
  176. }
  177. int graphWidth = numLanes * LANE_WIDTH + RIGHT_PAD;
  178. int rowHeight = ROW_HEIGHT;
  179. // create an image buffer and render the lanes
  180. BufferedImage image = new BufferedImage(graphWidth, rowHeight*numCommits, BufferedImage.TYPE_INT_ARGB);
  181. Graphics2D g = null;
  182. try {
  183. g = image.createGraphics();
  184. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  185. LanesRenderer renderer = new LanesRenderer();
  186. for (int i = 0; i < commitList.size(); i++) {
  187. PlotCommit<Lane> commit = commitList.get(i);
  188. Graphics row = g.create(0, i*rowHeight, graphWidth, rowHeight);
  189. try {
  190. renderer.paint(row, commit, rowHeight, graphWidth);
  191. } finally {
  192. row.dispose();
  193. row = null;
  194. }
  195. }
  196. } finally {
  197. if (g != null) {
  198. g.dispose();
  199. g = null;
  200. }
  201. }
  202. // write the image buffer to the client
  203. response.setContentType("image/png");
  204. if (numCommits > 1) {
  205. response.setHeader("Cache-Control", "public, max-age=60, must-revalidate");
  206. response.setDateHeader("Last-Modified", JGitUtils.getCommitDate(commitList.get(0)).getTime());
  207. }
  208. OutputStream os = response.getOutputStream();
  209. ImageIO.write(image, "png", os);
  210. os.flush();
  211. image.flush();
  212. image = null;
  213. } catch (Exception e) {
  214. e.printStackTrace();
  215. } finally {
  216. if (is != null) {
  217. is.close();
  218. is = null;
  219. }
  220. if (rw != null) {
  221. rw.dispose();
  222. rw = null;
  223. }
  224. if (r != null) {
  225. r.close();
  226. r = null;
  227. }
  228. }
  229. }
  230. private Stroke stroke(final int width) {
  231. if (width < strokeCache.length)
  232. return strokeCache[width];
  233. return new BasicStroke(width);
  234. }
  235. static class CommitList extends PlotCommitList<Lane> {
  236. final List<Color> laneColors;
  237. final LinkedList<Color> colors;
  238. CommitList() {
  239. laneColors = new ArrayList<Color>();
  240. laneColors.add(new Color(133, 166, 214));
  241. laneColors.add(new Color(221, 205, 93));
  242. laneColors.add(new Color(199, 134, 57));
  243. laneColors.add(new Color(131, 150, 98));
  244. laneColors.add(new Color(197, 123, 127));
  245. laneColors.add(new Color(139, 136, 140));
  246. laneColors.add(new Color(48, 135, 144));
  247. laneColors.add(new Color(190, 93, 66));
  248. laneColors.add(new Color(143, 163, 54));
  249. laneColors.add(new Color(180, 148, 74));
  250. laneColors.add(new Color(101, 101, 217));
  251. laneColors.add(new Color(72, 153, 119));
  252. laneColors.add(new Color(23, 101, 160));
  253. laneColors.add(new Color(132, 164, 118));
  254. laneColors.add(new Color(255, 230, 59));
  255. laneColors.add(new Color(136, 176, 70));
  256. laneColors.add(new Color(255, 138, 1));
  257. laneColors.add(new Color(123, 187, 95));
  258. laneColors.add(new Color(233, 88, 98));
  259. laneColors.add(new Color(93, 158, 254));
  260. laneColors.add(new Color(175, 215, 0));
  261. laneColors.add(new Color(140, 134, 142));
  262. laneColors.add(new Color(232, 168, 21));
  263. laneColors.add(new Color(0, 172, 191));
  264. laneColors.add(new Color(251, 58, 4));
  265. laneColors.add(new Color(63, 64, 255));
  266. laneColors.add(new Color(27, 194, 130));
  267. laneColors.add(new Color(0, 104, 183));
  268. colors = new LinkedList<Color>();
  269. repackColors();
  270. }
  271. private void repackColors() {
  272. colors.addAll(laneColors);
  273. }
  274. @Override
  275. protected Lane createLane() {
  276. final Lane lane = new Lane();
  277. if (colors.isEmpty())
  278. repackColors();
  279. lane.color = colors.removeFirst();
  280. return lane;
  281. }
  282. @Override
  283. protected void recycleLane(final Lane lane) {
  284. colors.add(lane.color);
  285. }
  286. }
  287. static class Lane extends PlotLane {
  288. private static final long serialVersionUID = 1L;
  289. Color color;
  290. @Override
  291. public boolean equals(Object o) {
  292. return super.equals(o) && color.equals(((Lane)o).color);
  293. }
  294. @Override
  295. public int hashCode() {
  296. return super.hashCode() ^ color.hashCode();
  297. }
  298. }
  299. class LanesRenderer extends AbstractPlotRenderer<Lane, Color> implements Serializable {
  300. private static final long serialVersionUID = 1L;
  301. final Color commitDotFill = new Color(220, 220, 220);
  302. final Color commitDotOutline = new Color(110, 110, 110);
  303. transient Graphics2D g;
  304. void paint(Graphics in, PlotCommit<Lane> commit, int h, int w) {
  305. g = (Graphics2D) in.create();
  306. try {
  307. if (commit != null)
  308. paintCommit(commit, h);
  309. } finally {
  310. g.dispose();
  311. g = null;
  312. }
  313. }
  314. @Override
  315. protected void drawLine(Color color, int x1, int y1, int x2, int y2, int width) {
  316. if (y1 == y2) {
  317. x1 -= width / 2;
  318. x2 -= width / 2;
  319. } else if (x1 == x2) {
  320. y1 -= width / 2;
  321. y2 -= width / 2;
  322. }
  323. g.setColor(color);
  324. g.setStroke(stroke(width));
  325. g.drawLine(x1, y1, x2, y2);
  326. }
  327. @Override
  328. protected void drawCommitDot(int x, int y, int w, int h) {
  329. g.setColor(commitDotFill);
  330. g.setStroke(strokeCache[2]);
  331. g.fillOval(x + 2, y + 1, w - 2, h - 2);
  332. g.setColor(commitDotOutline);
  333. g.drawOval(x + 2, y + 1, w - 2, h - 2);
  334. }
  335. @Override
  336. protected void drawBoundaryDot(int x, int y, int w, int h) {
  337. drawCommitDot(x, y, w, h);
  338. }
  339. @Override
  340. protected void drawText(String msg, int x, int y) {
  341. }
  342. @Override
  343. protected Color laneColor(Lane myLane) {
  344. return myLane != null ? myLane.color : Color.black;
  345. }
  346. @Override
  347. protected int drawLabel(int x, int y, Ref ref) {
  348. return 0;
  349. }
  350. }
  351. }