Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 816d0337fddae9dc71a7f1ec72671b38d8d553d8 (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
/******************************************************************************
 *  Copyright (c) 2012, 2016 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
 *****************************************************************************/
package org.eclipse.egit.ui.internal.history.command;

import java.io.File;
import java.io.IOException;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
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.Activator;
import org.eclipse.egit.ui.JobFamilies;
import org.eclipse.egit.ui.internal.CompareUtils;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.blame.BlameOperation;
import org.eclipse.egit.ui.internal.history.GitHistoryPage;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.team.core.history.IFileRevision;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * Handler to blame a path on a selected commit
 */
public class ShowBlameHandler extends AbstractHistoryCommandHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		GitHistoryPage page = getPage(event);
		if (page == null) {
			return null;
		}
		Object input = page.getInputInternal().getSingleItem();
		if (input == null) {
			return null;
		}
		Repository repo = getRepository(event);
		if (repo == null) {
			return null;
		}
		String path = getPath(repo, page);
		if (path == null) {
			return null;
		}
		RevCommit commit = getSelectedCommit(event);
		if (commit == null) {
			return null;
		}
		try {
			IFileRevision revision = CompareUtils.getFileRevision(path, commit,
					repo, null);
			if (revision instanceof CommitFileRevision) {
				BlameOperation op = new BlameOperation(
						(CommitFileRevision) revision,
						HandlerUtil.getActiveShell(event),
						page.getSite().getPage());
				JobUtil.scheduleUserJob(op, UIText.ShowBlameHandler_JobName,
						JobFamilies.BLAME);
			}
		} catch (IOException e) {
			Activator.showError(UIText.ShowBlameHandler_errorMessage, e);
		}
		return null;
	}

	private String getPath(Repository repo, GitHistoryPage page) {
		Object input = page.getInputInternal().getSingleItem();
		if (input == null) {
			return null;
		}
		if (input instanceof IFile) {
			IFile file = (IFile) input;
			RepositoryMapping mapping = RepositoryMapping.getMapping(file);
			if (mapping != null) {
				return mapping.getRepoRelativePath(file);
			}
		} else if (input instanceof File) {
			return getRepoRelativePath(repo, (File) input);
		}
		return null;
	}
}

Back to the top