Last active
April 4, 2022 13:26
-
-
Save Hobart2967/045fe878a9c2a3514e2147518631fdc7 to your computer and use it in GitHub Desktop.
gitmemory.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| GIT_MEMORY_CONFIG=~/.bash/resources/.gitmemory.json | |
| alias oldcd=cd | |
| reset-git-memory() { | |
| rm -rf $GIT_MEMORY_CONFIG | |
| } | |
| #region Setup Memory | |
| if ! command -v jq &> /dev/null; then | |
| echo "jq is not installed. Please install it." | |
| else | |
| if [ ! -f $GIT_MEMORY_CONFIG ]; then | |
| echo "$RESET\n\tGit Memory: No $GIT_MEMORY_CONFIG file found. Creating one." | |
| echo '{ "repositories": [] }' > $GIT_MEMORY_CONFIG | |
| fi | |
| remember_git() { | |
| if [ ! -d '.git' ]; then | |
| return; | |
| fi | |
| REPO_PATH=$1 | |
| JQ_REQUEST=".repositories | index(\"$REPO_PATH\")" | |
| INDEX_OF_REPO=$(jq "$JQ_REQUEST" $GIT_MEMORY_CONFIG) | |
| if [ "$INDEX_OF_REPO" = "null" ]; then | |
| echo "Git Memory: Found new repository at $REPO_PATH\n" | |
| jq ".repositories += [\"$REPO_PATH\"]" $GIT_MEMORY_CONFIG > $GIT_MEMORY_CONFIG.tmp | |
| mv $GIT_MEMORY_CONFIG.tmp $GIT_MEMORY_CONFIG | |
| fi | |
| } | |
| cd_gitmemory(){ | |
| oldcd "$@"; | |
| remember_git $@; | |
| } | |
| alias cd='cd_gitmemory' | |
| remember_git $PWD; | |
| fi | |
| #endregion | |
| function gitmemory() { | |
| if [ -z "$1" ]; then | |
| echo "Usage: gitmemory ls|rm|reset" | |
| return | |
| fi | |
| if [ "$1" = "ls" ]; then | |
| jq ".repositories[]" $GIT_MEMORY_CONFIG | |
| elif [ "$1" = "rm" ]; then | |
| if [ -z "$2" ]; then | |
| echo "Usage: gitmemory rm <repo>" | |
| return | |
| fi | |
| REPO_PATH=$2 | |
| JQ_REQUEST=".repositories | index(\"$REPO_PATH\")" | |
| INDEX_OF_REPO=$(jq "$JQ_REQUEST" $GIT_MEMORY_CONFIG) | |
| if [ "$INDEX_OF_REPO" = "null" ]; then | |
| echo "Git Memory: $REPO_PATH not found in $GIT_MEMORY_CONFIG" | |
| return | |
| fi | |
| echo "Git Memory: Removing $REPO_PATH from $GIT_MEMORY_CONFIG" | |
| jq "del(.repositories[] | select(. == \"$REPO_PATH\"))" $GIT_MEMORY_CONFIG > $GIT_MEMORY_CONFIG.tmp | |
| mv $GIT_MEMORY_CONFIG.tmp $GIT_MEMORY_CONFIG | |
| elif [ "$1" = "reset" ]; then | |
| reset-git-memory | |
| else | |
| echo "Usage: gitmemory ls|rm|reset" | |
| fi | |
| } | |
| function yesterworkday() { | |
| # Monday or Sunday | |
| if [[ "1" == "$(date +%u)" ]] || [[ "7" == "$(date +%u)" ]] | |
| then | |
| echo "last friday.midnight" | |
| else | |
| echo "yesterday.midnight" | |
| fi | |
| } | |
| echo -e "\n" | |
| printf %"$COLUMNS"s |tr " " "-" | |
| echo -e "\n\n\t$YELLOW Git Memory: Things that you have done since last workday at midnight: $RESET\n" | |
| SINCE=$(yesterworkday) | |
| HAS_COMMITS=0 | |
| for row in $(jq -r '.repositories[]' $GIT_MEMORY_CONFIG); do | |
| if [ ! -d "$row" ] || [ ! -d "$row/.git" ]; then | |
| echo -e "\t $bg[red]$fg[white] ⚠ $(basename $row) ⚠ $RESET ➭ seems to be gone! $RESET Is this still a repository you want to check?" | |
| continue; | |
| fi | |
| COMMIT_HISTORY=$( (cd $row && git log --since="$SINCE" --branches --abbrev-commit --pretty="format:%ci\t%B") ) | |
| if [ ! -z "$COMMIT_HISTORY" ]; then | |
| echo -e "\t$fg[cyan] ➭ $(basename $row)$RESET in $(dirname $row):\n" | |
| HAS_COMMITS=1 | |
| fi | |
| echo $COMMIT_HISTORY | while read -r commit; do | |
| if [[ -z "$commit" ]]; then | |
| continue | |
| fi | |
| echo "$GREEN\t\t ➦ $commit" | |
| done | |
| done | |
| echo -e "$YELLOW" | |
| if [ $HAS_COMMITS -eq 0 ]; then | |
| echo -e "\t\t$RED ⨻ Nothing ⨻ $YELLOW\n" | |
| fi | |
| printf %"$COLUMNS"s |tr " " "-" | |
| echo -e "\n$RESET" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment