James Moger
2013-05-31 9b26b74d198aa4efbe4b25f6667b98eb5261e13d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Copyright 2013 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit.wicket.panels;
 
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
 
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.StringResourceModel;
import org.eclipse.jgit.lib.Repository;
 
import com.gitblit.Constants;
import com.gitblit.GitBlit;
import com.gitblit.Keys;
import com.gitblit.models.DailyLogEntry;
import com.gitblit.models.PushLogEntry;
import com.gitblit.models.RepositoryCommit;
import com.gitblit.models.RepositoryModel;
import com.gitblit.utils.PushLogUtils;
import com.gitblit.utils.StringUtils;
import com.gitblit.utils.TimeUtils;
import com.gitblit.wicket.WicketUtils;
import com.gitblit.wicket.pages.CommitPage;
import com.gitblit.wicket.pages.ComparePage;
import com.gitblit.wicket.pages.PushesPage;
import com.gitblit.wicket.pages.SummaryPage;
import com.gitblit.wicket.pages.TagPage;
import com.gitblit.wicket.pages.TreePage;
import com.gitblit.wicket.pages.UserPage;
 
public class PushesPanel extends BasePanel {
 
    private static final long serialVersionUID = 1L;
 
    private final boolean hasPushes;
    
    private boolean hasMore;
 
    public PushesPanel(String wicketId, final RepositoryModel model, Repository r, int limit, int pageOffset, boolean showRepo) {
        super(wicketId);
        boolean pageResults = limit <= 0;
        int pushesPerPage = GitBlit.getInteger(Keys.web.pushesPerPage, 10);
        if (pushesPerPage <= 1) {
            pushesPerPage = 10;
        }
 
        List<PushLogEntry> pushes;
        if (pageResults) {
            pushes = PushLogUtils.getPushLogByRef(model.name, r, pageOffset * pushesPerPage, pushesPerPage);
        } else {
            pushes = PushLogUtils.getPushLogByRef(model.name, r, limit);
        }
 
        // inaccurate way to determine if there are more commits.
        // works unless commits.size() represents the exact end.
        hasMore = pushes.size() >= pushesPerPage;
        hasPushes = pushes.size() > 0;
        
        setup(pushes, showRepo);
        
        // determine to show pager, more, or neither
        if (limit <= 0) {
            // no display limit
            add(new Label("morePushes").setVisible(false));
        } else {
            if (pageResults) {
                // paging
                add(new Label("morePushes").setVisible(false));
            } else {
                // more
                if (pushes.size() == limit) {
                    // show more
                    add(new LinkPanel("morePushes", "link", new StringResourceModel("gb.morePushes",
                            this, null), PushesPage.class,
                            WicketUtils.newRepositoryParameter(model.name)));
                } else {
                    // no more
                    add(new Label("morePushes").setVisible(false));
                }
            }
        }
    }
    
    public PushesPanel(String wicketId, List<PushLogEntry> pushes) {
        super(wicketId);
        hasPushes = pushes.size() > 0;
        setup(pushes, true);
        add(new Label("morePushes").setVisible(false));
    }
    
