commit | author | age
|
5e3521
|
1 |
/* |
JM |
2 |
* Copyright 2013 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.panels; |
|
17 |
|
|
18 |
import java.text.DateFormat; |
|
19 |
import java.text.MessageFormat; |
|
20 |
import java.text.SimpleDateFormat; |
|
21 |
import java.util.ArrayList; |
|
22 |
import java.util.Calendar; |
|
23 |
import java.util.Date; |
|
24 |
import java.util.List; |
|
25 |
import java.util.TimeZone; |
|
26 |
|
|
27 |
import org.apache.wicket.markup.html.basic.Label; |
|
28 |
import org.apache.wicket.markup.repeater.Item; |
|
29 |
import org.apache.wicket.markup.repeater.data.DataView; |
|
30 |
import org.apache.wicket.markup.repeater.data.ListDataProvider; |
|
31 |
import org.apache.wicket.model.StringResourceModel; |
|
32 |
import org.eclipse.jgit.lib.Repository; |
|
33 |
import org.eclipse.jgit.transport.ReceiveCommand.Type; |
|
34 |
|
|
35 |
import com.gitblit.Constants; |
|
36 |
import com.gitblit.Keys; |
|
37 |
import com.gitblit.models.RefLogEntry; |
|
38 |
import com.gitblit.models.RepositoryCommit; |
|
39 |
import com.gitblit.models.RepositoryModel; |
|
40 |
import com.gitblit.models.UserModel; |
|
41 |
import com.gitblit.utils.RefLogUtils; |
|
42 |
import com.gitblit.utils.StringUtils; |
|
43 |
import com.gitblit.utils.TimeUtils; |
|
44 |
import com.gitblit.wicket.WicketUtils; |
|
45 |
import com.gitblit.wicket.pages.CommitPage; |
|
46 |
import com.gitblit.wicket.pages.ComparePage; |
|
47 |
import com.gitblit.wicket.pages.ReflogPage; |
|
48 |
import com.gitblit.wicket.pages.TagPage; |
|
49 |
import com.gitblit.wicket.pages.TicketsPage; |
|
50 |
import com.gitblit.wicket.pages.TreePage; |
|
51 |
import com.gitblit.wicket.pages.UserPage; |
|
52 |
|
|
53 |
public class ReflogPanel extends BasePanel { |
|
54 |
|
|
55 |
private static final long serialVersionUID = 1L; |
|
56 |
|
|
57 |
private final boolean hasChanges; |
|
58 |
|
|
59 |
private boolean hasMore; |
|
60 |
|
|
61 |
public ReflogPanel(String wicketId, final RepositoryModel model, Repository r, int limit, int pageOffset) { |
|
62 |
super(wicketId); |
|
63 |
boolean pageResults = limit <= 0; |
|
64 |
int changesPerPage = app().settings().getInteger(Keys.web.reflogChangesPerPage, 10); |
|
65 |
if (changesPerPage <= 1) { |
|
66 |
changesPerPage = 10; |
|
67 |
} |
|
68 |
|
|
69 |
List<RefLogEntry> changes; |
|
70 |
if (pageResults) { |
|
71 |
changes = RefLogUtils.getLogByRef(model.name, r, pageOffset * changesPerPage, changesPerPage); |
|
72 |
} else { |
|
73 |
changes = RefLogUtils.getLogByRef(model.name, r, limit); |
|
74 |
} |
|
75 |
|
|
76 |
// inaccurate way to determine if there are more commits. |
|
77 |
// works unless commits.size() represents the exact end. |
|
78 |
hasMore = changes.size() >= changesPerPage; |
|
79 |
hasChanges = changes.size() > 0; |
|
80 |
|
|
81 |
setup(changes); |
|
82 |
|
|
83 |
// determine to show pager, more, or neither |
|
84 |
if (limit <= 0) { |
|
85 |
// no display limit |
|
86 |
add(new Label("moreChanges").setVisible(false)); |
|
87 |
} else { |
|
88 |
if (pageResults) { |
|
89 |
// paging |
|
90 |
add(new Label("moreChanges").setVisible(false)); |
|
91 |
} else { |
|
92 |
// more |
|
93 |
if (changes.size() == limit) { |
|
94 |
// show more |
|
95 |
add(new LinkPanel("moreChanges", "link", new StringResourceModel("gb.moreChanges", |
|
96 |
this, null), ReflogPage.class, |
|
97 |
WicketUtils.newRepositoryParameter(model.name))); |
|
98 |
} else { |
|
99 |
// no more |
|
100 |
add(new Label("moreChanges").setVisible(false)); |
|
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
} |
|
105 |
|
|
106 |
public ReflogPanel(String wicketId, List<RefLogEntry> changes) { |
|
107 |
super(wicketId); |
|
108 |
hasChanges = changes.size() > 0; |
|
109 |
setup(changes); |
|
110 |
add(new Label("moreChanges").setVisible(false)); |
|
111 |
} |
|
112 |
|
|
113 |
protected void setup(List<RefLogEntry> changes) { |
|
114 |
|
|
115 |
ListDataProvider<RefLogEntry> dp = new ListDataProvider<RefLogEntry>(changes); |
|
116 |
DataView<RefLogEntry> changeView = new DataView<RefLogEntry>("change", dp) { |
|
117 |
private static final long serialVersionUID = 1L; |
|
118 |
|
|
119 |
@Override |
|
120 |
public void populateItem(final Item<RefLogEntry> changeItem) { |
|
121 |
final RefLogEntry change = changeItem.getModelObject(); |
|
122 |
|
|
123 |
String dateFormat = app().settings().getString(Keys.web.datetimestampLongFormat, "EEEE, MMMM d, yyyy HH:mm Z"); |
|
124 |
TimeZone timezone = getTimeZone(); |
|
125 |
DateFormat df = new SimpleDateFormat(dateFormat); |
|
126 |
df.setTimeZone(timezone); |
|
127 |
Calendar cal = Calendar.getInstance(timezone); |
|
128 |
|
|
129 |
String fullRefName = change.getChangedRefs().get(0); |
|
130 |
String shortRefName = fullRefName; |
|
131 |
String ticketId = null; |
|
132 |
boolean isTag = false; |
|
133 |
boolean isTicket = false; |
|
134 |
if (shortRefName.startsWith(Constants.R_TICKET)) { |
|
135 |
ticketId = fullRefName.substring(Constants.R_TICKET.length()); |
|
136 |
shortRefName = MessageFormat.format(getString("gb.ticketN"), ticketId); |
|
137 |
isTicket = true; |
|
138 |
} else if (shortRefName.startsWith(Constants.R_HEADS)) { |
|
139 |
shortRefName = shortRefName.substring(Constants.R_HEADS.length()); |
|
140 |
} else if (shortRefName.startsWith(Constants.R_TAGS)) { |
|
141 |
shortRefName = shortRefName.substring(Constants.R_TAGS.length()); |
|
142 |
isTag = true; |
|
143 |
} |
|
144 |
|
|
145 |
String fuzzydate; |
|
146 |
TimeUtils tu = getTimeUtils(); |
|
147 |
Date changeDate = change.date; |
|
148 |
if (TimeUtils.isToday(changeDate, timezone)) { |
|
149 |
fuzzydate = tu.today(); |
|
150 |
} else if (TimeUtils.isYesterday(changeDate, timezone)) { |
|
151 |
fuzzydate = tu.yesterday(); |
|
152 |
} else { |
|
153 |
// calculate a fuzzy time ago date |
|
154 |
cal.setTime(changeDate); |
|
155 |
cal.set(Calendar.HOUR_OF_DAY, 0); |
|
156 |
cal.set(Calendar.MINUTE, 0); |
|
157 |
cal.set(Calendar.SECOND, 0); |
|
158 |
cal.set(Calendar.MILLISECOND, 0); |
|
159 |
Date date = cal.getTime(); |
|
160 |
fuzzydate = getTimeUtils().timeAgo(date); |
|
161 |
} |
|
162 |
changeItem.add(new Label("whenChanged", fuzzydate + ", " + df.format(changeDate))); |
|
163 |
|
|
164 |
Label changeIcon = new Label("changeIcon"); |
|
165 |
if (Type.DELETE.equals(change.getChangeType(fullRefName))) { |
|
166 |
WicketUtils.setCssClass(changeIcon, "iconic-trash-stroke"); |
|
167 |
} else if (isTag) { |
|
168 |
WicketUtils.setCssClass(changeIcon, "iconic-tag"); |
|
169 |
} else if (isTicket) { |
|
170 |
WicketUtils.setCssClass(changeIcon, "fa fa-ticket"); |
|
171 |
} else { |
|
172 |
WicketUtils.setCssClass(changeIcon, "iconic-upload"); |
|
173 |
} |
|
174 |
changeItem.add(changeIcon); |
|
175 |
|
|
176 |
if (change.user.username.equals(change.user.emailAddress) && change.user.emailAddress.indexOf('@') > -1) { |
|
177 |
// username is an email address - 1.2.1 push log bug |
|
178 |
changeItem.add(new Label("whoChanged", change.user.getDisplayName())); |
|
179 |
} else if (change.user.username.equals(UserModel.ANONYMOUS.username)) { |
|
180 |
// anonymous change |
|
181 |
changeItem.add(new Label("whoChanged", getString("gb.anonymousUser"))); |
|
182 |
} else { |
|
183 |
// link to user account page |
|
184 |
changeItem.add(new LinkPanel("whoChanged", null, change.user.getDisplayName(), |
|
185 |
UserPage.class, WicketUtils.newUsernameParameter(change.user.username))); |
|
186 |
} |
|
187 |
|
|
188 |
boolean isDelete = false; |
|
189 |
boolean isRewind = false; |
|
190 |
String what; |
|
191 |
String by = null; |
|
192 |
switch(change.getChangeType(fullRefName)) { |
|
193 |
case CREATE: |
|
194 |
if (isTag) { |
|
195 |
// new tag |
|
196 |
what = getString("gb.pushedNewTag"); |
|
197 |
} else { |
|
198 |
// new branch |
|
199 |
what = getString("gb.pushedNewBranch"); |
|
200 |
} |
|
201 |
break; |
|
202 |
case DELETE: |
|
203 |
isDelete = true; |
|
204 |
if (isTag) { |
|
205 |
what = getString("gb.deletedTag"); |
|
206 |
} else { |
|
207 |
what = getString("gb.deletedBranch"); |
|
208 |
} |
|
209 |
break; |
|
210 |
case UPDATE_NONFASTFORWARD: |
|
211 |
isRewind = true; |
|
212 |
default: |
|
213 |
what = MessageFormat.format(change.getCommitCount() > 1 ? getString("gb.pushedNCommitsTo") : getString("gb.pushedOneCommitTo"), change.getCommitCount()); |
|
214 |
|
|
215 |
if (change.getAuthorCount() == 1) { |
|
216 |
by = MessageFormat.format(getString("gb.byOneAuthor"), change.getAuthorIdent().getName()); |
|
217 |
} else { |
|
218 |
by = MessageFormat.format(getString("gb.byNAuthors"), change.getAuthorCount()); |
|
219 |
} |
|
220 |
break; |
|
221 |
} |
|
222 |
changeItem.add(new Label("whatChanged", what)); |
|
223 |
changeItem.add(new Label("byAuthors", by).setVisible(!StringUtils.isEmpty(by))); |
|
224 |
changeItem.add(new Label("refRewind", getString("gb.rewind")).setVisible(isRewind)); |
|
225 |
|
|
226 |
if (isDelete) { |
|
227 |
// can't link to deleted ref |
|
228 |
changeItem.add(new Label("refChanged", shortRefName)); |
|
229 |
} else if (isTag) { |
|
230 |
// link to tag |
|
231 |
changeItem.add(new LinkPanel("refChanged", null, shortRefName, |
|
232 |
TagPage.class, WicketUtils.newObjectParameter(change.repository, fullRefName))); |
|
233 |
} else if (isTicket) { |
|
234 |
// link to ticket |
|
235 |
changeItem.add(new LinkPanel("refChanged", null, shortRefName, |
|
236 |
TicketsPage.class, WicketUtils.newObjectParameter(change.repository, ticketId))); |
|
237 |
} else { |
|
238 |
// link to tree |
|
239 |
changeItem.add(new LinkPanel("refChanged", null, shortRefName, |
|
240 |
TreePage.class, WicketUtils.newObjectParameter(change.repository, fullRefName))); |
|
241 |
} |
|
242 |
|
|
243 |
int maxCommitCount = 5; |
|
244 |
List<RepositoryCommit> commits = change.getCommits(); |
|
245 |
if (commits.size() > maxCommitCount) { |
|
246 |
commits = new ArrayList<RepositoryCommit>(commits.subList(0, maxCommitCount)); |
|
247 |
} |
|
248 |
|
|
249 |
// compare link |
|
250 |
String compareLinkText = null; |
|
251 |
if ((change.getCommitCount() <= maxCommitCount) && (change.getCommitCount() > 1)) { |
|
252 |
compareLinkText = MessageFormat.format(getString("gb.viewComparison"), commits.size()); |
|
253 |
} else if (change.getCommitCount() > maxCommitCount) { |
|
254 |
int diff = change.getCommitCount() - maxCommitCount; |
|
255 |
compareLinkText = MessageFormat.format(diff > 1 ? getString("gb.nMoreCommits") : getString("gb.oneMoreCommit"), diff); |
|
256 |
} |
|
257 |
if (StringUtils.isEmpty(compareLinkText)) { |
|
258 |
changeItem.add(new Label("compareLink").setVisible(false)); |
|
259 |
} else { |
|
260 |
String endRangeId = change.getNewId(fullRefName); |
|
261 |
String startRangeId = change.getOldId(fullRefName); |
|
262 |
changeItem.add(new LinkPanel("compareLink", null, compareLinkText, ComparePage.class, WicketUtils.newRangeParameter(change.repository, startRangeId, endRangeId))); |
|
263 |
} |
|
264 |
|
|
265 |
ListDataProvider<RepositoryCommit> cdp = new ListDataProvider<RepositoryCommit>(commits); |
|
266 |
DataView<RepositoryCommit> commitsView = new DataView<RepositoryCommit>("commit", cdp) { |
|
267 |
private static final long serialVersionUID = 1L; |
|
268 |
|
|
269 |
@Override |
|
270 |
public void populateItem(final Item<RepositoryCommit> commitItem) { |
|
271 |
final RepositoryCommit commit = commitItem.getModelObject(); |
|
272 |
|
|
273 |
// author gravatar |
b57b9e
|
274 |
commitItem.add(new AvatarImage("commitAuthor", commit.getAuthorIdent(), null, 16, false)); |
5e3521
|
275 |
|
JM |
276 |
// merge icon |
|
277 |
if (commit.getParentCount() > 1) { |
|
278 |
commitItem.add(WicketUtils.newImage("commitIcon", "commit_merge_16x16.png")); |
|
279 |
} else { |
|
280 |
commitItem.add(WicketUtils.newBlankImage("commitIcon")); |
|
281 |
} |
|
282 |
|
|
283 |
// short message |
|
284 |
String shortMessage = commit.getShortMessage(); |
|
285 |
String trimmedMessage = shortMessage; |
|
286 |
if (commit.getRefs() != null && commit.getRefs().size() > 0) { |
|
287 |
trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG_REFS); |
|
288 |
} else { |
|
289 |
trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG); |
|
290 |
} |
|
291 |
LinkPanel shortlog = new LinkPanel("commitShortMessage", "list", |
|
292 |
trimmedMessage, CommitPage.class, WicketUtils.newObjectParameter( |
|
293 |
change.repository, commit.getName())); |
|
294 |
if (!shortMessage.equals(trimmedMessage)) { |
|
295 |
WicketUtils.setHtmlTooltip(shortlog, shortMessage); |
|
296 |
} |
|
297 |
commitItem.add(shortlog); |
|
298 |
|
|
299 |
// commit hash link |
|
300 |
int hashLen = app().settings().getInteger(Keys.web.shortCommitIdLength, 6); |
|
301 |
LinkPanel commitHash = new LinkPanel("hashLink", null, commit.getName().substring(0, hashLen), |
|
302 |
CommitPage.class, WicketUtils.newObjectParameter( |
|
303 |
change.repository, commit.getName())); |
|
304 |
WicketUtils.setCssClass(commitHash, "shortsha1"); |
|
305 |
WicketUtils.setHtmlTooltip(commitHash, commit.getName()); |
|
306 |
commitItem.add(commitHash); |
|
307 |
} |
|
308 |
}; |
|
309 |
|
|
310 |
changeItem.add(commitsView); |
|
311 |
} |
|
312 |
}; |
|
313 |
|
|
314 |
add(changeView); |
|
315 |
} |
|
316 |
|
|
317 |
public boolean hasMore() { |
|
318 |
return hasMore; |
|
319 |
} |
|
320 |
|
|
321 |
public boolean hideIfEmpty() { |
|
322 |
setVisible(hasChanges); |
|
323 |
return hasChanges; |
|
324 |
} |
|
325 |
} |