Skip to content

Instantly share code, notes, and snippets.

@Geri-Borbas
Created July 12, 2013 17:03
Show Gist options
  • Select an option

  • Save Geri-Borbas/5986026 to your computer and use it in GitHub Desktop.

Select an option

Save Geri-Borbas/5986026 to your computer and use it in GitHub Desktop.
Export every version of a given file from a Git repository.
#!/bin/sh
# Usage:
#
# export_snapshots 'eppz!alert/EPPZAlert.m'
# This will export every revision of the given file to 'Snaphots' folder with a filename scheme below.
#
# Snaphots/0_4a7cb92_BRViewController.m
# Snaphots/1_6cb85c7_BRViewController.m
# Snaphots/2_5550187_BRViewController.m
# ...and so on.
#
# export_snapshots
# This will export every revision of the whole repository to folders similar to above
#
# Snaphots/0_4a7cb92/
# Snaphots/1_6cb85c7/
# Snaphots/2_5550187/
# ...and so on.
#
# Get parameters
DESTINATION_PATH='Snapshots'
FILE_PATH=$1
FILE_NAME=$(basename $FILE_PATH)
echo 'Exporting <'$FILE_NAME'> located in <'$FILE_PATH'> to <'$DESTINATION_PATH'>.'
# Make 'Snapshots' directory if not exist.
if [ ! -d "$DESTINATION_PATH" ]; then
mkdir $DESTINATION_PATH
fi
# Switch (read parameters later on)
if [ 'a' = 'a' ]
then
# Enumerate commits
COUNTER=0
for COMMIT in $(git log --oneline $FILEPATH | cut -f 1 -d " "); do
COMMIT_FILE_PATH=$FILE_PATH
COMMIT_DESTINATION_PATH=$DESTINATION_PATH
# Checkout given file
echo 'Checking out commit ('$COUNTER') <'$COMMIT'> file <'$COMMIT_FILE_PATH'> then move to <'$COMMIT_DESTINATION_PATH'>.'
git checkout $COMMIT $COMMIT_FILE_PATH
# Copy to 'Snapshots'
cp $FILE_PATH $DESTINATION_PATH/$COUNTER'_'$COMMIT'_'$FILE_NAME
COUNTER="$(expr "$COUNTER" '+' '1')"
done
# Checkout original state
echo 'Checking out <HEAD> file <'$COMMIT_FILE_PATH'> to discard previous checkouts.'
git checkout HEAD $COMMIT_FILE_PATH
else
# Enumerate commits
for COMMIT in $(git log --oneline | cut -f 1 -d " "); do
COMMIT_DESTINATION_PATH=$DESTINATION_PATH'/'$COUNTER'_'$COMMIT
# Clone whole revision to a given folder (would be great)
echo 'Checking out commit ('$COUNTER') <'$COMMIT'> to <'$COMMIT_DESTINATION_PATH'>.'
COUNTER="$(expr "$COUNTER" '+' '1')"
done
fi
# Happyend
echo 'Done.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment