commit 5a4832582a880febbcd07093af1ede1dd58b69fc Author: Aaron Fischer Date: Mon Dec 6 15:01:12 2021 +0100 Minimal working POC diff --git a/README.md b/README.md new file mode 100644 index 0000000..985e7cd --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# c.sh + +## Installation + +``` +cd /opt/tools/c/ +git clone https://git.okoyono.de/f/c.sh.git +cd c.sh +ln -s motd.sh /etc/profile.d/ +cp gitignore /.gitignore +echo "source /opt/tools/c/zsh.config" >> /root/.zshrc + +git init repo +cd repo +git config pager.diff false +git config advice.addIgnoredFile false +git config core.worktree / +``` + +## Usage + +``` +c help +``` + diff --git a/c.sh b/c.sh new file mode 100644 index 0000000..8f604a7 --- /dev/null +++ b/c.sh @@ -0,0 +1,66 @@ +#!/usr/bin/env bash + +[[ $# -eq 0 ]] && echo "no subcommand given" && exit 1 + +WORKDIR="$(dirname "$(realpath "$0")")" +PWD=`pwd` + +RESOURCE_TO_TRACK="" +[[ $1 = "track" && $# -eq 2 ]] && RESOURCE_TO_TRACK="$(realpath "$2")" + +cd $WORKDIR + +case $1 in + "commit") + shift + MESSAGE="$*" + [[ "$MESSAGE" = "" ]] && MESSAGE="config.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 "pull (pull the changes from git)" + echo "push (push the changes to git)" + echo "track (add a file or folder to git)" + echo "diff (show unpushed changes)" + ;; +esac + diff --git a/gitignore b/gitignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/gitignore @@ -0,0 +1 @@ +* diff --git a/motd.sh b/motd.sh new file mode 100644 index 0000000..ccf2f25 --- /dev/null +++ b/motd.sh @@ -0,0 +1,4 @@ +if [[ "$USER" = "root" ]]; then + /opt/tools/server-config/c.sh log +fi + diff --git a/zsh.config b/zsh.config new file mode 100644 index 0000000..4842d85 --- /dev/null +++ b/zsh.config @@ -0,0 +1,20 @@ +CSTATUS="" +update_cstatus() { + git_stat=`cd /opt/tools/server-config/; git diff --shortstat | tr -c -d '[0-9,]'` + if [[ "$git_stat" = "" ]]; then + CSTATUS="" + else + stats=(${(@s:,:)git_stat}) + CSTATUS="[%F{green}+${stats[2]}%f/%F{red}-${stats[3]}%f]" + fi +} + +autoload -U add-zsh-hook +add-zsh-hook preexec update_cstatus +add-zsh-hook precmd update_cstatus + +setopt PROMPT_SUBST +PS1='%T $CSTATUS %(?.%F{green}√.%F{red}?%?)%f %F{#666}%~%f %# ' + +alias c="/opt/tools/server-config/c.sh" +