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 public String run() {
81 return System.getProperty("java.library.path");
82 }
83 });
84 if (path == null)
85 return false;
86 File found = FS.searchPath(path, "cygpath.exe");
87 if (found != null)
88 cygpath = found.getPath();
89 return cygpath != null;
90 }
91
92
93
94
95 public FS_Win32_Cygwin() {
96 super();
97 }
98
99
100
101
102
103
104
105 protected FS_Win32_Cygwin(FS src) {
106 super(src);
107 }
108
109 public FS newInstance() {
110 return new FS_Win32_Cygwin(this);
111 }
112
113 public File resolve(final File dir, final String pn) {
114 String useCygPath = System.getProperty("jgit.usecygpath");
115 if (useCygPath != null && useCygPath.equals("true")) {
116 String w;
117 try {
118 w = readPipe(dir,
119 new String[] { cygpath, "--windows", "--absolute", pn },
120 "UTF-8");
121 } catch (CommandFailedException e) {
122 LOG.warn(e.getMessage());
123 return null;
124 }
125 if (!StringUtils.isEmptyOrNull(w)) {
126 return new File(w);
127 }
128 }
129 return super.resolve(dir, pn);
130 }
131
132 @Override
133 protected File userHomeImpl() {
134 final String home = AccessController
135 .doPrivileged(new PrivilegedAction<String>() {
136 public String run() {
137 return System.getenv("HOME");
138 }
139 });
140 if (home == null || home.length() == 0)
141 return super.userHomeImpl();
142 return resolve(new File("."), home);
143 }
144
145 @Override
146 public ProcessBuilder runInShell(String cmd, String[] args) {
147 List<String> argv = new ArrayList<String>(4 + args.length);
148 argv.add("sh.exe");
149 argv.add("-c");
150 argv.add(cmd + " \"$@\"");
151 argv.add(cmd);
152 argv.addAll(Arrays.asList(args));
153 ProcessBuilder proc = new ProcessBuilder();
154 proc.command(argv);
155 return proc;
156 }
157
158
159
160
161 @Override
162 public String relativize(String base, String other) {
163 final String relativized = super.relativize(base, other);
164 return relativized.replace(File.separatorChar, '/');
165 }
166
167
168
169
170 @Override
171 public ProcessResult runHookIfPresent(Repository repository, String hookName,
172 String[] args, PrintStream outRedirect, PrintStream errRedirect,
173 String stdinArgs) throws JGitInternalException {
174 return internalRunHookIfPresent(repository, hookName, args, outRedirect,
175 errRedirect, stdinArgs);
176 }
177
178
179
180
181 @Override
182 public File findHook(Repository repository, String hookName) {
183 final File gitdir = repository.getDirectory();
184 if (gitdir == null) {
185 return null;
186 }
187 final Path hookPath = gitdir.toPath().resolve(Constants.HOOKS)
188 .resolve(hookName);
189 if (Files.isExecutable(hookPath))
190 return hookPath.toFile();
191 return null;
192 }
193 }