首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >致命:直接从C#执行git时出现的C#=‘C#2’>歧义C#‘>错误

致命:直接从C#执行git时出现的C#=‘C#2’>歧义C#‘>错误
EN

Stack Overflow用户
提问于 2018-05-02 15:41:35
回答 1查看 335关注 0票数 1

我试图在C#中执行以下操作:

  1. 得到两个分支的区别。
  2. 重定向修补程序文件中的输出。
  3. 检查一个新的空分支。
  4. 将修补程序文件应用于此新分支。
  5. 添加文件&将此分支提交到远程回购。

我正在运行的当前git命令:

代码语言:javascript
复制
git checkout branch2
git diff branch1 > delta.patch
git checkout --orphan delta_branch 
git rm -rf . 
git apply delta.patch
git add -A  
git commit -m "Adding a temporary branch.." 
git push -u origin delta_branch

虽然从git可以很好地工作,但是当从C#执行它的时候,它就不起作用了,我得到了关于diff命令的以下消息:

代码语言:javascript
复制
git diff branch1 > delta.patch

编辑:

我用来运行上述每个命令的C#方法如下:

代码语言:javascript
复制
public void ExecuteGitCommand(string sourceDirectory, string gitExePath, string command)
        {
            ProcessStartInfo gitInfo = new ProcessStartInfo();
            gitInfo.CreateNoWindow = true;
            gitInfo.RedirectStandardError = true;
            gitInfo.RedirectStandardOutput = true;
            gitInfo.FileName = gitExePath;
            gitInfo.UseShellExecute = false;

            Process gitProcess = new Process();

            gitInfo.Arguments = command;
            gitInfo.WorkingDirectory = sourceDirectory;

            gitProcess.StartInfo = gitInfo;
            gitProcess.Start();

            string output;
            string error;

            using (StreamReader streamReader = gitProcess.StandardOutput)
            {
                output = streamReader.ReadToEnd();
            }

            using (StreamReader streamReader = gitProcess.StandardError)
            {
                error = streamReader.ReadToEnd();
            }

            Console.WriteLine("Output:");
            Console.WriteLine(output);

            if (!string.IsNullOrEmpty(error))
            {
                Console.WriteLine("Error:");
                Console.WriteLine(error);
            }

            gitProcess.WaitForExit();
            gitProcess.Close();
        }

它的名字叫:

代码语言:javascript
复制
string[] commands = new string[] { gitCheckout, gitDiff, gitCheckoutDelta, gitRmDeltaFiles, gitApplyPatch, gitAdd, gitCommit, gitPush };

foreach(string command in commands)
 {
     Console.WriteLine(command); //debug only
     ExecuteGitCommand(sourceDirectory, gitExePath, command);
 }

注意:我在项目的其他部分使用LibGit2Sharp,但在这个特定的情况下,我不能使用它,因为LibGit2Sharp没有实现https://github.com/libgit2/libgit2sharp/wiki/LibGit2Sharp-Hitchhiker%27s-Guide-to-Git#manipulation-commands

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-02 15:50:31

不能简单地重定向到Process.Start信息中的文件。这是一个shell操作,而不是您可以简单调用的东西。相反,您需要自己读取git应用程序的标准输出。例如:

代码语言:javascript
复制
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;

startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;

startInfo.FileName = "git.exe";
startInfo.Arguments = "diff branch1";

Process process = new Process();
process.StartInfo = startInfo;
process.Start();

while ((line = process.StandardOutput.ReadLine()) != null)
{
     // This is the output you're reading...
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50138541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档