module GitBak

gitbak namespace

gitbak namespace

gitbak namespace

Constants

DATE

… date (for gemspec) ;-)

SERVICES
VERSION

version and …

Public Class Methods

_binding() click to toggle source
# File lib/gitbak.rb, line 111
def self._binding; binding; end
api_get(url, opts, info, auth, f) click to toggle source

get data from API, optionally w/ auth @raise AuthError on 401

# File lib/gitbak.rb, line 63
def self.api_get(url, opts, info, auth, f)                    # {{{1
  g = f || -> x { x }
  o = auth ? { http_basic_authentication:
                 [auth[:user],auth[:pass]] } : {}
  begin
    open(url, o.merge(opts || {})) do |t|
      m = t.meta; d = t.read
      d.define_singleton_method(:meta) { m }    # monkey patch :-(
      g[d]
    end
  rescue OpenURI::HTTPError => e
    if e.io.status[0] == '401'
      raise AuthError, "401 for #{auth[:user]} #{info}"
    else
      raise
    end
  end
end
cat_json(pages) click to toggle source

concatenate json

# File lib/gitbak.rb, line 90
def self.cat_json(pages)
  pages.map { |x| JSON.parse x } .flatten 1
end
configure(file) click to toggle source

configure

# File lib/gitbak.rb, line 109
def self.configure(file)                                      # {{{1
  c = {}; x = Object.new
  x.instance_eval { def self._binding; binding; end }
  SERVICES.each_pair do |k,v|
    x.define_singleton_method(k) { |*a| v.configure c, *a }
  end
  x._binding.eval File.read(file), file; c
end
main(verbose, noact, config) click to toggle source

run!

# File lib/gitbak.rb, line 119
def self.main(verbose, noact, config)                         # {{{1
  SERVICES.each_pair do |k,v|
    begin
      v.fetch config, verbose
    rescue AuthError => e
      Misc.die! "authentication failure: #{e}"
    end
  end
  puts
  SERVICES.each_pair do |k,v|
    v.mirror config, verbose, noact
  end
  puts
  if verbose
    puts '=== Summary ==='; puts
    SERVICES.each_pair do |k,v|
      v.summarise config
    end
    puts
  end
end
mirror_repo(verbose, noact, remote, dir) click to toggle source

clone (from remote) or update repository (in dir), optionally verbose

# File lib/gitbak.rb, line 40
def self.mirror_repo(verbose, noact, remote, dir)             # {{{1
  name      = repo_name remote
  name_     = name + '.git'
  repo_dir  = "#{dir}/#{name_}"
  sys       = -> args { Misc.sys *args, verbose: verbose, noact: noact }
  FileUtils.mkdir_p dir
  if Misc.exists? repo_dir
    puts "$ cd #{repo_dir}" if verbose or noact
    FileUtils.cd(repo_dir) do
      sys[ %w{ git remote update } ]
    end
  else
    puts "$ cd #{dir}" if verbose or noact
    FileUtils.cd(dir) do
      sys[ %w{ git clone --mirror -n } + [remote, name_] ]
    end
  end
end
paginate(url, opts, info, auth, f, pag, join) click to toggle source

get paginated data from API

# File lib/gitbak.rb, line 83
def self.paginate(url, opts, info, auth, f, pag, join)
  pages = []; g = -> { api_get url, opts, info, auth, f }
  while url; pages << data = g[]; url = pag[data]; end
  join[pages]
end
repo_name(remote) click to toggle source

extract name from remote; e.g. “git@server:foo/bar.git” and “server/foo/bar.git” become “bar”

# File lib/gitbak.rb, line 34
def self.repo_name(remote)
  remote.sub(%r!^.*[/:]!, '').sub(/\.git$/, '')
end
show_mirror(*args) click to toggle source

show info about mirroring

# File lib/gitbak.rb, line 97
def self.show_mirror(*args)
  puts "==> #{args*' | '} <=="
end
show_summary(info, n) click to toggle source

show info about number of repos

# File lib/gitbak.rb, line 102
def self.show_summary(info, n)
  printf "  %3d | %s\n", n, info
end