Skip to content

Commit def9b35

Browse files
Copilotswissspidy
andcommitted
Add VCS checkout detection for plugin/theme updates with --include-vcs flag
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 62e8147 commit def9b35

5 files changed

Lines changed: 94 additions & 0 deletions

File tree

features/plugin-update.feature

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,36 @@ Feature: Update WordPress plugins
377377
"""
378378
Success: Updated 2 of 2 plugins.
379379
"""
380+
381+
@require-wp-5.2
382+
Scenario: Skip plugin update when plugin directory is a VCS checkout
383+
Given a WP install
384+
And I run `wp plugin install wordpress-importer --version=0.5 --force`
385+
And I run `wp plugin path wordpress-importer --dir`
386+
And save STDOUT as {PLUGIN_DIR}
387+
388+
When I run `mkdir {PLUGIN_DIR}/.git`
389+
And I try `wp plugin update wordpress-importer`
390+
Then STDERR should contain:
391+
"""
392+
Warning: wordpress-importer: Skipped update because a VCS checkout was detected.
393+
"""
394+
And STDERR should contain:
395+
"""
396+
Error: No plugins updated.
397+
"""
398+
And the return code should be 1
399+
400+
@require-wp-5.2
401+
Scenario: Update plugin in VCS checkout when --include-vcs is set
402+
Given a WP install
403+
And I run `wp plugin install wordpress-importer --version=0.5 --force`
404+
And I run `wp plugin path wordpress-importer --dir`
405+
And save STDOUT as {PLUGIN_DIR}
406+
407+
When I run `mkdir {PLUGIN_DIR}/.git`
408+
And I run `wp plugin update wordpress-importer --include-vcs`
409+
Then STDOUT should contain:
410+
"""
411+
Success: Updated 1 of 1 plugins.
412+
"""

features/theme-update.feature

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,34 @@ Feature: Update WordPress themes
257257
"""
258258
Success: Updated 2 of 2 themes.
259259
"""
260+
261+
Scenario: Skip theme update when theme directory is a VCS checkout
262+
Given a WP install
263+
And I run `wp theme install twentytwelve --version=3.0 --force`
264+
And I run `wp theme path twentytwelve --dir`
265+
And save STDOUT as {THEME_DIR}
266+
267+
When I run `mkdir {THEME_DIR}/.git`
268+
And I try `wp theme update twentytwelve`
269+
Then STDERR should contain:
270+
"""
271+
Warning: twentytwelve: Skipped update because a VCS checkout was detected.
272+
"""
273+
And STDERR should contain:
274+
"""
275+
Error: No themes updated.
276+
"""
277+
And the return code should be 1
278+
279+
Scenario: Update theme in VCS checkout when --include-vcs is set
280+
Given a WP install
281+
And I run `wp theme install twentytwelve --version=3.0 --force`
282+
And I run `wp theme path twentytwelve --dir`
283+
And save STDOUT as {THEME_DIR}
284+
285+
When I run `mkdir {THEME_DIR}/.git`
286+
And I run `wp theme update twentytwelve --include-vcs`
287+
Then STDOUT should contain:
288+
"""
289+
Success: Updated 1 of 1 themes.
290+
"""

src/Plugin_Command.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,9 @@ protected function install_from_repo( $slug, $assoc_args ) {
840840
* [--auto-update-indicated]
841841
* : Only update plugins where the server response indicates an automatic update. Updates to the version indicated by the server, not necessarily the latest version. Cannot be used with `--version`, `--minor`, or `--patch`.
842842
*
843+
* [--include-vcs]
844+
* : Include plugins that are version-controlled with a VCS (e.g. git, svn, hg). Skipped by default.
845+
*
843846
* ## EXAMPLES
844847
*
845848
* $ wp plugin update bbpress --version=dev

src/Theme_Command.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,9 @@ public function get( $args, $assoc_args ) {
792792
* [--auto-update-indicated]
793793
* : Only update themes where the server response indicates an automatic update. Updates to the version indicated by the server, not necessarily the latest version. Cannot be used with `--version`, `--minor`, or `--patch`.
794794
*
795+
* [--include-vcs]
796+
* : Include themes that are version-controlled with a VCS (e.g. git, svn, hg). Skipped by default.
797+
*
795798
* ## EXAMPLES
796799
*
797800
* # Update multiple themes

src/WP_CLI/CommandWithUpgrade.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,30 @@ function ( $item ) {
744744
}
745745
}
746746

747+
// Skip VCS-controlled items unless --include-vcs is set.
748+
if ( ! Utils\get_flag_value( $assoc_args, 'include-vcs', false ) && in_array( $this->item_type, [ 'plugin', 'theme' ], true ) ) {
749+
if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
750+
if ( file_exists( ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php' ) ) {
751+
require_once ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';
752+
} else {
753+
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
754+
}
755+
}
756+
$automatic_updater = new \WP_Automatic_Updater();
757+
foreach ( $items_to_update as $item_key => $item_info ) {
758+
if ( 'plugin' === $this->item_type ) {
759+
$item_dir = WP_PLUGIN_DIR . '/' . dirname( $item_key );
760+
} else {
761+
$item_dir = get_theme_root( $item_key ) . '/' . $item_key;
762+
}
763+
if ( $automatic_updater->is_vcs_checkout( $item_dir ) ) {
764+
WP_CLI::warning( "{$item_info['name']}: Skipped update because a VCS checkout was detected. Use --include-vcs to override." );
765+
++$skipped;
766+
unset( $items_to_update[ $item_key ] );
767+
}
768+
}
769+
}
770+
747771
if ( Utils\get_flag_value( $assoc_args, 'dry-run' ) ) {
748772
if ( empty( $items_to_update ) ) {
749773
WP_CLI::log( "No {$this->item_type} updates available." );

0 commit comments

Comments
 (0)