Friday, October 18, 2013

Lolcommits at Play

A continuation of my previous post, Lolcommits at Work...

Sharing is caring. Why stash all these awesome Lolcommits away in your hard drive, never to see the light of day? Let's upload our gifs to the GitHub repo and include links to them in our commit messages!

(Okay, maybe not for shared work repositories, but personal projects? Heck yeah!)

Perusing the issues page on Lolcommits, I came across much of the code to do this. Here's how:

Your post-commit file
You could throw all the code into the post-commit file itself, but that would get too complicated when the day comes that I want to execute multiple scripts following a git commit. Instead, I'll just call append-lol.rb. Yes, that's right, you can write git hooks in Ruby!

You don't need lines 2 and 3 if you're not using Ruby Version Manager. If you are using RVM, make sure you use something you actually have installed.

On line 4, put in the path to your repo's hooks folder /append-lols.rb.

#!/usr/bin/env bash
source "$HOME/.rvm/scripts/rvm"
rvm use ruby-2.0.0-p247
ruby ~/dev/rails_projects/demo_app/.git/hooks/append-lols.rb
view raw post-commit hosted with ❤ by GitHub


append-lols.rb
Finally, you can use this append-lols.rb file as is. Save it in your repo's .git/hooks folder. I've commented extensively so that you may follow along.

#Retrieve your most recent git commit hash and message.
#Anything inside backticks will execute as a command in your shell
#The backtick is located above the tab key on your keyboard
commit = `git log -1 --oneline`
#Isolate the hash and the message.
hash = commit.to_s.split(" ").first
message = commit.sub("#{hash} ", "")
#Unless the commit message already has a link to a gif...
unless(message.gsub("\n","").end_with?(".gif"))
#Create a gif with lolcommits!
`lolcommits --capture --animate=3`
#Find the path to your present working directory, the location
#of your repo on GitHub, your present working directory name
#and the path to which Lolcommits saves your gifs.
repopath = `git rev-parse --show-toplevel`.gsub("\n", "")
repourl = `git config --get remote.origin.url`.gsub("\n", "")
reponame = "#{`basename #{repopath}`.to_s}".gsub("\n", "")
path = "#{File.expand_path('~')}/.lolcommits/#{reponame}/"
#Find the gif with a filename that begins with your latest
#commit hash and create a directory in your repo for storing
#lols and copy in the gif
@filename = nil
Dir.open(path).each do |filename|
if filename.start_with?(hash)
image = path + filename
system("mkdir #{repopath}/lolcommits")
`cp #{image} #{repopath}/lolcommits/#{filename}`
@filename = filename
end
end
#Grab your username and repo name, assemble the url GitHub
#will save the gif to git add the gif and git commit with
#an amended message
unless @filename == nil
githubstuff = repourl.gsub("git@github.com:","").gsub(".git","").gsub("https://github.com/","")
github_image_url = "https://github.com/#{githubstuff}/blob/master/lolcommits/#{@filename}"
`git add #{repopath}/lolcommits/#{@filename}`
`git commit --amend --no-verify -m "#{message} #{github_image_url}"`
end
end
view raw append-lols.rb hosted with ❤ by GitHub
Now you're ready to commit! Once you've pushed, you should have a shiny new lolcommits folder in your repository's root and a lovely commit message linking to your gif, like this one!

No comments:

Post a Comment