class Twigg::Command::GitHost

This is an abstract superclass the holds code common to the “gerrit” and “github” subcommands.

Public Class Methods

new(*args) click to toggle source
Calls superclass method Twigg::Command::new
# File lib/twigg/command/git_host.rb, line 6
def initialize(*args)
  super
  @sub_subcommand = @args.first

  unless (1..2).cover?(@args.size) &&
    sub_subcommands.include?(@sub_subcommand)
    # eg. "Twigg::Command::{Gerrit,GitHub}" -> "gerrit/github"
    Help.new(self.class.to_s.split('::').last.downcase).run!
  end

  @repositories_directory = @args[1] || Config.repositories_directory
end

Public Instance Methods

run() click to toggle source
# File lib/twigg/command/git_host.rb, line 19
def run
  send @sub_subcommand
end

Private Instance Methods

address(*args) click to toggle source
# File lib/twigg/command/git_host.rb, line 88
def address(*args)
  raise NotImplementedError # subclass responsibility
end
clone() click to toggle source
# File lib/twigg/command/git_host.rb, line 40
def clone
  for_each_repo do |project|
    if File.directory?(project)
      print 'skipping (already present);' if @verbose
    else
      print 'cloning...' if @verbose
      git_clone(project)
    end
  end
end
for_each_repo(&block) click to toggle source
# File lib/twigg/command/git_host.rb, line 29
def for_each_repo(&block)
  Dir.chdir @repositories_directory do
    projects.each do |project|
      print @verbose ? "#{project}: " : '.'
      block.call(project)
      puts ' done' if @verbose
    end
    puts
  end
end
git(*args) click to toggle source

Convenience method for running a Git command that is expected to succeed (raises an error if a non-zero exit code is produced).

# File lib/twigg/command/git_host.rb, line 66
def git(*args)
  Process.wait(IO.popen(%w[git] + args).pid)
  raise unless $?.success?
end
git_clone(project) click to toggle source

Runs `git clone` to obtain the specified `project`.

# File lib/twigg/command/git_host.rb, line 72
def git_clone(project)
  git 'clone', '--quiet', address(project)
end
git_pull() click to toggle source

Runs `git fetch –quiet` followed by `git merge –ff-only –quiet FETCH_HEAD`.

We do this as two commands rather than a `git pull` because the latter is much fussier about tracking information being in place.

# File lib/twigg/command/git_host.rb, line 81
def git_pull
  git 'fetch', '--quiet'
  git 'merge', '--ff-only', '--quiet', 'FETCH_HEAD'
rescue => e
  # could die here if remote doesn't contain any commits yet
end
projects() click to toggle source

Returns the list of all projects hosted within a Git host.

# File lib/twigg/command/git_host.rb, line 93
def projects
  raise NotImplementedError # subclass responsibility
end
sub_subcommands() click to toggle source
# File lib/twigg/command/git_host.rb, line 25
def sub_subcommands
  %w[clone update]
end
update() click to toggle source
# File lib/twigg/command/git_host.rb, line 51
def update
  for_each_repo do |project|
    if File.directory?(project)
      Dir.chdir project do
        print 'pulling...' if @verbose
        git_pull
      end
    else
      print 'skipping (not present);' if @verbose
    end
  end
end