3 回答

TA贡献1829条经验 获得超7个赞
我知道这是个老问题,几天前我还在添加答案,我也面临同样的问题。
我认为你缺少的是-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();
}
我希望它能解决问题。

TA贡献1783条经验 获得超5个赞
我认为你是在正确的道路上。我会尝试在路径中使用 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();

TA贡献1828条经验 获得超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 )实现您想要的。如果您想解释命令运行的结果,那可能会更容易。
- 3 回答
- 0 关注
- 315 浏览
添加回答
举报