Skip to content

Instantly share code, notes, and snippets.

@beenotung
Created July 15, 2019 05:04
Show Gist options
  • Save beenotung/c4c1298701bc83f2617615e1706a4d3c to your computer and use it in GitHub Desktop.
Save beenotung/c4c1298701bc83f2617615e1706a4d3c to your computer and use it in GitHub Desktop.
#!/bin/bash
## use git to compress a single folder or file
## TODO use tmp folder to support operation within a git repo
set -e
set -o pipefail
with_progress=0
filename=''
dest=''
if [ "$1" == "-p" ] || [ "$1" == "--progress" ]; then
with_progress=1
filename="$2"
elif [ "$2" == "-p" ] || [ "$2" == "--progress" ]; then
with_progress=1
filename="$1"
else
filename="$1"
fi
if [ "$filename" == "" ]; then
filename='.'
else
filename=$(echo "$filename" | sed 's,/$,,')
fi
if [ "$filename" == "." ]; then
dest=$(basename "$PWD")
dest="$dest.git.tar.gz"
else
dest="$filename.git.tar.gz"
fi
if [ -d .git ]; then
echo "Error: this directory is already a git repo"
exit 1
fi
if [ -f "$dest" ]; then
echo -n "File $dest already exist, overwrite? [y/N]: "; read line
if [ "$line" != y ]; then
exit 0
fi
fi
git init
if [ $with_progress == 1 ]; then
git add -v "$filename"
git gc
tar cz --checkpoint-action="echo=%T" -f "$dest" .git
else
git add "$filename"
git gc
tar czf "$dest" .git
fi
rm -rf .git
echo "saved to $dest"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment