Have icon for "reset" entry in reflog
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / variables / GitTemplateVariableResolver.java
blob5b8dbe97b3ee795a34223e98ea7507129b039b0a
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Kyle J. Harms <harmsk@seas.wustl.edu>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal.variables;
13 import java.lang.reflect.InvocationTargetException;
14 import java.lang.reflect.Method;
15 import java.util.List;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.egit.core.AdapterUtils;
20 import org.eclipse.egit.core.project.RepositoryMapping;
21 import org.eclipse.egit.ui.Activator;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jface.text.templates.TemplateContext;
24 import org.eclipse.jface.text.templates.TemplateVariable;
25 import org.eclipse.jface.text.templates.TemplateVariableResolver;
26 import org.eclipse.jgit.annotations.Nullable;
27 import org.eclipse.jgit.lib.Repository;
28 import org.eclipse.jgit.lib.StoredConfig;
30 /**
31 * Resolves Git variables within templates
33 * @see <a
34 * href="http://help.eclipse.org/topic/org.eclipse.jdt.doc.user/concepts/concept-template-variables.htm">http://help.eclipse.org/topic/org.eclipse.jdt.doc.user/concepts/concept-template-variables.htm</a>
36 public class GitTemplateVariableResolver extends TemplateVariableResolver {
38 /**
39 * Creates an instance of <code>GitTemplateVariableResolver</code>.
41 * @param type
42 * the name of the type
43 * @param description
44 * the description for the type
46 public GitTemplateVariableResolver(String type, String description) {
47 super(type, description);
50 /**
51 * Creates an instance of <code>GitTemplateVariableResolver</code>.
53 public GitTemplateVariableResolver() {
54 super();
57 @Override
58 public void resolve(TemplateVariable variable, TemplateContext context) {
59 resolveVariable(variable, context);
62 /**
63 * Resolves the git_config variable for a project
65 * @param variable
66 * the current template variable.
67 * @param project
68 * the current project.
70 protected static void resolveVariable(TemplateVariable variable,
71 IProject project) {
72 final List<String> params = variable.getVariableType().getParams();
73 if (params.isEmpty()) {
74 variable.setValue(""); //$NON-NLS-1$
75 return;
78 final String gitKey = params.get(0);
79 if (gitKey == null || gitKey.length() == 0) {
80 variable.setValue(""); //$NON-NLS-1$
81 return;
84 // Get git's config
85 RepositoryMapping mapping = RepositoryMapping.getMapping(project);
86 Repository repository = null;
88 if (mapping != null) {
89 repository = mapping.getRepository();
91 if (repository == null) {
92 variable.setValue(""); //$NON-NLS-1$
93 return;
96 StoredConfig config = repository.getConfig();
98 // Get the value of the key
99 final String[] splits = gitKey.split("\\."); //$NON-NLS-1$
100 String section = null;
101 String subSection = null;
102 String name = null;
104 if (splits.length == 3) {
105 section = splits[0];
106 subSection = splits[1];
107 name = splits[2];
108 } else if (splits.length == 2) {
109 section = splits[0];
110 name = splits[1];
111 } else {
112 variable.setValue(""); //$NON-NLS-1$
113 return;
116 String gitValue = config.getString(section, subSection, name);
117 if (gitValue != null) {
118 variable.setValue(gitValue);
123 * Resolves the git_config variable
125 * @param variable
126 * the current template variable.
127 * @param context
128 * the current template context.
130 protected static void resolveVariable(TemplateVariable variable,
131 TemplateContext context) {
132 IProject project = getProject(context);
133 if (project != null) {
134 resolveVariable(variable, project);
139 * Retrieves the current project from a template context.
141 * @param context
142 * the current template context.
143 * @return the current project
145 @Nullable
146 protected static IProject getProject(TemplateContext context) {
147 // We can't use instanceof here because of the compiler error on 4.10
148 // platform saying that TemplateContext is always IAdaptable
149 if (IAdaptable.class.isInstance(context)) {
150 return AdapterUtils.adapt(context, IProject.class);
152 // Note: block below can be removed after EGit minimum target platform
153 // will be 4.10, see bug 539095 for details
154 if (Activator.hasJavaPlugin()) {
155 boolean hasPublicMethod = context.getClass().getSimpleName()
156 .equals("CodeTemplateContext"); //$NON-NLS-1$
157 try {
158 Method method;
159 if (hasPublicMethod) {
160 // CodeTemplateContext has public getJavaProject() method
161 method = context.getClass().getMethod("getJavaProject"); //$NON-NLS-1$
162 } else {
163 // JavaContext inherits from CompilationUnitContext which
164 // has protected getJavaProject() method
165 method = context.getClass().getSuperclass()
166 .getDeclaredMethod("getJavaProject"); //$NON-NLS-1$
167 method.setAccessible(true);
169 Object result = method.invoke(context);
170 if (result instanceof IJavaProject) {
171 IJavaProject javaProject = (IJavaProject) result;
172 return javaProject.getProject();
174 } catch (NoSuchMethodException | SecurityException
175 | IllegalAccessException | IllegalArgumentException
176 | InvocationTargetException e) {
177 return null;
180 return null;