Pulling changes into a Github fork from the original repository

If you maintain your own ongoing fork of a project on Github, you will inevitably want to pull in changes from the originator’s repository. Here’s how I usually go about it.

First add the other guy’s repository to your list of remotes:

cd my-fork
git remote add other-guy https://github.com/other-guy/other-guys-repo.git 

If you were to then list your remotes, you would have something like:

origin
other-guy 

Now it’s just a case of pulling from the relevant branch on their repo, in this case ‘master’:

git pull other-guy master

Hope that saves someone some time.

Writing Javascript that runs in multiple environments (with a one-liner)

With the introduction of server-side javascript, it’s nice to be able to utilise any Javascript libraries you may write in both the browser and on the server. I’ve seen a few different approaches to this but wanted a one-liner that I could just stick at the top of an anonymous function… this is what I came up with:

(function() {
  var MyLib = (typeof exports !== 'undefined') ? exports : this.MyLib = {};
 
  // MyLib.myFunction = ...
}());

Now in NodeJS you can do:

var MyLib = require('./mylib.js');

This also passes JS Lint using the following:

/*jslint */
/*global exports */
(function () {
    "use strict";
    var MyLib = (typeof exports !== 'undefined') ? exports : this.MyLib = {};
 
    MyLib.foo = 'bar';
}());

On Open Sourcing Your Competitive Advantage

It’s been a little quiet around here, so I thought I’d share something that’s been on mind for a while.

At what point in the lifecycle of a commercial project is it a good idea to Open Source?

Take Shopify for instance, they open sourced their payment integration code early on in the form of ActiveMerchant. This was undoubtably a wise move, as they benefitted from an entire Ruby community testing their code in the wild and contributing back in the form of new payment gateways.

I don’t know exactly when it was in the development of Shopify that ActiveMerchant was released but at what point are you open sourcing your competitive advantage. Don’t get me wrong, I love open source, I am just trying to determine when is a good time to open source and when are you just giving away your competitive advantage. For instance, if ActiveMerchant had been released 2 years before the release of Shopify, anyone could have used it in the development of a Shopify’esque product… but would anyone have even thought of using it in that way? Who knows?

Bite sized chunks

I feel it is a good idea to open source little and often. Obviously if Shopify had open sourced their entire shopping cart, subscription code and templating solution as a whole, you would have seen Shopify’s popping up all over the interweb and their business would likely have suffered. Instead they opted to open source their payment gateway code (ActiveMerchant) and templating library (Liquid) separately as self-contained projects.

A key ingredient

There comes a point when you simply should not open source certain code, this is when it contains the key ingredient to a commercial project. Do you think Coca-Cola would have been as big as they are today if they had released the complete recipe, including the key ingredient for their revolutionary new black fizzy pop? It’s all about the byproduct, for example if Coca-Cola had released the secret to their new screw on lid (I’m sure they didn’t invent screw on lids but bear with me), they would have got their name out there through recognition of their fancy lid, the pop community may have helped refine it’s design and coke may have stayed fizzy for longer… that’s one hell of a contrived example but hopefully you get my point. It doesn’t matter that every other pop out there will also stay fizzy longer, they’re not Coke.

I’d love to hear peoples examples of commercial projects where code has been open sourced. What result did it have on the project? Was it worthwhile or did it have a detrimental effect on your business?

Using git add -A

In my day-to-day Git workflow I often find myself doing the the following before a commit:

git add .
git add -u
git status

The first line adds any new or modified file contents to the Git index and the second uses the “update” flag which marks any deleted files as deleted in the Git index. I then proceed with a commit and all my changes are committed.

I find myself repeating this before every commit and so had a browse of the documentation to see if there was a simpler way. I found the “-A” or “–all” flag which I had never noticed before (is this new?). Now my commits are usually a two step process:

git add -A
git commit -m "Committing all changed and deleted files"

Hope someone finds this useful.

Releasing IQ::Color

At SonicIQ we have a number of RubyGems used internally that we haven’t found time to open-source yet i.e. cleanup the documentation and get into state that people can find useful. I’m going to make it my mission to get some of these out there which first involves releasing some of the smaller Gems that the larger ones depend on.

IQ::Color is a really simple Gem for converting colour values to and from CSS(3) notation, this is used by a couple of our larger Gems but I can see it being of use on it’s own. A simple example:

color1 = IQ::Color.RGB.new(127, 127, 255)
color1.to_css #=> "#7f7fff"
 
color2 = IQ::Color.from_css('rgba(127,127,255,0.4)')
color2.red    #=> 127
color2.green  #=> 127
color2.blue   #=> 255
color2.alpha  #=> 0.4
color2.to_css #=> "rgba(127,127,255,0.4)"

More examples can be found on GitHub.

Rails 3 and the little known :enable_starttls_auto option

I just got the following error from ActionMailer when trying to send to a Gmail address:

OpenSSL::SSL::SSLError (hostname was not match with the server certificate)

After some Googling I found the “enable_starttls_auto” option in ActionMailer, setting this to “false” in the relevant environment file fixed the problem:

  config.action_mailer.smtp_settings = {
    :address => '...',
    :user_name => '...',
    :domain => '...',
    :password => '...',
    :authentication => :login,
    :enable_starttls_auto => false
  }

This option apparently “Enables SMTP/TLS (STARTTLS) for this object if server accepts”. Not being up on my “SMTP/TLS”‘s, I have no idea what this means but it solves the problem.

Hope this saves someone some time.

Fixing {{model}} placeholders in Rails 2.3 when Rails 3 is installed

This was causing me all kinds of grief. I am running Rails 2.3 and Rails 3 apps on the same server and on installing Rails 3, the Rails 2.3 apps started displaying {{model}} and similar strings in views.

It turns out that Rails 3 uses i18n version 0.5.0 and with this version as far as I can see, the placeholder text in translations follow the %{text} format instead of the {{text}} format in 0.4.2 which Rails 2.3 was using. The only way I found to fix this was to declare the specific i18n version before the Rails gem gets required in the “load_rails_gem” method of “config/boot.rb” in my Rails 2.3 apps:

def load_rails_gem
  gem 'i18n', '0.4.2' # Add this line
 
  if version...
end

Doing this specifies that we want i18n 0.4.2 exactly, whereas Rails 2.3 specifies version “>= 0.1.3″ which would obviously includes “0.5.0″.

In running Rails 2.3 and Rails 3 apps side by side, I also ran into this problem and overcame it with the fixes by “bct” and “ronin-95281” in the comments… apparently Rails Core won’t fix this.

If anyone else has a cleaner fix for this I would love to hear about it.

Completely Flattening a Git Repository

Whilst working on a recent project, I ended up with a massive git repository due to frequent commits of large-ish files. As I was the only one working on the project and I was certain I didn’t need anything from the history, I wanted to be able to flatten all the commits in the repository.

I didn’t find exactly what I was after but found a handy tip on Stack Overflow. In essence you re-initialize your local repository and force a push to your remote. A word of warning, if you have anyone else pulling or pushing to the remote repository, they will hate you for this as it will completely screw up any revision history or branches they may be working on.

Here’s my modified version of the tip on Stack Overflow to take into account .gitignore and removing the existing .git repository:

$ cd {{project-directory}}
$ rm -rf .git
$ git init
$ git add .
$ git commit -m 'Initial commit'
$ git remote add origin {{url}}
$ git push --force

p.s. Don’t say I didn’t warn you!