Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to put 2 folders into a single changeset git and diff it using ksdiff to see the changeset nicely
#!/bin/sh
# ks_changeset v0.9 - 2022-05-03
# Enable "safe" mode - see http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
if [[ $# -lt 3 ]]; then
echo "Usage: ks_changeset.sh <FolderA> <FolderB> <DestinationGit>"
exit 1
fi
FOLDER_A=$1
FOLDER_B=$2
TARGET_GITDIR=$3
if [[ -d "$TARGET_GITDIR" ]]; then
echo "Error: Destination git already exists ($TARGET_GITDIR)"
exit 1
fi
rem_dsstores() {
find . -name '.DS_Store' -type f -delete
}
echo "Comparing '$FOLDER_A' \n to '$FOLDER_B' \nby creating a one changeset git repo at '$TARGET_GITDIR'"
echo ""
# create and initialize the git repo
mkdir "$TARGET_GITDIR"
cd "$TARGET_GITDIR"
git init --initial-branch compare
# create an empty commit to use as baseline
git commit --allow-empty -m "Empty" -q
git tag Empty
# cd back the previous directory for next copy operation
# protip ~+ expands to $PWD ~- to $OLDPWD
cd ~-
echo "Copying $FOLDER_A ..."
# copy the first folder (note the trailing slash on source is important to only copy the contents, not the folder)
cp -a "${FOLDER_A}/" "$TARGET_GITDIR"
# add the file to a changeset and commit
cd "$TARGET_GITDIR"
rem_dsstores
git add .
git commit -m "Contents of $FOLDER_A" -q
git tag FolderA
# clean out everything
git reset Empty -- .
git clean -f -d -q
echo "Copying $FOLDER_B ..."
# cd back the previous directory for next copy operation
cd ~-
# copy in folder b and prepare the diff
cp -a "${FOLDER_B}/" "$TARGET_GITDIR"
cd "$TARGET_GITDIR"
rem_dsstores
git add .
git commit -m "Contents of $FOLDER_B" -q
git tag FolderB
git difftool FolderA..FolderB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment