Skip to content

Instantly share code, notes, and snippets.

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 briantully/780d9a560769e3e830f6fe5d68c4d316 to your computer and use it in GitHub Desktop.
Save briantully/780d9a560769e3e830f6fe5d68c4d316 to your computer and use it in GitHub Desktop.
Checks for shared file modifications within a commit range
#!/usr/bin/env bash
#
# Checks the configured commit range to determine which commits modify files
# that were modified in an earlier commit.
#
# Example:
# Commit 1:
# foo.txt
# bar.txt
# Commit 2:
# bar.txt
# Commit 3:
# foo.txt
# Commit 4:
# foobar.txt
#
# Commit 2 will be considered dependent on Commit 1.
# Commit 3 will be considered dependent on Commit 1, but not 2.
# Commit 4 will not be considered to have any dependencies.
#
# This is a very shallow check which does not account for functional or thematic
# dependencies. It should be considered a starting point for further
# investigation.
#
# Usage:
# git-overlap start_commit end_commit
#
# Example:
# git-overlap develop feature/my-new-feature
set +x
declare -a COMMITS=($(git log --pretty='format:%h' ${1}..${2}))
declare -i LEN=${#COMMITS[@]}
indent() { sed 's/^/ /'; }
function compare_commit_files() {
local PARENT_FILES=$(git show --name-only ${1} | tail -r | awk '1;/^$/{exit}' | awk 'NF' | sort)
local CHILD_FILES=$(git show --name-only ${2} | tail -r | awk '1;/^$/{exit}' | awk 'NF' | sort)
local MODIFIED=$(comm -12 <(echo "${PARENT_FILES}") <(echo "${CHILD_FILES}"))
[[ "${MODIFIED}" ]]
}
echo "Checking the following commits for overlapping changes:"
echo
echo " ${COMMITS[@]}"
echo
echo
for (( index = 0; index < $(($LEN-1)); index ++ )); do
declare -a DEPENDS_ON=()
CURRENT=${COMMITS[ $index ]}
echo -n "Parent ${CURRENT} -> "
for (( child_index = $(($index + 1)); child_index < $LEN; child_index ++ )); do
CURRENT_CHILD=${COMMITS[$child_index]}
echo -n "${CURRENT_CHILD} "
compare_commit_files $CURRENT $CURRENT_CHILD
if [[ $? -eq true ]]; then
DEPENDS_ON+=("${CURRENT_CHILD}")
fi
done
echo
if [[ ${#DEPENDS_ON[@]} -gt 0 ]]; then
echo
echo "${CURRENT} shares changes with ${DEPENDS_ON[@]}" | indent
echo
for depend in ${DEPENDS_ON[@]}; do
git log --format=%B -n 1 ${depend} | awk 'NF' | indent | indent
done
echo
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment