Skip to content

Instantly share code, notes, and snippets.

@LarryRuane
Created January 14, 2020 15:41
Show Gist options
  • Save LarryRuane/333441994b36b36fee597082e93790b4 to your computer and use it in GitHub Desktop.
Save LarryRuane/333441994b36b36fee597082e93790b4 to your computer and use it in GitHub Desktop.
bash script to demonstrate how a reviewer can see what changed after a force-push
#!/usr/bin/env bash
#
# This script demonstrates how a reviewer can see only what's
# changed after a developer has overwritten a commit by doing a
# force-push to correct a bug or make a reviewer-requested change.
# The reviewer doesn't really want to have to re-review the entire
# change; it may be hard to pick out exactly what was force-pushed.
#
# The key idea is for the reviewer to make a temporary branch before
# re-fetching the developer's branch.
#
# This script is stand-alone, self-contained, and doesn't require or
# interact with github, but is intended for use with github (or
# equivalent).
set -e # exit on error
rm -rf review-force-push-demo
mkdir review-force-push-demo
cd review-force-push-demo
git init
# This commit simulates history before the PR, and is not
# considered part of the PR.
echo hi version 1 > hello
git add .
git commit -m'first commit, v1'
# Here is the first PR commit.
echo hi version 2 but this has a bug > hello
git add .
git commit -m'second commit, v2'
# A reviewer found a bug, should makes a temporary branch:
git branch -c tmp
# The developer wants to fix it the bug without adding a
# separate commit, instead will re-do the second commit and
# force-push to github (github not simulated here).
# Developer removes ("pops off") the most recent commit,
# fixes the bug, and force-pushes (force-push not done here):
git reset HEAD~1
echo hi new version 2, bug fixed > hello
git add .
git commit -m'revised second commit, v2'
# git push -f # to github
# Reviewer would like to see just the fix -- the commit may be
# quite large, and difficult to see what's changed since the
# last review.
git checkout tmp
prevhash=$(git log --pretty=format:'%H' -n 1)
git checkout master
latesthash=$(git log --pretty=format:'%H' -n 1)
# This generates a diff of only the bug fix, the earlier and
# later versions of the second commit!
git diff $prevhash..$latesthash
# output:
#
# diff --git a/hello b/hello
# index 39ca47e..458f91c 100644
# --- a/hello
# +++ b/hello
# @@ -1 +1 @@
# -hi version 2 but this has a bug
# +hi new version 2, bug fixed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment