Interacting with Git

Discussion in 'Ruby' started by paul h, Jun 12, 2011.

  1. paul h

    paul h Guest

    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
     
    paul h, Jun 12, 2011
    #1
    1. Advertising

  2. paul h

    Jen Guest

    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 12/06/11 15:05, paul h wrote:
    > 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
    >
     
    Jen, Jun 12, 2011
    #2
    1. Advertising

  3. paul h

    paul h Guest

    On Jun 12, 5:23 pm, Jen <> wrote:
    > 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

    >
    > Hope this helps,
    > Jen.
    >
    > On 12/06/11 15:05, paul h wrote:
    >
    >
    >
    > > 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
     
    paul h, Jun 12, 2011
    #3
  4. 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.
     
    David Masover, Jun 12, 2011
    #4
  5. paul h

    paul h Guest

    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
     
    paul h, Jun 13, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timothy Parez

    Interacting with Windows Services

    Timothy Parez, Apr 7, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,834
  2. John
    Replies:
    4
    Views:
    516
    Joe Agster
    Jul 2, 2003
  3. =?Utf-8?B?bGF4bWk=?=

    interacting windows application

    =?Utf-8?B?bGF4bWk=?=, Jun 8, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    376
    adnan boz
    Jun 8, 2004
  4. =?Utf-8?B?ZGF2aWQ=?=
    Replies:
    2
    Views:
    488
    Bruce Barker
    Nov 21, 2005
  5. Brian

    Interacting with Outlook

    Brian, Jan 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    441
    Craig Deelsnyder
    Jan 5, 2006
Loading...

Share This Page