Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perforce - source-code formatting with pre-commit hook

I'm using Perforce version control system (http://www.perforce.com/) and would like to format source code files (mainly XML) when developers submit their files to Perforce. I know that Git and SVN allow script hooks that provide for that.

Is there a way to change files that are being submitted to Perforce using some kind of a hook? How can I do that on Perforce?

Thanks!

like image 488
Avi Shefi Avatar asked Jul 14 '26 14:07

Avi Shefi


1 Answers

When I've done these sorts of policy-enforcement tools in the past, I've done it post-commit.

That is, after the submit completes, my tool retrieves the newly-submitted files, re-formats them according to the policy that I'm enforcing, and submits the re-formatted files as a follow-on change.

I do this by writing a tool that monitors changes similarly to the way the change review daemon monitors changes, so that the tool notices new submits and reviews the new files to see if they comply to the organization policy.

I generally have the tool perform a "revert -a" prior to the submit, so that if the files were formatted according to policy by the original developer, no second submission occurs.

I actually think this is a better approach than trying to do it during the submit:

  1. The change that is submitted is exactly as the user provided it, with the identical content as provided by the user
  2. The modifications that are due to the tool are clearly visible in a separate submission, which makes it very easy to recognize when the tool has gone astray and damaged the file during its re-formatting (such tool bugs do occur).
  3. The net effect, overall, is the desired one: the files at the head of the branch are formatted according to company policy.
like image 168
Bryan Pendleton Avatar answered Jul 17 '26 21:07

Bryan Pendleton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!