Skip to content

Instantly share code, notes, and snippets.

@dgfitch
Created April 28, 2015 17:58
Show Gist options
  • Save dgfitch/dd97efc062a4f64a99cb to your computer and use it in GitHub Desktop.
Save dgfitch/dd97efc062a4f64a99cb to your computer and use it in GitHub Desktop.
git-checkpoint: Script to allow non-git-experts to use git with a single command, "git checkpoint"
#!/bin/sh
# git-checkpoint
# ==============
# Minimal version tracking with git using a single command.
#
# Intended to ease people new to git into easy versioning
# in a single server repository location, but they can also
# expand on their knowledge and use other git commands as
# they learn.
#
# INSTALL:
# -------
# Put it somewhere, and then run
# git config --global alias.checkpoint '!sh /path/to/checkpoint'
#
# USAGE:
# -----
# git checkpoint <optional message>
# Creates a git repo in the current directory if
# you are not already in a git tree.
# Commits all file changes with the given message.
#
# FEATURES:
# --------
# - Very few!
# - Wraps git very simply
# - On init, warns user if directory is bigger than it seems like it should be
# - On init, warns user if there's already a .git in a subdirectory
# - Warns user if size of a file in commit is larger than expected
#
# EXAMPLE:
# -------
# {you are in a random directory of files, all alike}
# % git checkpoint I like this stuff
# => creates a git repo and commits
# % vim README.md
# % git checkpoint added a readme
# => commits again
#
# {THE END, HOORAY}
# Dan Fitch - 20150428
# First, we check if a repo exists
if `git rev-parse > /dev/null 2>&1`; then
echo "Repository detected"
else
# Check that the directory isn't huge
actualsize=$(du -sm "." | cut -f 1)
if [ $actualsize -ge 5 ]; then
echo "The current directory is more than 5mb. That doesn't seem very scripty."
echo "If you're sure you want to create a repository here, run \`git init\` manually."
exit 1
fi
# Check no subdirectories have a .git repo
countgits=$(find . -name ".git" | wc -l)
if [ $countgits -ge 1 ]; then
echo "WARNING! You are trying to create a git repo in a folder which has git stuff"
echo "down inside it already. If you are sure you want to do this, run \`git init\` manually."
echo "If you want to get fancy, you may want to investigate git-submodule or git-subtree."
exit 2
fi
echo "No git repository detected, initializing here in $(pwd)"
git init -q
fi
# We store the current directory and go to the top level
current=$(pwd)
cd "$(git rev-parse --show-toplevel)"
# Check if any of the files being added are huge
to_stage=$(git status -s | cut -c 4-)
for f in $to_stage;
do
actualsize=$(du -m "$f" | cut -f 1)
if [ $actualsize -ge 2 ]; then
echo "Whoa, you are trying to commit $f which is $actualsize megabytes."
echo "If you are sure you want to do this, do \`git add $(pwd)/$f\`."
exit 7
fi
done
git add -A .
message="$@"
if [ -z "$message" ]; then
message="Checkpoint"
fi
git commit -m "$message"
cd $current
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment