Refactor exec of a command and reading one line into utility
Change-Id: Ia9e5afe7f29c3e5e74b8d226441ed429fb229c82 Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
This commit is contained in:
parent
14b358a6fb
commit
240769e023
|
@ -43,7 +43,10 @@
|
||||||
|
|
||||||
package org.eclipse.jgit.util;
|
package org.eclipse.jgit.util;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.security.AccessController;
|
import java.security.AccessController;
|
||||||
import java.security.PrivilegedAction;
|
import java.security.PrivilegedAction;
|
||||||
|
|
||||||
|
@ -210,4 +213,43 @@ static File searchPath(final String path, final String... lookFor) {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a command and return a single line of output as a String
|
||||||
|
*
|
||||||
|
* @param dir
|
||||||
|
* Working directory for the command
|
||||||
|
* @param command
|
||||||
|
* as component array
|
||||||
|
* @param encoding
|
||||||
|
* @return the one-line output of the command
|
||||||
|
*/
|
||||||
|
protected static String readPipe(File dir, String[] command, String encoding) {
|
||||||
|
try {
|
||||||
|
final Process p = Runtime.getRuntime().exec(command, null, dir);
|
||||||
|
final BufferedReader lineRead = new BufferedReader(
|
||||||
|
new InputStreamReader(p.getInputStream(), encoding));
|
||||||
|
String r = null;
|
||||||
|
try {
|
||||||
|
r = lineRead.readLine();
|
||||||
|
} finally {
|
||||||
|
p.getOutputStream().close();
|
||||||
|
p.getErrorStream().close();
|
||||||
|
lineRead.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
try {
|
||||||
|
if (p.waitFor() == 0 && r != null && r.length() > 0)
|
||||||
|
return r;
|
||||||
|
break;
|
||||||
|
} catch (InterruptedException ie) {
|
||||||
|
// Stop bothering me, I have a zombie to reap.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,10 +43,7 @@
|
||||||
|
|
||||||
package org.eclipse.jgit.util;
|
package org.eclipse.jgit.util;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.security.AccessController;
|
import java.security.AccessController;
|
||||||
import java.security.PrivilegedAction;
|
import java.security.PrivilegedAction;
|
||||||
|
|
||||||
|
@ -69,36 +66,11 @@ public String run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public File resolve(final File dir, final String pn) {
|
public File resolve(final File dir, final String pn) {
|
||||||
try {
|
String w = readPipe(dir, //
|
||||||
final Process p;
|
new String[] { cygpath, "--windows", "--absolute", pn }, //
|
||||||
|
"UTF-8");
|
||||||
p = Runtime.getRuntime().exec(
|
if (w != null)
|
||||||
new String[] { cygpath, "--windows", "--absolute", pn },
|
return new File(w);
|
||||||
null, dir);
|
|
||||||
p.getOutputStream().close();
|
|
||||||
|
|
||||||
final BufferedReader lineRead = new BufferedReader(
|
|
||||||
new InputStreamReader(p.getInputStream(), "UTF-8"));
|
|
||||||
String r = null;
|
|
||||||
try {
|
|
||||||
r = lineRead.readLine();
|
|
||||||
} finally {
|
|
||||||
lineRead.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
try {
|
|
||||||
if (p.waitFor() == 0 && r != null && r.length() > 0)
|
|
||||||
return new File(r);
|
|
||||||
break;
|
|
||||||
} catch (InterruptedException ie) {
|
|
||||||
// Stop bothering me, I have a zombie to reap.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
// Fall through and use the default return.
|
|
||||||
//
|
|
||||||
}
|
|
||||||
return super.resolve(dir, pn);
|
return super.resolve(dir, pn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue