Script to migrate WordPress plugins from SVN to github

Update: This script is almost obsolete as github will switch to their v3 API on may 1st, 2012 and doesn't display the API token any more...

With this this script you can easily migrate a WordPress plugin from the wordpress.org svn repository to github. This script will NOT import the full history, only the last commit.

Use your usual git workflow, and when you are ready to publish on wordpress.org use git svn dcommit, and git svn tag.

#!/usr/bin/env bash

intro="
Usage: bash wp_plugin_to_github.sh plugin_name

This script will create a local git repository of your wordpress plugin.
Because of the extremely slow fetching of history from wordpress.org svn
this script will THROW AWAY your complete history. However, the history
will still be on wordpress.org and if you really care you can fetch it from
there later.

Please make sure that you have uploaded your local public ssh key to your
github account!

"

echo $intro

# Test if a program is available, exit if not
function test_executable() {
  bin=$1
  command -v $bin &>/dev/null || {
    echo "I require $bin but it's not installed. Aborting." >&2;
    exit 1;
  }
}

# Test if all required tools are available
function test_dependencies() {
  for dependency in svn curl git; do
    test_executable $dependency
  done
}

# Test if git svn is available
function test_git_svn_dependency() {
  git svn help > /dev/null 2>&1
  if [ $? != 0 ]; then
    echo "git svn doesn't seem to be installed"
    exit 1
  fi
}

# Initialize the local repository
function init_local_repository() {
  if [ -d $plugin ]; then
    echo "There already is a local directory $plugin... aborting"
    exit 1
  fi

  mkdir $plugin
  cd $plugin

  git svn init -s http://svn.wp-plugins.org/$plugin

  # List the recent log
  svn log http://svn.wp-plugins.org/$plugin | head -20
  
  echo "================================================================"
  echo "Please enter the revision you want to work with."
  echo "IMPORTANT: You can not use a tag commit (Create tag nn message)."
  echo "================================================================"
  echo
  read revision
  
  git svn fetch -$revision

  if [ $? != 0 ]; then
    echo "Couldn't initialize the repository"
    exit 1
  fi
  
  cd ..
}

# Ask for the plugin's name
function get_plugin_name() {
  echo "Please enter your plugin's name as listed on wp.org"
  read plugin
}

# Ask for github credentials
function get_github_credentials() {
  echo "Please enter your github username"
  read login
  echo "Please enter your github API token, see https://github.com/settings/admin"
  read token
}

# Create the remote github repository
function init_github_repository() {
  cd $plugin
  desc="WordPress plugin $plugin"
  curl -F "login=$login" -F "token=$token" https://github.com/api/v2/json/repos/create -F "name=wp-$plugin" -F "description=$desc"
  git remote add origin git@github.com:$login/wp-$plugin.git
  git push -u origin master
  cd ..
}

test_dependencies
test_git_svn_dependency

if [ -n $1 ]; then
  plugin=$1
else
  get_plugin_name
fi

echo "Plugin $plugin"

if [[ -n $2 || -n $3 ]]; then
  login=$2
  token=$3
else
  get_github_credentials
fi

init_local_repository
init_github_repository

Published on March 2, 2012 at 8:03 p.m. by Nicolas and tagged WordPress, svn, git, github. You can follow the discussion with the comment feed for this post. Feeling generous? Donate!

0 comments

Start a new thread

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.