Mac Pro 利用PHP匯出SVN新增或修改過的檔案例子

來源:互聯網
上載者:User

先前在 Windows 作業系統下,習慣用 TortoiseSVN 匯出新增或修改過的檔案(【相當實用】如何讓TortoiseSVN匯出新增或修改過的檔案  ),最近換成了 Mac Pro 膝上型電腦,一時沒找到類似 TortoiseSVN 好用的用戶端工具。好吧,利用PHP寫個匯出小工具:

1、工具檔案名稱:svn.php,其內容如下:

 

 代碼如下 複製代碼

 <?php
/**
 * 匯出指定版本之間的差異檔案,如 100 和 200 之間的差異則匯出 100(不包括) - 200(包括) 的所有修改
 * 【SVN命令列】
 * 1、查看版本間差異
 * svn diff -r 2359:2360 --summarize --username simon --password simonfit svn://112.73.80.56/SJF/source
 * 2、匯出某個版本檔案到本地
 * svn export -r 2360 svn://112.73.80.56/SJF/source/common/controller/WechatBaseController.class.php /root/2/files_2359_2360/common/controller/WechatBaseController.class.php --username wenjianbao --password wjb888
 *
 * */

// 根目錄
define('SITE_PATH', dirname(__FILE__));


// SVN 帳號資訊
$svn_url = 'svn://112.73.80.56/SJF/source';
$svn_username = 'wenjianbao';
$svn_password = '5c95e61387c478c85ccf45e6a8ae6de3';

 


$error_msg = 'You must useage like ' . $_SERVER['argv'][0] . ' old_version(不包括) new_version(包括)';
if ($_SERVER['argc'] != 3)
{
 echo $error_msg;
 exit(1);
}

if ($_SERVER['argv'][1] > $_SERVER['argv'][2])
{
 echo $error_msg;
 exit(1);
}

$old_version = $_SERVER['argv'][1];
$new_version = $_SERVER['argv'][2];

$work_path = SITE_PATH . "/file_${old_version}_${new_version}";

echo "開始分析版本差異...\n";
$diff_cmd = "svn diff -r ${old_version}:${new_version} --summarize --username ${svn_username} --password ${svn_password} ${svn_url}";
exec($diff_cmd, $diff_list, $return);
$diff_list = (array)$diff_list;
foreach ($diff_list as $diff_info)
{
 echo $diff_info . "\n";
}

# 清空舊資料
@system('rm -rf ' . SITE_PATH . '/file_*');
@system('rm -rf ' . SITE_PATH . '/diff_*');

# 建立檔案夾
dir_mkdir($work_path);

$diff_count = count($diff_list);
if ($diff_count < 1)
{
 echo "版本間沒有差異";
 exit(1);
}

$diff_count = 0;
$diff_file_path = SITE_PATH . "/diff_${old_version}_${new_version}.txt";

# 匯出版本檔案
echo "開始匯出...\n";
foreach ($diff_list as $diff_info)
{
 if (preg_match('/([\w]+)\s+(svn:.+)/', $diff_info, $matches))
 {
  $svn_file_mode = $matches[1];
  $svn_file_name = $matches[2];

  // A、M、D、AM即增加且修改
  // 檔案被刪除
  if ($svn_file_mode == 'D')
  {
   continue;
  }
  $diff_count++;

  // 寫日誌
  file_write($diff_file_path, $matches[0] . "\n", 'a');

  // 下載到本地
  $local_file_path = $work_path . str_replace($svn_url, '', $svn_file_name);
  $local_file_dir = dirname($local_file_path);
  dir_mkdir($local_file_dir);

  $export_cmd = "svn export -r ${new_version} ${svn_file_name} ${local_file_path} --username ${svn_username} --password ${svn_password}";
  system($export_cmd);
 }
}

echo "共匯出${diff_count}個差異檔案";
exit(0);

 

 

 


/**
 * 建立檔案夾
 *
 * @param string $path      檔案夾路徑
 * @param int    $mode      存取權限
 * @param bool   $recursive 是否遞迴建立
 * @return bool
 */
function dir_mkdir($path = '', $mode = 0777, $recursive = true)
{
 clearstatcache();
 if (!is_dir($path))
 {
  mkdir($path, $mode, $recursive);
  return chmod($path, $mode);
 }

 return true;
}

/**
 * 寫檔案
 *
 * @param string $filename 檔案名稱
 * @param string $text     要寫入的文本字串
 * @param string $openmod  文本寫入模式('w':覆蓋重寫,'a':文本追加)
 * @return bool
 */
function file_write($filename = '', $text = '', $openmod = 'w')
{
 if (@$fp = fopen($filename, $openmod))
 {
  flock($fp, 2);
  fwrite($fp, $text);
  fclose($fp);
  return true;
 }
 else
 {
  return false;
 }
}

2、使用方法

在 svn.php 檔案中修改下自己的 SVN伺服器的地址和帳號,以命令列模式運行下即可,如 匯出版本號碼 100 到 200 之間的差異檔案,


php svn.php 100 200

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.