Created
March 29, 2018 15:07
-
-
Save taikedz/2cff8c3bf0302bc910841f9e53645fe6 to your computer and use it in GitHub Desktop.
Extract the full contents of the files changed during a given commit
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 | |
### Extract the changed files of a commit Usage:help | |
# | |
# Given a commit, extract the full file contents of that commit, and write them to the destination directory (outside the repo) | |
# | |
# extractor-git.sh {copy|cat} COMMIT DESTDIR | |
# | |
# 'cat' simply cats the data, but no file metadata is preserved. No change is made to the repo state | |
# | |
# 'copy' switches to the target branch/commit and copies files over to the destination, then switches back | |
# | |
###/doc | |
do_cat() { | |
# +++++++++++++++++++++++++++++++++++++++ | |
# Cat each to destination | |
echo "$files" | while read targetfile; do | |
mkdir -p "$dest/$(dirname "$targetfile")" | |
git show "$commit":"$targetfile" > "$dest/$targetfile" | |
done | |
} | |
do_copy() { | |
# +++++++++++++++++++++++++++++++++++++++ | |
# Checkout, copyover | |
curbranch="$(git status|grep -oP "On branch .+"|sed 's/On branch //')" | |
git checkout "$commit" || { | |
echo "Could not switch to target commit - try stashing, or use 'cat' mode" | |
exit 1 | |
} | |
echo "$files" | while read targetfile; do | |
mkdir -p "$dest/$(dirname "$targetfile")" | |
cp "$targetfile" "$dest/$targetfile" | |
done | |
git checkout "$curbranch" | |
} | |
if [[ ! -d ./.git ]]; then | |
echo "You must run this script from the root of the repo" | |
exit 2 | |
fi | |
action="$1" | |
commit="$2" | |
dest="$3" | |
# All files changed in the commit | |
files="$( git log -n 1 "$commit" --name-only |tail -n +2 | grep -vP '^(\s|[a-zA-Z]+:|$)' )" | |
"do_$action" "$commit" "$dest" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment