Skip to content

Commit 1b1bb17

Browse files
committed
Use $this->repo instead of passing the variable to functions.
Add support for folder in case the repository can contain multiple folders for different project
1 parent 910733d commit 1b1bb17

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

class-phpcs-diff-svn.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ class PHPCS_Diff_SVN {
99
// Used to store details about the repo the class was initialized with.
1010
public $repo; // Specific repository - eg.: plugin's name.
1111
public $repo_url; // SVN repository URL.
12+
public $folder = ''; // In case the repository has some other specific folders.
1213

13-
function __construct( $repo ) {
14+
function __construct( $repo, $folder = false ) {
15+
16+
$repo = sanitize_title( $repo );
1417

1518
switch ( $repo ) {
1619

@@ -22,9 +25,13 @@ function __construct( $repo ) {
2225
}
2326

2427
$this->repo = $repo;
28+
29+
if ( false !== $folder && 0 === validate_file( $folder ) ) {
30+
$this->folder = $folder;
31+
}
2532
}
2633

27-
public function get_diff( $folder, $end_revision, $start_revision = null, $options = array() ) {
34+
public function get_diff( $end_revision, $start_revision = null, $options = array() ) {
2835
$summarize = false;
2936
$xml = false;
3037
$ignore_space_change = false;
@@ -43,7 +50,6 @@ public function get_diff( $folder, $end_revision, $start_revision = null, $optio
4350
}
4451

4552
$end_revision = (int) $end_revision;
46-
$folder = str_replace( '..', '', $folder ); // Prevent moving up a directory
4753

4854
if ( $start_revision && is_numeric( $start_revision ) ) {
4955
$start_revision = (int) $start_revision;
@@ -52,7 +58,7 @@ public function get_diff( $folder, $end_revision, $start_revision = null, $optio
5258
$start_revision = 1;
5359
}
5460

55-
$repo_url = esc_url_raw( trailingslashit( $this->repo_url ) . trailingslashit( $this->repo ) . $folder );
61+
$repo_url = esc_url_raw( trailingslashit( $this->repo_url ) . trailingslashit( $this->repo ) . $this->folder );
5662

5763
$diff = shell_exec(
5864
sprintf( 'svn diff %s --non-interactive --no-auth-cache --username %s --password %s -r %d:%d %s %s %s',

class-phpcs-diff.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@ public function set_excluded_extensions( $excluded_exts ) {
4848
$this->excluded_extensions = $excluded_exts;
4949
}
5050

51-
public function run( $repo, $oldest_rev, $newest_rev ) {
51+
public function run( $oldest_rev, $newest_rev ) {
5252

5353
$oldest_rev = absint( $oldest_rev );
5454
$newest_rev = absint( $newest_rev );
55-
$repo = sanitize_title( $repo );
5655

57-
$cache_key = md5( 'phpcs_' . $repo . $oldest_rev . $newest_rev );
56+
$cache_key = md5( 'phpcs_' . $this->version_control->repo . $oldest_rev . $newest_rev );
5857
$cache_group = 'vip-phpcs';
5958

6059
if ( true !== $this->nocache ) {
@@ -64,7 +63,7 @@ public function run( $repo, $oldest_rev, $newest_rev ) {
6463
}
6564
}
6665

67-
$diff = trim( $this->version_control->get_diff( $repo, $newest_rev, $oldest_rev, array( 'ignore-space-change' => true ) ) );
66+
$diff = trim( $this->version_control->get_diff( $newest_rev, $oldest_rev, array( 'ignore-space-change' => true ) ) );
6867

6968
$this->stop_the_insanity();
7069

wp-cli-command.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class PHPCS_Diff_CLI_Command extends WP_CLI_Command {
4141
* wp phpcs-diff --repo="hello-dolly" --start_revision=99998 --end_revision=100000
4242
*
4343
* @subcommand phpcs-diff
44-
* @synopsis --repo=<repo> --start_revision=<start-revision> --end_revision=<end-revision> [--standard=<standard>] [--format=<format>] [--nocache] [--ignore-diff-too-big] [--excluded-exts=<excluded-exts>]
44+
* @synopsis --repo=<repo> --start_revision=<start-revision> --end_revision=<end-revision> [--standard=<standard>] [--format=<format>] [--nocache] [--ignore-diff-too-big] [--excluded-exts=<excluded-exts>] [--folder=<folder>]
4545
*/
4646
public function __invoke( $args, $assoc_args ) {
4747

@@ -58,10 +58,14 @@ public function __invoke( $args, $assoc_args ) {
5858
if ( true === array_key_exists( 'excluded-exts', $assoc_args ) && false === empty( $assoc_args['excluded-exts'] ) ) {
5959
$excluded_exts = array_map( 'sanitize_text_field', explode( ',', $assoc_args['excluded-exts'] ) );
6060
}
61+
$folder = false;
62+
if ( true === array_key_exists( 'folder', $assoc_args ) && false === empty( sanitize_text_field( $assoc_args['folder'] ) && 0 === validate_file( sanitize_text_field( $assoc_args['folder'] ) ) ) ) {
63+
$folder = sanitize_text_field( $assoc_args['folder'] );
64+
}
6165

6266
// @todo: replace SVN version control backend with any other parser you might want to use - eg.: git
6367
require_once( __DIR__ . 'class-phpcs-diff-svn.php' );
64-
$phpcs = new PHPCS_Diff( new PHPCS_Diff_SVN( $repo ) );
68+
$phpcs = new PHPCS_Diff( new PHPCS_Diff_SVN( $repo, $folder ) );
6569

6670
if ( true === array_key_exists( 'ignore-diff-too-big', $assoc_args ) ) {
6771
$phpcs->set_no_diff_too_big( true );

0 commit comments

Comments
 (0)