Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of tgit #99

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ tsearch: ${CONFIG_FOLDER} ${BIN_FOLDER} tyaml
install tsearch/params.yaml ${CONFIG_FOLDER}
@echo "done!"

tgit: ${CONFIG_FOLDER} ${BIN_FOLDER}
@echo "Installing tgit..."
install -m 555 tgit/tgit ${BIN_FOLDER}
install -m 555 tgit/dmenu_tgit ${BIN_FOLDER}
install -m 555 tgit/tgit_status ${BIN_FOLDER}
@echo "done!"

tpomodoro: ${CONFIG_FOLDER} ${BIN_FOLDER}
@echo "Installing tpomodoro..."
install -m 555 tpomodoro/tpomodoro ${BIN_FOLDER}
Expand Down Expand Up @@ -80,7 +87,7 @@ uninstall:
rm -f ${BIN_FOLDER}/tprogbar
@echo "done!"

install: tsearch ttodo tmenu tyaml tnotes tgoeswall tpomodoro tprogbar
install: tsearch ttodo tmenu tyaml tnotes tgoeswall tpomodoro tprogbar tgit
@echo "tinytools installed successfully!"

.PHONY: install tsearch tpomodoro ttodo tmenu tyaml tnotes tgoeswall uninstall tprogbar
.PHONY: install tsearch tpomodoro ttodo tmenu tyaml tnotes tgoeswall uninstall tprogbar tgit
29 changes: 29 additions & 0 deletions tgit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# `tgit`
A simple script to list all the important repositories in your local system.

```
$ tgit
```

## tinytools + dmenu = :heart:

This script supports dmenu as an interface, so you can list and manage your repos quickly through a bindkey:

```
$ dmenu_tgit
```

## dependencies:
- git
- find
- awk

# Team

| <img src="https://github.com/Calebe94.png?size=200" alt="Edimar Calebe Castanho"> | <img src="https://github.com/gbgabo.png?size=200" alt="Gabriel Gaboardi"> |
|:---------------------------------------------------------------------------------:|:-------------------------------------------------------------------------:|
| [Edimar Calebe Castanho (Calebe94)](https://github.com/Calebe94) | [Gabriel Gaboardi (Gabo)](https://github.com/gbgabo) |

# License

All software is covered under [GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.en.html).
24 changes: 24 additions & 0 deletions tgit/dmenu_tgit
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

repo=$(echo "$(tgit)" | dmenu -i -p Repos: | awk '{print $2}')

[[ -n $repo ]] || exit
path="$HOME/$repo"
prompt=$(print "$repo" | awk -F'/' 'NF>1{print $(NF)}') && prompt="$prompt [$(tgit_status $path)]:"
options="open\nstatus\ndiff\ncheckout"
option=$(echo -e "$options" | dmenu -i -p "$prompt")

[[ -n $option ]] || exit
case "$option" in
"checkout")
branches="$(cd $path && git branch -r)"
branch=$(echo -e "$branches" | dmenu -i -p Checkout:)
x-terminal-emulator -e 'sh -c "cd '$path' && git '$option $branch'; $SHELL"'
;;
"open")
codium $path
;;
*)
x-terminal-emulator -e 'sh -c "cd '$path' && git '$option'; $SHELL"'
;;
esac
62 changes: 62 additions & 0 deletions tgit/tgit
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash

#This file is part of the TinyTools distribution (https://github.com/Calebe94/TinyTools).
#Copyright (C) TinyTools

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, version 3 of the License.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.

repos=''
ignore_regex='antigen|vim'

update() {
repos=$(find $HOME -name ".git" | awk -F/ 'BEGIN { OFS = FS } NF{NF-=1};1')
[[ -n $ignore_regex ]] && repos=$(echo -e "$repos" | grep -E -v "$ignore_regex")
touch /tmp/repos.dat
echo -e "$repos" > /tmp/repos.dat
}

get_repos(){
if [ -f "/tmp/repos.dat" ]; then
echo -e "$(cat "/tmp/repos.dat")"
else
update && echo -e "$(cat "/tmp/repos.dat")"
fi
}

list() {
for i in $repos
do
cd $i
if [[ "$(git rev-parse --git-dir)" == ".git" ]]; then
status=$(tgit_status $i $1)
name="${i/"$HOME/"/''}"
list="$list\n[$status] $name"
fi
done
tmpfile=$(mktemp)
echo -e "$list" >> $tmpfile
list=$(column $tmpfile -t)
rm "$tmpfile"
echo -e "$list"
}

repos=$(get_repos)

while getopts uc option
do
case "${option}" in
u) update && exit;;
c) echo -e "$(list -c)" && exit ;;
esac
done
echo -e "$(list)"
122 changes: 122 additions & 0 deletions tgit/tgit_status
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/bin/bash
#
# Git status
#

# ------------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------------
RED='\033[0;31m'
CYAN='\033[0;36m'
LIGHT_GREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

