Class: Git::Superproject::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/git/superproject.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_value_pairs) ⇒ Config

Returns a new instance of Config.



36
37
38
39
40
41
42
43
44
45
# 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

Class Method Details

.from(file) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/git/superproject.rb', line 28

def self.from(file)
  new(
    `git config --file #{file} --list`
    .split($RS)
    .map(&:strip)
  )
end

Instance Method Details

#add(name, *repos) ⇒ Object



51
52
53
54
55
# 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 = []) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object



47
48
49
# File 'lib/git/superproject.rb', line 47

def list(name)
  log_to($stdout, name)
end

#remove(name, *repos) ⇒ Object



57
58
59
60
61
# File 'lib/git/superproject.rb', line 57

def remove(name, *repos)
  repos.each do |repo|
    remove_from(name, repo)
  end
end

#save(file) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# 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_jsonObject



97
98
99
# File 'lib/git/superproject.rb', line 97

def to_json
  @superprojects.to_json
end