Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5370dea5a5d9b65d7a67d3c97b6619ed8f106f2a (plain) (blame)
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
/*******************************************************************************
 *  Copyright (c) 2011, 2017 GitHub Inc. and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *    Robin Stocker (independent)
 *    Thomas Wolf <thomas.wolf@paranor.ch> - Bug 477248
 *******************************************************************************/
package org.eclipse.egit.ui.internal.commit;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.egit.core.internal.IRepositoryCommit;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.internal.PreferenceBasedDateFormatter;
import org.eclipse.egit.ui.internal.UIIcons;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.history.FileDiff;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.notes.Note;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.eclipse.ui.model.WorkbenchAdapter;

/**
 * Class that encapsulates a particular {@link Repository} instance and
 * {@link RevCommit} instance.
 *
 * This class computes and provides access to the {@link FileDiff} objects
 * introduced by the commit.
 */
public class RepositoryCommit extends WorkbenchAdapter
		implements IAdaptable, IRepositoryCommit {

	/**
	 * NAME_LENGTH
	 */
	public static final int NAME_LENGTH = 8;

	private Repository repository;

	private RevCommit commit;

	private FileDiff[] diffs;

	private RepositoryCommitNote[] notes;

	/**
	 * Marks this commit as a stash commit.
	 */
	private boolean stash;

	/**
	 * Create a repository commit
	 *
	 * @param repository
	 * @param commit
	 */
	public RepositoryCommit(Repository repository, RevCommit commit) {
		Assert.isNotNull(repository, "Repository cannot be null"); //$NON-NLS-1$
		Assert.isNotNull(commit, "Commit cannot be null"); //$NON-NLS-1$
		this.repository = repository;
		this.commit = commit;
	}

	@Override
	public Object getAdapter(Class adapter) {
		if (Repository.class == adapter)
			return repository;

		if (RevCommit.class == adapter)
			return commit;

		return Platform.getAdapterManager().getAdapter(this, adapter);
	}

	/**
	 * Abbreviate commit id to {@link #NAME_LENGTH} size.
	 *
	 * @return abbreviated commit id
	 */
	public String abbreviate() {
		return commit.abbreviate(NAME_LENGTH).name();
	}

	/**
	 * Get repository name
	 *
	 * @return repo name
	 */
	public String getRepositoryName() {
		if (!repository.isBare())
			return repository.getDirectory().getParentFile().getName();
		else
			return repository.getDirectory().getName();
	}

	/**
	 * Get repository
	 *
	 * @return repository
	 */
	@Override
	public Repository getRepository() {
		return repository;
	}

	/**
	 * Get rev commit
	 *
	 * @return rev commit
	 */
	@Override
	public RevCommit getRevCommit() {
		return commit;
	}

	/**
	 * Get the changes between this commit and all parent commits
	 *
	 * @return non-null but possibly empty array of {@link FileDiff} instances.
	 */
	public FileDiff[] getDiffs() {
		if (diffs == null) {
			RevCommit[] parents = commit.getParents();
			if (isStash() && commit.getParentCount() > 0)
				parents = new RevCommit[] { commit.getParent(0) };

			try (RevWalk revWalk = new RevWalk(repository);
					TreeWalk treewalk = new TreeWalk(revWalk.getObjectReader())) {
				treewalk.setRecursive(true);
				treewalk.setFilter(TreeFilter.ANY_DIFF);
				for (RevCommit parent : commit.getParents())
					revWalk.parseBody(parent);
				diffs = FileDiff.compute(repository, treewalk, commit, parents,
						null, TreeFilter.ALL);
			} catch (IOException e) {
				diffs = new FileDiff[0];
			}
		}
		return diffs;
	}

	/**
	 * Gets the changes between this commit and specific parent commits
	 *
	 * @param parents
	 *            parents to which the current commit is compared
	 *
	 * @return non-null but possibly empty array of {@link FileDiff} instances.
	 */
	public FileDiff[] getDiffs(RevCommit... parents) {
		FileDiff[] diffsResult = null;
		try (RevWalk revWalk = new RevWalk(repository);
				TreeWalk treewalk = new TreeWalk(revWalk.getObjectReader())) {
			treewalk.setRecursive(true);
			treewalk.setFilter(TreeFilter.ANY_DIFF);
			loadParents();
			diffsResult = FileDiff.compute(repository, treewalk, commit,
					parents, null, TreeFilter.ALL);
		} catch (IOException e) {
			diffsResult = new FileDiff[0];
		}
		return diffsResult;
	}

	private void loadParents() throws IOException {
		try (RevWalk revWalk = new RevWalk(repository)) {
			for (RevCommit parent : commit.getParents())
				revWalk.parseBody(parent);
		}
	}

	/**
	 * Get notes for this commit.
	 *
	 * @return non-null but possibly empty array of {@link RepositoryCommitNote}
	 *         instances.
	 */
	public RepositoryCommitNote[] getNotes() {
		if (notes == null) {
			List<RepositoryCommitNote> noteList = new ArrayList<>();
			try {
				Repository repo = getRepository();
				Git git = Git.wrap(repo);
				RevCommit revCommit = getRevCommit();
				for (Ref ref : repo.getRefDatabase()
						.getRefsByPrefix(Constants.R_NOTES)) {
					Note note = git.notesShow().setNotesRef(ref.getName())
							.setObjectId(revCommit).call();
					if (note != null)
						noteList.add(new RepositoryCommitNote(this, ref, note));
				}
				notes = noteList.toArray(new RepositoryCommitNote[noteList
						.size()]);
			} catch (Exception e) {
				Activator.logError("Error showing notes", e); //$NON-NLS-1$
				notes = new RepositoryCommitNote[0];
			}
		}
		return notes;
	}

	@Override
	public Object[] getChildren(Object o) {
		return new Object[0];
	}

	@Override
	public ImageDescriptor getImageDescriptor(Object object) {
		return UIIcons.CHANGESET;
	}

	@Override
	public String getLabel(Object o) {
		return abbreviate();
	}

	@Override
	public Object getParent(Object o) {
		return null;
	}

	/**
	 * @param object
	 * @return styled text
	 */
	@Override
	public StyledString getStyledText(Object object) {
		StyledString styled = new StyledString();
		styled.append(abbreviate());
		styled.append(": "); //$NON-NLS-1$
		styled.append(commit.getShortMessage());

		PersonIdent author = commit.getAuthorIdent();
		PersonIdent committer = commit.getCommitterIdent();
		if (author != null && committer != null) {
			PreferenceBasedDateFormatter formatter = PreferenceBasedDateFormatter
					.create();
			if (author.getName().equals(committer.getName())) {
				styled.append(
						MessageFormat.format(UIText.RepositoryCommit_AuthorDate,
								author.getName(), formatter.formatDate(author)),
						StyledString.QUALIFIER_STYLER);
			} else {
				styled.append(MessageFormat.format(
						UIText.RepositoryCommit_AuthorDateCommitter,
								author.getName(), formatter.formatDate(author),
						committer.getName()), StyledString.QUALIFIER_STYLER);
			}
		}
		return styled;
	}

	/**
	 * Marks this commit as a stash commit.
	 *
	 * @param stash
	 *            true whether this is a stash commit
	 */
	public void setStash(boolean stash) {
		this.stash = stash;
	}

	/**
	 * Whether this is a stash commit.
	 *
	 * @return true if this is a stash commit
	 */
	public boolean isStash() {
		return stash;
	}

}

Back to the top