Automatic patch review script for perforce
Asked by Ohad Gal:

How can I automaitcally create a patch review for a certain changelist in perforce, including newly added files?


Answers:
author: Ohad Gal
body: You can use these DOS scripts I wrote.

The solution is comprised of a main script and a helper script. The main script receives the changelist number and calls the second script to handle newly added files.

You can call the first script anything you like but the second script has to be called CreateNewPatch.bat (or you can change the relevant line in the first script).

Instructions:

1. Save both of the scripts in your workspace root directory
2. Make sure you call the second script "CreateNewPatch.bat" or that you change the relevant line in the first script
3. Run the first script from your workspace root directory passing the changelist number as the parameter (e.g. CreatePatch 861)
4. Upload the created patch file as a patch review in crucible.

Any comments are welcome.

Enjoy!

-Moshe

**Script 1 (main script):**
> @echo off
>
> REM Create date and time tokens to use in the output patch file name
>
> REM time will be HHMM
>
> REM date will be the month date and year all together depending on your locality settings.
>
> REM you might need to change the script to handle a long date format if thats what your
>
> REM system shows when echoing %date%
>
> FOR /F "delims=:. tokens=1-2" %%A IN ("%time%") DO set now=%%A%%B
>
> FOR /F "delims=/ tokens=1-3" %%A IN ("%date%") DO set today=%%A%%B%%C
>
> REM The outputted patch file will be called: patch_#(changelist number)_(date)_(time).txt
>
> REM You can change the output file format to your liking
>
> set patchname=patch_#%1_%today%_%now%.txt
>
> REM Make sure we don't add files to a previous patch
>
> IF EXIST %patchname% del %patchname%
>
> REM For each file that is opened we either call the helper batch file for added files or the 'P4 diff' for edited files
>
> set command=p4 opened -c %1
>
> set command2=FOR /f "usebackq delims=# tokens=1-2" %%I IN (\`%command%\`) DO FOR /f "tokens=3" %%A IN ("%%J") DO IF %%A==add (call CreateNewPatch %%I %patchname%) ELSE (p4 diff -dcu "%%I")
>
> %command2% \>\> %patchname%

**Script no. 2 (the helper script):**
> @echo off
>
> set depotFileName=%1
>
> REM Find local filename using the current directory (depot root path) as the base
>
> FOR /F "tokens=2\* delims=/" %%A in ("%1") do set localFileNameBase=%cd%\\%%A\\%%B
>
> REM Change front slashes to back slashes in local file name
>
> set localFileName=%localFileNameBase:/=\\%
>
> REM Count the number of lines in the file
>
> set /a lines=0
>
> FOR /F "usebackq tokens=1\* delims=" %%A IN (\`findstr /R /N "\^" "%localFileName%"\`) DO set /a lines+=1
>
> REM Output the file in unified diff format into the patch file (given as parameter 2)
>
> @echo ==== %1#1 - %localFileName% ====\>\>%2
>
> @echo @@ -1,0 +1,%lines% @@\>\>%2
>
> FOR /F "usebackq tokens=1,2\* delims=:" %%A IN (\`findstr /R /N "\^" "%localFileName%"\`) DO @echo +%%B %%C\>\>%2

comments: author: Ohad Gal
body: I have since changed the first script file a bit to allow entering the changelist number after running the script, that way you can run the script (from a shortcut) and be prompted for the desired changelist:

**Script no. 1:**

```
@echo off SET /P changeNum=Enter the changelist number: echo Change number: %changeNum%REM Create date and time tokens to use in the output patch file nameREM time will be HHMMREM date will be the month date and year all together depending on your locality settings.REM you might need to change the script to handle a long date format if thats what yourREM system shows when echoing %date%FOR /F "delims=:. tokens=1-2" %%A IN ("%time%") DO set now=%%A%%BFOR /F "delims=/ tokens=1-3" %%A IN ("%date%") DO set today=%%A%%B%%CREM The outputted patch file will be called: patch_#(changelist number)_(date)_(time).txtREM You can change the output file format to your likingset patchname=patch_#%changeNum%_%today%_%now: =0%.txtecho %patchname%REM Make sure we don't add files to a previous patchIF EXIST %patchname% del %patchname%REM For each file that is opened we either call the helper batch file for added files or the 'P4 diff' for edited filesset command=p4 opened -c %changeNum%set command2=FOR /f "usebackq delims=# tokens=1-2" %%I IN (`%command%`) DO FOR /f "tokens=3" %%A IN ("%%J") DO IF %%A==add (call CreateNewPatch "%%I" %patchname%) ELSE (p4 diff -dcu "%%I")%command2% >> %patchname%explorer .
```


Tags: automation

patch

perforce

review

script