Konubinix' opinionated web of thoughts

Snippets to Ease Code Reviews

Here, I try to share some snippets of code that I use to perform code reviews. In fact, I use those snippet in real life using babel.

You might want to use the workarounds I created in here in case you want to use those.

I assume that you use git and that the repository is already up-to-date.

I try to have sensible git commands that show merges when necessary and won’t bother with spaces or newline.

See From a useless Git Diff to a useful one (Example).

Starting the review

I first need to make sure to go in the repository and that I understand the range of commits I am about to review.

cd "${directory}"
clean () { git notes list|cut -f2 -d ' '|xargs --no-run-if-empty git notes remove ; rm -rf "${patches_directory}" ; }
rm -rf "${patches_directory}"
mkdir -p "${patches_directory}"
hash="$(git hash "${review}")"
basehash="$(git hash "${base}")"
parent="$(git-merge-base ${base} ${hash})"
git notes append "${parent}" -m "Merge base"
git notes append "${hash}" -m "To review"
git notes append "${basehash}" -m "Base"
echo "Ready to make the review of ${review} (${hash}) on top of ${base} (${basehash}). Common ancestor is ${parent}"

Once you know what you want to see, gitk is soo amazing.

gitk "${parent}.." "${hash}" &

gitk --simplify-by-decoration "${parent}.." "${hash}" &

Show the code to review

Show only the patches

git format-patch --src-prefix=../../ --dst-prefix=../../ --output-directory "${patches_directory}" --ignore-space-at-eol --ignore-space-change --ignore-all-space --ignore-blank-lines "${parent}..${hash}"|sed -r "s|^(.+/([^/]+))$|- [ ] [[file+emacs:\1][\2]]|"

clk git format-patch-with-merges --src-prefix=../../ --dst-prefix=../../ --output-directory "${patches_directory}" "${parent}..${hash}" |sed -r "s|^(.+/([^/]+))$|- [ ] [[file+emacs:\1][\2]]|"

git log --src-prefix=../../ --dst-prefix=../../ --reverse -m --patch --cc  "${parent}..${hash}" > "${patches_directory}/log_patch.diff"
echo "${patches_directory}/log_patch.diff"

Show the diffs

git diff --src-prefix=../../ --dst-prefix=../../ --ignore-space-at-eol --ignore-space-change --ignore-all-space --ignore-blank-lines "${parent}..${hash}" > "${patches_directory}/diff.diff"
echo "${patches_directory}/diff.diff"

git diff --src-prefix=../../ --dst-prefix=../../ --ignore-space-at-eol --ignore-space-change --ignore-all-space --ignore-blank-lines "${base}..${hash}" > "${patches_directory}/diff_against_upstream.diff"
echo "${patches_directory}/diff_against_upstream.diff"

Show the code with my additions

My changes only


clk git format-patch-with-merges@sh --src-prefix=../../ --dst-prefix=../../ --output-directory "${patches_directory}" "${hash}..HEAD" |sed -r "s|^(.+/([^/]+))$|- [ ] [[file+emacs:\1][\2]]|"


git diff --src-prefix=../../ --dst-prefix=../../ --ignore-space-at-eol --ignore-space-change --ignore-all-space --ignore-blank-lines "${hash}..HEAD" > "${patches_directory}/mychanges.diff"
echo "${patches_directory}/mychanges.diff"

My changes, along with the reviewed code


clk git format-patch-with-merges@sh --src-prefix=../../ --dst-prefix=../../ --output-directory "${patches_directory}" "${parent}..HEAD" |sed -r "s|^(.+/([^/]+))$|- [ ] [[file+emacs:\1][\2]]|"


git diff --src-prefix=../../ --dst-prefix=../../ --ignore-space-at-eol --ignore-space-change --ignore-all-space --ignore-blank-lines "${parent}..HEAD" > "${patches_directory}/head.diff"
echo "${patches_directory}/head.diff"

git diff --src-prefix=../../ --dst-prefix=../../ --ignore-space-at-eol --ignore-space-change --ignore-all-space --ignore-blank-lines "${base}..${HEAD}" > "${patches_directory}/diff.diff"
echo "${patches_directory}/diff.diff"

To have an idea of the progress

cat "${patches_directory}"/*.patch|wc -l

Cleanup after the code review