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