Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 101e187a434f8c839d5e77324bf59907fc63685d (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
/******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/
package org.eclipse.egit.ui.internal.commit;

import java.io.IOException;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.internal.UIIcons;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.notes.Note;
import org.eclipse.jgit.notes.NoteMap;
import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.RawParseUtils;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
 * Repository commit note class.
 */
public class RepositoryCommitNote extends PlatformObject implements
		IWorkbenchAdapter {

	private RepositoryCommit commit;

	private Ref ref;

	private Note note;

	/**
	 * Create repository commit note
	 *
	 * @param commit
	 * @param ref
	 * @param note
	 */
	public RepositoryCommitNote(RepositoryCommit commit, Ref ref, Note note) {
		Assert.isNotNull(commit, "Commit cannot be null"); //$NON-NLS-1$
		Assert.isNotNull(ref, "Ref cannot be null"); //$NON-NLS-1$
		Assert.isNotNull(note, "Note cannot be null"); //$NON-NLS-1$
		this.commit = commit;
		this.ref = ref;
		this.note = note;
	}

	/**
	 * Get note text. This method open and read the note blob each time it is
	 * called.
	 *
	 * @return note text or empty string if lookup failed.
	 */
	public String getNoteText() {
		try {
			ObjectLoader loader = commit.getRepository().open(note.getData(),
					Constants.OBJ_BLOB);
			byte[] contents;
			if (loader.isLarge())
				contents = IO.readWholeStream(loader.openStream(),
						(int) loader.getSize()).array();
			else
				contents = loader.getCachedBytes();
			return RawParseUtils.decode(contents);
		} catch (IOException e) {
			Activator.logError("Error loading note text", e); //$NON-NLS-1$
		}
		return ""; //$NON-NLS-1$
	}

	@Override
	public Object getAdapter(Class adapter) {
		if (RepositoryCommit.class == adapter)
			return this.commit;

		if (Repository.class == adapter)
			return this.commit.getRepository();

		return super.getAdapter(adapter);
	}

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

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

	@Override
	public String getLabel(Object o) {
		return NoteMap.shortenRefName(ref.getName());
	}

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

}

Back to the top