Skip to content

Instantly share code, notes, and snippets.

@ariporad
Last active December 31, 2015 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariporad/87ec031a436a1ced102d to your computer and use it in GitHub Desktop.
Save ariporad/87ec031a436a1ced102d to your computer and use it in GitHub Desktop.
Archives

Archives

I frequently find that I have old projects that I don't necessarily want ot delete, but I want to get out of the way. This is my setup to make that easy.

My setup has two parts: A repo (ex. ariporad/archive), and a script (this one):

  • The repo This repo is a collection of orphan branches, one per project. Since some of these projects are private (and I'm lazy), the entire repo is private.
  • The script This script takes the name of a file/folder ($folder) as an argument. It then:
    1. Zipps it into a temporary directory ($tmpdir), and appends the datetime to the folder name.
    2. Creates a new git repo in $tmpdir, and commits everything.
    3. Adds the archive repo as a remote
    4. Tries to push to $branch ($branch = $folder, all lowercase).
    5. If that fails, it: 1. Fetches origin 2. Rebases onto origin/$branch 3. Pushes again (if that fails, display an error and exit).
    6. Deletes $tmpdir

This was really useful for me, and it also cleaned up some hard drive space, which I was lacking. I hope you like it.

MIT licensed.

#!/bin/bash
##
# Archive.sh
# By @ariporad, http://ariporad.com, ari@ariporad.com
#
# Instructions, usage and other information is avalible here: http://git.io/vEFqV.
#
# Licensed under the MIT License: http://ariporad.mit-license.org.
##
##
# Config
##
VERSION="1.0.1"
TMP_DIR=".archive-tmp-$RANDOM`date -u +%s`$$" # Random, PID, Datetime
FOLDER="$1"
CWD=`pwd`
GITHUB_REPO="YOUR_GITHUB_USERNAME/archive"
GIT_BRANCH="$(echo $FOLDER | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)" # https://automatthias.wordpress.com/2007/05/21/slugify-in-a-shell-script/
DATE=`date +%m-%d-%Y-%H%M` # http://stackoverflow.com/a/1401495/1928484
OUTPUT_ZIP="$TMP_DIR/$(basename $FOLDER)@$DATE"
##
# Helpers
##
print() {
printf "$@\n"
}
section() {
print ""
print "=============================================================================="
if [ "$#" -ne "0" ]; then
print "$@..."
print "=============================================================================="
fi
print ""
}
##
# The Code
##
# Exiting
exitBad() {
print "NOT OK."
# http://stackoverflow.com/a/13864829/1928484
if [[ -z "$1" ]]; then
exit $1
else
exit 1
fi
}
exitGood() {
print "OK."
exit 0;
}
# TmpDir
removeTmpDir() {
rm -rf "$TMP_DIR"
}
setupTmpDir() {
section "Making temporary directory"
removeTmpDir
mkdir -p "$TMP_DIR"
}
cdToTmpDir() {
section "\`cd\`-ing into the TmpDir"
cd "$TMP_DIR"
}
cleanup() {
section "Cleanup"
cd "$CWD"
rm -rf "$FOLDER"
rm -rf "$TMP_DIR"
}
# Git
setupGitRepo() {
section "Creating git repo"
git init
git add . || true
MSG="Archive $FOLDER.
On `date`.
By _The Archiver_ (v$VERSION)."
git commit -am "$MSG"
}
setupGitRemotes() {
git remote add origin git@github.com:ariporad/archive.git
}
pushToGithub() {
section "Pushing to GitHub"
setupGitRemotes
# http://stackoverflow.com/a/22010339/1928484
{
git push origin "master:$GIT_BRANCH" &&
print "\n\nPush Successful!"
} || {
print "\n\nERROR! Push Failed! Attempting to fix it..."
section "Attempting to push"
{
git fetch # Get what's already up there
git rebase "origin/$GIT_BRANCH" # Re-play our commit on top of it
git push origin "master:$GIT_BRANCH" &&
print "\n\nPush Successful!"
} || {
cat <<EOF
\n\nPush Failed! We're not really sure why (See http://xkcd.com/1597), but see above
for whatever git said it was.
Run this command to move in to the tmp dir and fix it:
cd $TMP_DIR
When you're done, run:
cd $CWD && rm -rf $TMP_DIR
EOF
exitBad
}
}
}
# Moving
moveProject() {
section "Zipping \`$FOLDER\` to \`$OUTPUT_ZIP\`"
zip -r "$OUTPUT_ZIP" "$FOLDER"
}
##
# GO‽‽‽
##
clear
# This is `cat` because `<<EOF` goes to stdin.
cat <<EOF
___________.__
\__ ___/| |__ ____
| | | | \_/ __ \
| | | Y \ ___/
|____| |___| /\___ >
\/ \/
_____ .__ .__
/ _ \_______ ____ | |__ |__|__ __ ___________
/ /_\ \_ __ \_/ ___\| | \| \ \/ // __ \_ __ \\
/ | \ | \/\ \___| Y \ |\ /\ ___/| | \/
\____|__ /__| \___ >___| /__| \_/ \___ >__|
\/ \/ \/ \/
Welcome to the Archiver!
This utility makes it easy to archive a project you no longer need!
It works if it ends with 'OK'.
Here's what's about to happen:
1. The project (a.k.a the folder \`$1\`) will get zipped in to a
temporary directory (./$OUTPUT_ZIP).
2. That dir will then be pushed to an orphan branch called \`$GIT_BRANCH\`
in \`$GITHUB_REPO\` on GitHub. (\`$GITHUB_REPO#$GIT_BRANCH\`).
3. \`./$1\` will be moved to trash.
EOF
section
read -p "Press [Enter] to begin..."
################################################################################
setupTmpDir
moveProject
cdToTmpDir
setupGitRepo
pushToGithub
cleanup
section "Done"
exitGood
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment