Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 41cdfe335f0204a0361094b47901b2062bee8dbe (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*******************************************************************************
 * Copyright (c) 2012, 2015 Kyle J. Harms <harmsk@seas.wustl.edu>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.ui.internal.variables;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Adapters;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.egit.ui.Activator;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.text.templates.TemplateContext;
import org.eclipse.jface.text.templates.TemplateVariable;
import org.eclipse.jface.text.templates.TemplateVariableResolver;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;

/**
 * Resolves Git variables within templates
 *
 * @see <a
 *      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>
 */
public class GitTemplateVariableResolver extends TemplateVariableResolver {

	/**
	 * Creates an instance of <code>GitTemplateVariableResolver</code>.
	 *
	 * @param type
	 *            the name of the type
	 * @param description
	 *            the description for the type
	 */
	public GitTemplateVariableResolver(String type, String description) {
		super(type, description);
	}

	/**
	 * Creates an instance of <code>GitTemplateVariableResolver</code>.
	 */
	public GitTemplateVariableResolver() {
		super();
	}

	@Override
	public void resolve(TemplateVariable variable, TemplateContext context) {
		resolveVariable(variable, context);
	}

	/**
	 * Resolves the git_config variable for a project
	 *
	 * @param variable
	 *            the current template variable.
	 * @param project
	 *            the current project.
	 */
	protected static void resolveVariable(TemplateVariable variable,
			IProject project) {
		final List<String> params = variable.getVariableType().getParams();
		if (params.isEmpty()) {
			variable.setValue(""); //$NON-NLS-1$
			return;
		}

		final String gitKey = params.get(0);
		if (gitKey == null || gitKey.length() == 0) {
			variable.setValue(""); //$NON-NLS-1$
			return;
		}

		// Get git's config
		RepositoryMapping mapping = RepositoryMapping.getMapping(project);
		Repository repository = null;

		if (mapping != null) {
			repository = mapping.getRepository();
		}
		if (repository == null) {
			variable.setValue(""); //$NON-NLS-1$
			return;
		}

		StoredConfig config = repository.getConfig();

		// Get the value of the key
		final String[] splits = gitKey.split("\\."); //$NON-NLS-1$
		String section = null;
		String subSection = null;
		String name = null;

		if (splits.length == 3) {
			section = splits[0];
			subSection = splits[1];
			name = splits[2];
		} else if (splits.length == 2) {
			section = splits[0];
			name = splits[1];
		} else {
			variable.setValue(""); //$NON-NLS-1$
			return;
		}

		String gitValue = config.getString(section, subSection, name);
		if (gitValue != null) {
			variable.setValue(gitValue);
		}
	}

	/**
	 * Resolves the git_config variable
	 *
	 * @param variable
	 *            the current template variable.
	 * @param context
	 *            the current template context.
	 */
	protected static void resolveVariable(TemplateVariable variable,
			TemplateContext context) {
		IProject project = getProject(context);
		if (project != null) {
			resolveVariable(variable, project);
		}
	}

	/**
	 * Retrieves the current project from a template context.
	 *
	 * @param context
	 *            the current template context.
	 * @return the current project
	 */
	@Nullable
	protected static IProject getProject(TemplateContext context) {
		// We can't use instanceof here because of the compiler error on 4.10
		// platform saying that TemplateContext is always IAdaptable
		if (IAdaptable.class.isInstance(context)) {
			return Adapters.adapt(context, IProject.class);
		}
		// Note: block below can be removed after EGit minimum target platform
		// will be 4.10, see bug 539095 for details
		if (Activator.hasJavaPlugin()) {
			boolean hasPublicMethod = context.getClass().getSimpleName()
					.equals("CodeTemplateContext"); //$NON-NLS-1$
			try {
				Method method;
				if (hasPublicMethod) {
					// CodeTemplateContext has public getJavaProject() method
					method = context.getClass().getMethod("getJavaProject"); //$NON-NLS-1$
				} else {
					// JavaContext inherits from CompilationUnitContext which
					// has protected getJavaProject() method
					method = context.getClass().getSuperclass()
							.getDeclaredMethod("getJavaProject"); //$NON-NLS-1$
					method.setAccessible(true);
				}
				Object result = method.invoke(context);
				if (result instanceof IJavaProject) {
					IJavaProject javaProject = (IJavaProject) result;
					return javaProject.getProject();
				}
			} catch (NoSuchMethodException | SecurityException
					| IllegalAccessException | IllegalArgumentException
					| InvocationTargetException e) {
				return null;
			}
		}
		return null;
	}
}

Back to the top