我正在尝试将ruby-progressbar gem用于ruby-git gem
我的目标是在git clone期间捕获动态进度条,以便我可以获得此git clone的进度条
我试图以这种方式使用
def my_method
p = ProgressBar.create(:format => '%a %B %p%% %t')
Git.clone('git://github.com/ankit8898/rubymotion-inspect.git','my_repo',:path => '.') do
p.increment
end
end
我无法像我期望的那样获得进度条。
我初始化进度条的方式有什么问题吗?
提前致谢!
答案 0 :(得分:4)
Git.clone
(https://github.com/schacon/ruby-git/blob/master/lib/git.rb#L87)不期待阻止。所以你传递的块就被忽略了。
我不知道如何做到这一点,除了修改ruby-git
gem以启用进度通知。
答案 1 :(得分:1)
Git
库为所有命令添加2>&1
。因此,您的克隆命令最终会执行,如:
git clone ... 2>&1
最终抑制所有输出。您需要做的就是覆盖run_command
中名为Git::Lib
的单个方法,并删除2>&1
。您可以在irb
:
class Git::Lib
class << self
attr_accessor :verbose
end
def run_command(git_cmd, &block)
git_cmd = git_cmd.gsub("2>&1", "").chomp if self.class.verbose
if block_given?
IO.popen(git_cmd, &block)
else
`#{git_cmd}`.chomp
end
end
end
我已经定义了一个额外的verbose
属性。因此,只要您需要实际的git输出,只需设置Git::Lib.verbose = true
并运行Git.clone
或任何其他命令,即可打印实际输出。
这样做,一旦你设置Git::Lib.verbose = true
,然后拨打Git.clone
,它就会显示这样的git进度条:
Cloning into 'rapidftr-addon-cpims'...
remote: Counting objects: 207, done.
remote: Compressing objects: 100% (108/108), done.
remote: Total 207 (delta 95), reused 201 (delta 90)
Receiving objects: 50% (105/207), 83.10 KiB | 112 KiB/s...
# ^^ The above line is git's progress bar, it will keep updating
它可能无法以您期望的特定格式显示进度条,但在下载时仍会显示动态更新。
编辑:添加了示例输出