我可以获得 git clone 命令的进度(以 grit 发出)并输出到 stdout 吗?

发布于 2024-12-06 18:27:08 字数 2690 浏览 0 评论 0原文

我相当有信心这是不可能的,或者我错过了一个明显的选项,但在咨询 grit 的 Git 类,要点链接 在这篇文章中,以及其他关于 SO 的坚毅标记问题,我都空白了。

我正在使用 grit 来执行一系列安装我的应用程序的 rake 任务。其中一项任务会克隆一些存储库。

使用 linked gist 中的代码作为示例,这是 git 克隆在 grit 中的输出(应该在 irb、ruby 1.9.2 中进行 gem install grit 后开箱即用:

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

我的问题是这样的:我可以恢复克隆命令的标准输出的其余部分吗?克隆真的发生了吗? “Cloning into /tmp/aws...\n”是输出的第一行,仅在克隆完成后返回。

第二个问题是:如果在使用 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 需要最新版本的 gem,这自 2011 年 1 月 23 日以来,截至今天,即 2011 年 9 月 26 日,尚未更新(非常感谢 Daniel Brockman 在下面指出这一点)。你必须从github克隆它手动构建它。

解决方案

如果您提供,克隆会将正确的信息传递给 stderr :process_info:progress (我原来的例子失败了,因为我有一个过时的 gem)。以下是工作代码。由于上述原因,您需要手动构建 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有木有妳兜一样 2024-12-13 18:27:08

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文