顯示具有 svn 標籤的文章。 顯示所有文章
顯示具有 svn 標籤的文章。 顯示所有文章

2015年5月26日 星期二

Collabnet SVN Edge 心得

起源:
某日 svn 的網址忽然打不開 ( http://192.168.x.x:18080/svn/project/trunk/ )
登錄csvn後台 https://192.168.x.x:4434/csvn/ 後顯示錯誤:
Unable to browse repositories, as the Subversion server is not running.

然而csvn的管理帳號admin又找不到密碼登不進去

主機系統:
CentOS 6.6 

解法:
重置 Collabnet SVN 的admin密碼
1. 關閉Collabnet SVN服務。找到csvn的資料夾 ( 該主機csvn放在 /home/data/csvn ) 然後執行資料夾下的
# ./bin/csvn stop
2. 找csvn資料夾下的 csvn-production-hsqldb.script 檔案並編輯,該主機放在 ./data/csvn-production-hsqldb.script
3. 在csvn-production-hsqldb.script搜索admin,找到類似下面這一行
INSERT INTO USER VALUES(1,8,'9cdb7018925e8f3ada81b1e475f111b9',TRUE,'admin','admin@example.com','admin user','Super Administrator')
4. 將密碼欄位 '9cdb7018925e8f3ada81b1e475f111b9' 改成 '21232f297a57a5a743894a0e4a801fc3' 
5. 重啟csvn服務 ./bin/csvn start ,然後就可以用 帳號:admin,密碼:admin 登錄csvn後台
6. 用管理帳號admin登錄後台後點擊啟動svn服務,這樣svn 的網址 ( http://192.168.x.x:18080/svn/project/trunk/ ) 就能重新訪問了


註:
該台主機上沒裝svn,只裝csvn也能在主機上跑svn 的主機服務

參考資料:

2015年3月11日 星期三

將別的repository的commit合併到svn上

情境:
有兩個同專案的程式,因網路關係,其中一個有一陣子被放生到一個新的git上(因為git沒有網路也能用),現在要把git(程式較新)的commit移回svn上(git分出去後無提交新的commit)
假設svn倉庫在 http://svn.domain/trunk/project/
git倉庫在 ssh://bear@192.168.0.115:/srv/http/git_repo
本機端git svn 操作在git_svn_repo
本機端保留的git本地倉庫在 /your/git_repo/path

解法:
You'll need to add the other repository as a remote, then fetch its changes. From there you see the commit and you can cherry-pick it.

使用git svn 拉回svn的程式
$ git svn clone http://svn.domain/trunk/project/ git_svn_repo
$ cd git_svn_repo
新增git的遠端倉庫,並命名git_repo
$ git remote add git_repo file:///your/git_repo/path #我在本地端另外目錄有pull git倉庫的東西,先用他的commit 去merge
抓回git_repo
$ git fetch git_repo
檢查branch
$ git branch -a
查git倉庫的log
$ git log remotes/git_repo/master
將git倉庫你要合併的commit cherry-pick到master(他將會推上svn)
$ git cherry-pick commit_id
上傳到svn
$ git svn dcommit


將git_svn_repo的更新推到git
$ git remote rm git_repo #因為前面用本機保留的git本地倉庫,所以刪掉重新把remote設成git遠端倉庫
$ git remote add origin ssh://bear@192.168.0.115:/srv/http/git_repo
直接推送會報錯:
$ git push origin master
lman@192.168.0.115's password:
To ssh://bear@192.168.0.115:/srv/http/git_repo
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'ssh://bear@192.168.0.115:/srv/http/git_repo'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解法:
$ git pull origin master # 這會多一個merge的commit
(編輯修改後)
$ git push origin master
即可將git_svn_repo的修改內容推送到遠端git倉庫

不過上面那段是錯的,原因:這樣git_svn_repo的修改就送不到svn上了



參考資料:
http://stackoverflow.com/questions/5120038/is-it-possible-to-cherry-pick-a-commit-from-another-git-repository

2015年1月16日 星期五

git 搬移log到svn下

需求:
git下的所有log資料搬到svn 某新資料夾下(svn其他資料夾已有log)

過程照參考資料做基本上就可以了...
1. Create Subversion-aware Git clone ( 首先你要安裝git-sv )
 $ git svn clone http://svn.localhost/svn/trunk/company
 $ cd trunk
 $ git fetch git@your.domain:username/project.git # 我是抓我gitlab上的git

 $ git branch tmp $(cut -b-40 .git/FETCH_HEAD)
$(cut -b-40 .git/FETCH_HEAD) 是原本gitlab最後一個commit id
 $ git tag -a -m "Last fetch" last tmp

2. Apply initial commit
 $ INIT_COMMIT=$(git log tmp --pretty=format:%H | tail -1)
$(git log tmp --pretty=format:%H | tail -1)是tmp branch log的最後一個 commit id
 $ git checkout $INIT_COMMIT .  #注意最後要有點(.)
 $ git commit -C $INIT_COMMIT

3. Rebase and submit
 $ git rebase master tmp
 $ git branch -M tmp master

 $ git svn dcommit

4. Update Google Code
 ...
 $ mv .git/refs/tags/newlast .git/refs/tags/last #這步我沒做



參考資料:
http://code.google.com/p/support/wiki/ImportingFromGit

2015年1月9日 星期五

php的svn_log()速度過慢

目標:
在網頁上顯示svn log

使用php的svn_log()函數
php官方文件的svn_log():
array svn_log ( string $repos_url [, int $start_revision [, int $end_revision [, int $limit= 0 [, int $flags = SVN_DISCOVER_CHANGED_PATHS | SVN_STOP_ON_COPY ]]]] )
$start_revision 、$end_revision和$limit  設0 代表無限制
$flags:
SVN_DISCOVER_CHANGED_PATHS - 顯示所有值(預設)
SVN_OMIT_MESSAGES - 不顯示msg和paths
SVN_STOP_ON_COPY - 不顯示paths

例:
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_USERNAME, 'username');
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_PASSWORD, 'password');
$log = svn_log('http://svn.xxx/repos/trunk', 1, 2, 1);
OUTPUT:
$log:Array
(
    [0] => Array
        (
            [rev] => 1
            [author] => root
            [msg] => init
            [date] => 2014-10-29T04:13:15.632694Z
            [paths] => Array
                (
                    [0] => Array
                        (
                            [action] => A
                            [path] => /branches
                        )
                )
        )
)


