Skip to content

Instantly share code, notes, and snippets.

@mowat27
Created October 28, 2022 10:48
Show Gist options
  • Save mowat27/d8002fa17f79db760fda8aeff45236ca to your computer and use it in GitHub Desktop.
Save mowat27/d8002fa17f79db760fda8aeff45236ca to your computer and use it in GitHub Desktop.
Code Archaeology Notes
#!/usr/bin/env bash
# Useful git commands using the rails codebase as an example:
# git clone [email protected]:rails/rails.git
# and my own example later
# git clone [email protected]:mowat27/maths.git
# You'll also need bat (brew install bat)
# ------------------------------------------------------------------------------
# 1. What happened?
cd /Users/adrian/desktop/rails
# What's changed recently
git log
# First ever commit
git log --reverse
# Abbreviated history
git log --oneline
# For a range of commits
git log cdb51080b1..b4029ba4a9 --oneline
# Since a commit
git log cdb51080b1.. --oneline
# What changed?
# 5b0680437c is interesting, commit message was :golf:
git show 5b0680437c
# Why does a file exist?
# .yardopts is new to me, why is it there?
# see all the commits against a file
git log .yardopts
# now we can dig into one
git show e555388e1603f9915051b873b18bf60642bc3e85
# or we can abbreviate
git show e555388e16
# ------------------------------------------------------------------------------
# 2. Who Changed What?
# It might be useful to see who was the most recent person to change
# each line in a file
git blame .yardopts
# or for a bigger file we can zoom in
# e.g to a method in
# activerecord/lib/active_record/tasks/database_tasks.rb
git blame -L 63,77 activerecord/lib/active_record/tasks/database_tasks.rb
# Looks like something changed in 2018:
#
# a2827ec9811 (eileencodes 2018-01-10 10:25:13 -0500 65) current = ActiveRecord::Base.connection.migration_context.current_environment
# a2827ec9811 (eileencodes 2018-01-10 10:25:13 -0500 66) stored = ActiveRecord::Base.connection.migration_context.last_stored_environment
#
# so let's have a look
git log a2827ec9811
# ------------------------------------------------------------------------------
# 3. When did it break?
cd /Users/adrian/desktop/example
# The script does simple maths
./maths.rb + 1 2 3
./maths.rb - 1 2 3
./maths.rb / 4 2
# The problem is that multiply is always returning 0
./maths.rb x 2 4
# Looking at the code - it's a bit obscure!
bat --plain maths.rb
# What happened?
git log --oneline
# Who did it?
git blame maths.rb
# When did it break?
git bisect start
git bisect bad
git bisect good 52a9b0c
# repeatedly
./maths.rb x 2 4
git bisect # [good|bad]
# until
# afa85672c8f2b1a41a920e9be162646018b775f0 is the first bad commit
# Lands you at the last good commit so we can see what changed
git show afa85672c8f2b1a41a920e9be162646018b775f0
# Get out of bisect mode
git bisect reset
# Alternatively
# We can just run a script
git bisect start
git bisect bad
git bisect good 52a9b0c
git bisect run tmp/try_multiply.sh
# 3.1 Looking for the first working commit in rails
# Get the first commit on inflector
git log --oneline --reverse activesupport/lib/active_support/inflector.rb
# Write a script that checks for good and bad
bat --plain tmp/try_inflector.rb
# Confusingly we need to say invert good and bad
# the working version is bad and a broken version is good
git bisect start
git bisect bad
git bisect good dc3d6eb9b4
# Run the script
tmp/try_inflector.rb
# and git bisect good|bad
# ad nauseum
# Automate
git bisect run tmp/try_inflector.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment