Patrick Ward words, code, and music

Merge Two Git Repos Into One

I needed to merge two distinct git repositories into a single parent repository as subdirectories the other day. I wanted to remove the separate repositories and add them as subdirectories within the parent repo, all while retaining the original repo history.

After a few trial and errors, I found the following worked well for me.

First, create the new parent repo and send it up to Github or Bitbucket:

  mkdir newrepo
  cd newrepo
  git init
  git remote add origin git@servername:username/newrepo.git
  vim README.md
  git add .
  git commit -m "Initial commit for merged repo"
  git push

Simple enough. Next, I created a clone of each repo I wanted to merge and ran filter-branch on them to ensure that each repo looked as if it had always been developed within it's own directory.

  git clone oldrepo1 oldrepo1_copy
  cd oldrepo1_copy

  # This moves all files into the repo1 directory
  git filter-branch --prune-empty --tree-filter '
  if [[ ! -e repo1 ]]; then
    mkdir -p repo1
    git ls-tree --name-only $GIT_COMMIT \
    | xargs -I files mv files repo1
  fi'

The above procedure was run for each repository I wanted to merge into the newrepo.

Finally, I merged each of the filtered repos into the new parent repo.

  cd newrepo
  git remote add -f repo1 ../oldrepo1_copy
  git merge -s ours --no-commit repo1/master
  git read-tree --prefix=/ -u repo1/master
  git commit -m 'Merged oldrepo1'

  # Once last check to make sure we have everything
  git pull -s subtree repo1 master

  # Finally, remote the remote and push
  git remote remove repo1
  git push

Again, the above procedure was run for each sub-repo I was merging in to the new repository.

The final result is that I have a single repository with each older repository as a subdirectory with all of the previous history in tact.

Setting Up a Laravel Application on Webfaction

I was working on a simple Laravel 4 application for a client recently and decided to put it on the Webfaction hosting platform. The platform itself is quite nice, but in order to make the Laravel application work properly, I had to go through a series of steps:

Setting Up Composer

Installing composer took a few extra steps to avoid errors and fit within the directory structure of the Webfaction user account:

  cd ~
  ln -s `which php55` ~/bin/php
  export PATH=$HOME/bin:$PATH
  curl -sS https://getcomposer.org/installer | php53

Once composer was installed, I was able to cd into my webapp directory and run the composer update:

  cd ~/webapps/MyLaravelApp/
  php55 ~/composer.phar update

Setting Up the Web Application

Once composer was installed and the Laravel dependencies were added, I was able to continue deploying the application.

Webfaction uses web applications to drive individual webapps. At first, I thought I could just add the Laravel application. However, Laravel apps require the document root to be set to the public directory. So, using the regular Webfaction application for that won't work. The recommended solution is to add a Symbolic Link application and add the absolute path of the public directory to the Extra Info section of the application setup.

  1. Within the control panel, go to "Domains/Websites --> Applications"
  2. Choose "Add a New Applciation"
  3. Enter the name of your application (e.g. MyLaravelApp_Public) - This is just a symbolic link to the public directory of the Laravel application I setup earlier.
  4. Choose "Symbolic link" for the "App Category"
  5. Choose the appropriate PHP version for the "App Type"
  6. Ener the absolute path of the Laravel public directory to the "Extra Info" text field. For example:

    /home/[username]/webapps/MyLaravelApp/public

Next, add this symbolic link application as the root application:

  1. Within the control panel, go to "Domains/Websites --> Websites"
  2. Choose the website you're working with
  3. Under "Contents" choose "Add an application --> Reuse and existing application"
  4. Choose the symbolic link application you created earlier.
  5. Finally, don't for get to save the website

At this point, you should have a working Laravel application on the Webfaction hostng platform.

Installing R from Homebrew on Mac OS X

I'm taking the Computing for Data Analysis on Coursera and needed to install the R language files. I prefer to install programs like R via homebrew. But, I ran into a few errors along the way. After a few tries and reading through issue 587 on Github, I was able to successfully install R via the following steps:

  1. First, I installed XQuartz to use as the X11 window system.
  2. Up next, the homebrew command line instructions:
brew update
brew tap homebrew/science
brew install r --env=std

It was the --env=std option that enabled me to fully install the R framework.

Setting Up Magento From Within the Shell

Setting up Magento can be a pain sometimes, so when I'm starting a new extension or working on a new client site, I like to minimize that pain.

Because I'm a sucker for the command line shell, I also try to do as much as possible from within that environment. Thankfully, Magento provides the ability to install from the command line. I find installing from the shell to be less error prone and, in most cases, quicker.

So, these are the basic steps I take each time I need to install a Magento system. I'm assuming you've already created a new database in MySQL.

Sample Data

I keep a copy of the latest Magento sample data on hand. But, if you don't have a copy, you'll find it on their website in the community downloads section. Sample data is great for building new extensions as it provides you with a working store complete with products, categories, etc. However, it may not be appropriate if you're working on a new client store.

If you are going to use sample data, it's important that you install this into a new database before you run the Magento installation itself.

So, from the command line, I do a simple MySQL import:

mysql -uusername -p magento_dev \
< magento_sample_data_for_1.6.1.0.sql

Installing Magento

Next, I'll tackle the installing of Magento. For that, Magento has several command line options that can be passed to the install.php file. Because I sometimes make mistakes and have to redo the installation, I'll often just write the commands into a single install.sh file. This let's me double-check the parameters and have a handly install script that can be used multiple times.

I tend to use all of the installation command line parameters with the exception of the database table prefix command. So, my install.sh file usually looks like this:

#!/bin/sh

php -f install.php -- \
--license_agreement_accepted yes \
--locale en_US \
--timezone "America/New_York" \
--default_currency USD \
--db_host localhost \
--db_name magento_dev \
--db_user root --db_pass root \
--url "http://mage.dev/" \
--use_rewrites yes \
--use_secure yes \
--secure_base_url "http://mage.dev/" \
--use_secure_admin yes \
--admin_lastname Admin \
--admin_firstname Magento \
--admin_email "magento@localhost" \
--admin_username admin \
--admin_password password101

I place this above the actual document root for the webserver, which makes installing Magento as simple as:

../install.sh

Indexing

Magento also has an indexer tool you can use from the command line. So, after I run the installation, I'll immediately index the tables before I open the web interface. From within the Magento root directory, the following command will cd into the shell folder, index the tables, and return you to the root directory.

cd shell && php -f indexer.php reindexall && cd ..

That's it!

I find this simple process to be the best way to install Magento, whether I'm in development or production. It's simple, reproducible, and quick.