From: paul h Date: 2011-06-13T16:55:34+09:00 Subject: Re: Interacting with Git On Jun 12, 9:18 pm, David Masover wrote: > 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. 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