我可以获得git clone命令的进度,在grit中发出,输出到stdout吗?

时间:2011-09-26 21:09:42

标签: ruby git grit

我非常有信心这是不可能的,或者我错过了一个显而易见的选择,但在咨询grit's Git class后,要点与in this SO post相关联,以及其他关于SO的标记问题,我我的空白。

我正在使用grit来完成一系列安装我的应用程序的rake任务。其中一个任务克隆了一些存储库。

使用linked gist中的代码作为示例,这是grit中git cloning的输出(在irb,ruby 1.9.2中gem install grit之后应该开箱即用):< / p>

> 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" 

我的问题是:我可以恢复克隆命令的其余部分stdout,最好是在实际发生克隆时吗? “克隆到/tmp/aws...\”是第一行输出,只有在克隆完成后才会返回。

第二个问题是:如果在使用grit时无法恢复克隆的进度,是否还有另一种显示命令进度的方法?我担心的是我们的一些存储库非常大,而且我想给我的rakefile用户一些东西,所以他们不会断定在尝试与远程通信时脚本只是挂起。

作为参考,git clone命令的“正常”输出将是:

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

快速说明:此处描述的部分功能,即:process_info需要最新版本的宝石,自2011年1月23日起至今,2011年9月26日尚未更新(非常感谢Daniel Brockman在下面指出了这一点。您必须clone it from githubbuild it manually.

克隆将正确的信息传递给stderr,如果你给它:process_info:progress(我原来的例子失败了,因为我有一个过时的宝石)。以下是工作代码。您需要手动构建gem,原因如上所述,至少在此时。

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

1 个答案:

答案 0 :(得分:2)

你只获得第一行输出的原因是它的其余部分被发送到stderr,而不是stdout。您可以传递:process_info选项以获取退出状态,stdout和stderr。请参阅https://github.com/mojombo/grit/blob/master/lib/grit/git.rb#L290