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