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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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;
  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 javax.imageio.ImageIO;
  35. import javax.servlet.ServletException;
  36. import javax.servlet.http.HttpServlet;
  37. import javax.servlet.http.HttpServletRequest;
  38. import javax.servlet.http.HttpServletResponse;
  39. import org.eclipse.jgit.lib.Ref;
  40. import org.eclipse.jgit.lib.Repository;
  41. import org.eclipse.jgit.revplot.AbstractPlotRenderer;
  42. import org.eclipse.jgit.revplot.PlotCommit;
  43. import org.eclipse.jgit.revplot.PlotCommitList;
  44. import org.eclipse.jgit.revplot.PlotLane;
  45. import org.eclipse.jgit.revplot.PlotWalk;
  46. import org.eclipse.jgit.revwalk.RevCommit;
  47. import com.gitblit.utils.JGitUtils;
  48. import com.gitblit.utils.StringUtils;
  49. /**
  50. * Handles requests for branch graphs
  51. *
  52. * @author James Moger
  53. *
  54. */
  55. public class BranchGraphServlet extends HttpServlet {
  56. private static final long serialVersionUID = 1L;
  57. private static final int LANE_WIDTH = 14;
  58. // must match tr.commit css height
  59. private static final int ROW_HEIGHT = 24;
  60. private static final int RIGHT_PAD = 2;
  61. private final Stroke[] strokeCache;
  62. public BranchGraphServlet() {
  63. super();
  64. strokeCache = new Stroke[4];
  65. for (int i = 1; i < strokeCache.length; i++)
  66. strokeCache[i] = new BasicStroke(i);
  67. }
  68. /**
  69. * Returns an url to this servlet for the specified parameters.
  70. *
  71. * @param baseURL
  72. * @param repository
  73. * @param objectId
  74. * @param numberCommits
  75. * @return an url
  76. */
  77. public static String asLink(String baseURL, String repository, String objectId, int numberCommits) {
  78. if (baseURL.length() > 0 && baseURL.charAt(baseURL.length() - 1) == '/') {
  79. baseURL = baseURL.substring(0, baseURL.length() - 1);
  80. }
  81. return baseURL + Constants.BRANCH_GRAPH_PATH + "?r=" + repository
  82. + (objectId == null ? "" : ("&h=" + objectId))
  83. + (numberCommits > 0 ? ("&l=" + numberCommits) : "");
  84. }
  85. @Override
  86. protected long getLastModified(HttpServletRequest req) {
  87. String repository = req.getParameter("r");
  88. String objectId = req.getParameter("h");
  89. Repository r = null;
  90. try {
  91. r = GitBlit.self().getRepository(repository);
  92. if (StringUtils.isEmpty(objectId)) {
  93. objectId = JGitUtils.getHEADRef(r);
  94. }
  95. RevCommit commit = JGitUtils.getCommit(r, objectId);
  96. return JGitUtils.getCommitDate(commit).getTime();
  97. } finally {
  98. if (r != null) {
  99. r.close();
  100. }
  101. }
  102. }
  103. @Override
  104. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  105. throws ServletException, IOException {
  106. InputStream is = null;
  107. Repository r = null;
  108. PlotWalk rw = null;
  109. try {
  110. String repository = request.getParameter("r");
  111. String objectId = request.getParameter("h");
  112. String length = request.getParameter("l");
  113. r = GitBlit.self().getRepository(repository);
  114. rw = new PlotWalk(r);
  115. if (StringUtils.isEmpty(objectId)) {
  116. objectId = JGitUtils.getHEADRef(r);
  117. }
  118. rw.markStart(rw.lookupCommit(r.resolve(objectId)));
  119. // default to the items-per-page setting, unless specified
  120. int maxCommits = GitBlit.getInteger(Keys.web.itemsPerPage, 50);
  121. if (!StringUtils.isEmpty(length)) {
  122. int l = Integer.parseInt(length);
  123. if (l > 0) {
  124. maxCommits = l;
  125. }
  126. }
  127. // fetch the requested commits plus some extra so that the last
  128. // commit displayed *likely* has correct lane assignments
  129. CommitList commitList = new CommitList();
  130. commitList.source(rw);
  131. commitList.fillTo(2*maxCommits);
  132. // determine the appropriate width for the image
  133. int numLanes = 0;
  134. int numCommits = Math.min(maxCommits, commitList.size());
  135. for (int i = 0; i < numCommits; i++) {
  136. PlotCommit<Lane> commit = commitList.get(i);
  137. int pos = commit.getLane().getPosition();
  138. numLanes = Math.max(numLanes, pos + 1);
  139. }
  140. int graphWidth = numLanes * LANE_WIDTH + RIGHT_PAD;
  141. int rowHeight = ROW_HEIGHT;
  142. // create an image buffer and render the lanes
  143. BufferedImage image = new BufferedImage(graphWidth, rowHeight*numCommits, BufferedImage.TYPE_INT_ARGB);
  144. Graphics2D g = null;
  145. try {
  146. g = image.createGraphics();
  147. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  148. LanesRenderer renderer = new LanesRenderer();
  149. for (int i = 0; i < numCommits; i++) {
  150. PlotCommit<Lane> commit = commitList.get(i);
  151. Graphics row = g.create(0, i*rowHeight, graphWidth, rowHeight);
  152. try {
  153. renderer.paint(row, commit, rowHeight, graphWidth);
  154. } finally {
  155. row.dispose();
  156. row = null;
  157. }
  158. }
  159. } finally {
  160. if (g != null) {
  161. g.dispose();
  162. g = null;
  163. }
  164. }
  165. // write the image buffer to the client
  166. response.setContentType("image/png");
  167. if (numCommits > 0) {
  168. response.setHeader("Cache-Control", "public, max-age=60, must-revalidate");
  169. response.setDateHeader("Last-Modified", JGitUtils.getCommitDate(commitList.get(0)).getTime());
  170. }
  171. OutputStream os = response.getOutputStream();
  172. ImageIO.write(image, "png", os);
  173. os.flush();
  174. image.flush();
  175. image = null;
  176. } catch (Exception e) {
  177. e.printStackTrace();
  178. } finally {
  179. if (is != null) {
  180. is.close();
  181. is = null;
  182. }
  183. if (rw != null) {
  184. rw.dispose();
  185. rw = null;
  186. }
  187. if (r != null) {
  188. r.close();
  189. r = null;
  190. }
  191. }
  192. }
  193. private Stroke stroke(final int width) {
  194. if (width < strokeCache.length)
  195. return strokeCache[width];
  196. return new BasicStroke(width);
  197. }
  198. static class CommitList extends PlotCommitList<Lane> {
  199. final List<Color> laneColors;
  200. final LinkedList<Color> colors;
  201. CommitList() {
  202. laneColors = new ArrayList<Color>();
  203. laneColors.add(new Color(133, 166, 214));
  204. laneColors.add(new Color(221, 205, 93));
  205. laneColors.add(new Color(199, 134, 57));
  206. laneColors.add(new Color(131, 150, 98));
  207. laneColors.add(new Color(197, 123, 127));
  208. laneColors.add(new Color(139, 136, 140));
  209. laneColors.add(new Color(48, 135, 144));
  210. laneColors.add(new Color(190, 93, 66));
  211. laneColors.add(new Color(143, 163, 54));
  212. laneColors.add(new Color(180, 148, 74));
  213. laneColors.add(new Color(101, 101, 217));
  214. laneColors.add(new Color(72, 153, 119));
  215. laneColors.add(new Color(23, 101, 160));
  216. laneColors.add(new Color(132, 164, 118));
  217. laneColors.add(new Color(255, 230, 59));
  218. laneColors.add(new Color(136, 176, 70));
  219. laneColors.add(new Color(255, 138, 1));
  220. laneColors.add(new Color(123, 187, 95));
  221. laneColors.add(new Color(233, 88, 98));
  222. laneColors.add(new Color(93, 158, 254));
  223. laneColors.add(new Color(175, 215, 0));
  224. laneColors.add(new Color(140, 134, 142));
  225. laneColors.add(new Color(232, 168, 21));
  226. laneColors.add(new Color(0, 172, 191));
  227. laneColors.add(new Color(251, 58, 4));
  228. laneColors.add(new Color(63, 64, 255));
  229. laneColors.add(new Color(27, 194, 130));
  230. laneColors.add(new Color(0, 104, 183));
  231. colors = new LinkedList<Color>();
  232. repackColors();
  233. }
  234. private void repackColors() {
  235. colors.addAll(laneColors);
  236. }
  237. @Override
  238. protected Lane createLane() {
  239. final Lane lane = new Lane();
  240. if (colors.isEmpty())
  241. repackColors();
  242. lane.color = colors.removeFirst();
  243. return lane;
  244. }
  245. @Override
  246. protected void recycleLane(final Lane lane) {
  247. colors.add(lane.color);
  248. }
  249. }
  250. static class Lane extends PlotLane {
  251. private static final long serialVersionUID = 1L;
  252. Color color;
  253. @Override
  254. public boolean equals(Object o) {
  255. return super.equals(o) && color.equals(((Lane)o).color);
  256. }
  257. @Override
  258. public int hashCode() {
  259. return super.hashCode() ^ color.hashCode();
  260. }
  261. }
  262. class LanesRenderer extends AbstractPlotRenderer<Lane, Color> implements Serializable {
  263. private static final long serialVersionUID = 1L;
  264. final Color commitDotFill = new Color(220, 220, 220);
  265. final Color commitDotOutline = new Color(110, 110, 110);
  266. transient Graphics2D g;
  267. void paint(Graphics in, PlotCommit<Lane> commit, int h, int w) {
  268. g = (Graphics2D) in.create();
  269. try {
  270. if (commit != null)
  271. paintCommit(commit, h);
  272. } finally {
  273. g.dispose();
  274. g = null;
  275. }
  276. }
  277. @Override
  278. protected void drawLine(Color color, int x1, int y1, int x2, int y2, int width) {
  279. if (y1 == y2) {
  280. x1 -= width / 2;
  281. x2 -= width / 2;
  282. } else if (x1 == x2) {
  283. y1 -= width / 2;
  284. y2 -= width / 2;
  285. }
  286. g.setColor(color);
  287. g.setStroke(stroke(width));
  288. g.drawLine(x1, y1, x2, y2);
  289. }
  290. @Override
  291. protected void drawCommitDot(int x, int y, int w, int h) {
  292. g.setColor(commitDotFill);
  293. g.setStroke(strokeCache[2]);
  294. g.fillOval(x + 2, y + 1, w - 2, h - 2);
  295. g.setColor(commitDotOutline);
  296. g.drawOval(x + 2, y + 1, w - 2, h - 2);
  297. }
  298. @Override
  299. protected void drawBoundaryDot(int x, int y, int w, int h) {
  300. drawCommitDot(x, y, w, h);
  301. }
  302. @Override
  303. protected void drawText(String msg, int x, int y) {
  304. }
  305. @Override
  306. protected Color laneColor(Lane myLane) {
  307. return myLane != null ? myLane.color : Color.black;
  308. }
  309. @Override
  310. protected int drawLabel(int x, int y, Ref ref) {
  311. return 0;
  312. }
  313. }
  314. }