[SCM]源码管理 - Perforce命令行
一 P4(perforce)环境和sample project
p4 环境:
sample project:
二 P4命令行
1)p4env.bat
set path=C:\Program Files\Perforce;%path%
set P4Port=localhost:1666
set p4user=AAA
set p4passwd=aaa
set p4client=TestProject_AAA
set p4Root=//depot/TestProject
set p4LocalRoot=c:\MyLocalTestProject
set p4Drive=H:
if exist %p4Drive% (subst /d %p4Drive%)
subst %p4Drive% %p4LocalRoot%
set P4Port=localhost:1666
set p4user=AAA
set p4passwd=aaa
set p4client=TestProject_AAA
set p4Root=//depot/TestProject
set p4LocalRoot=c:\MyLocalTestProject
set p4Drive=H:
if exist %p4Drive% (subst /d %p4Drive%)
subst %p4Drive% %p4LocalRoot%
在所有命令执行前设置p4port,p4user,p4password,p4client;
一般需要subst虚拟盘来避免绝对路径,保证开发环境在所有的developers上可用。
2)p4login+logout.bat
call p4env.bat
echo %p4passwd%|p4 login
pause
p4 logout
pause
echo %p4passwd%|p4 login
pause
p4 logout
pause
所有p4命令执行前需要执行p4 login来获得ticket(表示用当前%p4user%来login)。
3)p4set.bat
call p4env.bat
p4 set
pause
p4 set
pause
查看当前的p4的环境变量的设置,包含p4user,p4passwd,p4client,p4port...。
4) p4sync.bat
call p4env.bat
REM sync to head
p4 sync -f %p4Root%/...
pause
REM sync to CL
p4 sync -f %p4Root%/...@9
pause
REM sync to label
p4 sync -f %p4Root%/...@label1
pause
REM sync to head
p4 sync -f %p4Root%/...
pause
REM sync to CL
p4 sync -f %p4Root%/...@9
pause
REM sync to label
p4 sync -f %p4Root%/...@label1
pause
可以sync到最新head,sync到指定的CL(changelist),或是sync到指定的label;
sync到指定的label时,只sync在label中包含的文件,在label后新加的文件不sync。
5)p4label.bat
call p4env.bat
REM label the head version and create the label
p4 tag -l label2 %p4Root%/...
pause
REM label to one CL and create the label
REM this is the best way to label p4 by one CL
p4 tag -l label1_1 %p4Root%/...@12
pause
REM show all files in one label
p4 files @label2
pause
REM label the head version and create the label
p4 tag -l label2 %p4Root%/...
pause
REM label to one CL and create the label
REM this is the best way to label p4 by one CL
p4 tag -l label1_1 %p4Root%/...@12
pause
REM show all files in one label
p4 files @label2
pause
label用来标识特定版本的某些文件;
label可以标识当前的p4上的最新文件,也可以标识某些文件在指定CL时刻的版本;
Labelsync 使用当前client的内容来更新label。需要先使用label来创建label。
6)p4latestcl.bat
call p4env.bat
for /f "tokens=2" %%i IN ('p4 changes -m 1 %p4Root%/...') do (@echo %%i > CL.txt)
set /p latestCL=<CL.txt
echo %latestCL%
pause
for /f "tokens=2" %%i IN ('p4 changes -m 1 %p4Root%/...') do (@echo %%i > CL.txt)
set /p latestCL=<CL.txt
echo %latestCL%
pause
用来获取当前最新的CL。
7)p4checkin.bat
call p4env.bat
set FileBeChanged=%p4Drive%\TestProject\components.txt
REM generate p4 change form for new CL
printf "\n">change.txt
printf "Change:\tnew\n">>change.txt
printf "\n">>change.txt
printf "Status:\tnew\n">>change.txt
printf "\n">>change.txt
printf "Description:\n">>change.txt
printf "\tUpdate BBB components.txt!\n">>change.txt
pause
REM new CL
cat change.txt | p4 change -i | awk '{print $2}'>newCL.txt
set /p newcl=<newcl.txt
pause
REM add the changed files into the CL
p4 edit -c %newcl% %FileBeChanged%
pause
REM update BBB component to 5
attrib -r %FileBeChanged%
set newversion=5
sed s/'BBB.*'/'BBB %newversion%'/ %FileBeChanged% > temp.txt
cat temp.txt> %FileBeChanged%
pause
REM submit the CL
p4 submit -c %newcl%
pause
set FileBeChanged=%p4Drive%\TestProject\components.txt
REM generate p4 change form for new CL
printf "\n">change.txt
printf "Change:\tnew\n">>change.txt
printf "\n">>change.txt
printf "Status:\tnew\n">>change.txt
printf "\n">>change.txt
printf "Description:\n">>change.txt
printf "\tUpdate BBB components.txt!\n">>change.txt
pause
REM new CL
cat change.txt | p4 change -i | awk '{print $2}'>newCL.txt
set /p newcl=<newcl.txt
pause
REM add the changed files into the CL
p4 edit -c %newcl% %FileBeChanged%
pause
REM update BBB component to 5
attrib -r %FileBeChanged%
set newversion=5
sed s/'BBB.*'/'BBB %newversion%'/ %FileBeChanged% > temp.txt
cat temp.txt> %FileBeChanged%
pause
REM submit the CL
p4 submit -c %newcl%
pause
components.txt如下:
BBB 4
CCC 9
此脚本演示了自动修改componets.txt文件,然后自动submit的过程;
其中p4 change用来产生新的CL,需要输入change.txt文件;
其中p4 edit用来把修改的文件加入到CL;
其中p4 submit用来提交CL;
第三方的sed.exe,cat.exe。
完!