Skip to content

Instantly share code, notes, and snippets.

View avdgaag's full-sized avatar

Arjan van der Gaag avdgaag

View GitHub Profile
@avdgaag
avdgaag / DojoS2E6.sh
Last active December 10, 2015 09:39
Shell one-liner to create a histogram from a log file.
export COL=$COLUMNS; awk -F '[ ()]|ms' '/\[CatalogItem\] Retrieved/ { c[int($8 / $6 / 100) * 100 + 50] += $6 } END { for(l in c) { print l, c[l] } }' production.log | awk 'BEGIN { max = 0 } { if($2 > max) max = $2; c[$1] = $2 } END { for(l in c) { printf("%5d %d\n", l, c[l] / max * (ENVIRON["COL"] - 7)) } }' | awk '{ r = ""; s = $2; while(s-- > 0) { r = r "#" }; printf("%6d %s\n", $1, r); }' | sort -n
@avdgaag
avdgaag / auto_reference_pt_ids.rb
Last active December 15, 2015 02:39
Auto-labeling Git commits with Pivotal Tracker IDs found in the current branch name. This script is meant to be used as a Git `commit-msg` hook. It expects a single argument, the location of the temporary file with the commit message the user entered, and it outputs that same message with maybe some extra labels added to it.
#!/usr/bin/env ruby
# ## Introduction
#
# Auto-labeling Git commits with issues tracker IDs found in the current
# branch name. This script is meant to be used as a Git commit-msg hook. It
# expects a single argument, the location of the temporary file with the commit
# message the user entered, and it outputs that same message with maybe some
# extra labels added to it.
#
# ## Example
@avdgaag
avdgaag / post-checkout.sh
Created March 19, 2013 19:30
Git post-checkout hook that automatically updates your bundled gems (if applicable), re-generates your tags index using exuberant ctags, and tries to advise you about pending migrations.
#!/bin/sh
set -e
OLDREV=$1
NEWREV=$2
IS_BRANCH_CHECKOUT=$3
function has_changes_to() {
git diff --name-only "$OLDREV" "$NEWREV" -- "$1" | grep "$1" > /dev/null
@avdgaag
avdgaag / gist:6865420
Last active December 24, 2015 21:29
Run Rubocop using the project settings on modified files in your Git repository. Store this in `.git/hooks/pre-commit` to reject commits with style offsenses.
#!/bin/bash
git diff --cached --name-status --diff-filter=ACM | awk '/\.rb$/ { print $2 }' | xargs rubocop -f s
exit_code=$?
if [[ $exit_code != 0 ]] ; then
echo 'Your commit was rejected because Rubocop found style guide violations.'
echo 'Run `rake rubocop` to test your code and inspect the offenses Rubocop'
echo 'has found.'
echo
echo 'If you really, REALLY want to commit this code, skip your pre-commit'
echo 'hooks with `git commit --no-verify`.'
@avdgaag
avdgaag / gist:7427844
Created November 12, 2013 09:10
Git pre-commit hook to reject commits that contain Git conflict markers.
#!/bin/sh
# Add this to .git/hooks/pre-commit to reject any commits
# that contain Git conflict markers.
if git diff --cached --name-only --diff-filter=ACM | xargs grep -H -n -E '^(<|>|=|\|){7}' > /dev/null; then
echo "\033[31mWarning:\033[0m You have left some Git conflict markers in."
echo 'Your commit is bad and you should feel bad.'
echo
git diff --cached --name-only --diff-filter=ACM | xargs grep -H -n -E '^(<|>|=|\|){7}'
exit 1
@avdgaag
avdgaag / things.sh
Last active January 20, 2023 17:50
Command-line read-only interface to your local Things database of to do items.
#!/bin/bash
#
# DESCRIPTION
#
# Simple read-only comand-line interface to your Things 2 database. Since
# Things uses a SQLite database (which should come pre-installed on your Mac)
# we can simply query it straight from the command line.
#
# We only do read operations since we don't want to mess up your data.
#
@avdgaag
avdgaag / issue-completion.vim
Created July 28, 2014 18:26
Autocomplete Github issue numbers from Vim using a custom completion function, Ruby and the Github API.
function! MyOmniFunc(findstart, base)
if a:findstart
let existing = matchstr(getline('.')[0:col('.')-1],'#\d*$')
return col('.')-1-strlen(existing)
endif
ruby << EOF
require 'open-uri'
require 'json'
def issues(repo, token)
@avdgaag
avdgaag / gist:6dc26d8fbba2cd4ce458
Last active August 29, 2015 14:09
Page objects in Ruby for use with Capybara
class ProductForm
attr_reader :page, :form
def initialize(page)
@page = page
@form = page.find('form.new_product')
end
def title=(new_title)
form.fill_in 'product_title', with: new_title
@avdgaag
avdgaag / literate.rb
Created March 4, 2015 20:00
AsciiDoc filter for literate programming-style code blocks.
#!/usr/bin/env ruby
require 'open3'
require 'tmpdir'
module Literate
class Line
INDENT = ''.freeze
def self.indent(str)
@avdgaag
avdgaag / gist:c66ad5868d8e699dd639
Last active September 22, 2015 11:56
Example setup API for ROM::Lotus
# Lotus has different applications in a single project, optionally with different configurations and/or mappings.
# ROM::Lotus should/could conigure a different setup per application, rather than a single global setup.
# Relations could be global (defined in ./lib) while mappings might live in both (./apps/web/ and ./lib).
# This would resemble how Lotus::Model is organised.
#
# The only problem here is that Lotus has no hooks for loading framework (it's all hard-coded)s, and
# therefore we can't easily extend the application load process with our custom setup code without
# resorting to monkeypatching.
# apps/web/application.rb