class Git::Superproject::Config

Public Class Methods

from(file) click to toggle source
# File lib/git/superproject.rb, line 28
def self.from(file)
  new(
    `git config --file #{file} --list`
    .split($RS)
    .map(&:strip)
  )
end
new(key_value_pairs) click to toggle source
# File lib/git/superproject.rb, line 36
def initialize(key_value_pairs)
  @superprojects = Hash.new do |superprojects, name|
    superprojects[name] = Set.new # guarantee uniqueness
  end

  key_value_pairs.each do |key_value|
    key, value = key_value.split('=')
    process(key, value)
  end
end

Public Instance Methods

add(name, *repos) click to toggle source
# File lib/git/superproject.rb, line 51
def add(name, *repos)
  repos.each do |repo|
    add_to(name, repo)
  end
end
edit(name, finder, candidates = []) click to toggle source
# File lib/git/superproject.rb, line 63
def edit(name, finder, candidates = [])
  log_to(IO.console, name)

  tempfile = Tempfile.new('superproject_')
  tempfile.write(candidates.join($RS))
  tempfile.close # also ensures flushing!

  until (repos = `#{finder} < #{tempfile.path}`.strip).empty?
    repos.split("\0").each do |repo|
      if repos_for(name).include? repo
        IO.console.puts "Removing #{repo} from #{name}..."
        remove_from(name, repo)
      else
        IO.console.puts "Adding #{repo} to #{name}..."
        add_to(name, repo)
      end
    end
  end

  log_to(IO.console, name)
end
list(name) click to toggle source
# File lib/git/superproject.rb, line 47
def list(name)
  log_to($stdout, name)
end
remove(name, *repos) click to toggle source
# File lib/git/superproject.rb, line 57
def remove(name, *repos)
  repos.each do |repo|
    remove_from(name, repo)
  end
end
save(file) click to toggle source
# File lib/git/superproject.rb, line 85
def save(file)
  # create backup of original file
  FileUtils.mv(file, "#{file}~") if File.exist? file

  @superprojects.keys.each do |name|
    write_to(file, name)
  end

  # copy across all the comments from the original file
  `egrep '^# ' "#{file}~" >> "#{file}"`
end
to_json() click to toggle source
# File lib/git/superproject.rb, line 97
def to_json
  @superprojects.to_json
end

Private Instance Methods

add_to(name, repo) click to toggle source
# File lib/git/superproject.rb, line 110
def add_to(name, repo)
  repo =~ %r{\A[-.\w]+/[-.\w]+\z}
  raise "Invalid repo name: #{repo}" unless $LAST_MATCH_INFO

  @superprojects[name] << repo
end
log_to(io, name) click to toggle source
# File lib/git/superproject.rb, line 125
def log_to(io, name)
  return unless io.tty?

  config = Tempfile.new("#{name}_")
  write_to(config.path, name)

  io.write(File.read(config))
end
process(key, repo) click to toggle source
# File lib/git/superproject.rb, line 103
def process(key, repo)
  key =~ /\Asuperproject\.(?<name>.*)\.repo\z/
  raise "Invalid superproject key: #{key}" unless $LAST_MATCH_INFO

  add_to($LAST_MATCH_INFO[:name], repo)
end
remove_from(name, repo) click to toggle source
# File lib/git/superproject.rb, line 117
def remove_from(name, repo)
  @superprojects[name].delete(repo)
end
repos_for(name) click to toggle source
# File lib/git/superproject.rb, line 121
def repos_for(name)
  @superprojects[name].to_a.sort
end
write_to(file, name) click to toggle source
# File lib/git/superproject.rb, line 134
def write_to(file, name)
  key = "superproject.#{name}.repo"
  repos_for(name).each do |repo|
    `git config --file #{file} --add #{key} #{repo}`
  end
end