Permalink
Browse files

Merged changes by odd_poet

  • Loading branch information...
2 parents 73ed3ff + 263c7fc commit ee04785fbffde3741fd96e3a1eb6d970a30f0620 @dfyx dfyx committed Mar 17, 2011
View
3 .gitignore 100644 → 100755
@@ -1,2 +1 @@
-extra/ssh/private_key
-extra/ssh/private_key.pub
+.*.swp
View
5 README.rdoc 100644 → 100755
@@ -1,3 +1,8 @@
+= Patches
+* work with gitolite
+* support subproject git repo. (e.g. git@site.name:parent_project/subproject.git)
+* fix bugs.
+
= Redmine Gitolite
A Redmine plugin which manages your gitolite configuration based on your projects and memberships in Redmine. Includes Public Key management views (extracted from http://plan.io).
View
4 app/models/gitolite_public_key.rb 100644 → 100755
@@ -1,6 +1,6 @@
class GitolitePublicKey < ActiveRecord::Base
- STATUS_ACTIVE = true
- STATUS_LOCKED = false
+ STATUS_ACTIVE = 1
+ STATUS_LOCKED = 0
belongs_to :user
validates_uniqueness_of :title, :scope => :user_id
View
@@ -1,8 +1,20 @@
<% if @project.repository && @project.repository.is_a?(Repository::Git) %>
+<% repo_urls = Gitolite::get_urls(@project) %>
<div class="box">
<h3>Git Repository</h3>
<p>
- <%= Gitolite::renderUrls(Setting.plugin_redmine_gitolite['developerBaseUrls'], @project.identifier, false,"") %>
+ <strong>Developer URL</strong>
+ <ul>
+ <% repo_urls[:developer].each do |url| %>
+ <li> <%= url %>
+ <% end %>
+ </ul>
+ <strong>Read-Only URL</strong>
+ <ul>
+ <% repo_urls[:read_only].each do |url| %>
+ <li> <%= url %>
+ <% end %>
+ </ul>
</p>
</div>
<% end %>
View
@@ -17,16 +17,15 @@
<pre> mkdir <%= @project.identifier %>
cd <%= @project.identifier %>
git init
- touch README
- git add README
- git commit -m 'initial commit'
- git remote add origin <%= Setting.plugin_redmine_gitolite['developerBaseUrls'] + @project.identifier + '.git' %>
+ git add .
+ git commit -m 'Initializing <%= @project %> repository'
+ git remote add origin <%= Setting.plugin_redmine_gitolite['developerBaseUrls'] + Gitolite.repository_name(@project) + '.git' %>
git push origin master
</pre>
<h2>Existing Git Repo?</h2>
<pre> cd existing_git_repo
- git remote add origin <%= Setting.plugin_redmine_gitolite['developerBaseUrls'] + @project.identifier + '.git' %>
+ git remote add origin <%= Setting.plugin_redmine_gitolite['developerBaseUrls'] + Gitolite.repository_name(@project) + '.git' %>
git push origin master
</pre>
View
4 init.rb 100644 → 100755
@@ -9,9 +9,9 @@
Redmine::Plugin.register :redmine_gitolite do
name 'Redmine Gitolite plugin'
- author 'Joshua Hogendorn, originally Jan Schulz-Hofen'
+ author 'Christian Käser, Zsolt Parragi, Yunsang Choi, Joshua Hogendorn, Jan Schulz-Hofen and others'
description 'Enables Redmine to update a gitolite server.'
- version '0.0.1alpha'
+ version '0.1.0'
settings :default => {
'gitoliteUrl' => 'git@localhost:gitolite-admin.git',
'gitoliteIdentityFile' => '/srv/projects/redmine/miner/.ssh/id_rsa',
View
@@ -1,51 +1,25 @@
require 'lockfile'
-require 'inifile'
require 'net/ssh'
require 'tmpdir'
-require 'gitolite/gitolite_config'
+require 'gitolite_conf.rb'
module Gitolite
- def self.renderReadOnlyUrls(baseUrlStr, projectId,parent)
- rendered = ""
- if (baseUrlStr.length == 0)
- return rendered
- end
-
- baseUrlList = baseUrlStr.split("%p")
- if (not defined?(baseUrlList.length))
- return rendered
- end
-
- rendered = rendered + "<strong>Read Only Url:</strong><br />"
- rendered = rendered + "<ul>"
-
- rendered = rendered + "<li>" + baseUrlList[0] +(parent ? "" : "/"+parent+"/")+ projectId + baseUrlList[1] + "</li>"
-
- rendered = rendered + "</ul>\n"
-
- return rendered
- end
-
- def self.renderUrls(baseUrlStr, projectId, isReadOnly, parent)
- rendered = ""
- if(baseUrlStr.length == 0)
- return rendered
- end
- baseUrlList=baseUrlStr.split(/[\r\n\t ,;]+/)
-
- if(not defined?(baseUrlList.length))
- return rendered
- end
+ def self.repository_name project
+ parent_name = project.parent ? repository_name(project.parent) : ""
+ return "#{parent_name}/#{project.identifier}".sub(/^\//, "")
+ end
+
+ def self.get_urls(project)
+ urls = {:read_only => [], :developer => []}
+ read_only_baseurls = Setting.plugin_redmine_gitolite['readOnlyBaseUrls'].split(/[\r\n\t ,;]+/)
+ developer_baseurls = Setting.plugin_redmine_gitolite['developerBaseUrls'].split(/[\r\n\t ,;]+/)
+ project_path = repository_name(project) + ".git"
- rendered = rendered + "<strong>" + (isReadOnly ? "Read Only" : "Developer") + " " + (baseUrlList.length == 1 ? "URL" : "URLs") + ": </strong><br/>"
- rendered = rendered + "<ul>";
- for baseUrl in baseUrlList do
- rendered = rendered + "<li>" + "<span style=\"width: 95%; font-size:10px\">" + baseUrl+ (parent ? "" : "/"+parent+"/") + projectId + ".git</span></li>"
- end
- rendered = rendered + "</ul>\n"
- return rendered
+ read_only_baseurls.each {|baseurl| urls[:read_only] << baseurl + project_path}
+ developer_baseurls.each {|baseurl| urls[:developer] << baseurl + project_path}
+ return urls
end
def self.update_repositories(projects)
@@ -88,9 +62,7 @@ def self.update_repositories(projects)
# clone repo
%x[env GIT_SSH=#{ssh_with_identity_file} git clone #{Setting.plugin_redmine_gitolite['gitoliteUrl']} #{local_dir}/gitolite]
-
- conf = GitoliteConfig.new(File.join(local_dir,'gitolite','conf','gitolite.conf'))
- original = conf.clone
+ conf = Config.new(File.join(local_dir, 'gitolite', 'conf', 'gitolite.conf'))
changed = false
projects.select{|p| p.repository.is_a?(Repository::Git)}.each do |project|
@@ -117,33 +89,31 @@ def self.update_repositories(projects)
end
end
- # update users in config file
- name = "#{project.identifier}"
- conf.clear_users name
- conf.add_users name, :r, read_users.map{|u| u.gitolite_public_keys.active}.flatten.map{ |key| "#{key.identifier}" }
-
- # TODO: we should handle two different groups for this
- # conf.add_users name, :rw, read_users.map{|u| u.gitolite_public_keys.active}.flatten.map{ |key| "#{key.identifier}" }
- conf.add_users name, :rwp, write_users.map{ |user| "#{user.login.underscore}" }
-
- # TODO: gitweb and git daemon support!
+ # write config file
+ repo_name = repository_name(project)
+ read_users = read_users.map{|u| u.login.underscore}
+ write_users = write_users.map{|u| u.login.underscore}
+ conf.set_read_user repo_name, read_users
+ conf.set_write_user repo_name, write_users
+
+ # TODO: gitweb and git daemon support!
end
-
- unless conf.eql?(original)
- conf.write
+
+ if conf.changed?
+ conf.save
changed = true
end
if changed
git_push_file = File.join(local_dir, 'git_push.sh')
-
- # Changed to unix-style
- # TODO: platform independent code
- new_dir= File.join(local_dir,'gitolite')
+ new_dir= File.join(local_dir,'gitolite')
+
File.open(git_push_file, "w") do |f|
+ f.puts "#!/bin/sh"
f.puts "cd #{new_dir}"
- f.puts "git add keydir/* conf/gitolite.conf"
+ f.puts "git add keydir/*"
+ f.puts "git add conf/gitolite.conf"
f.puts "git config user.email '#{Setting.mail_from}'"
f.puts "git config user.name 'Redmine'"
f.puts "git commit -a -m 'updated by Redmine Gitolite'"
@@ -1,72 +0,0 @@
-
-module Gitolite
-
- class GitoliteConfig
-
- attr_accessor :repos
-
- def initialize fn
- @fn = fn
-
- read
- end
-
- def clear_users repo
- @repos[repo] = { :rwp => [], :rw => [], :r => [] }
- end
-
- def add_users repo, type, list
- @repos[repo] ||= { :rwp => [], :rw => [], :r => [] }
- @repos[repo][type] = @repos[repo][type] | list
-
- end
-
-
- def write
- File.open(@fn, 'w') do |f|
- @repos.each do |name, users|
- f.puts " repo\t#{name}"
- f.puts " RW+ = #{users[:rwp].uniq.join(' ')}" unless users[:rwp].empty?
- f.puts " RW = #{users[:rw].uniq.join(' ')}" unless users[:rw].empty?
- f.puts " R = #{users[:r].uniq.join(' ')}" unless users[:r].empty?
- end
- end
- end
-
- private
-
- def read
- # TODO: error handling
- @repos = {}
- parent = nil
- File.open(@fn, 'r') do |f|
- while s = f.gets do
- if s =~ /repo[ \t]+([-a-zA-Z1-9_]+)/
- parent = $1.chomp
- @repos[parent] = { :r => [], :rw => [], :rwp => [] }
- end
-
- users = []
- usertype = nil
- if s =~ /RW\+[ \t]+=(.+)/
- usertype = :rwp
- users = $1.split
- end
- if s =~ /RW[ \t]+=(.+)/
- usertype = :rw
- users = $1.split
- end
- if s =~ /R[ \t]+=(.+)/
- usertype = :r
- users = $1.split
- end
-
- if usertype
- @repos[parent][usertype] = @repos[parent][usertype] | users
- end
- end
- end
- end
- end
-
-end
View
@@ -0,0 +1,97 @@
+module Gitolite
+ class Config
+ def initialize file_path
+ @path = file_path
+ load
+ end
+
+ def save
+ File.open(@path, "w") do |f|
+ f.puts content
+ end
+ @original_content = content
+ end
+
+ def add_write_user repo_name, users
+ repository(repo_name).add "RW+", users
+ end
+
+ def set_write_user repo_name, users
+ repository(repo_name).set "RW+", users
+ end
+
+ def add_read_user repo_name, users
+ repository(repo_name).add "R", users
+ end
+
+ def set_read_user repo_name, users
+ repository(repo_name).set "R", users
+ end
+
+ def changed?
+ @original_content != content
+ end
+
+ private
+ def load
+ @original_content = []
+ @repositories = {}
+ cur_repo_name = nil
+ File.open(@path).each_line do |line|
+ @original_content << line
+ tokens = line.strip.split
+ if tokens.first == 'repo'
+ cur_repo_name = tokens.last
+ @repositories[cur_repo_name] = AccessRights.new
+ next
+ end
+ cur_repo_right = @repositories[cur_repo_name]
+ if cur_repo_right and tokens[1] == '='
+ cur_repo_right.add tokens.first, tokens[2..-1]
+ end
+ end
+ @original_content = @original_content.join
+ end
+
+ def repository repo_name
+ @repositories[repo_name] ||= AccessRights.new
+ end
+
+
+ def content
+ content = []
+ @repositories.each do |repo, rights|
+ content << "repo\t#{repo}"
+ rights.each do |perm, users|
+ content << "\t#{perm}\t=\t#{users.join(' ')}" if users.length > 0
+ end
+ content << ""
+ end
+ return content.join("\n")
+ end
+
+ end
+
+ class AccessRights
+ def initialize
+ @rights = {}
+ end
+
+ def add perm, users
+ @rights[perm.to_sym] ||= []
+ @rights[perm.to_sym] << users
+ @rights[perm.to_sym].flatten!
+ @rights[perm.to_sym].uniq!
+ end
+
+ def set perm, users
+ @rights[perm.to_sym] = []
+ add perm, users
+ end
+
+ def each
+ @rights.each {|k,v| yield k, v}
+ end
+ end
+end
+

0 comments on commit ee04785

Please sign in to comment.