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