因為某些commit提交檔案太多,即使調整$flags,svn_log()速度依然很慢
只好使用原生的svn log來在網頁上顯示紀錄
PHP:
exec("export LANG=zh_TW.UTF8; svn log file:///path/to/svndb/repos/ ", $svn_log);
注意:
export LANG=zh_TW.UTF8; 很重要,否則出來會是亂碼,我用system("export")看不到LANG的設定,而且export LANG=zh_TW.UTF8;和 svn log 必須寫在同一個exec()中

然後細節在處理$svn_log陣列即可顯示到網頁上

參考資料:
http://php.net/manual/en/function.svn-log.php

2015年1月7日 星期三

svn merge log的測試shell script

承前篇 Arch啟用http存取svn和svn搬移log到另一個repos下
後來寫了個建構環境和merge log的script:
建立該篇文章的環境:
用法:
$ bash create_env.sh /home/svn/ #在 /home/svn/ 下建立test和test2的repos,並建立那篇文章的資料結構
#!/bin/bash
# $ bash create_env.sh 
# eg. # bash create_env.sh /home/svn/

SVN_REPOS_PATH=$1
cd ${SVN_REPOS_PATH}
svnadmin create test
svnadmin create test2

# cook test repos
svn checkout file://${SVN_REPOS_PATH}test test_checkout
cd test_checkout
mkdir trunk tags branches
touch trunk/hello.txt
svn add *
svn commit -m "hello test"
svn update
svn log
cd ..

# cook test2 repos
svn checkout file://${SVN_REPOS_PATH}test2 test2_checkout
cd test2_checkout
mkdir trunk tags branches
mkdir -p trunk/media/cloud/web/internet
svn add *
svn commit -m "hello test2"
svn update
svn log
cd ..

rm -rf test_checkout test2_checkout
ls -l

merge log:
用法:
# bash test_merge_log.sh /home/svn/test /home/svn/test2 trunk/media/cloud/web/internet # 把test的log搬到test2的 trunk/media/cloud/web/internet 資料夾下
#!/bin/bash
# merge REPOS1 log to REPOS2 folder
# run it as root
# # bash test_merge_log.sh   
# eg. # bash test_merge_log.sh /home/svn/test /home/svn/test2 trunk/media/cloud/web/internet

SVN_REPOS1_PATH=$1
SVN_REPOS2_PATH=$2
REPOS2_FOLDER=$3

