Subversionプロパティ情報の利用
2015 年 3 月 13 日 by yasukuniSVNのリポジトリにはプロパティという付随情報を持たせることができます。
このプロパティは課題管理システムとの連携やSVNクライアントの振舞いを制御すること等に利用されています。
今回、そのプロパティに独自の属性を追加することで管理に役立てることが出来ないか試してみます。
1.リポジトリの作成
[subversion@xxx ~]$ cd /tmp/ [subversion@xxx tmp]$ mkdir svn [subversion@xxx tmp]$ svnadmin create /tmp/svn/freeStyle
2.チェックアウト & 独自プロパティの設定
1で作成したリポジトリをチェックアウトし、svn propsetコマンドを使用して
プロパティ(tag:repositoryNameJp)を設定してみます。
#設定後はコミット操作が必要となります。
svn propset PROPNAME [PROPVAL | -F VALFILE] PATH...
[subversion@xxx tmp]$ svn checkout file:///tmp/svn/freeStyle /tmp/wc_freeStyle
Checked out revision 0.
[subversion@xxx tmp]$ cd /tmp/wc_freeStyle
[subversion@xxx wc_freeStyle]$ svn propset tag:repositoryNameJp "ふり~すたいる" .
property 'tag:repositoryNameJp' set on '.'
[subversion@xxx wc_freeStyle]$ svn commit --message "tag:repositoryNameJpを設定"
Sending . Committed revision 1.
[subversion@xxx wc_freeStyle]$
3.プロパティ取得の確認
svnlookコマンド又は、svnコマンドのサブコマンドのpropgetを使用して
先ほど設定したプロパティが正しく取得できるか確認してみます。
svnlook propget REPOS_PATH PROPNAME [PATH_IN_REPOS]
[subversion@xxx ~]$ svnlook propget /tmp/svn/freeStyle/ tag:repositoryNameJp / ふり~すたいる[subversion@xxx ~]$
svn propget PROPNAME [TARGET[@REV]...]
[subversion@xxx ~]$ cd /tmp/wc_freeStyle/ [subversion@xxx wc_freeStyle]$ svn propget tag:repositoryNameJp ふり~すたいる
4.リポジトリ一覧の作成
この様にして、独自のプロパティの設定/取得が行えることが確認できたので
リポジトリの一覧情報を生成するスクリプトを作成してみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | <?php /********************************************************************** configurations **********************************************************************/ $SVN_DIR = '/var/svn' ; date_default_timezone_set( 'Asia/Tokyo' ); /**********************************************************************/ //リポジトリのリストを取得 $cmd = "find $SVN_DIR -type f | egrep '^$SVN_DIR/[^/]+/format' | sed -e 's|/format||g' -e 's|$SVN_DIR/||g'" ; exec ( $cmd , $repositoryNames ); $repositoryList = array (); foreach ( $repositoryNames as $repositoryName ){ $repository = array (); $repository [ 'name' ] = $repositoryName ; //リポジトリ(日本語)名称の取得 $cmdRepositoryNameJp = "svnlook propget $SVN_DIR/$repositoryName/ tag:repositoryNameJp / 2> /dev/null" ; unset( $repositoryNameJp ); exec ( $cmdRepositoryNameJp , $repositoryNameJp , $ret ); $repository [ 'nameJp' ] = $ret ==0 ? $repositoryNameJp [0] : "" ; //リポジトリ情報(最終更新者,リビジョン,日時)の取得 $cmdRepositoryInfo = "svn info file://$SVN_DIR/$repositoryName 2> /dev/null" ; unset( $repositoryInfo ); exec ( $cmdRepositoryInfo , $repositoryInfo , $ret ); $repository [ 'author' ] = "" ; $repository [ 'date' ] = "" ; $repository [ 'revision' ] = "" ; if ( $ret == 0){ foreach ( $repositoryInfo as $repositoryInfoLine ){ if (preg_match ( "/^Last Changed Author: (.*)$/" , $repositoryInfoLine , $regs )) { $repository [ 'author' ] = $regs [1]; } if (preg_match ( "/^Last Changed Rev: (.*)$/" , $repositoryInfoLine , $regs )) { $repository [ 'revision' ] = $regs [1]; } if (preg_match ( "/^Last Changed Date: (\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}).*$/" , $repositoryInfoLine , $regs )) { $repository [ 'date' ] = $regs [1]; } } } //最終コミットログ取得 $repository [ 'log' ] = "" ; if ( $repository [ 'revision' ] != "" ){ $rev = $repository [ 'revision' ]; $cmdRepositoryLog = "svn log -r $rev file://$SVN_DIR/$repositoryName 2> /dev/null | tail -n+4 | head -n-1" ; unset( $repositoryLog ); exec ( $cmdRepositoryLog , $repositoryLog , $ret ); if ( $ret == 0 && count ( $repositoryLog ) > 0){ $repository [ 'log' ] = $repositoryLog [0]; //一行目のみ取得 } } array_push ( $repositoryList , $repository ); } var_dump( $repositoryList ); //$smarty->assign("repositoryList", $repositoryList); //$smarty->display('index.tpl'); ?> |
※”Last Changed Author”などの文字列で引っ掛けているところは
環境によって日本語で結果が表示されることがありました。
※11行目のコマンドはリポジトリの抽出です。formatファイルが存在するディレクトリをリポジトリとみなしています。(この判定方法の正誤は不明です。)
5.最後に
今回、独自のプロパティを追加することで管理情報を新たに持たせることが出来るようになりました。
更にコミットフックと合わせると、凍結済のプロパティ属性を持つ場合はコミットさせない等リポジトリの操作を制御することもできるのでは無いかと思います。
タグ: Linux, PHP, Subversion, ツール, 開発環境