Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Groups to separate class, groups keys not include @ and fix includes …

…loading
  • Loading branch information...
commit ba0048fb3f73b34833cbef05b95338fd7e625967 1 parent 83dc560
@simonoff simonoff authored wingrunr21 committed
View
65 lib/gitolite/config.rb
@@ -1,5 +1,4 @@
require 'tempfile'
-require File.join(File.dirname(__FILE__), 'config', 'repo')
module Gitolite
class Config
@@ -7,7 +6,7 @@ class Config
def initialize(config)
@repos = {}
- @groups = Hash.new { |k,v| k[v] = [] }
+ @groups = {}
@filename = File.basename(config)
process_config(config)
end
@@ -27,23 +26,38 @@ def add_repo(repo, overwrite = false)
end
def rm_repo(repo)
- if repo.instance_of?(Gitolite::Config::Repo)
- @repos.delete(repo.name)
- else
- repo.is_a?(Symbol) ? @repos.delete(repo.to_s) : @repos.delete(repo)
- end
+ name = normalize_repo_name(repo)
+ @repos.delete(name)
end
def has_repo?(repo)
- if repo.instance_of?(Gitolite::Config::Repo)
- @repos.has_key?(repo.name)
- else
- repo.is_a?(Symbol) ? @repos.has_key?(repo.to_s) : @repos.has_key?(repo)
- end
+ name = normalize_repo_name(repo)
+ @repos.has_key?(name)
end
def get_repo(repo)
- repo.is_a?(Symbol) ? @repos[repo.to_s] : @repos[repo]
+ name = normalize_repo_name(repo)
+ @repos[name]
+ end
+
+ def add_group(group, owerwrite = false)
+ raise ArgumentError, "Group must be of type Gitolite::Config::Group!" unless group.instance_of? Gitolite::Config::Group
+ @groups[group.name] = group
+ end
+
+ def rm_group(group)
+ name = normalize_group_name(group)
+ @groups.delete(name)
+ end
+
+ def has_group?(group)
+ name = normalize_group_name(group)
+ @groups.has_key?(name)
+ end
+
+ def get_group(group)
+ name = normalize_group_name(group)
+ @groups[name]
end
def to_file(path=".", filename=@filename)
@@ -122,12 +136,12 @@ def process_config(config)
@repos[c].set_git_config(key, value)
end
#group definition
- when /^(@\S+) = ?(.*)/
+ when /^@(\S+) = ?(.*)/
group = $1
users = $2.split
- @groups[group].concat users
- @groups[group].uniq!
+ @groups[group] = Group.new(group) unless has_group?(group)
+ @groups[group].add_users(users)
#gitweb definition
when /^(\S+)(?: "(.*?)")? = "(.*)"$/
repo = $1
@@ -158,6 +172,25 @@ def process_config(config)
end
end
+ def normalize_repo_name(repo)
+ normalize_name(repo, Gitolite::Config::Repo)
+ end
+
+ def normalize_group_name(group)
+ normalize_name(group, Gitolite::Config::Group)
+ end
+
+ def normalize_name(context, constant)
+ case context
+ when constant
+ context.name
+ when Symbol
+ context.to_s
+ else
+ context
+ end
+ end
+
#Raised when something in a config fails to parse properly
class ParseError < RuntimeError
end
View
30 lib/gitolite/config/group.rb
@@ -0,0 +1,30 @@
+module Gitolite
+ class Config
+ #Represents a group inside the gitolite configuration. The name and users
+ #options are all encapsulated in this class
+ class Group
+
+ attr_accessor :name, :users
+
+ def initialize(name)
+ @name = name
+ @users = []
+ end
+
+ def clean_users
+ @users = []
+ end
+
+ def add_users(*users)
+ users.flatten.sort.uniq.each do |user|
+ @users << user
+ end
+ end
+
+ def remove_user(user)
+ @users.delete(user)
+ end
+
+ end
+ end
+end
View
36 spec/config_spec.rb
@@ -180,6 +180,42 @@
r2.description.should == r.description
end
end
+
+ describe "#add_group" do
+ it 'should throw an ArgumentError for non-Gitolite::Config::Group objects passed in' do
+ lambda{ @config.add_group("not-a-group") }.should raise_error(ArgumentError)
+ end
+
+ it 'should add a given group to the groups list' do
+ g = Gitolite::Config::Group.new('cool_group')
+ ngroups = @config.groups.size
+ @config.add_group(g)
+ @config.groups.size.should eql (ngroups + 1)
+ @config.has_group?(:cool_group).should be true
+ end
+
+ end
+
+ describe "#rm_group" do
+ it 'should remove a group for the Gitolite::Config::Group object given' do
+ g = @config.get_group(:oss_repos)
+ g2 = @config.rm_group(g)
+ g2.name.should == g.name
+ end
+
+ it 'should remove a group given a string containing the name' do
+ g = @config.get_group(:oss_repos)
+ g2 = @config.rm_group('oss_repos')
+ g2.name.should == g.name
+ end
+
+ it 'should remove a group given a symbol representing the name' do
+ g = @config.get_group(:oss_repos)
+ g2 = @config.rm_group(:oss_repos)
+ g2.name.should == g.name
+ end
+ end
+
end
describe "#to_file" do
Please sign in to comment.
Something went wrong with that request. Please try again.