Interacting with Git

Hi all,

I need to interact with Git via Ruby on my server and hope someone can
point me in the right direction.

This is the env I have:

Paperless office (Windows boxes), using Git to back up files to the
repo on my server (Centos 5)

Back Office server application built with Rails manages customer
information/emails/standard letters/todo lists etc

Ruby 1.9.2

This is the problem I have:

The Rails app needs to pull backed-up files from the Git repo into a
private part of the server so that the Rails app can read/evaluate/
display files backed up from the office.

Therefore, I need ruby to:

git pull …
enter password
wait for git to complete pulling files in
hand control back to the Rails app to analyse the files and perform
any back office processing as I see fit

Obviously, I could just ssh into my server and pull the files myself,
but that relies on me being around the office all the time, what I
really need to do is:

a) automate the process and,
b) provide a function in the Rails app so that non-technical staff can
press a button on a web page and begin the process (this part is
simple if I can solve a), above)

I’ve been through the Pickaxe book, and am going to look closer at PTY
and the ‘expect’ method later today and see if I can figure it out
with these, but I am self taught, not ‘classically trained’ , so I
would imagine this may spawn a whole new branch of learning for me in
regards to processes and subprocesses, which I need to learn anyhow,
so would appreciate any pointers any of the ruby experts out there
might be willing to provide.

Thanks

Paul

Have you tried capistrano? That’s what I use on both my windows
development machine, my windows server, and my linux server.

Here, I added a gist showing what you can do with capistrano. You can
also customize all of the rake tasks to do whatever you want within the
deploy scope:

Note: I run this off a windows 7 64-bit development box and deploy to a
linux remote server. But, you can deploy remotely, locally, and set the
deployment path to anything. Once everything is set and working, you
customize your rake tasks and can work on automation from there.

Hi Paul,
I am currently working on a project that aims to use git as a back-end
data store. I am using the ‘grit’ Ruby library. I believe it is used to
power git hum and will enable you to do what you require. I’m however
not an expert, and documentation is a bit thin on the ground.

You can install it by typing:
gem install grit

the url is:
http://grit.rubyforge.org/

Hope this helps,
Jen.

On Jun 12, 5:23pm, Jen [email protected] wrote:

http://grit.rubyforge.org/
Hi Jen,

Many, many thanks for the tip. I’ve just had a look at the
documentation and it appears that this is more than I could have hoped
for - the world of ruby just gets better and better.

It looks like I now won’t need to clone or pull the files out of the
repo in order to access them from my Rails app, and by being able to
traverse the Git tree, my Rails app can easily just work on the the
changed files, access data from old files, deleted files, etc. I
should be able to get access to the complete documentation history for
my office, for any given set of dates.

Many thanks

Paul

On Sunday, June 12, 2011 09:05:27 AM paul h wrote:

Therefore, I need ruby to:

git pull …
enter password
wait for git to complete pulling files in
hand control back to the Rails app to analyse the files and perform
any back office processing as I see fit

Suggestion: Set up public-key authentication with ssh. If you’re
paranoid, you
could fire up an ssh-agent and do ssh-add while booting the application
(requiring you to be there for the boot), but it seems to me that having
your
Rails app know your ssh password isn’t any less dangerous than having an
ssh
key file somewhere accessible to your Rails app.

Then, maybe you want to have Git log stuff, but there’s no longer any
reason
you need to interact with Git other than fire off the command and check
the
exit code. In other words…

I’ve been through the Pickaxe book, and am going to look closer at PTY
and the ‘expect’ method later today and see if I can figure it out
with these…

You don’t need that, you don’t need Grit unless you find it useful for
other
things. The simplest thing that could work is:

if system ‘git pull …’

success

else
logger.error “git pull failed with exit code #{$?}”
end

It gets a little more complicated if you need to log the git output from
Ruby.
I’m sure there’s a better way to do this:

require ‘open3’
Open3.popen3 ‘git pull …’ do |stdin, stdout, stderr, wait_thr|
stdin.close
threads = []
threads << Thread.new {
stdout.each_line { |line|
logger.info line
}
}
threads << Thread.new {
stderr.each_line { |line|
logger.error line
}
}
threads.each(&:join)
stdout.close
stderr.close
if wait_thr.value.success?
# success
else
logger.error “Git pull failed with exit code
#{wait_thr.value.exitcode}”
end
end

Finally, you probably want to replace the string ‘git pull’ with
separate
string arguments, like:

Open3.popen ‘git’, ‘pull’, …

Aside from saving you some string concatenation, it also means you don’t
have
to deal with quoting things for the shell.

On Jun 12, 9:18pm, David M. [email protected] wrote:

Suggestion: Set up public-key authentication with ssh. If you’re paranoid, you

and the ‘expect’ method later today and see if I can figure it out

}

success

Aside from saving you some string concatenation, it also means you don’t have
to deal with quoting things for the shell.

Hi David,

Thanks for the extra solution.

I will probably end up using both yours and Jens ideas. Using Grit
allows me to access historical information - which is important -
while pulling the files (a lot of which are PDF, excel and word docs)
allows me to easily display the doc on screen to view or re-print.

Thanks

Paul