[英]How to open and use Git Bash through c# code
I'm trying to include opening Git Bash, pushing and pulling in my c# code.我正在尝试包括打开 Git Bash、推入和拉入我的 c# 代码。 Whilst opening Git Bash with Process.Start()
is not the problem, I cannot manage to write commands into Git Bash.虽然使用Process.Start()
打开 Git Bash 不是问题,但我无法将命令写入 Git Bash。
I've tried including commands in ProcessStartInfo.Arguments
, as well as redirecting the standard Output.我试过在ProcessStartInfo.Arguments
包含命令,以及重定向标准输出。 Both has not worked at all.两者都没有工作。 Down below you can see the different code snippets I tried.在下面,您可以看到我尝试过的不同代码片段。
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 opens but nothing is written in the command line. Git Bash 打开但没有在命令行中写入任何内容。
I know it is old question, still adding answer as few days ago I was also facing same issue.我知道这是个老问题,仍在添加答案,因为几天前我也遇到了同样的问题。
I think what you are missing is -c
parameter.我认为您缺少的是-c
参数。 I used below code and it solved this issue.我使用了下面的代码,它解决了这个问题。 -c
tells git-bash to execute whatever follows, it is similar to -cmd
parameter in command line. -c
告诉 git-bash 执行以下任何内容,它类似于命令行中的-cmd
参数。
In below mentioned function -在下面提到的功能中 -
fileName
= path of git-bash.exe. fileName
= git-bash.exe 的路径。
command
= git command which you want to execute. command
= 要执行的 git 命令。
workingDir
= Local path of git repository. 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();
}
I hope it solves the issue.我希望它能解决问题。
I think you are on the right way.我认为你走在正确的道路上。 I would try to use the git in the path, but it should be possible to also use the git-bash.exe
directly, at my machine it is located here: C:\\Program Files\\Git\\git-bash.exe
.我会尝试在路径中使用 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();
Like @S.Spieker already told you in it's good answer, no need to use git bash (it makes it harder to achieve and less performant), just call directly the git executable.就像@S.Spieker 已经在它的好答案中告诉您的那样,无需使用 git bash(它使其更难实现且性能更低),只需直接调用 git 可执行文件即可。
You could have a look to the GitExtensions code that is doing that: https://github.com/gitextensions/gitextensions/blob/027eabec3be497f8d780cc68ece268c64a43a7d5/GitExtensionsVSIX/Git/GitCommands.cs#L112您可以查看正在执行此操作的 GitExtensions 代码: https : //github.com/gitextensions/gitextensions/blob/027eabec3be497f8d780cc68ece268c64a43a7d5/GitExtensionsVSIX/Git/GitCommands.cs#L112
You could also achieve what you want using libgit2sharp ( https://github.com/libgit2/libgit2sharp ).您还可以使用 libgit2sharp ( https://github.com/libgit2/libgit2sharp ) 实现您想要的。 That could be easier if you want to interpret the results of the command run.如果您想解释命令运行的结果,这可能会更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.