Skip to content

Instantly share code, notes, and snippets.

@MarcusJohnson91
Forked from magnetikonline/README.md
Created December 30, 2020 10:28
Show Gist options
  • Save MarcusJohnson91/a34a0447e047eb928e9fb227f075f63c to your computer and use it in GitHub Desktop.
Save MarcusJohnson91/a34a0447e047eb928e9fb227f075f63c to your computer and use it in GitHub Desktop.
Extract all files at every commit of a Git repository.

Extract all files at every commit of a Git repository

Bash script to iterate all commits of a given Git repository and extract all files within each commit.

Example

With a Git repository at /path/to/repository and an empty directory at /path/to/output we can run:

./export.sh /path/to/repository /path/to/output

Export 1d9048853b8073e43e43e3250300a82f93d2f431 -> /path/to/output/1d9048853b8073e43e43e3250300a82f93d2f431
Export 5bbaa3bb20b8cb2517b12e9bfb6bc7aef666b9a9 -> /path/to/output/5bbaa3bb20b8cb2517b12e9bfb6bc7aef666b9a9
Export fc56f7e038c272fbdd5e609c508de10056e74278 -> /path/to/output/fc56f7e038c272fbdd5e609c508de10056e74278
Export 75355bae79b22481007e4be13d13a387b7b1df0b -> /path/to/output/75355bae79b22481007e4be13d13a387b7b1df0b
Export d9e4a5742359610028597061babc19d76a937936 -> /path/to/output/d9e4a5742359610028597061babc19d76a937936

Target path /path/to/output will now contain directories named as each Git commit SHA1.

#!/bin/bash -e
function getRepoSHA1List {
pushd "$1" >/dev/null
git rev-list --all
popd >/dev/null
}
function exportCommits {
local commitSHA1
local IFS=$'\n'
for commitSHA1 in $(getRepoSHA1List "$1"); do
# build export directory for commit and create
local exportDir="${2%%/}/$commitSHA1"
echo "Export $commitSHA1 -> $exportDir"
mkdir --parents "$exportDir"
# create archive from commit then unpack to export directory
git \
--git-dir "$1/.git" \
archive \
--format tar \
"$commitSHA1" | \
tar \
--directory "$exportDir" \
--extract
done
}
# verify arguments
if [[ (! -d $1) || (! -d $2) ]]; then
echo "Usage: $(basename "$0") GIT_DIR OUTPUT_DIR"
exit
fi
if [[ ! -d "$1/.git" ]]; then
echo "Error: it seems [$1] is not a Git repository?" >&2
exit 1
fi
# execute
exportCommits "$1" "$2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment