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 package org.eclipse.jgit.util;
45
46 import java.io.File;
47 import java.io.PrintStream;
48 import java.nio.file.Files;
49 import java.nio.file.Path;
50 import java.security.AccessController;
51 import java.security.PrivilegedAction;
52 import java.util.ArrayList;
53 import java.util.Arrays;
54 import java.util.List;
55
56 import org.eclipse.jgit.api.errors.JGitInternalException;
57 import org.eclipse.jgit.errors.CommandFailedException;
58 import org.eclipse.jgit.lib.Constants;
59 import org.eclipse.jgit.lib.Repository;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63
64
65
66
67
68 public class FS_Win32_Cygwin extends FS_Win32 {
69 private final static Logger LOG = LoggerFactory
70 .getLogger(FS_Win32_Cygwin.class);
71
72 private static String cygpath;
73
74
75
76
77 public static boolean isCygwin() {
78 final String path = AccessController
79 .doPrivileged(new PrivilegedAction<String>() {
80 @Override
81 public String run() {
82 return System.getProperty("java.library.path");
83 }
84 });
85 if (path == null)
86 return false;
87 File found = FS.searchPath(path, "cygpath.exe");
88 if (found != null)
89 cygpath = found.getPath();
90 return cygpath != null;
91 }
92
93
94
95
96 public FS_Win32_Cygwin() {
97 super();
98 }
99
100
101
102
103
104
105
106 protected FS_Win32_Cygwin(FS src) {
107 super(src);
108 }
109
110 @Override
111 public FS newInstance() {
112 return new FS_Win32_Cygwin(this);
113 }
114
115 @Override
116 public File resolve(final File dir, final String pn) {
117 String useCygPath = System.getProperty("jgit.usecygpath");
118 if (useCygPath != null && useCygPath.equals("true")) {
119 String w;
120 try {
121 w = readPipe(dir,
122 new String[] { cygpath, "--windows", "--absolute", pn },
123 "UTF-8");
124 } catch (CommandFailedException e) {
125 LOG.warn(e.getMessage());
126 return null;
127 }
128 if (!StringUtils.isEmptyOrNull(w)) {
129 return new File(w);
130 }
131 }
132 return super.resolve(dir, pn);
133 }
134
135 @Override
136 protected File userHomeImpl() {
137 final String home = AccessController
138 .doPrivileged(new PrivilegedAction<String>() {
139 @Override
140 public String run() {
141 return System.getenv("HOME");
142 }
143 });
144 if (home == null || home.length() == 0)
145 return super.userHomeImpl();
146 return resolve(new File("."), home);
147 }
148
149 @Override
150 public ProcessBuilder runInShell(String cmd, String[] args) {
151 List<String> argv = new ArrayList<>(4 + args.length);
152 argv.add("sh.exe");
153 argv.add("-c");
154 argv.add(cmd + " \"$@\"");
155 argv.add(cmd);
156 argv.addAll(Arrays.asList(args));
157 ProcessBuilder proc = new ProcessBuilder();
158 proc.command(argv);
159 return proc;
160 }
161
162
163
164
165 @Override
166 public String relativize(String base, String other) {
167 final String relativized = super.relativize(base, other);
168 return relativized.replace(File.separatorChar, '/');
169 }
170
171
172
173
174 @Override
175 public ProcessResult runHookIfPresent(Repository repository, String hookName,
176 String[] args, PrintStream outRedirect, PrintStream errRedirect,
177 String stdinArgs) throws JGitInternalException {
178 return internalRunHookIfPresent(repository, hookName, args, outRedirect,
179 errRedirect, stdinArgs);
180 }
181
182
183
184
185 @Override
186 public File findHook(Repository repository, String hookName) {
187 final File gitdir = repository.getDirectory();
188 if (gitdir == null) {
189 return null;
190 }
191 final Path hookPath = gitdir.toPath().resolve(Constants.HOOKS)
192 .resolve(hookName);
193 if (Files.isExecutable(hookPath))
194 return hookPath.toFile();
195 return null;
196 }
197 }