    protected void setup(List<PushLogEntry> pushes, final boolean showRepo) {
        final int hashLen = GitBlit.getInteger(Keys.web.shortCommitIdLength, 6);
 
        String dateFormat = GitBlit.getString(Keys.web.datestampLongFormat, "EEEE, MMMM d, yyyy");
        final TimeZone timezone = getTimeZone();
        final DateFormat df = new SimpleDateFormat(dateFormat);
        df.setTimeZone(timezone);
        final Calendar cal = Calendar.getInstance(timezone);
        
        ListDataProvider<PushLogEntry> dp = new ListDataProvider<PushLogEntry>(pushes);
        DataView<PushLogEntry> pushView = new DataView<PushLogEntry>("push", dp) {
            private static final long serialVersionUID = 1L;
 
            public void populateItem(final Item<PushLogEntry> pushItem) {
                final PushLogEntry push = pushItem.getModelObject();
                String fullRefName = push.getChangedRefs().get(0);
                String shortRefName = fullRefName;
                boolean isTag = false;
                boolean isPull = false;
                if (shortRefName.startsWith(Constants.R_HEADS)) {
                    shortRefName = shortRefName.substring(Constants.R_HEADS.length());
                } else if (shortRefName.startsWith(Constants.R_TAGS)) {
                    shortRefName = shortRefName.substring(Constants.R_TAGS.length());
                    isTag = true;
                } else if (shortRefName.startsWith(Constants.R_PULL)) {
                    shortRefName = "#" + shortRefName.substring(Constants.R_PULL.length());
                    if (shortRefName.endsWith("/head")) {
                        // strip pull request head from name 
                        shortRefName = shortRefName.substring(0, shortRefName.length() - "/head".length());
                    }                    
                    isPull = true;
                }
                boolean isDigest = push instanceof DailyLogEntry;
                
                String fuzzydate;
                TimeUtils tu = getTimeUtils();
                Date pushDate = push.date;
                if (TimeUtils.isToday(pushDate, timezone)) {
                    fuzzydate = tu.today();
                } else if (TimeUtils.isYesterday(pushDate, timezone)) {
                    fuzzydate = tu.yesterday();
                } else {
                    // calculate a fuzzy time ago date
                    cal.setTime(pushDate);
                    cal.set(Calendar.HOUR_OF_DAY, 0);
                    cal.set(Calendar.MINUTE, 0);
                    cal.set(Calendar.SECOND, 0);
                    cal.set(Calendar.MILLISECOND, 0);
                    pushDate = cal.getTime();
                    fuzzydate = getTimeUtils().timeAgo(pushDate);
                }
                pushItem.add(new Label("whenPushed", fuzzydate + ", " + df.format(pushDate)));
 
                Label pushIcon = new Label("pushIcon");
                if (showRepo) {
                    // if we are showing the repo, we are showing multiple
                    // repos.  use the repository hash color to differentiate
                    // the icon.
                    String color = StringUtils.getColor(StringUtils.stripDotGit(push.repository));
                    WicketUtils.setCssStyle(pushIcon, "color: " + color);
                }
                if (isTag) {
                    WicketUtils.setCssClass(pushIcon, "iconic-tag");
                } else if (isPull) {
                    WicketUtils.setCssClass(pushIcon, "iconic-share");
                } else if (isDigest) {
                    WicketUtils.setCssClass(pushIcon, "iconic-loop");
                } else {
                    WicketUtils.setCssClass(pushIcon, "iconic-upload");
                }
                pushItem.add(pushIcon);
 
                if (isDigest && !isTag) {
                    pushItem.add(new Label("whoPushed").setVisible(false));
                } else {
                    if (push.user.username.equals(push.user.emailAddress) && push.user.emailAddress.indexOf('@') > -1) {
                        // username is an email address - 1.2.1 push log bug
                        pushItem.add(new Label("whoPushed", push.user.getDisplayName()));
                    } else {
                        // link to user account page
                        pushItem.add(new LinkPanel("whoPushed", null, push.user.getDisplayName(),
                                UserPage.class, WicketUtils.newUsernameParameter(push.user.username)));
                    }
                }
                
                String preposition = "gb.of";
                boolean isDelete = false;
                boolean isRewind = false;
                String what;
                String by = null;
                switch(push.getChangeType(fullRefName)) {
                case CREATE:
                    if (isTag) {
                        // new tag
                        if (isDigest) {
                            what = getString("gb.createdNewTag");
                            preposition = "gb.in";
                        } else {
                            what = getString("gb.pushedNewTag");
                            preposition = "gb.to";
                        }
                    } else if (isPull) {
                        // merged pull request
                        what = getString("gb.mergedPullRequest");
                        preposition = "gb.in";
                    } else {
                        // new branch
                        if (isDigest) {
                            what = getString("gb.createdNewBranch");
                            preposition = "gb.in";
                        } else {
                            what = getString("gb.pushedNewBranch");
                            preposition = "gb.to";
                        }
                    }
                    break;
                case DELETE:
                    isDelete = true;
                    if (isTag) {
                        what = getString("gb.deletedTag");
                    } if (isPull) {
                        what = getString("gb.deletedTag");
                    } else {
                        what = getString("gb.deletedBranch");
                    }
                    preposition = "gb.from";
                    break;
                case UPDATE_NONFASTFORWARD:
                    isRewind = true;
                default:
                    if (isDigest) {
                        what = MessageFormat.format(push.getCommitCount() > 1 ? getString("gb.commitsTo") : getString("gb.oneCommitTo"), push.getCommitCount());
                    } else {
                        what = MessageFormat.format(push.getCommitCount() > 1 ? getString("gb.pushedNCommitsTo") : getString("gb.pushedOneCommitTo") , push.getCommitCount());
                    }
                    
                    if (push.getAuthorCount() == 1) {
                        by = MessageFormat.format(getString("gb.byOneAuthor"), push.getAuthorIdent().getName());
                    } else {
                        by = MessageFormat.format(getString("gb.byNAuthors"), push.getAuthorCount());    
                    }
                    break;
                }
                pushItem.add(new Label("whatPushed", what));
                pushItem.add(new Label("byAuthors", by).setVisible(!StringUtils.isEmpty(by)));
                
                pushItem.add(new Label("refRewind", getString("gb.rewind")).setVisible(isRewind));
                
                if (isDelete) {
                    // can't link to deleted ref
                    pushItem.add(new Label("refPushed", shortRefName));
                } else if (isTag) {
                    // link to tag
                    pushItem.add(new LinkPanel("refPushed", null, shortRefName,
                            TagPage.class, WicketUtils.newObjectParameter(push.repository, fullRefName)));
                } else if (isPull) {
                    // link to pull request
                    pushItem.add(new LinkPanel("refPushed", null, shortRefName,
                            TagPage.class, WicketUtils.newObjectParameter(push.repository, fullRefName)));
                } else {
                    // link to tree
                    pushItem.add(new LinkPanel("refPushed", null, shortRefName,
                        TreePage.class, WicketUtils.newObjectParameter(push.repository, fullRefName)));
                }
                
                if (showRepo) {
                    // to/from/etc
                    pushItem.add(new Label("repoPreposition", getString(preposition)));
 
                    String repoName = StringUtils.stripDotGit(push.repository);
                    pushItem.add(new LinkPanel("repoPushed", null, repoName,
                            SummaryPage.class, WicketUtils.newRepositoryParameter(push.repository)));
                } else {
                    // do not display repository name if we are viewing the push
                    // log of a repository.
                    pushItem.add(new Label("repoPreposition").setVisible(false));
                    pushItem.add(new Label("repoPushed").setVisible(false));
                }
                
                int maxCommitCount = 5;
                List<RepositoryCommit> commits = push.getCommits();
                if (commits.size() > maxCommitCount) {
                    commits = new ArrayList<RepositoryCommit>(commits.subList(0,  maxCommitCount));                    
                }
                
                // compare link
                String compareLinkText = null;
                if ((push.getCommitCount() <= maxCommitCount) && (push.getCommitCount() > 1)) {
                    compareLinkText = MessageFormat.format(getString("gb.viewComparison"), commits.size());
                } else if (push.getCommitCount() > maxCommitCount) {
                    int diff = push.getCommitCount() - maxCommitCount;
                    compareLinkText = MessageFormat.format(diff > 1 ? getString("gb.nMoreCommits") : getString("gb.oneMoreCommit"), diff);
                }
                if (StringUtils.isEmpty(compareLinkText)) {
                    pushItem.add(new Label("compareLink").setVisible(false));
                } else {
                    String endRangeId = push.getNewId(fullRefName);
                    String startRangeId = push.getOldId(fullRefName);
                    pushItem.add(new LinkPanel("compareLink", null, compareLinkText, ComparePage.class, WicketUtils.newRangeParameter(push.repository, startRangeId, endRangeId)));
                }
                
                final boolean showSwatch = showRepo && GitBlit.getBoolean(Keys.web.repositoryListSwatches, true);
                
                ListDataProvider<RepositoryCommit> cdp = new ListDataProvider<RepositoryCommit>(commits);
                DataView<RepositoryCommit> commitsView = new DataView<RepositoryCommit>("commit", cdp) {
                    private static final long serialVersionUID = 1L;
 
                    public void populateItem(final Item<RepositoryCommit> commitItem) {
                        final RepositoryCommit commit = commitItem.getModelObject();
 
                        // author gravatar
                        commitItem.add(new GravatarImage("commitAuthor", commit.getAuthorIdent().getName(),
                                commit.getAuthorIdent().getEmailAddress(), null, 16, false, false));
                        
                        // merge icon
                        if (commit.getParentCount() > 1) {
                            commitItem.add(WicketUtils.newImage("commitIcon", "commit_merge_16x16.png"));
                        } else {
                            commitItem.add(WicketUtils.newBlankImage("commitIcon"));
                        }
 
                        // short message
                        String shortMessage = commit.getShortMessage();
                        String trimmedMessage = shortMessage;
                        if (commit.getRefs() != null && commit.getRefs().size() > 0) {
                            trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG_REFS);
                        } else {
                            trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG);
                        }
                        LinkPanel shortlog = new LinkPanel("commitShortMessage", "list",
                                trimmedMessage, CommitPage.class, WicketUtils.newObjectParameter(
                                        push.repository, commit.getName()));
                        if (!shortMessage.equals(trimmedMessage)) {
                            WicketUtils.setHtmlTooltip(shortlog, shortMessage);
                        }
                        commitItem.add(shortlog);
 
                        // commit hash link
                        LinkPanel commitHash = new LinkPanel("hashLink", null, commit.getName().substring(0, hashLen),
                                CommitPage.class, WicketUtils.newObjectParameter(
                                        push.repository, commit.getName()));
                        WicketUtils.setCssClass(commitHash, "shortsha1");
                        WicketUtils.setHtmlTooltip(commitHash, commit.getName());
                        commitItem.add(commitHash);
                        
                        if (showSwatch) {
                            // set repository color
                            String color = StringUtils.getColor(StringUtils.stripDotGit(push.repository));
                            WicketUtils.setCssStyle(commitItem, MessageFormat.format("border-left: 2px solid {0};", color));
                        }
                    }
                };
 
                pushItem.add(commitsView);
            }
        };
        
        add(pushView);
    }
 
    public boolean hasMore() {
        return hasMore;
    }
    
    public boolean hideIfEmpty() {
        setVisible(hasPushes);
        return hasPushes;
    }
}