ajermakovics
2012-09-18 32dc8c83e3f8f0e67c9e0c11d536371244852d18
commit | author | age
32dc8c 1 /*
A 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
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import java.text.MessageFormat;
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.diff.DiffEntry.ChangeType;
30 import org.eclipse.jgit.lib.Repository;
31 import org.eclipse.jgit.revwalk.RevCommit;
32
33 import com.gitblit.GitBlit;
34 import com.gitblit.Keys;
35 import com.gitblit.models.PathModel.PathChangeModel;
36 import com.gitblit.models.SubmoduleModel;
37 import com.gitblit.utils.DiffUtils;
38 import com.gitblit.utils.DiffUtils.DiffOutputType;
39 import com.gitblit.utils.JGitUtils;
40 import com.gitblit.wicket.WicketUtils;
41 import com.gitblit.wicket.panels.CommitHeaderPanel;
42 import com.gitblit.wicket.panels.CommitLegendPanel;
43 import com.gitblit.wicket.panels.LinkPanel;
44
45 public class CommitDiffPage extends RepositoryPage {
46
47     public CommitDiffPage(PageParameters params) {
48         super(params);
49
50         Repository r = getRepository();
51
52         DiffOutputType diffType = DiffOutputType.forName(GitBlit.getString(Keys.web.diffStyle,
53                 DiffOutputType.GITBLIT.name()));
54
55         RevCommit commit = null, otherCommit = null;
56
57         if( objectId.contains("..") )
58         {
59             String[] parts = objectId.split("\\.\\.");
60             commit = getCommit(r, parts[0]);
61             otherCommit = getCommit(r, parts[1]);
62         }
63         else
64         {
65             commit = getCommit();
66         }
67
68         String diff;
69
70         if(otherCommit == null)
71         {
72             diff = DiffUtils.getCommitDiff(r, commit, diffType);
73         }
74         else
75         {
76             diff = DiffUtils.getDiff(r, commit, otherCommit, diffType);
77         }
78
79         List<String> parents = new ArrayList<String>();
80         if (commit.getParentCount() > 0) {
81             for (RevCommit parent : commit.getParents()) {
82                 parents.add(parent.name());
83             }
84         }
85
86         // commit page links
87         if (parents.size() == 0) {
88             add(new Label("parentLink", getString("gb.none")));
89         } else {
90             add(new LinkPanel("parentLink", null, parents.get(0).substring(0, 8),
91                     CommitDiffPage.class, newCommitParameter(parents.get(0))));
92         }
93         add(new BookmarkablePageLink<Void>("patchLink", PatchPage.class,
94                 WicketUtils.newObjectParameter(repositoryName, objectId)));
95         add(new BookmarkablePageLink<Void>("commitLink", CommitPage.class,
96                 WicketUtils.newObjectParameter(repositoryName, objectId)));
97
98         add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
99
100         // changed paths list
101         List<PathChangeModel> paths;
102
103         if( otherCommit == null )
104         {
105             paths = JGitUtils.getFilesInCommit(r, commit);
106         }
107         else
108         {
109             paths = JGitUtils.getFilesInCommit(r, otherCommit);
110         }
111
112         add(new CommitLegendPanel("commitLegend", paths));
113         ListDataProvider<PathChangeModel> pathsDp = new ListDataProvider<PathChangeModel>(paths);
114         DataView<PathChangeModel> pathsView = new DataView<PathChangeModel>("changedPath", pathsDp) {
115             private static final long serialVersionUID = 1L;
116             int counter;
117
118             public void populateItem(final Item<PathChangeModel> item) {
119                 final PathChangeModel entry = item.getModelObject();
120                 Label changeType = new Label("changeType", "");
121                 WicketUtils.setChangeTypeCssClass(changeType, entry.changeType);
122                 setChangeTypeTooltip(changeType, entry.changeType);
123                 item.add(changeType);
124
125                 boolean hasSubmodule = false;
126                 String submodulePath = null;
127                 if (entry.isTree()) {
128                     // tree
129                     item.add(new LinkPanel("pathName", null, entry.path, TreePage.class,
130                             WicketUtils
131                                     .newPathParameter(repositoryName, entry.commitId, entry.path)));
132                 } else if (entry.isSubmodule()) {
133                     // submodule
134                     String submoduleId = entry.objectId;
135                     SubmoduleModel submodule = getSubmodule(entry.path);
136                     submodulePath = submodule.gitblitPath;
137                     hasSubmodule = submodule.hasSubmodule;
138
139                     item.add(new LinkPanel("pathName", "list", entry.path + " @ " +
140                             getShortObjectId(submoduleId), TreePage.class,
141                             WicketUtils
142                                     .newPathParameter(submodulePath, submoduleId, "")).setEnabled(hasSubmodule));
143                 } else {
144                     // blob
145                     item.add(new LinkPanel("pathName", "list", entry.path, BlobPage.class,
146                             WicketUtils
147                                     .newPathParameter(repositoryName, entry.commitId, entry.path)));
148                 }
149
150                 // quick links
151                 if (entry.isSubmodule()) {
152                     // submodule
153                     item.add(new BookmarkablePageLink<Void>("patch", PatchPage.class, WicketUtils
154                             .newPathParameter(submodulePath, entry.objectId, entry.path)).setEnabled(false));
155                     item.add(new BookmarkablePageLink<Void>("view", CommitPage.class, WicketUtils
156                             .newObjectParameter(submodulePath, entry.objectId)).setEnabled(hasSubmodule));
157                     item.add(new BookmarkablePageLink<Void>("blame", BlamePage.class, WicketUtils
158                             .newPathParameter(submodulePath, entry.objectId, entry.path)).setEnabled(false));
159                     item.add(new BookmarkablePageLink<Void>("history", HistoryPage.class, WicketUtils
160                             .newPathParameter(submodulePath, entry.objectId, entry.path))
161                             .setEnabled(hasSubmodule));
162                 } else {
163                     // tree or blob
164                     item.add(new BookmarkablePageLink<Void>("patch", PatchPage.class, WicketUtils
165                             .newPathParameter(repositoryName, entry.commitId, entry.path)));
166                     item.add(new BookmarkablePageLink<Void>("view", BlobPage.class, WicketUtils
167                             .newPathParameter(repositoryName, entry.commitId, entry.path)));
168                     item.add(new BookmarkablePageLink<Void>("blame", BlamePage.class, WicketUtils
169                             .newPathParameter(repositoryName, entry.commitId, entry.path)));
170                     item.add(new BookmarkablePageLink<Void>("history", HistoryPage.class, WicketUtils
171                             .newPathParameter(repositoryName, entry.commitId, entry.path))
172                             .setEnabled(!entry.changeType.equals(ChangeType.ADD)));
173                 }
174                 WicketUtils.setAlternatingBackground(item, counter);
175                 counter++;
176             }
177         };
178         add(pathsView);
179         add(new Label("diffText", diff).setEscapeModelStrings(false));
180     }
181
182     @Override
183     protected String getPageName() {
184         return getString("gb.commitdiff");
185     }
186
187     private RevCommit getCommit(Repository r, String rev)
188     {
189         RevCommit otherCommit = JGitUtils.getCommit(r, rev);
190         if (otherCommit == null) {
191             error(MessageFormat.format(getString("gb.failedToFindCommit"), rev, repositoryName, getPageName()), true);
192         }
193         return otherCommit;
194     }
195 }