Interacting with Git


P

paul h

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
 
Ad

Advertisements

J

Jen

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.
 
P

paul h

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/

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
 
D

David Masover

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.
 
Ad

Advertisements

P

paul h

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top