Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping up a method with ruby-progressbar gem

I am trying to use ruby-progressbar gem with ruby-git gem

My aim is to capture the dynamic progress bar during the git clone so that i can get the progress bar for this git clone

I am trying to use in this way

 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

I am not able to get the progress bar as i expect it.

Anything wrong with the way i have initialized Progress bar ?

Thanks in advance!

like image 970
AnkitG Avatar asked May 13 '13 11:05

AnkitG


2 Answers

Git.clone (https://github.com/schacon/ruby-git/blob/master/lib/git.rb#L87) doesn't expect a block. So the block you pass is simply ignored.

I don't see how this is possible, short of modifying ruby-git gem to enable progress notifications.

like image 190
Sergio Tulentsev Avatar answered Nov 17 '22 14:11

Sergio Tulentsev


The Git library adds 2>&1 to all the commands. So your clone command ends up being executed like:

git clone ... 2>&1

Which ends up suppressing all output. All you need to do is override a single method called run_command in Git::Lib, and remove that 2>&1. You can try this in 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

I've defined an extra verbose attribute. So whenever you need the actual git outputs, just set Git::Lib.verbose = true and run Git.clone or any other command, and the actual outputs will be printed.

What this will do is, once you set Git::Lib.verbose = true and then call Git.clone, it will display the git progress bar like this:

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

It may not show the progress bar in the specific format that you expect, but it will still show dynamic updates as the download happens.

Edit: Added sample outputs

like image 36
Subhas Avatar answered Nov 17 '22 14:11

Subhas