Thursday, 8 October 2009

copy-merge in ClearCase (on Windows)

For various reasons, there are occasions when you want to merge exactly what has been changed on a branch to another without having ClearCase trying to perform the merge itself. A common issue is for binary elements, but I use it for deliveries as well: an identical copy of the element is "merged" from development to integration.

The solutions I've seen (for example from IBM) aren't satisfactory since they perform the operation on every element, disregarding if they need merging or not. I want to have traceability so that only the files needed are merged, allowing me to easily find what changes have been done between merges.

Here's my solution, made as an MS-DOS batch. Note that it's currently only working for snapshot views, since this was one of my requirements. It should be easy to adapt for dynamic views as well (I might just do that in a "near future" myself).

:: Clear previous mergeset
if exist %MERGESET% del /q %MERGESET%

::Automatically merge all directories
cleartool findmerge . -type d -fversion %VERSION% -merge -nc -log NUL

:: Find items eligible to merge, check these out and record them to file
echo Finding files to merge...
cleartool findmerge . -type f -fversion %VERSION% -log NUL -co -nc -exec "cmd /v:on /c echo !CLEARCASE_FXPN!;!CLEARCASE_PN! >>%MERGESET%"

:: Delete the target
echo Deleting targets...
for /f "tokens=2 delims=;" %%a in (%MERGESET%) do del /q "%%a"

:: Use cleartool get to copy contents from source
echo Copying from source to target...
for /f "tokens=1,2 delims=;" %%a in (%MERGESET%) do cleartool get -to "%%b" "%%a"
for /f "tokens=2 delims=;" %%a in (%MERGESET%) do attrib -R "%%a"

:: Create a merge arrow for the these items
echo Creating Merge arrows...
for /f "tokens=1,2 delims=;" %%a in (%MERGESET%) do cleartool mkhlink -unidir Merge "%%a" "%%b"
Some explanation needed: the %VERSION% is an input parameter holding the exact version merging from (e.g. /main/dev_branch/LATEST).

One "tough nut" for me was to find the /v:on-flag for calling cmd, in conjunction with the !-signs around environment variables. This is what makes them set at runtime, and not at time of calling the script (when it's empty).

No comments:

Post a Comment