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
;
31 * Resolves Git variables within templates
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
{
39 * Creates an instance of <code>GitTemplateVariableResolver</code>.
42 * the name of the type
44 * the description for the type
46 public GitTemplateVariableResolver(String type
, String description
) {
47 super(type
, description
);
51 * Creates an instance of <code>GitTemplateVariableResolver</code>.
53 public GitTemplateVariableResolver() {
58 public void resolve(TemplateVariable variable
, TemplateContext context
) {
59 resolveVariable(variable
, context
);
63 * Resolves the git_config variable for a project
66 * the current template variable.
68 * the current project.
70 protected static void resolveVariable(TemplateVariable variable
,
72 final List
<String
> params
= variable
.getVariableType().getParams();
73 if (params
.isEmpty()) {
74 variable
.setValue(""); //$NON-NLS-1$
78 final String gitKey
= params
.get(0);
79 if (gitKey
== null || gitKey
.length() == 0) {
80 variable
.setValue(""); //$NON-NLS-1$
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$
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;
104 if (splits
.length
== 3) {
106 subSection
= splits
[1];
108 } else if (splits
.length
== 2) {
112 variable
.setValue(""); //$NON-NLS-1$
116 String gitValue
= config
.getString(section
, subSection
, name
);
117 if (gitValue
!= null) {
118 variable
.setValue(gitValue
);
123 * Resolves the git_config variable
126 * the current template variable.
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.
142 * the current template context.
143 * @return the current project
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$
159 if (hasPublicMethod
) {
160 // CodeTemplateContext has public getJavaProject() method
161 method
= context
.getClass().getMethod("getJavaProject"); //$NON-NLS-1$
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
) {