Skip to content

Instantly share code, notes, and snippets.

View wteuber's full-sized avatar

Wolfgang Teuber wteuber

View GitHub Profile
@wteuber
wteuber / mysql-install.sh
Created May 28, 2019 07:53 — forked from cemeng/mysql-install.sh
Codeship Install MySQL 5.6
#!/bin/bash
# Install a custom MySQL 5.7 version - https://www.mysql.com
#
# To run this script on Codeship, add the following
# command to your project's setup commands:
# \curl -sSL https://raw.githubusercontent.com/codeship/scripts/master/packages/mysql-5.7.sh | bash -s
#
# Add the following environment variables to your project configuration
# (otherwise the defaults below will be used).
# * MYSQL_VERSION
@wteuber
wteuber / colored_git_diff_html.sh
Created May 27, 2019 12:59
Colored git word diff
# Mac OS X:
script -q colored_word_diff.txt git diff --word-diff
cat colored_word_diff.txt|ansi2html > colored_word_diff.html
@wteuber
wteuber / redirection.txt
Created August 17, 2018 10:33
All about redirection of terminal/file output.
|| visible in terminal || visible in file || existing
Syntax || StdOut | StdErr || StdOut | StdErr || file
==========++==========+==========++==========+==========++===========
> || no | yes || yes | no || overwrite
>> || no | yes || yes | no || append
|| | || | ||
2> || yes | no || no | yes || overwrite
2>> || yes | no || no | yes || append
|| | || | ||
&> || no | no || yes | yes || overwrite
@wteuber
wteuber / benford_generator.rb
Last active September 20, 2021 15:29
Random number generator with Benford's law distribution, fmi see https://en.wikipedia.org/wiki/Benford%27s_law and https://rosettacode.org/wiki/Benford%27s_law#Ruby
r=rand;(1..9).map{|n|Math.log10(1+1.0/n)}.inject([]){|a,e|a+[(a[-1]||0)+e]}.map.with_index{|p,i|[i+1,p]}.find{r<_2}[0]
@wteuber
wteuber / trace.rb
Created August 17, 2017 21:58
set_trace_func
set_trace_func -> (event, file, line, id, binding, classname) do
if event == 'call' && meth = binding.eval('__method__')
params = binding.method(meth).parameters.select{|e| e[0] != :block}
values = params.map{|_, var| [var, binding.local_variable_get(var)]}
printf "%8s %s:%-2d %15s %8s %s\n", event, file, line, id, classname, values.inspect
else
printf "%8s %s:%-2d %15s %8s\n", event, file, line, id, classname
end
end
@wteuber
wteuber / rubocop_stable_auto_correct.rb
Created June 2, 2017 10:51
Script to check & commit all stable auto-corrections of Rubocop
require 'fileutils'
# TODO get list of Cops that support auto-correct
# for each cop, run the following
# only commit if change was stable
# (each run takes ~2 minutes)
prefix = ARGV[0]
files = Dir["#{prefix}/**/*.rb"].sort
orig = Hash[files.map do |file|
FileUtils.cp(file, "#{file}.orig")
[file, %x(ruby --dump insns #{file}).gsub(/\s+\(\s*\d+\)$/, '')]
@wteuber
wteuber / mov2gif.sh
Last active January 27, 2017 10:31
Convert mov to gif (made for QuickTime Player screen recordings)
#! /usr/bin/env bash
if [ -f "$1" ]; then
echo "ffmpeg -i $1 -vcodec copy -acodec copy $1.mp4 ..."
ffmpeg -i $1 -vcodec copy -acodec copy $1.mp4
else
echo "ERROR: Could not find $1"
fi
if [ \( $? -eq 0 \) -a \( -f $1.mp4 \) ]; then
@wteuber
wteuber / memo.rb
Created January 11, 2017 11:19 — forked from LeFnord/memo.rb
ruby memoization benchmarks
require "benchmark"
class A
def name
@name ||= begin
rand
end
end
end

Subtree merging and you

If you’re using Git, you’re probably aware of submodules. They’re useful when you want to integrate and track another Git repository into your own. But there are two other options at your disposal.

The first one is to simply merge the other repository. This works fine as long as there are no conflicting files, but chances are there already are or will be in the future, which makes this option not too useful.

This is where the subtree merge strategy comes in. It allows you to merge another repository (let’s call it “Project B”) into your own, but into its own subdirectory. It takes a bit of effort to set everything up the first time, but after that a pull from the other repository is all that is needed to update.

Here’s how you set it up. First you need to add Project B as a remote:

@wteuber
wteuber / yaml_cmp.rb
Created September 5, 2016 07:21 — forked from carlosveucv/yaml_cmp.rb
Compare 2 yaml's in ruby (print matching keys)
require 'yaml'
def compare_yaml_hash(cf1, cf2, context = [])
cf1.each do |key, value|
if cf2.key?(key)
puts "Contains key : #{key} in path #{context.join(".")}"
end
value2 = cf2[key]
next unless value2