从C#函数运行git命令
本文关键字:git 命令 运行 函数 | 更新日期: 2023-09-27 18:25:19
当我的C#代码检测到被跟踪文件中的更改时,它如何运行git命令?为此,我正在编写一个VisualStudio/C#控制台项目。
我是.NET环境的新手,目前正在将自动GIT提交集成到文件夹中。我需要自动提交对已知文件夹的任何更改/添加/删除,并将其推送到git远程。感谢任何指导。非常感谢。
这是我所拥有的,最后一个是我需要一些指导的:
- Git存储库最初设置在具有适当忽略文件的文件夹上(已完成)
- 我正在使用C#FileSystemWatcher来捕捉对所述文件夹的任何更改(已完成)
- 一旦我的项目检测到更改,它就需要提交并推送这些更改(挂起)
项目需要运行的暂定命令:
git add -A
git commit "explanations_of_changes"
git push our_remote
注意:这个代码(没有用户交互)将是唯一一个致力于这个回购的实体,所以我不担心冲突,相信这个流程会起作用。
我意识到这是一个老问题,但我想添加我最近遇到的解决方案,以在未来帮助这些人。
PowerShell
类提供了一种与git交互的简单方法。这是.NET中System.Management.Automation
命名空间的一部分。请注意,System.Management.Automation.dll可通过NuGet获得。
string directory = ""; // directory of the git repository
using (PowerShell powershell = PowerShell.Create()) {
// this changes from the user folder that PowerShell starts up with to your git repository
powershell.AddScript($"cd {directory}");
powershell.AddScript(@"git init");
powershell.AddScript(@"git add *");
powershell.AddScript(@"git commit -m 'git commit from PowerShell in C#'");
powershell.AddScript(@"git push");
Collection<PSObject> results = powershell.Invoke();
}
在我看来,这比使用Process.Start()
方法更干净、更好。通过编辑添加到powershell
对象中的脚本,可以根据您的特殊需要对此进行修改。
正如@ArtemIllarionov所评论的,powershell.Invoke()
不返回错误,但Streams
属性具有输出信息。特别是针对错误的powerShell.Streams.Error
。
如果你想在C#中执行,你可以通过Process调用外部git命令。当你检测到文件更改时启动
string gitCommand = "git";
string gitAddArgument = @"add -A";
string gitCommitArgument = @"commit ""explanations_of_changes""";
string gitPushArgument = @"push our_remote";
Process.Start(gitCommand, gitAddArgument);
Process.Start(gitCommand, gitCommitArgument);
Process.Start(gitCommand, gitPushArgument);
不是最好的解决方案,但它在C#中有效
using System.Diagnostics;
using System.Text;
//Console.WriteLine(CommandOutput("git status"));
public static string CommandOutput(string command,
string workingDirectory = null)
{
try
{
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardError = procStartInfo.RedirectStandardInput = procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
if (null != workingDirectory)
{
procStartInfo.WorkingDirectory = workingDirectory;
}
Process proc = new Process();
proc.StartInfo = procStartInfo;
StringBuilder sb = new StringBuilder();
proc.OutputDataReceived += delegate (object sender, DataReceivedEventArgs e)
{
sb.AppendLine(e.Data);
};
proc.ErrorDataReceived += delegate (object sender, DataReceivedEventArgs e)
{
sb.AppendLine(e.Data);
};
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
return sb.ToString();
}
catch (Exception objException)
{
return $"Error in command: {command}, {objException.Message}";
}
}
试试LibGit2Sharp,git for.NET:的原生实现
https://github.com/libgit2/libgit2sharp/
一种替代方案是在项目中设置Grunt和TaskRunner。
Grunt应该能够自动检测项目中文件夹的更改,并执行相应的git命令来提交。
任务运行程序允许您在Visual Studio中初始化并运行Grunt。
Visual Studio团队表示,Task Runner将集成到Visual Studio的未来版本中,因此这可能是一个长期的解决方案。
注意:评论中已经提到了这一点,但我觉得值得一提的是,在文件保存到存储库时自动提交并不是最佳做法。您希望函数/原子代码更改被推入,而不是简单的文本更改。自动提交,风险自负。
PackageManager控制台是Powershell控制台。因此,您可以从那里运行git命令。