Running Bash Commands from C#

20,493

Instead of calling the bash.exe, simply call git and pass the status as argument:

processStartInfo.FileName = "git";
processStartInfo.Arguments = "status";
Share:
20,493
RoLYroLLs
Author by

RoLYroLLs

Apps on Apple's AppStore: - How Long Can You Family - How Long Can You Tap It? - How Long Can You Turn It? - How Long Can You Twist It? http://itunes.apple.com/us/artist/rolyrolls-enterprises-llc/id312106066

Updated on June 13, 2020

Comments

  • RoLYroLLs
    RoLYroLLs almost 4 years

    I am trying to figure out how to run a bash command from C# running on IIS 7/.Net 4.5.

    I've been searching the web and a lot of answers presume you have certain things installed/in place.

    I already have Git 1.9.4.msysgit.2 installed with Git Bash and Git Giu. I'm looking for some help as to what else I need installed to run even the simplest of bash commands. And how to run it.

    I've looked at posts like bash pipes - I am trying to call script from c# but that uses cygwin. Can I do the same without it and if so, how do I go about it?

    Goal

    If what I'm asking above doesn't make sense or seems to ask separate questions, here my ultimate goal. I'm trying to write my own server-side git hook. When a developer pushes their commits to our GitHub repo, I want GitHub to call our callback url. I want my callback url to run a git pull command to update our staging server with what was just pushed.

    I got to this question based on a previous question I asked at GitHub - setup auto deployment with remote server. based on answers there I'm trying to run a simple command, either but hard coding the command, or putting it in a script and running it, e.g.: cd $REPO_DIR && git pull origin $branch_name.

    I am aware of Jenkins and other software, but I want to perform these commands myself vs. installing another software.

    If further information is needed please feel free to ask.

    Update 1

    So based on a few answers below I've come up with the following

    using System.Diagnostics;
    
    Process process = new Process();
    
    ProcessStartInfo processStartInfo = new ProcessStartInfo();
    processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    processStartInfo.FileName = @"C:\Program Files (x86)\Git\bin\bash.exe";
    processStartInfo.WorkingDirectory = @"C:\myrepo\mysite";
    processStartInfo.Arguments = "git status";
    processStartInfo.RedirectStandardOutput = true;
    processStartInfo.RedirectStandardError = true;
    processStartInfo.UseShellExecute = false;
    
    process.StartInfo = processStartInfo;
    process.Start();
    
    String error = process.StandardError.ReadToEnd();
    String output = process.StandardOutput.ReadToEnd();
    
    ViewBag.Error = error;
    ViewBag.Ouput = output;
    

    With the code above I am getting "C:/Program Files (x86)/Git/bin/bash.exe": git: No such file or directory. I know the exe is there. What's am I doing wrong?

    Update 2

    As per @SurgeonofDeath comment I followed this post http://blog.countableset.ch/2012/06/07/adding-git-to-windows-7-path/ and added the paths of Git to my environmental variables. However I still am getting the same issues. Any ideas?

    Thanks.