dotfiles/.config/shell/projects

95 lines
3.1 KiB
Plaintext
Raw Normal View History

2021-11-23 12:04:09 +00:00
# A tool for managing multiple git projects quickly.
# Simply source this in your bashrc or zshrc.
# This assumes a common directory that contains all projects.
# Subfolders are detected as well.
# You can set $PROJECTS before or after sourcing this script, or adjust it below.
2021-05-05 06:55:44 +00:00
# The regular command is "p status" to update all projects and show their status.
2020-03-08 19:18:49 +00:00
# Common root for all projects
2021-11-23 12:04:09 +00:00
PROJECTS=${PROJECTS:-$DATA}
2020-03-08 19:18:49 +00:00
2021-05-05 06:56:44 +00:00
# The max depth to search for when listing projects.
2021-11-23 12:04:09 +00:00
# The actual depth is this value minus one,
# since it searches for ".git" folders at that depth.
_projects_subfolder_level=5
2020-03-08 19:18:49 +00:00
2021-11-23 12:04:09 +00:00
# Lists all projects under the current directory or $PROJECTS if none are found.
# Searches for ".git" folders and lists their parent directories.
2020-03-08 19:18:49 +00:00
listprojects() {
2021-11-23 12:04:09 +00:00
find $1 -mindepth 1 -maxdepth $_projects_subfolder_level -type d \
-path "${1:-.}/*/.git" -exec dirname {} \; -o \
-name "_*" -o -name ".*" -prune ||
2022-04-28 11:02:26 +00:00
{ test $# -eq 0 && listprojects $PROJECTS; }
2020-03-08 19:18:49 +00:00
}
# Underline the project names
highlight() { echo "$1"; }
2020-03-08 19:18:49 +00:00
2021-11-23 12:04:09 +00:00
# Open or select a project
project() {
cd $PROJECTS
if [ -d $2 ]
then cd "$2" && git origin "$@"
else git get "$@"
fi
}
# Lists all projects and evaluates the given command.
2020-03-08 19:18:49 +00:00
projects() {
2021-05-05 06:55:44 +00:00
test "$1" = "--all" && all="$1" && shift
2020-03-08 19:18:49 +00:00
case $1 in
2021-11-23 12:04:09 +00:00
("build")
2021-05-05 06:55:44 +00:00
# Builds all found gradle projects in parallel and then prints the results in batches
# WARNING: This is likely to considerably slow down your computer!
2020-03-08 19:18:49 +00:00
listprojects $all | while read d; do
if test "$(find $d -maxdepth 1 -name "*gradle*")"; then
builtin cd $d
2020-03-08 19:18:49 +00:00
buildlog="/tmp/build-$(basename $d)"
(nohup gradle build --no-daemon --console=rich &>$buildlog; highlight $(basename $d | awk '{print toupper($0)}') && cat $buildlog) &
fi
done
wait
;;
2021-11-23 12:04:09 +00:00
("diffs")
2020-03-08 19:18:49 +00:00
# Puts the diffs of all projects into a single diffs.txt in the current directory
listprojects $all | while read d; do
echo $(basename $d)>>diffs.txt
git -C $d diff -G.>>diffs.txt
printf "\n">>diffs.txt
done
;;
2021-11-23 12:04:09 +00:00
("status")
2020-03-08 19:18:49 +00:00
# Sets the current branch upstream to a remote branch of the same name, updates it and shows "git status -s -b"
listprojects $all | while read d; do
builtin cd $d
2020-03-08 19:18:49 +00:00
highlight $(basename $d)
git branch --set-upstream-to=origin/$(git curbranch)>/dev/null
git pull --all | grep -v "Already up to date."
git status -s -b
done
;;
2021-11-23 12:04:09 +00:00
(*)
# A one-line summary of the status of each project
# and execute the command entered on every project
2020-03-08 19:18:49 +00:00
com="$@"
listprojects $all | while read d; do
builtin cd $d
2020-03-08 19:18:49 +00:00
if [[ $com != gradle* ]] || (( $(f1 -name "*gradle*" | wc -l) > 0 )); then
2022-04-28 11:02:26 +00:00
#local st=()
declare -a st
2020-03-08 19:18:49 +00:00
local ahead=$(command git rev-list "${branch_name}"@{upstream}..HEAD 2>/dev/null | wc -l)
test $ahead -gt 0 && st+=("ahead $ahead")
local behind=$(command git rev-list HEAD.."${branch_name}"@{upstream} 2>/dev/null | wc -l)
test $behind -gt 0 && st+=("behind $behind")
local stashes=$(git stash list | wc -l)
test $stashes -gt 0 && st+=("stashed $stashes")
2021-05-05 06:55:44 +00:00
test "$st" && name+=" [$(IFS=, eval 'JOINED="${st[*]}"' && echo $JOINED)]"
2020-03-08 19:18:49 +00:00
highlight $(basename $d)
eval "$com"
fi
done
esac
2021-11-23 12:04:09 +00:00
builtin cd $PROJECTS
2020-03-08 19:18:49 +00:00
}
2022-04-28 11:02:26 +00:00