Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

BranchGraphServlet.java 11KB

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