Skip to content

Instantly share code, notes, and snippets.

@tivnet
Created July 3, 2015 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tivnet/07d8f09f917e9eb0812e to your computer and use it in GitHub Desktop.
Save tivnet/07d8f09f917e9eb0812e to your computer and use it in GitHub Desktop.
Git to SVN for WordPress
#!/usr/local/bin/php -q
<?php
/**
* Git to SVN
* @author Gregory Karpinsky (@tivnet)
*/
/**
* Default values for $_SERVER, because we run as CLI
*/
empty( $_SERVER['HTTP_HOST'] ) && $_SERVER['HTTP_HOST'] = 'localhost';
empty( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] = '/';
empty( $_SERVER['DOCUMENT_ROOT'] ) && $_SERVER['DOCUMENT_ROOT'] = __DIR__;
require_once '../wp-config.php';
/** @global WP_Filesystem_Direct $wp_filesystem */
global $wp_filesystem;
_setup_filesystem();
$PATH_SVN = _real_unix_path( ABSPATH . '../plugins.svn.wordpress.org/' );
$PATH_GIT = _strip_windows_drive_letter( $wp_filesystem->wp_plugins_dir() );
$PATH_EXCLUDE_LIST = _real_unix_path( 'exclude.txt' );
foreach ( glob( $PATH_SVN . '/' . '*' ) as $path_to ) {
if ( is_dir( $path_to ) ) {
$plugin_slug = basename( $path_to );
echo "\n${plugin_slug}\n";
$path_from = $PATH_GIT . $plugin_slug;
if ( is_dir( $path_from ) ) {
system( "rsync -avPW --delete --exclude-from=${PATH_EXCLUDE_LIST} ${path_from}/ ${path_to}/trunk" );
system( "rsync -avPW --delete ${path_from}/assets/ ${path_to}/assets" );
}
else {
echo "\n*** No GIT for ${plugin_slug}\n";
}
}
}
echo "\n\n";
/**
*
*/
function _setup_filesystem() {
require_once( ABSPATH . '/wp-admin/includes/file.php' );
if ( false === ( $credentials = request_filesystem_credentials( '/', 'direct', false, ABSPATH ) ) ) {
echo "Error: request_filesystem_credentials\n";
exit( 1 );
}
if ( ! WP_Filesystem( $credentials, ABSPATH ) ) {
echo "Error: WP_Filesystem\n";
exit( 1 );
}
}
/**
* @param string $folder
* @return string
*/
function _real_unix_path( $folder ) {
$folder = realpath( $folder );
$folder = _strip_windows_drive_letter( $folder );
$folder = _normalize_directory_separator( $folder );
return $folder;
}
/**
* Strip out windows drive letter if it's there.
* 'C:\path' becomes '\path'
* @param string $folder
* @return string
* *
* Code from
* @see WP_Filesystem_Base::find_folder
*/
function _strip_windows_drive_letter( $folder ) {
return preg_replace( '|^([a-z]{1}):|i', '', $folder );
}
/**
* Convert \windows\path to /unix/path
* @param string $folder
* @return string
*/
function _normalize_directory_separator( $folder ) {
if ( DIRECTORY_SEPARATOR === '\\' ) {
$folder = str_replace( DIRECTORY_SEPARATOR, '/', $folder );
}
return $folder;
}
@tivnet
Copy link
Author

tivnet commented Jul 3, 2015

exclude.txt example:

.git
.gitignore
.svn
.idea
.travis.yml
assets
tmp-wp-dev
codeception.yml
node_modules
tests
unit-tests
internal
Gruntfile.js
package.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment