Thursday, February 10, 2011

how to capture the result of `git status` in a Rails action?

If you run this code in a controller action (notice the backticks)

def index
  …
  `pwd >> tc.log` 
  `git status >> tc.log`
  `ls >> tc.log`
  …
end

, only the 1st and 3rd command will pipe their output to the tc.log file.

I'm trying to get the output of git status (f.ex) in a controller action and have tried many variations (capturing stdout, piping to a file, open3, ..) to no avail.

Any idea?

  • This is a total shot in the dark, but git status might not be writing to stdout.

    Try doing something like:

    `git status >> tc.log 2>&1`
    
    Alain Ravet : thanks, it worked. I didn't know about 2>&1.
    jonnii : No problem. Just so you know, 2>&1 will pipe both the stdout and stderr streams.
    jonnii : Also, it's good etiquette to mark the correct answer.
    From jonnii
  • Dude check out ruby-git. Wraps up git commands in Ruby objects!

0 comments:

Post a Comment