forked from jenkinsci/git-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitTool.java
More file actions
134 lines (107 loc) · 4.13 KB
/
Copy pathGitTool.java
File metadata and controls
134 lines (107 loc) · 4.13 KB
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
package hudson.plugins.git;
import hudson.DescriptorExtensionList;
import hudson.EnvVars;
import hudson.Extension;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.model.EnvironmentSpecific;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.scm.SCM;
import hudson.scm.SCMDescriptor;
import hudson.slaves.NodeSpecific;
import hudson.tools.ToolDescriptor;
import hudson.tools.ToolInstallation;
import hudson.tools.ToolProperty;
import hudson.util.FormValidation;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
/**
* Information about Git installation.
*
* @author Jyrki Puttonen
*/
public final class GitTool extends ToolInstallation implements NodeSpecific<GitTool>, EnvironmentSpecific<GitTool> {
@DataBoundConstructor
public GitTool(String name, String home, List<? extends ToolProperty<?>> properties) {
super(name, home, properties);
}
static transient final String defaultValueName = "Default";
public String getGitExe() {
return getHome();
}
@Initializer(after=InitMilestone.PLUGINS_STARTED)
public static void onLoaded() {
//Creates default tool installation if needed. Uses "git" or migrates data from previous versions
DescriptorImpl descriptor = (DescriptorImpl) Hudson.getInstance().getDescriptor(GitTool.class);
GitTool[] installations = getInstallations(descriptor);
if (installations.length > 0) {
//No need to initialize if there's already something
return;
}
DescriptorExtensionList<SCM, SCMDescriptor<?>> scms = GitSCM.all();
String defaultGitExe = File.separatorChar != '/' ? "git.exe" : "git";
for (SCMDescriptor<?> s : scms) {
if (s instanceof GitSCM.DescriptorImpl) {
//Get previous settings from descriptor
GitSCM.DescriptorImpl desc = (GitSCM.DescriptorImpl) s;
if (desc.getOldGitExe() != null) {
defaultGitExe = desc.getOldGitExe();
}
}
}
GitTool tool = new GitTool(defaultValueName, defaultGitExe, Collections.<ToolProperty<?>>emptyList());
descriptor.setInstallations(new GitTool[] { tool });
descriptor.save();
}
private static GitTool[] getInstallations(DescriptorImpl descriptor) {
GitTool[] installations = null;
try {
installations = descriptor.getInstallations();
} catch (NullPointerException e) {
installations = new GitTool[0];
}
return installations;
}
public GitTool forNode(Node node, TaskListener log) throws IOException, InterruptedException {
return new GitTool(getName(), translateFor(node, log), Collections.<ToolProperty<?>>emptyList());
}
public GitTool forEnvironment(EnvVars environment) {
return new GitTool(getName(), environment.expand(getHome()), Collections.<ToolProperty<?>>emptyList());
}
@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) Hudson.getInstance().getDescriptor(GitTool.class);
}
@Extension
public static class DescriptorImpl extends ToolDescriptor<GitTool> {
public DescriptorImpl() {
super();
load();
}
@Override
public String getDisplayName() {
return "Git";
}
@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
super.configure(req, json);
save();
return true;
}
public FormValidation doCheckHome(@QueryParameter File value)
throws IOException, ServletException {
Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
String path = value.getPath();
return FormValidation.validateExecutable(path);
}
}
}