No announcement yet.

Pre-commit hook to run pmd on the files that are being committed

  • Filter
  • Time
  • Show
Clear All
new posts

  • Pre-commit hook to run pmd on the files that are being committed

    Hi ,

    I am trying to write a script in PERL which can be called in pre-commit hook , so that it runs PMD on the files that are being committed and if any violations are found then the commit should be blocked .
    What i am trying to do is as follows :

    #1. Pass the REPO path and TXN id from pre commit.
    #2. Finding the added and modified files which have a .java extension , then copy all the java files to a temp folder .=>(I am finding difficulties in this part)
    #3. Invoke on the temp folder with required command line options for PMD.
    #4. in turn invokes the pmd java class that does the actual work and writes a report into a log
    #5. Then the script then checks the log for violation and returns 0 or 1 back to pre-commit.
    #6. pre-commit then return whatever status it got back from the script

    Correct me if anything above is not correct .

    So i wanted to know how to achieve the step 2 mentioned above . Is it possible to get all the java files being committed using svnlook and copy them to a temporary folder ?

    The code which i have tried till now is as follows
    $svnlook = '/usr/bin/svnlook';
    $repos = $ARGV[0];
    $txn = $ARGV[1];
    # to store the files which are being committed into a temp folder
    #`svnlook cat REPOS --transaction TXN > /Users/312213/work/test/temp`
    `$svnlook cat $repos -t $txn > /Users/312213/work/test/temp`
    if($? ne 0)
    print "Error while executing svnlook\n";
    # run pmd on the temp folder and get report(result) in xml format
    `sh /Users/312213/pmd-bin-5.0.4/bin/ pmd -d /Users/312213/work/test/temp  -f xml -R rulesets/java/basic.xml > /Users/312213/work/test/pmd_hook_report.xml`;
    if($? ne 0)
    print "Error while executing pmd\n";
    # parse the report xml file to check if it has any violation keyword , if yes then return 1 (any non zero value can be used) , else return 1 (no violations found , allow commit)
    if (grep{/violation/} <FILE>){
       print STDERR "Few problems found , commit not allowed ,  check /Users/312213/work/test/Dash_report1.xml for more info on problems\n";
    close FILE;
    Please ignore my coding style as this is the first time i am trying out something in perl.

  • #2
    Run 'svnlook changed' and get a list of all the paths modified in the transaction. Select lines with 'A' or 'M' in the first column and get the path starting from the fourth column. Paths that end with '/' are directories, other paths are files. Run 'svnlook cat' on the files.


    • #3
      Hi Philip , thanks for your solution , it worked exactly as i needed .