【问题标题】:How to open and use Git Bash through c# code如何通过c#代码打开和使用Git Bash
【发布时间】:2019-08-16 14:34:14
【问题描述】:

我正在尝试包括打开 Git Bash,推入和拉入我的 c# 代码。虽然用Process.Start() 打开 Git Bash 不是问题,但我无法将命令写入 Git Bash。

我尝试在ProcessStartInfo.Arguments 中包含命令,以及重定向标准输出。两者都没有工作。在下面你可以看到我尝试过的不同代码 sn-ps。

private void Output()
{
    //Try 1
    processStartInfo psi = new ProcessStartInfo();
    psi.FileName = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Git\Git Bash.lnk";
    psi.UseShellExecute = false;
    psi.RedirectStandardOutput = true;
    psi.Argument = "git add *";
    Process p = Process.Start(psi);
    string strOutput = p.StandardOutput.ReadToEnd();
    Console.WriteLine(strOutput);

    //Try 2
    ProcessStartInfo psi = new ProcessStartInfo(@"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Git\Git Bash.lnk");
    Process.Start(psi);
    psi.Arguments = "git add *";
    Process.Start(psi);

    //Try 3
    var escapedArgs = cmd.Replace("\"", "\\\"");
    var process = new Process()
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Git\Git Bash.lnk",
            Arguments = "cd C:\\Users\\strit\\autocommittest2\\autocommittest2\n",
             RedirectStandardOutput = true,
             UseShellExecute = false,
             CreateNoWindow = true,
         }
    };
    process.Start();
    string result = process.StandardOutput.ReadToEnd();
    process.WaitForExit();
}

Git Bash 打开,但命令行中没有写入任何内容。

【问题讨论】:

    标签: c# windows git git-bash


    【解决方案1】:

    我知道这是个老问题,几天前我还在添加答案,我也遇到了同样的问题。

    我认为您缺少的是-c 参数。我使用了下面的代码,它解决了这个问题。 -c 告诉 git-bash 执行后面的任何内容,它类似于命令行中的 -cmd 参数。

    在下面提到的功能中 -

    fileName = git-bash.exe 的路径。

    command = 你要执行的 git 命令。

    workingDir = git 存储库的本地路径。

    public static void ExecuteGitBashCommand(string fileName, string command, string workingDir)
    {
    
        ProcessStartInfo processStartInfo = new ProcessStartInfo(fileName, "-c \" " + command + " \"")
        {
            WorkingDirectory = workingDir,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            RedirectStandardInput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };
    
        var process = Process.Start(processStartInfo);       
        process.WaitForExit();
    
        string output = process.StandardOutput.ReadToEnd();
        string error = process.StandardError.ReadToEnd();
        var exitCode = process.ExitCode;
    
        process.Close();
    }
    

    希望能解决问题。

    【讨论】:

      【解决方案2】:

      我认为你是在正确的方式。我会尝试在路径中使用 git,但也应该可以直接使用 git-bash.exe,在我的机器上它位于:C:\Program Files\Git\git-bash.exe

      Process gitProcess = new Process();
      gitInfo.Arguments = YOUR_GIT_COMMAND; // such as "fetch origin"
      gitInfo.WorkingDirectory = YOUR_GIT_REPOSITORY_PATH;
      gitInfo.UseShellExecute = false; 
      
      gitProcess.StartInfo = gitInfo;
      gitProcess.Start();
      
      string stderr_str = gitProcess.StandardError.ReadToEnd();  // pick up STDERR
      string stdout_str = gitProcess.StandardOutput.ReadToEnd(); // pick up STDOUT
      
      gitProcess.WaitForExit();
      gitProcess.Close();
      

      【讨论】:

      • 谢谢,我更改了我的代码。问题:作为参数添加的命令没有作为常规的 Git Bash 命令插入。如果命令只是“测试”,我会得到 bash: /usr/bin/test: cannot execute binary file Exit 126. 如果命令是例如"cd c:" git bash 在打开后立即关闭。任何线索为什么会这样?代码: ProcessStartInfo startInfo = new ProcessStartInfo { FileName = @"C:\Program Files\git-bash.exe", Arguments = command, RedirectStandardOuput = true, UseShellExecute = false, CreateNoWIndow = true, } Process pro = Process.Start(startInfo ); pro.WaitForExit(); pro.Close();
      【解决方案3】:

      就像@S.Spieker 在它的好答案中已经告诉你的那样,不需要使用 git bash (这使得它更难实现并且性能更低),只需直接调用 git 可执行文件。

      您可以查看执行此操作的 GitExtensions 代码:https://github.com/gitextensions/gitextensions/blob/027eabec3be497f8d780cc68ece268c64a43a7d5/GitExtensionsVSIX/Git/GitCommands.cs#L112

      您还可以使用 libgit2sharp (https://github.com/libgit2/libgit2sharp) 实现您想要的。如果您想解释命令运行的结果,这可能会更容易。

      【讨论】:

        猜你喜欢
        • 2014-10-23
        • 2013-01-05
        • 2021-11-19
        • 2021-08-21
        • 1970-01-01
        • 1970-01-01
        • 2021-03-06
        • 1970-01-01
        • 2015-03-07
        相关资源
        最近更新 更多