Skip to content

Instantly share code, notes, and snippets.

@l0b0
Created August 8, 2012 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l0b0/3294978 to your computer and use it in GitHub Desktop.
Save l0b0/3294978 to your computer and use it in GitHub Desktop.
Git bisect example
#!/usr/bin/env bash
# Example of git-bisect use
# Commented code can be copied to verify the state of the code
# Alice creates a repository with a simple Bourne/Dash script
cd -- "$(mktemp -d)"
git init
> test.sh cat <<"EOF"
if [ "`id -u`" -ne 0 ]
then
echo 'Hi user'
fi
EOF
git add test.sh
git commit -m 'Initial commit'
# bash test.sh # Prints 'Hi user'
# sh test.sh # Prints 'Hi user'
# Bob adds some functionality
git branch --track root_test master
git checkout root_test
patch -p0 <<"EOF"
--- test.sh.orig
+++ test.sh
@@ -1,4 +1,6 @@
if [ "`id -u`" -ne 0 ]
then
echo 'Hi user'
+else
+ echo 'Hi root'
fi
EOF
git commit -am 'Print a message for root as well'
# bash test.sh # Prints 'Hi user'
# sh test.sh # Prints 'Hi user'
# sudo bash test.sh # Prints 'Hi root'
# sudo sh test.sh # Prints 'Hi root'
# Cyril adds a bashism
git branch --track bashism master
git checkout bashism
patch -p0 <<"EOF"
--- test.sh.orig
+++ test.sh
@@ -1,4 +1,4 @@
-if [ "`id -u`" -ne 0 ]
+if [[ "`id -u`" -ne 0 ]]
then
echo 'Hi user'
fi
EOF
git commit -am 'Bashified'
# bash test.sh # Prints 'Hi user'
# sh test.sh # Syntax error!
# Alice merges Bob and Cyril's changes
git checkout master
git merge root_test
git merge bashism
# bash test.sh # Prints 'Hi user'
# sh test.sh # Syntax error, and prints the wrong message 'Hi root'!
# sudo bash test.sh # Prints 'Hi root'
# sudo sh test.sh # Syntax error, but prints 'Hi root'
# Alice continues working
patch -p0 <<"EOF"
--- test.sh.orig
+++ test.sh
@@ -1,6 +1,6 @@
if [[ "`id -u`" -ne 0 ]]
then
- echo 'Hi user'
+ echo "Hi $USER"
else
echo 'Hi root'
fi
EOF
git commit -am 'Print user name instead of literal "user"'
# At this point, it's discovered that the script no longer works with plain sh
# Alice proceeds to bisect
git bisect start HEAD HEAD~3
# sh test.sh # Succeeds, so she marks the commit as good
git bisect good
# sh test.sh # Fails, so she marks the commit as bad
git bisect bad
# At this point, git points out that we've found the offending revision:
# Bisecting: 0 revisions left to test after this (roughly 0 steps)
# [SHA-1 ID] Bashified
git bisect reset
# This could also be done with an automatic bisect
# For that we need a test script
> bisect.sh cat <<"EOF"
sh test.sh | grep -q 'Hi user'
EOF
git bisect start HEAD HEAD~3
git bisect run sh bisect.sh
# This runs bisect.sh to test if the commits are valid, and gives the same
# result as the manual bisect
git bisect reset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment