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