lib/gitolite/gitolite_admin.rb in gitolite-0.0.1.alpha vs lib/gitolite/gitolite_admin.rb in gitolite-0.0.2.alpha

- old
+ new

@@ -1,61 +1,68 @@ module Gitolite class GitoliteAdmin attr_accessor :gl_admin, :ssh_keys, :config - CONF = "/conf/gitolite.conf" - KEYDIR = "/keydir" + CONF = "gitolite.conf" + CONFDIR = "conf" + KEYDIR = "keydir" #Intialize with the path to #the gitolite-admin repository def initialize(path, options = {}) @gl_admin = Grit::Repo.new(path) - @path = path @conf = options[:conf] || CONF + @confdir = options[:confdir] || CONFDIR @keydir = options[:keydir] || KEYDIR - @ssh_keys = load_keys(File.join(@path, @keydir)) - @config = Config.new(File.join(@path, @conf)) + @ssh_keys = load_keys(File.join(path, @keydir)) + @config = Config.new(File.join(path, @confdir, @conf)) end #Writes all aspects out to the file system #will also stage all changes def save + Dir.chdir(@gl_admin.working_dir) + #Process config file + new_conf = @config.to_file(@confdir) + @gl_admin.add(new_conf) #Process ssh keys - files = list_keys(File.join(@path, @keydir)).map{|f| File.basename f} + files = list_keys(@keydir).map{|f| File.basename f} keys = @ssh_keys.values.map{|f| f.map {|t| t.filename}}.flatten - - #Remove all keys we don't have a record for + to_remove = (files - keys).map { |f| File.join(@keydir, f)} - @gl_admin.remove(to_remove) unless to_remove.empty? - - #Write all keys to files, overwriting existing keys - keys.each do |key| - File.open(key, "w") do |f| - f.write key.to_s + @gl_admin.remove(to_remove) + + @ssh_keys.each_value do |key| + key.each do |k| + @gl_admin.add(k.to_file(@keydir)) end end end #commits all staged changes and pushes back #to origin def apply - status = @gl_admin.status + #TODO: generate a better commit message + @gl_admin.commit_index("Commit by gitolite gem") + @gl_admin.git.push({}, "origin", "master") end #Calls save and apply in order def save_and_apply + self.save + self.apply end - + def add_key(key) raise "Key must be of type Gitolite::SSHKey!" unless key.instance_of? Gitolite::SSHKey @ssh_keys[key.owner] << key end - + def rm_key(key) @ssh_keys[key.owner].delete key end private @@ -71,12 +78,15 @@ keys[owner] << new_key end keys end - + def list_keys(path) + old_path = Dir.pwd Dir.chdir(path) - Dir.glob("**/*.pub") + keys = Dir.glob("**/*.pub") + Dir.chdir(old_path) + keys end end end