James Moger
2012-09-10 fabe060d3a435f116128851f828e35c2af5fde67
commit | author | age
716745 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.wicket.pages;
17
008322 18 import java.text.DateFormat;
JM 19 import java.text.MessageFormat;
20 import java.text.SimpleDateFormat;
716745 21 import java.util.List;
JM 22
23 import org.apache.wicket.PageParameters;
24 import org.apache.wicket.markup.html.basic.Label;
25 import org.apache.wicket.markup.html.link.BookmarkablePageLink;
26 import org.apache.wicket.markup.repeater.Item;
27 import org.apache.wicket.markup.repeater.data.DataView;
28 import org.apache.wicket.markup.repeater.data.ListDataProvider;
29 import org.eclipse.jgit.lib.Constants;
30 import org.eclipse.jgit.revwalk.RevCommit;
31
008322 32 import com.gitblit.GitBlit;
JM 33 import com.gitblit.Keys;
34 import com.gitblit.models.AnnotatedLine;
35 import com.gitblit.utils.DiffUtils;
36 import com.gitblit.utils.StringUtils;
716745 37 import com.gitblit.wicket.WicketUtils;
JM 38 import com.gitblit.wicket.panels.CommitHeaderPanel;
39 import com.gitblit.wicket.panels.LinkPanel;
40 import com.gitblit.wicket.panels.PathBreadcrumbsPanel;
41
42 public class BlamePage extends RepositoryPage {
43
44     public BlamePage(PageParameters params) {
45         super(params);
46
47         final String blobPath = WicketUtils.getPath(params);
48
49         RevCommit commit = getCommit();
50
51         add(new BookmarkablePageLink<Void>("blobLink", BlobPage.class,
008322 52                 WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
716745 53         add(new BookmarkablePageLink<Void>("commitLink", CommitPage.class,
JM 54                 WicketUtils.newObjectParameter(repositoryName, objectId)));
55         add(new BookmarkablePageLink<Void>("commitDiffLink", CommitDiffPage.class,
56                 WicketUtils.newObjectParameter(repositoryName, objectId)));
57
58         // blame page links
59         add(new BookmarkablePageLink<Void>("headLink", BlamePage.class,
60                 WicketUtils.newPathParameter(repositoryName, Constants.HEAD, blobPath)));
61         add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
62                 WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
63
64         add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
65
66         add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
67
008322 68         String format = GitBlit.getString(Keys.web.datetimestampLongFormat,
a4ed6d 69                 "EEEE, MMMM d, yyyy HH:mm Z");
008322 70         final DateFormat df = new SimpleDateFormat(format);
JM 71         df.setTimeZone(getTimeZone());
72         List<AnnotatedLine> lines = DiffUtils.blame(getRepository(), blobPath, objectId);
73         ListDataProvider<AnnotatedLine> blameDp = new ListDataProvider<AnnotatedLine>(lines);
74         DataView<AnnotatedLine> blameView = new DataView<AnnotatedLine>("annotation", blameDp) {
716745 75             private static final long serialVersionUID = 1L;
008322 76             private int count;
JM 77             private String lastCommitId = "";
78             private boolean showInitials = true;
716745 79
008322 80             public void populateItem(final Item<AnnotatedLine> item) {
JM 81                 AnnotatedLine entry = item.getModelObject();
82                 item.add(new Label("line", "" + entry.lineNumber));
83                 item.add(new Label("data", StringUtils.escapeForHtml(entry.data, true))
84                         .setEscapeModelStrings(false));
85                 if (!lastCommitId.equals(entry.commitId)) {
86                     lastCommitId = entry.commitId;
87                     count++;
88                     // show the link for first line
89                     LinkPanel commitLink = new LinkPanel("commit", null,
90                             getShortObjectId(entry.commitId), CommitPage.class,
91                             newCommitParameter(entry.commitId));
92                     WicketUtils.setHtmlTooltip(commitLink,
93                             MessageFormat.format("{0}, {1}", entry.author, df.format(entry.when)));
94                     item.add(commitLink);
95                     showInitials = true;
96                 } else {
97                     if (showInitials) {
98                         showInitials = false;
99                         // show author initials
100                         item.add(new Label("commit", getInitials(entry.author)));
101                     } else {
102                         // hide the commit link until the next block
103                         item.add(new Label("commit").setVisible(false));
104                     }
105                 }
106                 if (count % 2 == 0) {
107                     WicketUtils.setCssClass(item, "even");
108                 } else {
109                     WicketUtils.setCssClass(item, "odd");
110                 }
716745 111             }
JM 112         };
113         add(blameView);
114     }
115
008322 116     private String getInitials(String author) {
JM 117         StringBuilder sb = new StringBuilder();
118         String[] chunks = author.split(" ");
119         for (String chunk : chunks) {
120             sb.append(chunk.charAt(0));
121         }
122         return sb.toString().toUpperCase();
123     }
124
716745 125     @Override
JM 126     protected String getPageName() {
127         return getString("gb.blame");
128     }
129 }