1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.util;
12
13 import static java.nio.charset.StandardCharsets.UTF_8;
14
15 import java.io.File;
16 import java.io.OutputStream;
17 import java.security.AccessController;
18 import java.security.PrivilegedAction;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22
23 import org.eclipse.jgit.api.errors.JGitInternalException;
24 import org.eclipse.jgit.errors.CommandFailedException;
25 import org.eclipse.jgit.lib.Repository;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30
31
32
33
34 public class FS_Win32_Cygwin extends FS_Win32 {
35 private static final Logger LOG = LoggerFactory
36 .getLogger(FS_Win32_Cygwin.class);
37
38 private static String cygpath;
39
40
41
42
43
44
45 public static boolean isCygwin() {
46 final String path = AccessController
47 .doPrivileged((PrivilegedAction<String>) () -> System
48 .getProperty("java.library.path")
49 );
50 if (path == null)
51 return false;
52 File found = FS.searchPath(path, "cygpath.exe");
53 if (found != null)
54 cygpath = found.getPath();
55 return cygpath != null;
56 }
57
58
59
60
61 public FS_Win32_Cygwin() {
62 super();
63 }
64
65
66
67
68
69
70
71 protected FS_Win32_Cygwin(FS src) {
72 super(src);
73 }
74
75
76 @Override
77 public FS newInstance() {
78 return new FS_Win32_Cygwin(this);
79 }
80
81
82 @Override
83 public File resolve(File dir, String pn) {
84 String useCygPath = System.getProperty("jgit.usecygpath");
85 if (useCygPath != null && useCygPath.equals("true")) {
86 String w;
87 try {
88 w = readPipe(dir,
89 new String[] { cygpath, "--windows", "--absolute", pn },
90 UTF_8.name());
91 } catch (CommandFailedException e) {
92 LOG.warn(e.getMessage());
93 return null;
94 }
95 if (!StringUtils.isEmptyOrNull(w)) {
96 return new File(w);
97 }
98 }
99 return super.resolve(dir, pn);
100 }
101
102
103 @Override
104 protected File userHomeImpl() {
105 final String home = AccessController.doPrivileged(
106 (PrivilegedAction<String>) () -> System.getenv("HOME")
107 );
108 if (home == null || home.length() == 0)
109 return super.userHomeImpl();
110 return resolve(new File("."), home);
111 }
112
113
114 @Override
115 public ProcessBuilder runInShell(String cmd, String[] args) {
116 List<String> argv = new ArrayList<>(4 + args.length);
117 argv.add("sh.exe");
118 argv.add("-c");
119 argv.add(cmd + " \"$@\"");
120 argv.add(cmd);
121 argv.addAll(Arrays.asList(args));
122 ProcessBuilder proc = new ProcessBuilder();
123 proc.command(argv);
124 return proc;
125 }
126
127 @Override
128 String shellQuote(String cmd) {
129 return QuotedString.BOURNE.quote(cmd.replace(File.separatorChar, '/'));
130 }
131
132
133 @Override
134 public String relativize(String base, String other) {
135 final String relativized = super.relativize(base, other);
136 return relativized.replace(File.separatorChar, '/');
137 }
138
139
140 @Override
141 public ProcessResult runHookIfPresent(Repository repository, String hookName,
142 String[] args, OutputStream outRedirect, OutputStream errRedirect,
143 String stdinArgs) throws JGitInternalException {
144 return internalRunHookIfPresent(repository, hookName, args, outRedirect,
145 errRedirect, stdinArgs);
146 }
147 }