svnadmin dump ${SVN_REPOS1_PATH} > test.dump
cat test.dump | svndumpfilter include "trunk" --drop-empty-revs --renumber-revs > test_trunk.dump
find . -name test_trunk.dump | xargs perl -ne 'open NEW, ">>test_trunk_new.dump"; s/trunk\///g; print NEW'
svnadmin load ${SVN_REPOS2_PATH} --parent-dir ${REPOS2_FOLDER} --ignore-uuid < test_trunk_new.dump
rm *.dump

# check
svn checkout file://${SVN_REPOS2_PATH} test2_checkout
cd test2_checkout
svn log
cd ..
rm -rf test2_checkout

原始碼:
https://github.com/kalecgos0616/tools/tree/master/svn

注意:
要在該tty 也cd到資料夾要用 . xxx.sh (要多一個點),否則用bash xxx.sh不會換目錄,但仍然會在該目錄執行指令

The reason is that each process has its own current directory, and when you execute a program from the shell it is run in a new process. The standard "cd", "pushd" and "popd" are builtin to the shell interpreter so that they affect the shell process.
翻譯:
每個process 擁有自己的資料夾,當你執行一個shell程式時等於開了一個新的process。"cd"那些指令被內置到shell解釋器,仍影響shell進程。使用source(.) 會讓該process在現在的tty環境執行

參考資料:
http://stackoverflow.com/questions/874452/change-current-directory-from-a-script

2015年1月6日 星期二

Arch啟用http存取svn和svn搬移log到另一個repos下

系統:
Arch Linux
svn, version 1.8.9+
Apache/2.4.10

目標:
1. 使用http存取svn
2. 搬移一個svn repos的log到另一個repos下

使用http存取svn
我們假設現有的svn倉庫放在這裡:
/home/svn/test
1. 編輯 /etc/httpd/conf/httpd.conf,要注意順序
LoadModule dav_module           modules/mod_dav.so # 取消註解
LoadModule dav_fs_module        modules/mod_dav_fs.so # 取消註解
LoadModule dav_svn_module       modules/mod_dav_svn.so # 新增這行
LoadModule authz_svn_module     modules/mod_authz_svn.so # 新增這行

使用SSL? => 不使用,所以後面設定檔設在vhost中

新建 /home/svn/.svn-policy-file:
[/]
* = r
[REPO_NAME:/]
USER_NAME = rw

新建 /home/svn/.svn-auth-file:
# htpasswd -cs /home/svn/.svn-auth-file USER_NAME #沒有檔案時,新建第一個user
# htpasswd -s /home/svn/.svn-auth-file OTHER_USER_NAME  #在檔案後面新建其他的user

設定權限:
# chown -R http:http /home/svn/test
編輯 /etc/httpd/conf/sites-available/svn.localhost.conf:
(注意:這邊我是設在vhost設定檔中、網路上有些設在httpd-ssl.conf、mod_dav_svn.conf中)
<VirtualHost *:80>
    DocumentRoot "/srv/http/" 必須設不同於svn repos的位置否則出現錯誤:{
Redirecting to URL 'http://svn.localhost/test':
Redirecting to URL 'http://svn.localhost/test':

svn: E195019: Redirect cycle detected for URL 'http://svn.localhost/test'
}
    ServerName svn.localhost
    ServerAdmin you@example.com
    ErrorLog "/var/log/httpd/svn.localhost-error_log"
    TransferLog "/var/log/httpd/svn.localhost-access_log"

    <Directory />
        Options +Indexes +FollowSymLinks +ExecCGI
        AllowOverride All
        Order deny,allow
        Allow from all
    Require all granted
    </Directory>
    <Location /test/>
       DAV svn
       #SVNParentPath /home/svn/test
       SVNPath /home/svn/test
       AuthzSVNAccessFile /home/svn/.svn-policy-file
       AuthName "SVN Repositories"
       AuthType Basic
       AuthUserFile /home/svn/.svn-auth-file
       Require valid-user
    </Location>
    # 如果有其他repos就複製上面粗體那段加在這後面
</VirtualHost>

設定完就加入網址
# a2ensite svn.localhost
重啟apache
# systemctl restart httpd
沒設dns的話記得要設hosts

接著打開瀏覽器 http://svn.localhost/test/ 就能看到svn了,便可使用svn checkout http://svn.localhost/test/trunk/ 來下載svn
注意:
必須使用svn checkout http://svn.localhost/test/trunk/ ,否則會出現錯誤:
$ svn checkout http://svn.localhost/test/
svn: E175002: Unexpected HTTP status 405 'Method Not Allowed' on '/test'

svn: E175002: Additional errors:
svn: E175002: PROPFIND request on '/test' failed: 405 Method Not Allowed

