从命令行运行Git

时间:2017-09-25 06:48:52

标签: java git cmd

我想通过调用' git revert -m 1'来恢复合并。来自Java的cmd命令。下面的代码恢复它并在控制台上输出输出作为OutputStream。我希望来自Git的Java通知是否恢复成功。我怎么能用String或boolean格式得到它?

 public void revert(String path, String CommitId) throws IOException, Exception
    {

        String revertCommand="git revert -m 1 "+CommitId;
        String commitCommand="git commit -m "+'"'+"Reverted a commit"+'"';
        //revert the commit in that branch
        String[] command =
            {
                "cmd",
            };
            Process p = Runtime.getRuntime().exec(command);
            new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
            new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
            PrintWriter stdin = new PrintWriter(p.getOutputStream());
            stdin.println(path);
            stdin.println("git checkout -b tempBranch release/2017.1");
            stdin.println(revertCommand);
            stdin.println(commitCommand);
            stdin.println("git push contrib/WMD-202817 tempBranch");
            // write any other commands you want here
            stdin.close();
            int returnCode = p.waitFor();
            System.out.println("Return code = " + returnCode);

    }


public class SyncPipe implements Runnable {
    public SyncPipe(InputStream istrm, OutputStream ostrm) {
          istrm_ = istrm;
          ostrm_ = ostrm;
      }
      public void run() {
          try
          {
              final byte[] buffer = new byte[1024];
              for (int length = 0; (length = istrm_.read(buffer)) != -1; )
              {
                  ostrm_.write(buffer, 0, length);
              }
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
      }
      private final OutputStream ostrm_;
      private final InputStream istrm_;
}

0 个答案:

没有答案