TortoiseSVN ログメッセージを後から編集する方法(リビジョン属性編集の許可)

TortoiseSVN を使っているとよくやってしまう

  • コミット時のログメッセージの付け忘れ
  • 付けるログメッセージをミスった

リポジトリを作成した時点での初期設定ではメッセージを後から編集することはできない。
正確には、「ログメッセージを編集」という項目はあるが編集しようとしてもできないようになっている。

TortoiseSVN-edit-message-error

このように「リポジトリが、リビジョン属性を変更できるようになっていません。管理者に post-revprop-change フックを作成するように頼んでください」というエラーがでる。

これは個人的には正しい動作だと思う。複数人が携わっている場合など、後からのメッセージの変更ができてしまうと問題が起きやすいからだ。

しかし、どうしても直したいと思う場合もある。(恥ずかしい誤記をしてしまった時とか)

というわけで、どうしてもログメッセージを後から編集する必要性が発生してしまった場合のお話。

環境

Windows10 pro 64bit
TortoiseSVN 1.14.0, Build 28885 - 64 Bit

前提条件

  • リポジトリの管理者であること。(リポジトリへのアクセス、編集権限を持っている事)
  • 他人も関わっていて、メッセージの編集を行うことで迷惑がかからないこと

フックのテンプレート hooks/pre-revprop-change.tmpl

リポジトリを作成した時点で、リポジトリ内にフック用のテンプレートファイルが用意されている。
リビジョン属性を変更しようとした際に5つの引数を伴って呼ばれるシェルスクリプト(bash)になっている。
通常は1を返すが、引数から色々判定を行って許可する場合だけ0を返せばいいようだ。

#!/bin/sh

# PRE-REVPROP-CHANGE HOOK
#
# The pre-revprop-change hook is invoked before a revision property
# is added, modified or deleted.  Subversion runs this hook by invoking
# a program (script, executable, binary, etc.) named 'pre-revprop-change'
# (for which this file is a template), with the following ordered
# arguments:
#
#   [1] REPOS-PATH   (the path to this repository)
#   [2] REV          (the revision being tweaked)
#   [3] USER         (the username of the person tweaking the property)
#   [4] PROPNAME     (the property being set on the revision)
#   [5] ACTION       (the property is being 'A'dded, 'M'odified, or 'D'eleted)
#
#   [STDIN] PROPVAL  ** the new property value is passed via STDIN.
#
# If the hook program exits with success, the propchange happens; but
# if it exits with failure (non-zero), the propchange doesn't happen.
# The hook program can use the 'svnlook' utility to examine the 
# existing value of the revision property.
#
# WARNING: unlike other hooks, this hook MUST exist for revision
# properties to be changed.  If the hook does not exist, Subversion 
# will behave as if the hook were present, but failed.  The reason
# for this is that revision properties are UNVERSIONED, meaning that
# a successful propchange is destructive;  the old value is gone
# forever.  We recommend the hook back up the old value somewhere.
#
# The default working directory for the invocation is undefined, so
# the program should set one explicitly if it cares.
#
# On a Unix system, the normal procedure is to have 'pre-revprop-change'
# invoke other programs to do the real work, though it may do the
# work itself too.
#
# Note that 'pre-revprop-change' must be executable by the user(s) who will
# invoke it (typically the user httpd runs as), and that user must
# have filesystem-level permission to access the repository.
#
# On a Windows system, you should name the hook program
# 'pre-revprop-change.bat' or 'pre-revprop-change.exe',
# but the basic idea is the same.
#
# The hook program runs in an empty environment, unless the server is
# explicitly configured otherwise.  For example, a common problem is for
# the PATH environment variable to not be set to its usual value, so
# that subprograms fail to launch unless invoked via absolute path.
# If you're having unexpected problems with a hook program, the
# culprit may be unusual (or missing) environment variables.
#
# CAUTION:
# For security reasons, you MUST always properly quote arguments when
# you use them, as those arguments could contain whitespace or other
# problematic characters. Additionally, you should delimit the list
# of options with "--" before passing the arguments, so malicious
# clients cannot bootleg unexpected options to the commands your
# script aims to execute.
# For similar reasons, you should also add a trailing @ to URLs which
# are passed to SVN commands accepting URLs with peg revisions.
#
# Here is an example hook script, for a Unix /bin/sh interpreter.
# For more examples and pre-written hooks, see those in
# the Subversion repository at
# http://svn.apache.org/repos/asf/subversion/trunk/tools/hook-scripts/ and
# http://svn.apache.org/repos/asf/subversion/trunk/contrib/hook-scripts/


REPOS="$1"
REV="$2"
USER="$3"
PROPNAME="$4"
ACTION="$5"

if [ "$ACTION" = "M" -a "$PROPNAME" = "svn:log" ]; then exit 0; fi

echo "Changing revision properties other than svn:log is prohibited" >&2
exit 1

フックの作成

テンプレートは bsah で書かれているがリポジトリの環境が Windows なのでバッチでかけばいいようだ。
テンプレート「pre-revprop-change.tmpl」と同じ場所に「pre-revprop-change.bat」を作成しその中に処理を書き込む。

set REPOS=%1
set REV=%2
set USER=%3
set PROPNAME=%4
set ACTION=%5

REM ログに対する変更(Modified)だけ許可する
if "%PROPNAME%"=="svn:log" (
    if "%ACTION%"=="M" (
        exit 0
    )
)

REM 許可できない場合にどんな引数が渡されていたかをチェックできるように引数をログ出力
echo ********* args ******** >&2
echo リポジトリ : %REPOS%    >&2
echo リビジョン : %REV%      >&2
echo ユーザー   : %USER%     >&2
echo プロパティ : %PROPNAME% >&2
echo アクション : %ACTION%   >&2
echo *********************** >&2

echo Changing revision properties other than svn:log is prohibited >&2
exit 1

これでログメッセージを変更可能になる。

上記のバッチでは、許可できない場合に引数の内容なども出力するようにしてあるので他の属性を編集したくなった場合の参考になればと思う。

実際に「作者を編集」をしてみると下記のようになり、引数の内容が出力されていることが分かる。

TortoiseSVN-edit-author-error
  • 「D:\Test_TortoiseSVN_repo」のリポジトリの
  • 「リビジョン2」に対して
  • 「pal」というユーザーが
  • 「作者」属性(svn:author)を
  • 「M」Modified しようとして
  • 許可されなかった

というのが良くわかる。

フックの削除

必要な修正が終わり必要なくなったらフックを削除しておく事。 「pre-revprop-change.bat」 を削除すればいい。

応用

引数にユーザーの情報もあるため、バッチの判定部分をいじって特定のユーザーのみ編集を許可することなどもできる。

if "%PROPNAME%"=="svn:log" (
    if "%ACTION%"=="M" (
        if "%USER%"=="ユーザー名" (
            exit 0
        )
    )
)

必要性はあまりないと思うけど、リビジョンも引数にあるのでリビジョン100以下は編集禁止とかもできる。