从C#函数运行git命令

kwvwclae  于 2023-04-19  发布在  Git
关注(0)|答案(6)|浏览(475)

当我的C#代码检测到跟踪文件中的更改时,如何运行git命令?我正在为此编写VisualStudio/C#控制台项目。
我是.NET环境的新手,目前正在将自动GIT提交集成到文件夹中。我需要自动提交已知文件夹上的任何更改/添加/删除,并将其推送到git remote。感谢您的指导。谢谢。
这是我所拥有的,最后一个是我需要一些指导的:

  1. Git仓库最初设置在文件夹上,并带有适当的忽略文件(完成)。
    1.我正在使用C# FileSystemWatcher来捕获文件夹上的任何更改(完成)。
    1.一旦我的项目检测到变更,它就需要提交并推送这些变更(待定)。
    项目需要运行的暂定命令:
git add -A
git commit "explanations_of_changes"
git push our_remote

注意:这段代码(没有用户交互)将是提交到这个仓库的唯一实体,所以我不担心冲突,相信这个流程会工作。

rvpgvaaj

rvpgvaaj1#

我知道这是一个老问题,但我想添加我最近遇到的解决方案,以帮助那些在未来。
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的错误。

5n0oy7gb

5n0oy7gb2#

如果你想在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#中工作

dbf7pr2w

dbf7pr2w3#

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}";
        }
    }
fnx2tebb

fnx2tebb4#

试试LibGit2Sharp,一个git for .NET的原生实现:
https://github.com/libgit2/libgit2sharp/

1cklez4t

1cklez4t5#

一种替代方法是在项目中设置Grunt和TaskRunner。
Grunt应该能够自动检测项目中文件夹(或多个文件夹)的更改,并执行相应的git命令来提交。
Task Runner允许您在Visual Studio中初始化和运行Grunt。
Visual Studio团队表示,Task Runner将集成到Visual Studio的未来版本中,因此这可能是一个长期的解决方案。
注意:在评论中已经提到了,但我觉得值得再次提及的是,每当文件保存到存储库时自动提交并不是最佳实践。您希望推送功能/原子代码更改,而不是简单的文本更改。自动提交风险自担。

js5cn81o

js5cn81o6#

包管理器控制台是Powershell控制台。所以你可以从那里运行git命令。

相关问题