Minimal working POC

This commit is contained in:
Aaron Fischer 2021-12-06 15:01:12 +01:00
commit 5a4832582a
No known key found for this signature in database
GPG key ID: CB8E2DBACD93CE77
5 changed files with 116 additions and 0 deletions

25
README.md Normal file
View file

@ -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
```

66
c.sh Normal file
View file

@ -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

1
gitignore Normal file
View file

@ -0,0 +1 @@
*

4
motd.sh Normal file
View file

@ -0,0 +1,4 @@
if [[ "$USER" = "root" ]]; then
/opt/tools/server-config/c.sh log
fi

20
zsh.config Normal file
View file

@ -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"