如何在 java 中从 ProcessBuilder 执行 "git fetch -v --progress <baseURL> <releaseBranch>" 时输入密码
How to feed in the password while executing "git fetch -v --progress <baseURL> <releaseBranch>" from ProcessBuilder in java
这个程序不允许输入密码,它基本上是在通过扫描仪读取命令输出时挂起的。基本上我正在尝试从 git 存储库中获取一个分支。
ProcessBuilder builder = new ProcessBuilder( "cmd" );
builder.redirectErrorStream(true);
Process process = null;
try {
process = builder.start();
} catch (IOException e) {
e.printStackTrace();
}
//get stdin of cmd
BufferedWriter p_stdin = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
// execute the desired command from list
List<String> cmdList = new ArrayList<String>();
String baseURL= "abc@xyz.com/a/b";
cmdList.add("cd c:\rep\project");
cmdList.add(String.format("git fetch -v --progress %s releaseBranch", baseURL));
cmdList.add("exit");
for (String cmd : cmdList) {
try {
//single execution
p_stdin.write(cmd);
p_stdin.newLine();
p_stdin.flush();
if(cmd.startsWith("git fetch")) {
//process.waitFor();
//p_stdin.wait();
p_stdin.write("XXXXX@xxx");
p_stdin.newLine();
p_stdin.flush();
}
} catch (IOException e) {
System.out.println(e);
}
}
Scanner scanner = new Scanner(process.getInputStream());
while ( scanner.hasNextLine()) {
System.out.println(scanner.nextLine()); //Process hangs at this point
}
process.getInputStream().close();
scanner.close();
but console hangs at C:\rep\project>git fetch -v --progress abc@xyz.com/a/b releaseBranch
point
abc@xyz.com 看起来像 SSH URL,这意味着您需要一个私钥:
- 无密码
- 或者使用缓存在 ssh-agent 中的密码(参见“Working with SSH key passphrases”)
如果您使用的是 HTTPS URL,如果 account in credential helper,您可以考虑缓存 密码 (这次是密码,不是口令)。 =14=]
重点是:最好让 Git 自己寻找 passphrase/password(在 ssh-agent 或凭据助手中),而不是尝试通过程序提供相同的敏感日期控制台。
这个程序不允许输入密码,它基本上是在通过扫描仪读取命令输出时挂起的。基本上我正在尝试从 git 存储库中获取一个分支。
ProcessBuilder builder = new ProcessBuilder( "cmd" );
builder.redirectErrorStream(true);
Process process = null;
try {
process = builder.start();
} catch (IOException e) {
e.printStackTrace();
}
//get stdin of cmd
BufferedWriter p_stdin = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
// execute the desired command from list
List<String> cmdList = new ArrayList<String>();
String baseURL= "abc@xyz.com/a/b";
cmdList.add("cd c:\rep\project");
cmdList.add(String.format("git fetch -v --progress %s releaseBranch", baseURL));
cmdList.add("exit");
for (String cmd : cmdList) {
try {
//single execution
p_stdin.write(cmd);
p_stdin.newLine();
p_stdin.flush();
if(cmd.startsWith("git fetch")) {
//process.waitFor();
//p_stdin.wait();
p_stdin.write("XXXXX@xxx");
p_stdin.newLine();
p_stdin.flush();
}
} catch (IOException e) {
System.out.println(e);
}
}
Scanner scanner = new Scanner(process.getInputStream());
while ( scanner.hasNextLine()) {
System.out.println(scanner.nextLine()); //Process hangs at this point
}
process.getInputStream().close();
scanner.close();
but console hangs at
C:\rep\project>git fetch -v --progress abc@xyz.com/a/b releaseBranch
point
abc@xyz.com 看起来像 SSH URL,这意味着您需要一个私钥:
- 无密码
- 或者使用缓存在 ssh-agent 中的密码(参见“Working with SSH key passphrases”)
如果您使用的是 HTTPS URL,如果 account in credential helper,您可以考虑缓存 密码 (这次是密码,不是口令)。 =14=]
重点是:最好让 Git 自己寻找 passphrase/password(在 ssh-agent 或凭据助手中),而不是尝试通过程序提供相同的敏感日期控制台。