summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/gitblit/wicket/panels/PushesPanel.java
blob: bab9c9e9814346328bffdb58eb0ce6b81ef47898 (plain)
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
/*
 * Copyright 2011 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.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.eclipse.jgit.lib.Repository;

import com.gitblit.Constants;
import com.gitblit.GitBlit;
import com.gitblit.Keys;
import com.gitblit.models.PushLogEntry;
import com.gitblit.models.RepositoryCommit;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.UserModel;
import com.gitblit.utils.PushLogUtils;
import com.gitblit.utils.StringUtils;
import com.gitblit.wicket.WicketUtils;
import com.gitblit.wicket.pages.CommitPage;
import com.gitblit.wicket.pages.GitSearchPage;
import com.gitblit.wicket.pages.SummaryPage;
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) {
		super(wicketId);
		boolean pageResults = limit <= 0;
		int itemsPerPage = GitBlit.getInteger(Keys.web.itemsPerPage, 50);
		if (itemsPerPage <= 1) {
			itemsPerPage = 50;
		}

		final Map<String, String> usernameLookup = new HashMap<String, String>();
		final int hashLen = GitBlit.getInteger(Keys.web.shortCommitIdLength, 6);
		List<PushLogEntry> entries = PushLogUtils.getPushLog(model.name, r, limit);
		// establish pusher identities
		for (PushLogEntry push : entries) {
			// handle push logs with email address instead of account name
			String username = push.user.username;
			if (push.user.username.indexOf('@') > -1) {
				// push username is an email address, reverse lookup for account
				if (!usernameLookup.containsKey(push.user.username)) {
					for (UserModel user : GitBlit.self().getAllUsers()) {
						if (push.user.username.equals(user.emailAddress)) {
							username = user.username;
							usernameLookup.put(push.user.username, username);
							break;
						}
					}
				} else {
					username = usernameLookup.get(push.user.username);
				}
			} else {
				// push username is an account name, lookup for email address
				if (!usernameLookup.containsKey(push.user.username)) {
					UserModel user = GitBlit.self().getUserModel(push.user.username);
					if (user != null) {
						push.user.emailAddress = user.emailAddress;
						usernameLookup.put(push.user.username, user.emailAddress);
					}
				} else {
					push.user.emailAddress = usernameLookup.get(push.user.username);
				}
			}
		}
		
		hasPushes = entries.size() > 0;
		
		ListDataProvider<PushLogEntry> dp = new ListDataProvider<PushLogEntry>(entries);
		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();
				
				
				pushItem.add(new GravatarImage("whoAvatar", push.getCommitterIdent(), 40));
				pushItem.add(new LinkPanel("whoPushed", null, push.user.getDisplayName(),
						UserPage.class, WicketUtils.newUsernameParameter(push.user.username)));
				pushItem.add(new Label("whatPushed", 
						MessageFormat.format(push.getCommitCount() > 1 ? "pushed {0} commits to":"pushed 1 commit to", push.getCommitCount())));
				String repoName = StringUtils.stripDotGit(model.name);
				pushItem.add(new LinkPanel("wherePushed", null, repoName,
						SummaryPage.class, WicketUtils.newRepositoryParameter(model.name)));
				pushItem.add(WicketUtils.createDateLabel("whenPushed", push.date, getTimeZone(), getTimeUtils()));

				ListDataProvider<RepositoryCommit> cdp = new ListDataProvider<RepositoryCommit>(push.getCommits());
				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 search link
						String author = commit.getAuthorIdent().getName();
						LinkPanel authorLink = new LinkPanel("commitAuthor", "list", author,
								GitSearchPage.class, WicketUtils.newSearchParameter(model.name,
										null, author, Constants.SearchType.AUTHOR));
						setPersonSearchTooltip(authorLink, author, Constants.SearchType.AUTHOR);
						commitItem.add(authorLink);
						
						// 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(
										model.name, commit.getName()));
						if (!shortMessage.equals(trimmedMessage)) {
							WicketUtils.setHtmlTooltip(shortlog, shortMessage);
						}
						commitItem.add(shortlog);

						commitItem.add(new RefsPanel("commitRefs", commit.repository, commit.getRefs()));

						// commit hash link
						LinkPanel commitHash = new LinkPanel("hashLink", null, commit.getName().substring(0, hashLen),
								CommitPage.class, WicketUtils.newObjectParameter(
										model.name, commit.getName()));
						WicketUtils.setCssClass(commitHash, "shortsha1");
						WicketUtils.setHtmlTooltip(commitHash, commit.getName());
						commitItem.add(commitHash);
						
//						item.add(new BookmarkablePageLink<Void>("diff", CommitDiffPage.class, WicketUtils
//								.newObjectParameter(repositoryName, entry.getName())).setEnabled(entry
//								.getParentCount() > 0));
//						item.add(new BookmarkablePageLink<Void>("tree", TreePage.class, WicketUtils
//								.newObjectParameter(repositoryName, entry.getName())));
					}
				};
				
				pushItem.add(commitsView);
			}
		};
		add(pushView);

		// determine to show pager, more, or neither
//		if (limit <= 0) {
//			// no display limit
//			add(new Label("moreLogs", "").setVisible(false));
//		} else {
//			if (pageResults) {
//				// paging
//				add(new Label("moreLogs", "").setVisible(false));
//			} else {
//				// more
//				if (commits.size() == limit) {
//					// show more
//					add(new LinkPanel("moreLogs", "link", new StringResourceModel("gb.moreLogs",
//							this, null), LogPage.class,
//							WicketUtils.newRepositoryParameter(repositoryName)));
//				} else {
//					// no more
//					add(new Label("moreLogs", "").setVisible(false));
//				}
//			}
//		}
	}

	public boolean hasMore() {
		return hasMore;
	}
	
	public boolean hideIfEmpty() {
		setVisible(hasPushes);
		return hasPushes;
	}
}