若想要checkout出 svn.localhost/test/ 下所有東西(含branches、tags、trunk),則必須打開SSL用https去存取
打開SSL:
# cd /etc/httpd/conf/
# openssl req -new -x509 -keyout server.key -out server.crt -days 365 -nodes
(會在這資料夾下產生server.key和server.crt)
編輯 /etc/httpd/conf/extra/httpd-ssl.conf:
#SSLSessionCache        "shmcb:/run/httpd/ssl_scache(512000)" #註解掉,我不確定為什麼在這邊要註解掉,因為用 journalctl -xn 查到這行有syntax error,所以註解掉
#SSLSessionCacheTimeout  300 #註解掉
在</VirtualHost> 前加入:
<Location /svn>
   DAV svn
   SVNParentPath /home/svn/
   AuthzSVNAccessFile /home/svn/.svn-policy-file
   AuthName "SVN Repositories"
   AuthType Basic
   AuthUserFile /home/svn/.svn-auth-file
   Require valid-user
</Location>
編輯 /etc/httpd/conf/httpd.conf :
LoadModule ssl_module modules/mod_ssl.so #取消註解
Include conf/extra/httpd-ssl.conf #取消註解

重啟apache後就能用 svn checkout https://svn.localhost/svn/test (因為沒吃到vhost,所以 https://localhost/svn/test 也可以)下載含有trunk、branches、tags的svn資料了,只是他第一次連會要你接受他的憑證

如果要移除svn 在console的某個網址(http)的登錄設定:
$ rm ~/.subversion/auth/svn.simple/xxx #用grep查網址再刪除他

搬移一個svn repos的log到另一個repos下
有兩個repos
svn checkout file:///home/svn/test:
test
├── branches
├── tags
└── trunk
    └── hello.txt
svn checkout file:///home/svn/test2:
test2
├── branches
├── tags
└── trunk
    └── media
        └── cloud
            └── web
                └── internet
欲把test的trunck下的code(hello.txt)搬到test2的trunk/media/cloud/web/internet 下面:
$ sudo svnadmin dump /home/svn/test > test.dump 
$ cat test.dump | svndumpfilter include "trunk" --drop-empty-revs --renumber-revs > test_trunk.dump #只留trunk的資料
用vim移除trunk的資料夾,若沒做這步,之後檔案會移到 trunk/media/cloud/web/internet/trunk 下面
$ vim test_trunk.dump
:1,$s/trunk\///g
或用bash+perl 取代,注意:find到的檔案(test_trunk.dump)和>>儲存的檔案(test_trunk_new.dump)不能為同一隻檔案,否則會造成無窮迴圈,必須另外儲存
$ find . -name test_trunk.dump | xargs perl -ne 'open NEW, ">>test_trunk_new.dump"; s/trunk\///g; print NEW'
將dump檔load進test2 repos
$ sudo svnadmin load /home/svn/test2 --parent-dir trunk/media/cloud/web/internet --ignore-uuid < test_trunk_new.dump
檢查:
$ svn checkout file:///home/svn/test2/ test2_new
$ tree test2_new
test2_new
├── branches
├── tags
└── trunk
    └── media
        └── cloud
            └── web
                └── internet
                    ├── hello.txt
                    └── trunk
查log有沒有搬移進去:
$ cd test2_new/ ; svn log
------------------------------------------------------------------------
r2 | bear | 2015-01-06 16:33:57 +0800 (二, 06  1月 2015) | 1 line

hello test
------------------------------------------------------------------------
r1 | bear | 2015-01-07 09:29:23 +0800 (三, 07  1月 2015) | 1 line

hello test2
------------------------------------------------------------------------
最後再手動移除 trunk/media/cloud/web/internet 下的trunk資料夾,即成功

參考資料:
https://wiki.archlinux.org/index.php/Subversion_Setup
https://blog.tinned-software.net/merge-two-svn-repositories/ # Merge two SVN repositories
http://stackoverflow.com/questions/1948318/how-to-configure-svn-web-access-for-different-write-permissions # 注意:用SVNPath時要用 <Location /svn/> 而非 <Location /svn>
http://stackoverflow.com/questions/11240581/subversion-405-method-not-allowed # 405 Method Not Allowed 可改用https解決
http://stackoverflow.com/questions/10524646/tortoise-svn-giving-me-redirect-cycle-detected-for-url-domain-svn # DocumentRoot 和 SVNPath 不能設一樣
http://stackoverflow.com/questions/9234852/how-to-replace-strings-in-a-file-and-save-it-as-another-file # bash+perl 取代檔案並另存新檔