0

I am trying to set up a system where, when I commit changes to my GitHub repository, my server also receives those commits.

To do that, I set my GitHub repo to send a post-receive hook to a URL on my rails app. I have a controller that will handle this.

The problem is this: how can I run git commands in a rails controller? I get how to do it in the command line.

Do I just type something like this:

# controller method where post-receive hook is sent
def commit
 git pull origin development
end
user2205763
  • 1,499
  • 2
  • 14
  • 30
  • possible duplicate of [Making git auto-commit](http://stackoverflow.com/questions/420143/making-git-auto-commit) – user2205763 Aug 10 '14 at 06:48

3 Answers3

2

Do this!

def commit
 system "git pull origin development"
end
  • 1
    Also a good option. I prefer an abstraction for things like error handling, but this is certainly the quickest and simplest way to go. – Vidya Oct 26 '13 at 16:30
1

There is quite a big difference between what your stated goal is and what the command you're planning to use does. In order to get the commits, all you need to run is git fetch. You can use system or backticks in order to run shell commands from your ruby code, but you need to be careful about what you run. You should keep in mind that a pull is a merge, which means that at any time changes could conflict and it would leave your repository waiting for human intervention (not to mention files with conflict markers all over them) as it's part of the high-level user interface, and not meant at all to be scripted.

If you do want the version of the files in the tip, to do deployment or similar, you can check out http://gitolite.com/the-list-and-irc/deploy.html for some different ways to do that, depending on the situation.

You can use a combination of shelling out to git fetch and git checkout or rugged in your code to perform this update, and be aware that this is all IO-bound (part of it over the network) which means that it can take an arbitrary amount of time to run, which may not work too well with the rest of your app if it's blocking ruby's execution.

Carlos Martín Nieto
  • 5,207
  • 1
  • 15
  • 16
0

Try a Ruby abstraction on top of Git like Rugged or Ruby/Git. Both remain active while other similar libraries seem to have fallen by the wayside.

Vidya
  • 29,932
  • 7
  • 42
  • 70