c.sh/c.sh

67 lines
1.3 KiB
Bash

#!/usr/bin/env bash
[[ $# -eq 0 ]] && echo "no subcommand given" && exit 1
WORKDIR="$(dirname "$(realpath "$0")")/../repo"
PWD=`pwd`
RESOURCE_TO_TRACK=""
[[ $1 = "track" && $# -eq 2 ]] && RESOURCE_TO_TRACK="$(realpath "$2")"
cd $WORKDIR
case $1 in
"commit")
shift
MESSAGE="$*"
[[ "$MESSAGE" = "" ]] && MESSAGE="c.sh commit from $HOSTNAME"
git add --all
git commit -m "$MESSAGE"
;;
"track")
if [[ $# -ne 2 ]]; then
echo "no file or folder given"
exit 1
fi
# Check if there is a .git folder. If so, we need to temporary
# rename it, so we can track the file/folder.
GIT_DIR=
TRAVERSAL_PATH="$RESOURCE_TO_TRACK"
while [[ "$TRAVERSAL_PATH" != "/" ]]; do
if [[ -d "$TRAVERSAL_PATH/.git" ]]; then
GIT_DIR="$TRAVERSAL_PATH/.git"
break
fi
TRAVERSAL_PATH="$(dirname $TRAVERSAL_PATH)"
done
[[ "$GIT_DIR" != "" ]] && mv "$GIT_DIR" "$GIT_DIR.keep"
git add -v -f "$RESOURCE_TO_TRACK"
[[ "$GIT_DIR" != "" ]] && mv "$GIT_DIR.keep" "$GIT_DIR"
;;
"diff")
git status -s
git diff -p
;;
"revert")
echo "Not implemented"
;;
"log")
echo "Last changes:"
git log --pretty=format:' * %cr: %s' | head -3
;;
*)
echo "commit (save snapshot of the current state)"
echo "track (add a file or folder to git)"
echo "diff (show unpushed changes)"
echo "log (show last commits)"
;;
esac