2015-06-09 49 views
0

我想從一個git進程輸出到另一個git命令的輸入。但我無法工作。 Git版本1.9.5.msysgit.0MSysGit管道編碼

我想代碼做什麼:

git show e46b22be3031ed53e80472002d332a2588f122c9 | git patch-id 

Output: 
c2a55fcbc40da401f84ae3b72c250e55f827001a e46b22be3031ed53e80472002d332a2588f122c9 

我的管道輸出到一個文件,這樣我有一些與進行比較:

git show e46b22be3031ed53e80472002d332a2588f122c9 > git.show.txt 

重定向從所述文件的輸入產生了預期的結果:

git patch-id < commit.show.txt 

Output: 
c2a55fcbc40da401f84ae3b72c250e55f827001a e46b22be3031ed53e80472002d332a2588f122c9 

對HEL p我解決問題,我寫了一個代理應用程序:

  1. 讀取混帳顯示管道輸入
  2. 管道輸入寫入一個文件,這樣我就可以用git.show.txt
  3. 進行比較
  4. 管道輸入寫入到輸出,使下一個應用程序可以把它管道

所以使用現在是:

git show e46b22be3031ed53e80472002d332a2588f122c9 | ConsolePipeConsumer.exe | git patch-id 

Incorrect Output: 
cf55b48fa7c9ed6f7214641c4d709667abfeb078 e46b22be3031ed53e80472002d332a2588f122c9 

我寫的臨時文件是二進制文件,等於git.show.txt,因此讀取輸入的方式如預期的那樣工作。

我只是無法弄清楚代理頁面用於我的代理應用程序的標準輸出。我嘗試了所有我能想到的。還是有其他缺失?

下面的代碼:

using System; 
using System.IO; 
using System.Text; 
namespace ConsolePipeConsumer 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      try 
      { 
       var encoding = Encoding.Default;     
       Console.SetIn(new StreamReader(Console.OpenStandardInput(8192), encoding)); 
       Console.Error.WriteLine("StdOut.Encoding={0}", Console.Out.Encoding); 
       // What encoding to use for output? 
       //Console.SetOut(new StreamWriter(Console.OpenStandardOutput(8192), encoding)); 
       int c; 
       var sb = new StringBuilder(); 
       while ((c = Console.Read()) >= 0) 
       { 
        sb.Append((char)c); 
       } 
       Console.Write(sb.ToString()); 
       var tmp = Path.GetTempFileName(); 
       File.WriteAllText(tmp, sb.ToString(), encoding); 
       Console.Error.WriteLine("Piped input written to: " + tmp); 
      } 
      catch (Exception ex) 
      { 
       Console.Error.WriteLine(ex.ToString()); 
      } 
     } 
    } 
} 
+1

我可能將要發生什麼。我將ConsolePipeConsumer的輸出傳送給另一個ConsolePipeConsumer實例,並注意數據被截斷。 – careri

+1

這是很不明確的你問,你似乎正在重寫'tee' – edi9999

+0

你爲什麼讀寫字符*,並試圖擔心編碼?如果目的不是*改變*編碼,那麼你應該可以讀寫*字節*。 –

回答

1

那好像是錯誤是退出我的代理應用程序之前,我不沖水Console.Out。

我還通過閱讀/寫入字節來改進愛德華湯普森的建議。

這裏是一個代理應用程序的一個工作實施的是:

  1. 注意到的例如傳送的輸出git
  2. 寫入有關輸入的調試信息
  3. 然後將完全相同的數據輸出到輸出。

下面的代碼:

<!-- language: c# -->  

using System; 
using System.Diagnostics; 
using System.IO; 
using System.Text; 
using System.Threading; 
namespace ConsolePipeConsumer 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      try 
      { 
       var pid = Process.GetCurrentProcess().Id; 
       var buffer = new byte[1024]; 
       var tmp = new FileInfo(Path.GetTempFileName()); 

       using (var reader = new BinaryReader(Console.OpenStandardInput(8192))) 
       using (var writer = new BinaryWriter(Console.OpenStandardOutput(8192))) 
       using (var tmpWriter = new BinaryWriter(tmp.Create())) 
       { 
        int c; 
        int len = 0; 
        while ((c = reader.Read(buffer, 0, buffer.Length)) > 0) 
        { 
         writer.Write(buffer, 0, c); 
         tmpWriter.Write(buffer, 0, c); 
         len += c; 
        } 
        Console.Error.WriteLine("[{0}] Input length: {1}", pid, len); 

        Console.Error.WriteLine("[{0}] Piped input written to: ", pid); 
        Console.Error.WriteLine("[{0}] {1} (File Size={2})", pid, tmp, tmp.Length); 
        writer.Flush(); 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.Error.WriteLine(ex.ToString()); 
      } 
     } 
    } 
}