A git alias that inserts a random emoji in the commit message
Premise
Let’s write a git alias that would insert a random Emoji at the beginning of a git commit message. Both Github and Bitbucket support emojis, so why not?
Obtaining the emojis
There’s the wonderful cheat sheet for all the supported emojis, so I’ll use that. But first, we’ll need a little gem.
I used Nokogiri to parse the HTML on the cheat sheet site, but you may be more comfortable with the (almost dead) hpricot.
So, let’s fire up IRB and write the following:
Now that we have the page, let’s look at the data we need to get to. Open the developer tools and find in what way can we get access to the names of the emojis.
As it turns out, all the data we need has the name
class. So, we can obtain an array of all the emojis by writing the following query:
What we do here is take all the nodes that have a name
class and receive a Nokogiri::XML::NodeSet
. It turns out that the NodeSet
includes the Enumerable
, so we can map
it. To get the content from a Nokogiri Node, we just have to send content
to the node and using the symbol-to-proc, we get to the line of code shown above.
The next step is to write the emojis to a file. Easy:
We now have a file that contains all the emojis separated by spaces. Let’s move on.
Picking the emoji
Since most users don’t have Ruby on their systems, the script has to be written in Bash. Well, I wouldn’t mind learning some Bash anyway.
So, the aim is to select a random emoji from this list of strings.
Let’s create a file emoji.sh
in the home dir and put the hashbang in it:
Now we need to initialize an array of words and count them. Let’s start by:
Save and run using source emoji.sh
. If you see one of the four up there, everything’s great. Now let’s take all the data and put them in here (we don’t want a lot of files just laying around). What we get is similar to this:
And that’s it! If you don’t want to go through all these steps, here’s the final script.
The git alias itself
That’s the easiest one: find the .gitconfig
file in your home directory (or create one) and write:
[alias]
cj = !bash -c 'git commit -m \":`. ~/emoji.sh`: $0\"'
This requires some explanation:
- We invoke a
bash
command using!bash -c '...'
. Actually, it would be more useful te rewrite the script usingsh
, so that any system would support it. - We do the regular
git commit -m "..."
. Since we need to pass several words tocommit
, we escape the quotes. - By now we have
!bash -c 'git commit -m \"...\"'
- Inside the string we need to have: the emoji surrounded by colons and the commit message, which is sent as an argument to the
git cj
. So, the command will now look like!bash -c 'git commit -m \":<obtain_emoji>: $0\"'
($0 yields the first argument.) - Now let’s obtain the emoji. Since we’re in
bash
now and the script is also in bash, we can use thesource
command, or.
for short. The script echoes, so upon sourcing it we’ll get a string and that’s what we need! - And this is how we obtain the alias we wrote above.
Here’s an example of how it looks like on GitHub:
That’s it! We now have a git alias that would insert random emojis in the commits. That’s utterly pointless, but it’s fun!