Perforce 작업은 Perforce Helix P4V 라는 클라이언트를 통해 해야한다. P4 라는 명령어나 탐색기 플러그인이 있지만 쓸만하지 않은 것 같다.


여러 폴더에 workspace(working copy) 를 만들고 쓸 때 P4 설정이 폴더에 맞춰 바뀌지 않아서 P4 커맨드 실행시 짜증이 난다.


언리얼 에디터를 보니 소스 컨트롤을 연동할 때 workspace 의 경로를 보고 연동 가능한 workspace 목록을 보여주도록 되어 있었다. 비슷한 식으로 배치 파일을 구현해서 사용하면 된다.

: p4 clients 명령으로 현재 유저와 연동된 workspace 목록을 가져온다.
: 배치 파일 실행 경로가 workspace 경로를 포함하고 있는지 비교한다.
: 포함하고 있으면 p4 set 으로 P4CLIENT 를 설정한다.

for /f "tokens=2,4* delims= " %%a in ('p4 clients -u %UserName%') do call :remove_desc_and_call "%%a" "%%c"


p4 set P4CLIENT


exit /b 0


:remove_desc_and_call [%1 - a workspace name;%2 - a workspace path and a description]


setlocal EnableDelayedExpansion


set WsName=%~1

set WsPathAndDesc=%~2

set /a SepIdx=0


call :indexof "%WsPathAndDesc%" "'" "SepIdx"

set /a SplitIdx=%SepIdx%-1

call set WsPath=%%WsPathAndDesc:~0,%SplitIdx%%%


call :compare_and_set_p4_client "%WsName%" "%WsPath%"


endlocal


goto :eof


:compare_and_set_p4_client [%1 - a workspace name;%2 - a workspace path]


set WsName=%~1

set CurDir=%~dp0

set Target=%~2


echo call :startsWith "%CurDir%" "%Target%\"

call :startsWith "%CurDir%" "%Target%\"


if %errorlevel% == 1 (

p4 set P4CLIENT=%WsName%

)


goto :eof


:startsWith [%1 - string to be checked;%2 - string for checking ] 

@echo off

rem :: sets errorlevel to 1 if %1 starts with %2 else sets errorlevel to 0


setlocal EnableDelayedExpansion


set "string=%~1"

set "checker=%~2"

rem set "var=!string:%~2=&echo.!"

set LF=^



rem ** Two empty lines are required

rem echo off

for %%L in ("!LF!") DO (

  for /f "delims=" %%R in ("!checker!") do ( 

  rem set "var=!string:%%~R%%~R=%%~L!"

  set "var=!string:%%~R=#%%L!"

  )

)

for /f "delims=" %%P in (""!var!"") DO (

if "%%~P" EQU "#" goto :yes

goto :no

)

:yes

endlocal & verify set_error 2>nul

goto :eof

:no

endlocal & ( echo | shift )

goto :eof


:indexof [%1 - string ; %2 - find index of ; %3 - if defined will store the result in variable with same name]

@echo off

setlocal enableDelayedExpansion


set "str=%~1"

set "s=!str:%~2=&rem.!"

set s=#%s%

if "%s%" equ "#%~1" endlocal& if "%~3" neq "" (set %~3=-1&exit /b 0) else (echo -1&exit /b 0) 


  set "len=0"

  for %%A in (2187 729 243 81 27 9 3 1) do (

set /A mod=2*%%A

for %%Z in (!mod!) do (

if "!s:~%%Z,1!" neq "" (

set /a "len+=%%Z"

set "s=!s:~%%Z!"

) else (

if "!s:~%%A,1!" neq "" (

set /a "len+=%%A"

set "s=!s:~%%A!"

)

)

)

  )

  endlocal & if "%~3" neq "" (set %~3=%len%) else echo %len%

exit /b 0



728x90

+ Recent posts