Run git commands from C # function

How does my C # code run git commands when it detects changes in the tracked file? For this, I am writing a VisualStudio / C # console project.

I'm new to .NET and am currently working on integrating automated git into a folder. I need to automatically make any change / add / delete to a known folder and press it on the git remote. Any guidance is appreciated. Thank.

Here is what I have, and the last one is what I need guidance with:

  • The git repository is initially configured on the folder with the correct ignore file (done).
  • I am using C # FileSystemWatcher to catch any changes in the specified folder (made).
  • As soon as my project detects a change, it needs to commit and push these changes (expected).

Preliminary teams that the project should complete:

git add -A
git commit "explanations_of_changes"
git push our_remote

NOTE. This code (without user interaction) will be the only object executing this repo, so I don’t worry about conflicts and I think that this thread will work.

+8
source share
5 answers

If you want to do this in C #, you can call the external git Process.Start command when a file change is detected

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 );

Not the best solution, but it works in C #

+8
source

I understand this is an old question, but I wanted to add a solution that I recently came across to help them in the future.

PowerShell git. System.Management.Automation .NET. , 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 .

+12

- Grunt TaskRunner .

Grunt ( ) git .

Task Runner Grunt Visual Studio.

Visual Studio , Task Runner Visual Studio, .

. , , , , , . , / , . Auto-Commit .

+2
    //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;
            proc.Start();

            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.BeginOutputReadLine();
            proc.BeginErrorReadLine();
            proc.WaitForExit();
            return sb.ToString();
        }
        catch (Exception objException)
        {
            return $"Error in command: {command}, {objException.Message}";
        }
    }
+1
source

Package Manager Console - Powershell Console. This way you can run git commands.

-3
source

All Articles