7

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.

Community
  • 1
  • 1
RoLYroLLs
  • 3,113
  • 4
  • 38
  • 57
  • Just to be clear, when you say "bash" do you mean a command line program? or do you actually want a unix bash shell to be called? – lukevp Feb 12 '15 at 20:30
  • A better question I guess is if you can't just globally install ms git, so that it is callable from the normal command line? in that case, you could just use a normal invoke of a command line program and you wouldn't have to deal with the bash shells that ms git installs by default – lukevp Feb 12 '15 at 20:32
  • @lukevp good questions. What lead me to this question was this previous question I asked http://stackoverflow.com/questions/28485281/github-setup-auto-deployment-with-remote-server. Based on the answer I marked I would like to run a bash script command of `cd $REPO_DIR && git pull origin branchname` – RoLYroLLs Feb 12 '15 at 20:37
  • Unfortunately I probably won't be much help as I use Jenkins to detect changes in our repos. Sorry! – lukevp Feb 12 '15 at 20:39
  • XY problem... So you real question is "how to run executable with working folder set to particular location", but for some strange reason you want to do it via bash script (not CMD, not directly)... Please consider what your actual goal is. – Alexei Levenkov Feb 12 '15 at 20:41
  • @AlexeiLevenkov I'm probably not understanding some of the technology enough to ask the right questions. i will update my question with my true goal. – RoLYroLLs Feb 12 '15 at 20:43
  • @RoLYroLLs , as long as you have the Git is in your environment variables, you can use this http://stackoverflow.com/questions/1469764/run-command-prompt-commands – Hozikimaru Feb 12 '15 at 20:51
  • @SurgeonofDeath thanks! getting close to what I need. What if Git is not in mu environment variables, how can I work around that? I'm updating my answer to include what's i've done so far. – RoLYroLLs Feb 13 '15 at 19:36
  • @RoLYroLLs I think this will help : http://blog.countableset.ch/2012/06/07/adding-git-to-windows-7-path/ – Hozikimaru Feb 13 '15 at 19:38
  • @SurgeonofDeath thanks for that! That would be a last option if I can't get it to work without adding to the enviro-varibales. – RoLYroLLs Feb 13 '15 at 20:02
  • @RoLYroLLs, based on your update, please do not hard code the Program Files portion. Use what is mentioned on this article : http://stackoverflow.com/questions/194157/c-sharp-how-to-get-program-files-x86-on-windows-vista-64-bit – Hozikimaru Feb 13 '15 at 20:49

3 Answers3

2

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

processStartInfo.FileName = "git";
processStartInfo.Arguments = "status";
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
0

perhaps i misunderstood your question but what about execve? here is an excerpt of it's man page.

NAME

   execve - execute program

SYNOPSIS

   #include <unistd.h>

   int execve(const char *filename, char *const argv[],
              char *const envp[]);

DESCRIPTION

   execve() executes the program pointed to by filename.  filename must > be
   either a binary executable, or a script starting with  a  line  of  > the
   form:

       #! interpreter [optional-arg]
Marc Bredt
  • 905
  • 5
  • 13
  • I am not sure why this was posted as an answer, I think this should be a comment since it is a question? – Hozikimaru Feb 12 '15 at 22:37
  • it is in answer because it fits the question title. you can run a command or a script from c using `execve`. the question is just because i am worried about the git stuff. please let @RoLYrolls leave a comment here to see if i am totally wrong (and that is why it is designed as a question:) – Marc Bredt Feb 12 '15 at 23:57
  • @EmilKakkau thanks for your answer. is this code that can be run from C#? it looks similar to PHP to me (but i could be wrong). Thanks! – RoLYroLLs Feb 13 '15 at 19:35
  • @RoLYroLLs This actually looks very much like C. That was another reason why I am not sure if this could be an answer to this question. The tag indicates to C, I think there may be a confusion related to bash. – Hozikimaru Feb 13 '15 at 19:51
  • If it works with pure C there will definetly a pendent for C# but to figure that out use the search engine of your choice. i just had a short look and it seems that execve is available for C# too. – Marc Bredt Feb 13 '15 at 20:03
0

Check your PATH environment variable and update it

C:/Program Files (x86)/Git/bin/bash.exe": git: No such file or directory

means that it's git which is not found by bash.

1. Check the PATH environment variable in bash (which is and should remain different from Windows one)

Adjust this

processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

to make the terminal visible.

In the terminal you will create with the Process.Start()

Type:

echo ${PATH}

2. Update your path

  • You could update the global path of windows (which requires a restart)
  • You could update the user path of windows (which should require a logoff, but I'm not sure).
  • You just set Path to what you like with System.Environment.SetEnvironmentVariable before starting the Process

Additional note:

If you have like me several versions of bash, command interpreters, git and so on, it could be really messy if you try to concatenate all the paths and hope to find the ideal order. You could see some weird behavior of you beloved commands until you realize it's not the one you intend to run ... think of FIND.exe... And I didn't even think of the user-friendly interface of windows to edit environment variables ...

Fab
  • 14,327
  • 5
  • 49
  • 68