您的位置:首页 > 其它

修改 svn 中每次编辑提交的作者 author 名字

2016-05-25 00:00 387 查看
<?php

/**
* 得到 author:
*   svn propget --revprop svn:author -r $r $url --username {$username} --password {$password}";
* 修改 author:
*   svn propset --revprop svn:author -r $r NEWVAL $url
*/

echo "Hello World!\n";

# 定义常量.
define('SVN_ROOT', 'svn://svn.example.com/yourproject');
define('OPER_USER', 'adminuser');
define('OPER_PASSWORD', 'adminpassword');
define('MAX_REV', 12345);

# 第一步: 得到所有作者, 并写入文件 svn_authors.php, 该文件第二步使用.
if (false) {
$all_authors = fetch_all_authors();
var_dump($all_authors);
write_author_file($all_authors);
}

# 第二步: 修改 author, 根据第一步生成的映射文件, 注意: 该文件需要手工修改 old->new 名字映射.
if (true) {
update_svn_authors();
}

exit();

# 修改所有 author.
function update_svn_authors() {
include 'svn_authors.php';
#var_dump($author_change_map);
#exit();

for ($r = 1; $r < MAX_REV; ++$r) {
$old_author = robust_get_author(SVN_ROOT, $r);
if ($old_author === '') continue;

$new_author = $author_change_map[$old_author];
if ($old_author === $new_author) continue;   // 未改变.
if (!$new_author) continue;  // 未映射该用户, 则不要改变了.

robust_change_author(SVN_ROOT, $r, $new_author);
echo "Update rev $r author $old_author to $new_author\n";
}
}

function robust_change_author($svn_url, $r, $new_author) {
for ($times = 0; $times < 50; ++$times) {
if (change_author($svn_url, $r, $new_author))
return;
sleep($times*1);
}
}

# 修改指定 url 指定 rev 的 author.
function change_author($url, $r, $new_author) {
$username = OPER_USER;
$password = OPER_PASSWORD;

$cmd = "svn propset --revprop svn:author -r $r $new_author $url --username {$username} --password {$password}";
$output = null;
$return_var = null;
exec($cmd, $output, $return_var);

if ($return_var === 0) return true;

return false;
}

# 得到指定 $svn_url 的指定版本 $r 的编辑者.
function get_author($svn_url, $r) {
$username = OPER_USER;
$password = OPER_PASSWORD;

$cmd = "svn propget --revprop svn:author -r $r {$svn_url} --username {$username} --password {$password}";
#echo "The command is: $cmd \n";

$output = null;
$return_var = null;
exec($cmd, $output, $return_var);

#var_dump($return_var);
if ($return_var !== 0) {
# 发生错误.
return '';
}

#var_dump($output);
return $output[0];
}

# 重复多次尝试获取 author.
function robust_get_author($svn_url, $r) {
# 最多尝试 50 次.
for ($times = 0; $times < 50; ++$times) {
$author = get_author($svn_url, $r);
if ($author !== '')
return $author;
# 如果失败则休息一会再次试验.
sleep(1 * $times);
}
return 'error';
}

# 从 svn 库得到所有作者映射数组.
function fetch_all_authors() {
$all_authors = [];
for ($r = 1; $r < MAX_REV; ++$r) {
$author = robust_get_author(SVN_ROOT, $r);
if ($author) {
echo "svn:author at rev $r is: $author\n";
$all_authors[$author] = $author;
}
usleep(50*1000);   # 暂停 50 ms

write_author_file($all_authors);
}
return $all_authors;
}

# 输出所有作者到一个 php 文件中.
function write_author_file($authors) {
$file_name = 'svn_authors.php';
$fp = fopen($file_name, 'wt', false);
fwrite($fp, "<?php\n");
fwrite($fp, "\$authors = [\n");
foreach ($authors as $a) {
fwrite($fp, "    '$a' => '$a',\n");
}
fwrite($fp, "  ];\n");
fwrite($fp, "\n");
fclose($fp);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: