KOMATSU Shinichiro
koma2****@ms*****
2004年 12月 5日 (日) 22:46:51 JST
hiki-dev の皆様、はじめまして。 小松と申します。 なぜか cvs, svn といったリポジトリまわりのコードが hikifarm に入っているのが昔から気になっていたので、 この辺をスッキリさせてみました。 patch を添付しますが、やったことは、 (1) misc/hikifarm/index.cgi の ReposDefault, ReposCvs, ReposSvn を取り出して、 それぞれ hiki/repos/{default,cvs,svn}.rb に分割して放り込んだ。 (2) さらに、misc/plugin/{cvs,svn}.rb の中にある {cvs,svn}_{commit,delete} も Repos{Cvs,Svn} に入れた。 (3) misc/hikifarm/index.cgi の create_repos を hiki/util.rb に放り込み、hikifarm 以外からも使えるようにした。 (4) ここまでやると、plugin の cvs.rb と svn.rb は 1つにまとめてしまえることがわかったので、 これらはまとめた上で plugin/00default.rb に入れてしまった。 の 4点です。 メリットは、 [1] リポジトリまわりのコードを hikifarm 以外からも使えるようになった。 [2] 今まで、リポジトリ管理するためには hikifarm.conf で repos_type を指定しただけではダメで、 repos_type に応じて plugin を有効にしないといけなかったのだけど、 しばしばそれを忘れてハマることが多かった。 repos_type を指定するだけでリポジトリ管理ができるようになったので、 ハマる心配はなくなった。 といったところだと思います。 先日の IW2004 でかずひこさんが 「リポジトリまわりは plugin でなく hiki に入れてしまってもいいかも」 とおっしゃっていたので、patch を投げてみました。 検討していただければと思います。 -- ┏━━━━━━━━━━━━━━━━━━━━━━┓ 小松 晋一朗 koma2****@ms***** koma2****@momon***** http://straycat.ms.u-tokyo.ac.jp/~koma2/ ┗━━━━━━━━━━━━━━━━━━━━━━┛ -------------- next part -------------- diff --exclude=CVS -urN hiki.orig/hiki/repos/cvs.rb hiki/hiki/repos/cvs.rb --- hiki.orig/hiki/repos/cvs.rb Thu Jan 1 09:00:00 1970 +++ hiki/hiki/repos/cvs.rb Sun Dec 5 22:11:04 2004 @@ -0,0 +1,69 @@ +# $Id$ +# Copyright (C) 2003, Koichiro Ohba <koich****@meado*****> +# Copyright (C) 2003, Yasuo Itabashi <yasuo_itabashi{@}hotmail.com> +# You can distribute this under GPL. + +require 'hiki/repos/default' + +# CVS Repository Backend +module Hiki + class ReposCvs < ReposDefault + def setup() + oldpwd = Dir.pwd + begin + Dir.chdir( @data_path ) + system( "cvs -d #{@root} init > /dev/null 2>&1" ) + if not File.directory?(".CVSROOT") then + system( "cvs -d #{@root} co -d .CVSROOT CVSROOT > /dev/null 2>&1" ) + end + Dir.chdir( ".CVSROOT" ) + system( "cvs -d #{@root} update > /dev/null 2>&1" ) + ensure + Dir.chdir( oldpwd.untaint ) + end + end + def imported?( wiki ) + return File.directory?( "#{@root}/#{wiki}" ) + end + def import( wiki ) + oldpwd = Dir.pwd + begin + Dir.chdir( "#{@data_path}/#{wiki}/text" ) + system( "cvs -d #{@root} import -m 'Starting #{wiki}' #{wiki} T#{wiki} start > /dev/null 2>&1" ) + Dir.chdir( '..' ) + system( "cvs -d #{@root} co -d text #{wiki} > /dev/null 2>&1" ) + ensure + Dir.chdir( oldpwd.untaint ) + end + end + def update( wiki ) + oldpwd = Dir.pwd + begin + Dir.chdir( "#{@data_path}/#{wiki}/text" ) + system( "cvs -d #{@root} update > /dev/null 2>&1" ) + ensure + Dir.chdir( oldpwd.untaint ) + end + end + def commit( page ) + oldpwd = Dir.pwd.untaint + begin + Dir.chdir( "#{@data_path}/text" ) + system( "cvs -d #{@root} add -- #{page.escape} > /dev/null 2>&1".untaint ) + system( "cvs -d #{@root} ci -m '#{ENV['REMOTE_ADDR']} - #{ENV['REMOTE_HOST']}' > /dev/null 2>&1".untaint ) + ensure + Dir.chdir( oldpwd ) + end + end + def delete( page ) + oldpwd = Dir.pwd.untaint + begin + Dir.chdir( "#{@data_path}/text" ) + system( "cvs -d #{@root} remove -- #{page.escape} > /dev/null 2>&1".untaint ) + system( "cvs -d #{@root} ci -m '#{ENV['REMOTE_ADDR']} - #{ENV['REMOTE_HOST']}' > /dev/null 2>&1".untaint ) + ensure + Dir.chdir( oldpwd ) + end + end + end +end diff --exclude=CVS -urN hiki.orig/hiki/repos/default.rb hiki/hiki/repos/default.rb --- hiki.orig/hiki/repos/default.rb Thu Jan 1 09:00:00 1970 +++ hiki/hiki/repos/default.rb Sun Dec 5 22:11:04 2004 @@ -0,0 +1,29 @@ +# $Id$ +# Copyright (C) 2003, Koichiro Ohba <koich****@meado*****> +# Copyright (C) 2003, Yasuo Itabashi <yasuo_itabashi{@}hotmail.com> +# You can distribute this under GPL. + +# Null Repository Backend + +module Hiki + class ReposDefault + attr_reader :root, :data_path + def initialize(root, data_path) + @root = root + @data_path = data_path + end + def setup() + end + def imported?( wiki ) + return true + end + def import( wiki ) + end + def update( wiki ) + end + def commit( page ) + end + def delete( page ) + end + end +end diff --exclude=CVS -urN hiki.orig/hiki/repos/svn.rb hiki/hiki/repos/svn.rb --- hiki.orig/hiki/repos/svn.rb Thu Jan 1 09:00:00 1970 +++ hiki/hiki/repos/svn.rb Sun Dec 5 22:11:04 2004 @@ -0,0 +1,71 @@ +# $Id$ +# Copyright (C) 2003, Koichiro Ohba <koich****@meado*****> +# Copyright (C) 2003, Yasuo Itabashi <yasuo_itabashi{@}hotmail.com> +# You can distribute this under GPL. + +require 'hiki/repos/default' + +# Subversion Repository Backend +module Hiki + class ReposSvn < ReposDefault + def setup() + system( "svnadmin create #{@root} > /dev/null 2>&1" ) + end + def imported?( wiki ) + s = '' + open("|svn ls file://#{@root}/#{wiki}") do |f| + s << (f.gets( nil ) ? $_ : '') + end + + if %r|^trunk/$| =~ s then + return true + else + return false + end + end + def import( wiki ) + oldpwd = Dir.pwd + begin + Dir.chdir( "#{@data_path}/#{wiki}/text" ) + system( "svnadmin create #{@root}/#{wiki} > /dev/null 2>&1" ) + system( "svn import -m 'Starting #{wiki}' . file://#{@root}/#{wiki}/trunk > /dev/null 2>&1" ) + Dir.chdir( '..' ) + rmdir( 'text' ) + system( "svn checkout file://#{@root}/#{wiki}/trunk text > /dev/null 2>&1" ) + system( "svn propdel svn:mime-type -R text > /dev/null 2>&1" ) + ensure + Dir.chdir( oldpwd.untaint ) + end + end + def update( wiki ) + oldpwd = Dir.pwd + begin + Dir.chdir( "#{@data_path}/#{wiki}/text" ) + system( "svn update > /dev/null 2>&1" ) + ensure + Dir.chdir( oldpwd.untaint ) + end + end + def commit( page ) + oldpwd = Dir.pwd.untaint + begin + Dir.chdir( "#{@data_path}/text" ) + system( "svn add -- #{page.escape} > /dev/null 2>&1".untaint ) + system( "svn propdel svn:mime-type -- #{page.escape} > /dev/null 2>&1".untaint ) + system( "svn ci -m '#{ENV['REMOTE_ADDR']} - #{ENV['REMOTE_HOST']}' > /dev/null 2>&1".untaint ) + ensure + Dir.chdir( oldpwd ) + end + end + def delete( page ) + oldpwd = Dir.pwd.untaint + begin + Dir.chdir( "#{@data_path}/text" ) + system( "svn remove -- #{page.escape} > /dev/null 2>&1".untaint ) + system( "svn ci -m '#{ENV['REMOTE_ADDR']} - #{ENV['REMOTE_HOST']}' > /dev/null 2>&1".untaint ) + ensure + Dir.chdir( oldpwd ) + end + end + end +end diff --exclude=CVS -urN hiki.orig/hiki/util.rb hiki/hiki/util.rb --- hiki.orig/hiki/util.rb Mon Nov 15 23:45:23 2004 +++ hiki/hiki/util.rb Sun Dec 5 16:46:56 2004 @@ -213,5 +213,21 @@ arr[0...len-2].join('') + '..' end end + + # Create repository backend + def create_repos(repos_type, repos_root, data_path) + case repos_type + when 'cvs' + require 'hiki/repos/cvs' + return ReposCvs.new(repos_root, data_path) + when 'svn' + require 'hiki/repos/svn' + return ReposSvn.new(repos_root, data_path) + else + require 'hiki/repos/default' + return ReposDefault.new(repos_root, data_path) + end + end + module_function :create_repos end end diff --exclude=CVS -urN hiki.orig/misc/hikifarm/index.cgi hiki/misc/hikifarm/index.cgi --- hiki.orig/misc/hikifarm/index.cgi Sun Oct 31 19:37:58 2004 +++ hiki/misc/hikifarm/index.cgi Sun Dec 5 16:46:56 2004 @@ -113,118 +113,6 @@ } end -# Null Repository Backend -class ReposDefault - attr_reader :root, :data_path - def initialize(root, data_path) - @root = root - @data_path = data_path - end - def setup() - end - def imported?( wiki ) - return true - end - def import( wiki ) - end - def update( wiki ) - end -end - -# CVS Repository Backend -class ReposCvs < ReposDefault - def setup() - oldpwd = Dir.pwd - begin - Dir.chdir( @data_path ) - system( "cvs -d #{@root} init > /dev/null 2>&1" ) - if not File.directory?(".CVSROOT") then - system( "cvs -d #{@root} co -d .CVSROOT CVSROOT > /dev/null 2>&1" ) - end - Dir.chdir( ".CVSROOT" ) - system( "cvs -d #{@root} update > /dev/null 2>&1" ) - ensure - Dir.chdir( oldpwd.untaint ) - end - end - def imported?( wiki ) - return File.directory?( "#{@root}/#{wiki}" ) - end - def import( wiki ) - oldpwd = Dir.pwd - begin - Dir.chdir( "#{@data_path}/#{wiki}/text" ) - system( "cvs -d #{@root} import -m 'Starting #{wiki}' #{wiki} T#{wiki} start > /dev/null 2>&1" ) - Dir.chdir( '..' ) - system( "cvs -d #{@root} co -d text #{wiki} > /dev/null 2>&1" ) - ensure - Dir.chdir( oldpwd.untaint ) - end - end - def update( wiki ) - oldpwd = Dir.pwd - begin - Dir.chdir( "#{@data_path}/#{wiki}/text" ) - system( "cvs -d #{@root} update > /dev/null 2>&1" ) - ensure - Dir.chdir( oldpwd.untaint ) - end - end -end - -# Subversion Repository Backend -class ReposSvn < ReposDefault - def setup() - system( "svnadmin create #{@root} > /dev/null 2>&1" ) - end - def imported?( wiki ) - s = '' - open("|svn ls file://#{@root}/#{wiki}") do |f| - s << (f.gets( nil ) ? $_ : '') - end - - if %r|^trunk/$| =~ s then - return true - else - return false - end - end - def import( wiki ) - oldpwd = Dir.pwd - begin - Dir.chdir( "#{@data_path}/#{wiki}/text" ) - system( "svnadmin create #{@root}/#{wiki} > /dev/null 2>&1" ) - system( "svn import -m 'Starting #{wiki}' . file://#{@root}/#{wiki}/trunk > /dev/null 2>&1" ) - Dir.chdir( '..' ) - rmdir( 'text' ) - system( "svn checkout file://#{@root}/#{wiki}/trunk text > /dev/null 2>&1" ) - system( "svn propdel svn:mime-type -R text > /dev/null 2>&1" ) - ensure - Dir.chdir( oldpwd.untaint ) - end - end - def update( wiki ) - oldpwd = Dir.pwd - begin - Dir.chdir( "#{@data_path}/#{wiki}/text" ) - system( "svn update > /dev/null 2>&1" ) - ensure - Dir.chdir( oldpwd.untaint ) - end - end -end - -# Create repository backend -def create_repos(repos_type, repos_root, data_path) - case repos_type - when 'cvs' - return ReposCvs.new(repos_root, data_path) - when 'svn' - return ReposSvn.new(repos_root, data_path) - else - return ReposDefault.new(repos_root, data_path) - end -end def create_wiki( wiki, hiki, cgi_name, data_path ) Dir.mkdir( wiki.untaint ) @@ -328,10 +216,11 @@ #--- main ----------------------------------------------------------- require 'cgi' +require 'hiki/util' cgi = CGI::new msg = nil - @ repos = create_repos(repos_type, repos_root, data_path) + @ repos = Hiki::Util::create_repos(repos_type, repos_root, data_path) @repos.setup() diff --exclude=CVS -urN hiki.orig/misc/plugin/cvs.rb hiki/misc/plugin/cvs.rb --- hiki.orig/misc/plugin/cvs.rb Sat Jun 26 23:12:29 2004 +++ hiki/misc/plugin/cvs.rb Sun Dec 5 16:46:56 2004 @@ -1,37 +0,0 @@ -# $Id: cvs.rb,v 1.4 2004/06/26 14:12:29 fdiary Exp $ -# Copyright (C) 2003, Kazuhiko <kazuh****@fdiar*****> -# You can distribute this under GPL. - -#===== update_proc -add_update_proc { - cvs_commit if****@conf*****_root -} - -#===== delete_proc -add_delete_proc { - cvs_delete if****@conf*****_root -} - -#----- cvs commit on updating -def cvs_commit - oldpwd = Dir.pwd.untaint - begin - Dir.chdir( "#{@conf.data_path}/text" ) - system( "cvs -d #{@conf.repos_root} add -- #{@page.escape} > /dev/null 2>&1".untaint ) - system( "cvs -d #{@conf.repos_root} ci -m '#{ENV['REMOTE_ADDR']} - #{ENV['REMOTE_HOST']}' > /dev/null 2>&1".untaint ) - ensure - Dir.chdir( oldpwd ) - end -end - -#----- cvs delete on deleting -def cvs_delete - oldpwd = Dir.pwd.untaint - begin - Dir.chdir( "#{@conf.data_path}/text" ) - system( "cvs -d #{@conf.repos_root} remove -- #{@page.escape} > /dev/null 2>&1".untaint ) - system( "cvs -d #{@conf.repos_root} ci -m '#{ENV['REMOTE_ADDR']} - #{ENV['REMOTE_HOST']}' > /dev/null 2>&1".untaint ) - ensure - Dir.chdir( oldpwd ) - end -end diff --exclude=CVS -urN hiki.orig/misc/plugin/svn.rb hiki/misc/plugin/svn.rb --- hiki.orig/misc/plugin/svn.rb Tue Sep 7 00:08:44 2004 +++ hiki/misc/plugin/svn.rb Sun Dec 5 16:46:56 2004 @@ -1,39 +0,0 @@ -# $Id: svn.rb,v 1.6 2004/09/06 15:08:44 fdiary Exp $ -# Copyright (C) 2003, Koichiro Ohba <koich****@meado*****> -# Copyright (C) 2003, Yasuo Itabashi <yasuo_itabashi{@}hotmail.com> -# You can distribute this under GPL. - -#===== update_proc -add_update_proc { - svn_commit if****@conf*****_root -} - -#===== delete_proc -add_delete_proc { - svn_delete if****@conf*****_root -} - -#----- Subversion commit on updating -def svn_commit - oldpwd = Dir.pwd.untaint - begin - Dir.chdir( "#{@conf.data_path}/text" ) - system( "svn add -- #{@page.escape} > /dev/null 2>&1".untaint ) - system( "svn propdel svn:mime-type -- #{@page.escape} > /dev/null 2>&1".untaint ) - system( "svn ci -m '#{ENV['REMOTE_ADDR']} - #{ENV['REMOTE_HOST']}' > /dev/null 2>&1".untaint ) - ensure - Dir.chdir( oldpwd ) - end -end - -#----- Subversion delete on deleting -def svn_delete - oldpwd = Dir.pwd.untaint - begin - Dir.chdir( "#{@conf.data_path}/text" ) - system( "svn remove -- #{@page.escape} > /dev/null 2>&1".untaint ) - system( "svn ci -m '#{ENV['REMOTE_ADDR']} - #{ENV['REMOTE_HOST']}' > /dev/null 2>&1".untaint ) - ensure - Dir.chdir( oldpwd ) - end -end diff --exclude=CVS -urN hiki.orig/plugin/00default.rb hiki/plugin/00default.rb --- hiki.orig/plugin/00default.rb Tue Sep 28 22:47:28 2004 +++ hiki/plugin/00default.rb Sun Dec 5 22:01:17 2004 @@ -1,6 +1,8 @@ # $Id: 00default.rb,v 1.12 2004/09/28 13:47:28 fdiary Exp $ # Copyright (C) 2002-2003 TAKEUCHI Hitoshi <hitos****@namar*****> +require 'hiki/util' + #============================== # tDiary plugins for Hiki #============================== @@ -84,6 +86,7 @@ #===== update_proc add_update_proc { updating_mail if****@conf*****_on_update + Hiki::Util::create_repos(@conf.repos_type, @conf.repos_root, @conf.data_path).commit(@page) } #----- send a mail on updating @@ -169,3 +172,8 @@ data[:tools] = menu.collect! {|i| %Q!<span class="adminmenu">#{i}</span>! }.join(" \n").sanitize end + +#===== delete_proc +add_delete_proc { + Hiki::Util::create_repos(@conf.repos_type, @conf.repos_root, @conf.data_path).delete(@page) +}