Last active
October 13, 2019 11:57
-
-
Save kickthemooon/6ec1ed9343ea306ab6b0283a8a92f57a to your computer and use it in GitHub Desktop.
Bash script to create a two way diff list of files between 2 git branches. It filters out files from the list which are not present in the current branch.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
cd ${0%/*} | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
--branch-one=*) | |
SOURCE_BRANCH="${1#*=}" | |
;; | |
--branch-two=*) | |
DESTINATION_BRANCH="${1#*=}" | |
;; | |
--output-path=*) | |
OUTPUT_PATH="${1#*=}" | |
;; | |
--grep-regex=*) | |
GREP_REGEX="${1#*=}" | |
;; | |
--path-prefix=*) | |
PATH_PREFIX="${1#*=}" | |
;; | |
*) | |
echo "ERROR: invalid arguments, required --branch-one=? --branch-two=? --output-path=?, optional --path-filters=?,?,?.. --path-prefix=? " | |
exit 1 | |
esac | |
shift | |
done | |
############ | |
# VALIDATE # | |
############ | |
if [[ -z "${SOURCE_BRANCH}" ]]; then | |
echo "ERROR: argument --branch-one=? is required" | |
exit 1 | |
fi | |
if [[ -z "${DESTINATION_BRANCH}" ]]; then | |
echo "ERROR: --branch-two=? is required" | |
exit 1 | |
fi | |
if [[ -z "${OUTPUT_PATH}" ]]; then | |
echo "ERROR: --output-path=? is required" | |
exit 1 | |
fi | |
if [[ ! -d $(dirname ${OUTPUT_PATH}) ]]; then | |
echo "ERROR: --output-path=? $(dirname ${OUTPUT_PATH}) does not exist" | |
exit 1 | |
fi | |
########### | |
# PREPARE # | |
########### | |
TMP_DIR="/tmp/${RANDOM}" | |
mkdir -p ${TMP_DIR} | |
OUTPUT="${OUTPUT_PATH}" | |
SOURCE_TARGET="${TMP_DIR}/source-target.txt" | |
TARGET_SOURCE="${TMP_DIR}/target-source.txt" | |
TWO_WAY="${TMP_DIR}/diff-two-way.txt" | |
DUPLICATES_FILTERED="${TMP_DIR}/duplicates-filtered.txt" | |
GREP_FILTER=".*" | |
if [[ ! -z "${GREP_REGEX}" ]]; then | |
GREP_FILTER="${GREP_REGEX}" | |
fi | |
##################### | |
# EXTRACT DIFF LIST # | |
##################### | |
git fetch | |
# changes feature-branch against develop | |
git diff --name-only ${DESTINATION_BRANCH} ${SOURCE_BRANCH} -- \ | |
| grep -E "${GREP_FILTER}" \ | |
> ${SOURCE_TARGET} | |
# changes develop against feature-branch | |
git diff --name-only ${SOURCE_BRANCH} ${DESTINATION_BRANCH} -- \ | |
| grep -E "${GREP_FILTER}" \ | |
> ${TARGET_SOURCE} | |
cat ${SOURCE_TARGET} > ${TWO_WAY} | |
cat ${TARGET_SOURCE} >> ${TWO_WAY} | |
# remove duplicates | |
awk '!a[$0]++' ${TWO_WAY} > ${DUPLICATES_FILTERED} | |
# filter files that don't exist in current branch | |
> ${OUTPUT} | |
for file in `cat ${DUPLICATES_FILTERED}` | |
do | |
if [[ -f $(pwd)/../../${file} ]]; then | |
echo ${file} | awk -v PREFIX="${PATH_PREFIX}" '{print PREFIX$1}' >> ${OUTPUT} | |
fi | |
done | |
rm -rf ${TMP_DIR} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/kickthemooon/6ec1ed9343ea306ab6b0283a8a92f57a#file-two-way-diff-sh-L98
Adjust line 98 to your git repo root directory