From 44a1618b6e3e892ad6058671ae409ef9645ecd5a Mon Sep 17 00:00:00 2001 From: Romain Pelisse Date: Tue, 6 Mar 2012 08:42:11 +0000 Subject: [PATCH] Applied patch 3497021 from Remi Delmas - CPD now returns non zero status if duplication is found. git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7638 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/etc/changelog.txt | 5 +++++ pmd/src/main/java/net/sourceforge/pmd/cpd/CPD.java | 8 ++++++-- pmd/src/site/xdocs/cpd.xml | 4 ++-- pmd/src/site/xdocs/credits.xml | 1 + 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index d15634df28..bec7bb4457 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -29,6 +29,11 @@ TODO - Release blockers - Must implement before this release can be finally fini Fixed bug 3470274: Using Label for lines in XMLRenderer Fixed bug 3175710: NPE in InsufficientStringBufferDeclaration + +CPD: +- Exit with status code 4 when CPD detects code duplication (Patch ID: 3497021) + + --- diff --git a/pmd/src/main/java/net/sourceforge/pmd/cpd/CPD.java b/pmd/src/main/java/net/sourceforge/pmd/cpd/CPD.java index 5a8f6d5bc2..72c480c01d 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/cpd/CPD.java +++ b/pmd/src/main/java/net/sourceforge/pmd/cpd/CPD.java @@ -21,7 +21,8 @@ public class CPD { private static final int MISSING_FILES = 1; private static final int MISSING_ARGS = 2; private static final int MISSING_REQUIRED_ARGUMENT = 3; - + private static final int DUPLICATE_CODE_FOUND = 4; + private Map source = new TreeMap(); private CPDListener listener = new CPDNullListener(); private Tokens tokens = new Tokens(); @@ -220,7 +221,10 @@ public class CPD { } cpd.go(); - System.out.println(renderer.render(cpd.getMatches())); + if(cpd.getMatches().hasNext()) { + System.out.println(renderer.render(cpd.getMatches())); + System.exit(DUPLICATE_CODE_FOUND); + } } catch (Exception e) { e.printStackTrace(); } diff --git a/pmd/src/site/xdocs/cpd.xml b/pmd/src/site/xdocs/cpd.xml index 09361ad9aa..48b5f966c2 100644 --- a/pmd/src/site/xdocs/cpd.xml +++ b/pmd/src/site/xdocs/cpd.xml @@ -137,8 +137,8 @@ $ java -Xmx512m net.sourceforge.pmd.cpd.CPD --minimum-tokens 100 --files /usr/lo - -

Suggestions? Comments? Post them here. Thanks!

+

Please note that if CPD detects duplicated source code, it will exit with status 4 (since 5.0). This behavior has been introduced to ease CPD integration into scrips or + hook, such as SVN hooks.

diff --git a/pmd/src/site/xdocs/credits.xml b/pmd/src/site/xdocs/credits.xml index 96e1b9c10f..76af0b35c1 100644 --- a/pmd/src/site/xdocs/credits.xml +++ b/pmd/src/site/xdocs/credits.xml @@ -327,6 +327,7 @@
  • Suresh - new rule DontUseFloatTypeForLoopIndices
  • Dinesh Bolkensteyn and SonarSource - Java 7 grammar support
  • Tom Wheeler - contribute a launch script for CPD GUI
  • +
  • Remi Delmas - change CPD CLI to return a non null value when code duplication is found.