4

I'm fairly confident this is either not possible or I'm missing an obvious option, but after consulting grit's Git class, the gist linked in this SO post, and the other grit tagged questions on SO, I'm coming up blank.

I'm using grit for a series of rake tasks that install my application. One of these tasks clones a few repositories.

Using the code in the linked gist as an example, this is the output of git cloning in grit (should work out of the box after a gem install grit in irb, ruby 1.9.2):

> require 'grit'
> gritty = Grit::Git.new('/tmp/filling-in')
=> #<Grit::Git:0x007f93ae105df8 @git_dir="/tmp/filling-in", @work_tree="/tmp/filling-in", @bytes_read=0>
> gritty.clone({:quiet => false, :verbose => true, :progress => true, :branch => '37s', :timeout => false}, "git://github.com/cookbooks/aws.git", "/tmp/aws") 
=> "Cloning into /tmp/aws...\n" 

My question is this: Can I recover the rest of the clone command's stdout, preferably while the clone is actually happening? The "Cloning into /tmp/aws...\n" is the first line of output, and is only returned once the clone completes.

A secondary question would be: If it's not possible to recover the clone's progress while it's happening with grit, is there another way to show progress of the command while it happens? My concern is a few of our repositories are quite large, and I'd like to give my rakefile's users something so they don't conclude the script is simply hanging while trying to communicate with the remote.

For reference, the "normal" out put of the git clone command would be:

$ git clone git://github.com/cookbooks/aws.git /tmp/test-aws
Cloning into /tmp/test-aws...
remote: Counting objects: 12364, done.
remote: Compressing objects: 100% (3724/3724), done.
remote: Total 12364 (delta 7220), reused 12330 (delta 7203)
Receiving objects: 100% (12364/12364), 5.92 MiB | 70 KiB/s, done.
Resolving deltas: 100% (7220/7220), done.

Quick note: Some of the functionality described here, namely :process_info requires an up-to-date version of the gem, which hasn't been updated since 23 Jan 2011, as of today, 26 Sept 2011 (many thanks to Daniel Brockman for pointing that out, below). You'll have to clone it from github and build it manually.

SOLUTION

Clone passes the correct information to stderr if you give it :process_info and :progress (my original example failed because I had an outdated gem). The following is working code. You need to build the gem manually for reasons described above, at least at this time.

> require 'grit'
> repo = Grit::Git.new('/tmp/throw-away')
> process = repo.clone({:process_info => true, :progress => true, :timeout => false}, 'git://github.com/cookbooks/aws.git', '/tmp/testing-aws-again')  # output supressed
> print process[2]   # i.e. the stderr string of the output
remote: Counting objects: 12364, done.
remote: Compressing objects: 100% (3724/3724), done.
remote: Total 12364 (delta 7220), reused 12330 (delta 7203)
Receiving objects: 100% (12364/12364), 5.92 MiB | 801 KiB/s, done.
Resolving deltas: 100% (7220/7220), done.
Community
  • 1
  • 1
Christopher
  • 42,720
  • 11
  • 81
  • 99

1 Answers1

2

The reason you’re only getting the first line of output is that the rest of it is sent to stderr, not stdout. There is a :process_info option you can pass to get both exit status, stdout and stderr. See https://github.com/mojombo/grit/blob/master/lib/grit/git.rb#L290.

Daniel Brockman
  • 18,826
  • 3
  • 29
  • 40
  • Sorry, should have added that info to the original post. I did try the `:process_info` option (I might be using it incorrectly). When running the same command above, but with slimmed down options that I know work without `:process_info`, it won't clone at all. It immediately spits out an empty string: `> gritty.clone({:branch => '37s', :timeout => false, :process_info => true}, "git://github.com/cookbooks/aws.git", "/tmp/aws-new") => "" ` – Christopher Sep 26 '11 at 22:35
  • 1
    By setting `Grit.debug = true`, you will see what is going on. Furthermore, by using [blame](https://github.com/mojombo/grit/blame/master/lib/grit/git.rb), you find that `:process_info` was introduced in [this commit](https://github.com/mojombo/grit/commit/019bae7c61b0236c093e1bb9eef3b134567b9aa7), from 2011-02-06; however, the [latest version of Grit](https://rubygems.org/gems/grit) is from 2011-01-13. – Daniel Brockman Sep 26 '11 at 22:48
  • Got the most recent version of the gem (and hence `:procces_info`) working, but the `[exitstatus, out, err]` tuple still returns the first line of the clone and nothing more. Output from the my first comment's irb command: `=> [0, "Cloning into /tmp/aws-new...\n", ""]` – Christopher Sep 26 '11 at 23:08
  • Added an update to the question with a working theory, but I'm really not 100% confident I'm right. – Christopher Sep 27 '11 at 02:16