SPACESHIP_GIT_STATUS_SHOW="${SPACESHIP_GIT_STATUS_SHOW=true}"
SPACESHIP_GIT_STATUS_PREFIX="${SPACESHIP_GIT_STATUS_PREFIX=" ["}"
SPACESHIP_GIT_STATUS_SUFFIX="${SPACESHIP_GIT_STATUS_SUFFIX="]"}"
SPACESHIP_GIT_STATUS_COLOR="${SPACESHIP_GIT_STATUS_COLOR="red"}"
SPACESHIP_GIT_STATUS_UNTRACKED="${SPACESHIP_GIT_STATUS_UNTRACKED="?"}"
SPACESHIP_GIT_STATUS_ADDED="${SPACESHIP_GIT_STATUS_ADDED="${LIGHT_GREEN}+${NC}"}"
SPACESHIP_GIT_STATUS_MODIFIED="${SPACESHIP_GIT_STATUS_MODIFIED="${YELLOW}!${NC}"}"
SPACESHIP_GIT_STATUS_RENAMED="${SPACESHIP_GIT_STATUS_RENAMED="»"}"
SPACESHIP_GIT_STATUS_DELETED="${SPACESHIP_GIT_STATUS_DELETED="${RED}✘${NC}"}"
SPACESHIP_GIT_STATUS_STASHED="${SPACESHIP_GIT_STATUS_STASHED="$"}"
SPACESHIP_GIT_STATUS_UNMERGED="${SPACESHIP_GIT_STATUS_UNMERGED="="}"
SPACESHIP_GIT_STATUS_AHEAD="${SPACESHIP_GIT_STATUS_AHEAD="${CYAN}⇡${NC}"}"
SPACESHIP_GIT_STATUS_BEHIND="${SPACESHIP_GIT_STATUS_BEHIND="⇣"}"
SPACESHIP_GIT_STATUS_DIVERGED="${SPACESHIP_GIT_STATUS_DIVERGED="⇕"}"

# ------------------------------------------------------------------------------
# Section
# ------------------------------------------------------------------------------

# We used to depend on OMZ git library,
# But it doesn't handle many of the status indicator combinations.
# Also, It's hard to maintain external dependency.
# See PR #147 at https://git.io/vQkkB
# See git help status to know more about status formats
# spaceship_git_status() {
# [[ $SPACESHIP_GIT_STATUS_SHOW == false ]] && return

# spaceship::is_git || return

INDEX=''
git_status=""

INDEX=$(cd "$1" && command git status --porcelain -b 2> /dev/null)

# Check for untracked files
if $(echo "$INDEX" | command grep -E '^\?\? ' &> /dev/null); then
git_status="$SPACESHIP_GIT_STATUS_UNTRACKED$git_status"
fi

# Check for staged files
if $(echo "$INDEX" | command grep '^A[ MDAU] ' &> /dev/null); then
git_status="$SPACESHIP_GIT_STATUS_ADDED$git_status"
elif $(echo "$INDEX" | command grep '^M[ MD] ' &> /dev/null); then
git_status="$SPACESHIP_GIT_STATUS_ADDED$git_status"
elif $(echo "$INDEX" | command grep '^UA' &> /dev/null); then
git_status="$SPACESHIP_GIT_STATUS_ADDED$git_status"
fi

# Check for modified files
if $(echo "$INDEX" | command grep '^[ MARC]M ' &> /dev/null); then
git_status="$SPACESHIP_GIT_STATUS_MODIFIED$git_status"
fi

# Check for renamed files
if $(echo "$INDEX" | command grep '^R[ MD] ' &> /dev/null); then
git_status="$SPACESHIP_GIT_STATUS_RENAMED$git_status"
fi

# Check for deleted files
if $(echo "$INDEX" | command grep '^[MARCDU ]D ' &> /dev/null); then
git_status="$SPACESHIP_GIT_STATUS_DELETED$git_status"
elif $(echo "$INDEX" | command grep '^D[ UM] ' &> /dev/null); then
git_status="$SPACESHIP_GIT_STATUS_DELETED$git_status"
fi

# Check for stashes
if $(command git rev-parse --verify refs/stash >/dev/null 2>&1); then
git_status="$SPACESHIP_GIT_STATUS_STASHED$git_status"
fi

# Check for unmerged files
if $(echo "$INDEX" | command grep '^U[UDA] ' &> /dev/null); then
git_status="$SPACESHIP_GIT_STATUS_UNMERGED$git_status"
elif $(echo "$INDEX" | command grep '^AA ' &> /dev/null); then
git_status="$SPACESHIP_GIT_STATUS_UNMERGED$git_status"
elif $(echo "$INDEX" | command grep '^DD ' &> /dev/null); then
git_status="$SPACESHIP_GIT_STATUS_UNMERGED$git_status"
elif $(echo "$INDEX" | command grep '^[DA]U ' &> /dev/null); then
git_status="$SPACESHIP_GIT_STATUS_UNMERGED$git_status"
fi

# Check whether branch is ahead
is_ahead=false
if $(echo "$INDEX" | command grep '^## [^ ]\+ .*ahead' &> /dev/null); then
is_ahead=true
fi

# Check whether branch is behind
is_behind=false
if $(echo "$INDEX" | command grep '^## [^ ]\+ .*behind' &> /dev/null); then
is_behind=true
fi

# Check wheather branch has diverged
if [[ "$is_ahead" == true && "$is_behind" == true ]]; then
git_status="$SPACESHIP_GIT_STATUS_DIVERGED$git_status"
else
[[ "$is_ahead" == true ]] && git_status="$SPACESHIP_GIT_STATUS_AHEAD$git_status"
[[ "$is_behind" == true ]] && git_status="$SPACESHIP_GIT_STATUS_BEHIND$git_status"
fi

if [[ "$2" == "-c" ]]; then
echo -e "$git_status"
else
echo $(echo -e "$git_status" | sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g")
fi