Skip to content

Instantly share code, notes, and snippets.

@whylom
whylom / gist:4383284
Created December 26, 2012 21:29
How many commits did we push to master in 2012?

Get the first commit of 2012

$ git log --pretty=format:'%H' --after="2011-12-31" | tail -1
d41d8cd98f00b204e9800998ecf8427e

Count the number of commits since then (inclusive)

$ git rev-list d41d8cd98f00b204e9800998ecf8427e^..HEAD | wc -l 
44555
@whylom
whylom / gist:3018099
Created June 29, 2012 13:58
git purge (delete unmerged branches starting with "_")

When using Git, I find myself doing a lot of what I call "exploratory merging" into a throwaway local branch. This is to preview what a scary merge might look like master, or to isolate a bug in a staging environment by selectively merging in certain of the branches that have been deployed.

The names of my throwaway branches always start with an underscore, so I can easily see them when I git branch. Below is a Bash one-liner (aliased as git purge) that deletes unmerged branches whose names start with "_".

[alias]
  purge   = !git branch --no-merged | tr -d "* " | grep ^_ | xargs git branch -D
@whylom
whylom / README.md
Created April 23, 2012 18:01
How to install Graphite on Vagrant with Chef

Here are some quick notes on installing Graphite on a Vagrant virtual machine using Chef recipes from GitHub. This more or less assumes you know what you're doing with Vagrant and Chef.

Notes

  • I used the "lucid32" Ubuntu base box you see in most Vagrant examples (http://files.vagrantup.com/lucid32.box)
  • Where possible, I used the cookbooks from Opscode. I was forced to use alternatives for Python (did not work for me) and for Graphite (no Opscode cookbook as of now).
  • The Graphite recipe depends on the Python, Apache2, and runit recipes.
  • I'm running the apt recipe first to update the Ubuntu package manager. The out-of-date manager that ships with the lucid32 box errored out for me when running the Python recipe.

Cookbooks Used

@whylom
whylom / gist:2432236
Created April 20, 2012 22:07
leggo: idea for a utility to kill a process that's using a specific port
$ leggo 3306
Port 3306 is being used by "mysqld"
process 66117 | started 4:53PM | up for 0:01.25
/usr/local/Cellar/mysql/5.5.20/bin/mysqld
--basedir=/usr/local/Cellar/mysql/5.5.20
--datadir=/usr/local/var/mysql
--plugin-dir=/usr/local/Cellar/mysql/5.5.20/lib/plugin
@whylom
whylom / gist:2431034
Created April 20, 2012 18:57
Count the files and lines of code in a directory tree.
# Shell out to the fast and reliable `wc` utility.
def lines(path)
# output of wc (returned by backticks) is formatted like this:
# " 111 filename.ext"
`wc -l #{path}`.strip.split(' ').first.to_i
end
# **/* gets all files and directories in all subdirectories.
files = Dir["/path/to/directory/**/*"]
@whylom
whylom / gist:2361395
Created April 11, 2012 18:58
Flavors of Ruby string interpolation
# First, some variables!
a, b = %w(foo bar)
# Good old-fashioned string interpolation.
"#{a} is not #{b}" #=> "foo is not bar"
# Today, I learned that this craziness works!
"%s is not %s" % [a, b] #=> "foo is not bar"
@whylom
whylom / gist:2322771
Created April 6, 2012 20:39
How to access a remote MySQL server via an SSH tunnel

For security reasons, many hosting providers restrict external access to the default MySQL port 3306. But if you have SSH access to the database server, you can access the MySQL server from your local machine by using an SSH tunnel. An SSH tunnel forwards bits back & forth between a port on your machine and a port on the remote machine, all lovingly encrypted with SSH.

Here's how you do it! But first, my assumptions:

  1. You already have configured SSH access to the remote server.
  2. You want to access port 3306 on the remote server.
  3. You want to access port 3307 on your local machine. This is simply because many devs (myself included) routinely have MySQL running on port 3307.

Here is the Unix command to open an SSH tunnel to hostname_or_ip with the user account username

@whylom
whylom / gist:2322673
Created April 6, 2012 20:23
Dump a MySQL schema without the data.
mysqldump --no-data -u root -ppw funky_database > funky_database.sql
@whylom
whylom / blink.js
Created April 3, 2012 18:22
How to start and stop a light from blinking.
@whylom
whylom / gist:2139733
Created March 20, 2012 18:53
Grep an array of words (with captures!)
words = ["Life", "Liberty", "and", "the", "pursuit", "of", "Happiness"]
words.grep(/^[A-Z]/)
#=> ["Life", "Liberty", "Happiness"]
words.grep(/^([A-Z])/) { $1 }
#=> ["L", "L", "H"]