Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4a4911e1c4f266fdbef62e00be817ceb1cc39df1 (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, 2012 GitHub Inc. and others.
 *  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
 *    François Rey - gracefully ignore linked resources
 *****************************************************************************/
package org.eclipse.egit.ui.internal.actions;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.egit.core.internal.job.JobUtil;
import org.eclipse.egit.core.internal.storage.CommitFileRevision;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.egit.ui.JobFamilies;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.blame.BlameOperation;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * Show blame annotations action handler
 */
public class ShowBlameActionHandler extends RepositoryActionHandler {

	/** @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent) */
	@Override
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		Data data = getData(getSelection(event));

		if (data == null)
			return null;

		Repository repository = data.repository;
		String path = data.repositoryRelativePath;
		IStorage storage = data.storage;
		RevCommit startCommit = data.startCommit;
		Shell shell = HandlerUtil.getActiveShell(event);
		IWorkbenchPage page = HandlerUtil.getActiveSite(event).getPage();
		JobUtil.scheduleUserJob(new BlameOperation(repository, storage, path,
				startCommit, shell, page), UIText.ShowBlameHandler_JobName,
				JobFamilies.BLAME);
		return null;
	}

	@Override
	public boolean isEnabled() {
		IStructuredSelection selection = getSelection();
		return getData(selection) != null;
	}

	private static Data getData(IStructuredSelection selection) {
		if (selection.size() != 1)
			return null;

		Object element = selection.getFirstElement();
		if (element instanceof IResource) {
			IResource resource = (IResource) element;

			if (resource instanceof IStorage) {
				IStorage storage = (IStorage) resource;
				RepositoryMapping mapping = RepositoryMapping
						.getMapping(resource);

				if (mapping != null) {
					String repoRelativePath = mapping
							.getRepoRelativePath(resource);
					return new Data(mapping.getRepository(), repoRelativePath,
							storage, null);
				}
			}
		} else if (element instanceof CommitFileRevision) {
			CommitFileRevision revision = (CommitFileRevision) element;
			try {
				IStorage storage = revision.getStorage(new NullProgressMonitor());
				return new Data(revision.getRepository(),
						revision.getGitPath(), storage, revision.getRevCommit());
			} catch (CoreException e) {
				// Don't enable
				return null;
			}
		}
		return null;
	}

	private static class Data {
		private final Repository repository;
		private final String repositoryRelativePath;
		private final IStorage storage;
		private final RevCommit startCommit;

		public Data(Repository repository, String repositoryRelativePath,
				IStorage storage, RevCommit startCommit) {
			this.repository = repository;
			this.repositoryRelativePath = repositoryRelativePath;
			this.storage = storage;
			this.startCommit = startCommit;
		}
	}
}

Back to the top