gis/gis

341 lines
11 KiB
Bash
Executable file

#!/usr/bin/env bash
# Text formatting variables
text_reset=$'\e[0m'
text_bold=$'\e[1m'
text_blue=$'\e[34m'
text_green=$'\e[32m'
text_magenta=$'\e[35m'
text_red=$'\e[31m'
text_yellow=$'\e[33m'
function print_usage {
cat <<EOF
Usage: gis [OPTIONS] [COMMAND]
Show a status summary of all Git repositories which are found recursively in
current work directory. The colon colon-separated environment variable
\$GIS_PATH or the '-p' argument can be used to modify the search path.
All 'fetch' and 'pull' operations are executed in parallel. The number of
background jobs can be limited with the environment variable \$GIS_JOBS or the
'-j' argument.
COMMANDS
fetch Execute 'git fetch --prune --all' for all found repositories
pull Execute 'git pull --recurse-submodules' for all found repositories
which are behind upstream, includes 'gis fetch'
OPTIONS
-j, --jobs N Limit 'fetch' and 'pull' commands to N parallel jobs
-p, --path PATH Use PATH for searching Git repositories
-h, --help Show this help message and exit
EOF
}
function error {
echo -e "${text_bold}${text_red}ERROR${text_reset} $1\n"
print_usage
exit 1
}
function execute_git_command_on_list {
IFS=";" read -r -a git_dirs <<< "$2"
for dir in "${git_dirs[@]}"; do
cd "$dir" || echo "Failed to cd into ${text_bold}${text_red}${dir}${text_reset}"
# Get repository name
repository_name=$(basename "$dir")
# Execute 'fetch' or 'pull'
if [ "$1" == "fetch" ]; then
output=$(git fetch --prune --all --porcelain &> >(sed "s/^/${text_bold}${text_blue}${repository_name}${text_reset} /"))
[ "$?" != 0 ] && echo -e "${text_bold}${text_blue}${repository_name}${text_reset} ${text_bold}${text_red}ERROR${text_reset} while fetching\n${output}"
elif [ "$1" == "pull" ]; then
git pull --recurse-submodules > >(sed "s/^/${text_bold}${text_magenta}${repository_name}${text_reset} /")
fi
done
}
# Parse arguments
jobs=0
fetch=false
pull=false
while (( "$#" )); do
case "$1" in
-j|--jobs)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
jobs="$2"
shift 2
else
error "Argument for ${text_bold}$1${text_reset} is missing"
fi
;;
-p|--path)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
paths+=("$(realpath "$2")")
shift 2
else
error "Argument for ${text_bold}$1${text_reset} is missing"
fi
;;
-h|--help)
print_usage
exit 0
;;
-*)
error "Unsupported option ${text_bold}$1${text_reset}"
;;
fetch)
fetch=true
shift
;;
f*)
error "Unsupported command ${text_bold}$1${text_reset}, did you mean ${text_bold}fetch${text_reset}?"
;;
pull)
fetch=true
pull=true
shift
;;
p*)
error "Unsupported command ${text_bold}$1${text_reset}, did you mean ${text_bold}pull${text_reset}?"
;;
*)
error "Unsupported command ${text_bold}$1${text_reset}"
;;
esac
done
# Add $GIS_PATH, current Git repository or current work directory to paths if none is given
if [ "${paths[*]}" == "" ]; then
if [ "$GIS_PATH" ]; then
OLDIFS=$IFS
IFS=":" read -r -a paths <<< "$GIS_PATH"
IFS=$OLDIFS
else
paths=("$(pwd)")
fi
fi
# Find Git repositories
git_dirs=()
for path in "${paths[@]}"; do
# Throw error if search path is not a directory
[ ! -d "$path" ] && error "Search path ${text_bold}${path}${text_reset} is not a directory"
# Find all Git repositories
OLDIFS=$IFS
IFS=$'\n' read -r -d '' -a found_git_dirs < <(find "$path" -type d -name ".git" -exec dirname {} \; | sort)
IFS=$OLDIFS
# Check if inside of a repository if none found
if [ "${#found_git_dirs[@]}" -eq 0 ]; then
if git rev-parse > /dev/null 2>&1; then
found_git_dirs+=("$(git rev-parse --show-toplevel)")
fi
fi
git_dirs+=("${found_git_dirs[@]}")
done
# Set default number of jobs if argument or $GIS_JOBS wasn't set
if [ "$jobs" == 0 ]; then
if [ "$GIS_JOBS" ]; then
jobs="$GIS_JOBS"
else
jobs="${#git_dirs[@]}"
fi
fi
if ! [[ "$jobs" =~ ^[0-9]+$ ]]; then
error "Number of parallel jobs must be a positive number"
fi
# Fetch Git repositories
if [ "$fetch" == true ]; then
if [ "${#git_dirs[@]}" -eq 1 ]; then
suffix="y"
else
suffix="ies"
fi
echo "${text_bold}${text_blue}Fetching${text_reset} ${#git_dirs[@]} repositor${suffix}"
# Create n lists of repositories where n is the number of parallel jobs
declare -a fetch_lists
for i in "${!git_dirs[@]}"; do
list_index=$((i % jobs))
if [ "${fetch_lists[$list_index]}" == "" ]; then
fetch_lists[list_index]="${git_dirs[$i]}"
else
fetch_lists[list_index]="${fetch_lists[$list_index]};${git_dirs[$i]}"
fi
done
# Fetch all lists in background
for fetch_list in "${fetch_lists[@]}"; do
execute_git_command_on_list "fetch" "${fetch_list}" &
fetch_pids+=("$!")
done
# Wait for fetching of all lists
for pid in "${fetch_pids[@]}"; do
wait "$pid"
done
echo
fi
# Pull Git repositories
if [ "$pull" == true ]; then
# Get Git repositories which are behind upstream
for dir in "${git_dirs[@]}"; do
cd "$dir" || echo "Failed to cd into ${text_bold}${text_red}${dir}${text_reset}"
branch_status=$(git status --short --branch --porcelain | head -n 1)
if [[ "$branch_status" =~ ^\#\#.*\[behind.*\] ]]; then
pull_dirs+=("$dir")
fi
done
if [ "${#pull_dirs[@]}" -eq 1 ]; then
suffix="y"
else
suffix="ies"
fi
echo "${text_bold}${text_magenta}Pulling${text_reset} ${#pull_dirs[@]} repositor${suffix}"
# Create n lists of repositories where n is the number of parallel jobs
declare -a pull_lists
for i in "${!pull_dirs[@]}"; do
list_index=$((i % jobs))
if [ "${pull_lists[$list_index]}" == "" ]; then
pull_lists[list_index]="${pull_dirs[$i]}"
else
pull_lists[list_index]="${pull_lists[$list_index]};${pull_dirs[$i]}"
fi
done
# Pull all lists in background
for pull_list in "${pull_lists[@]}"; do
execute_git_command_on_list "pull" "${pull_list}" &
pull_pids+=("$!")
done
# Wait for pulling of all lists
for pid in "${pull_pids[@]}"; do
wait "$pid"
done
echo
fi
# Get Git status of all directories
for dir in "${git_dirs[@]}"; do
cd "$dir" || echo "Failed to cd into ${text_bold}${text_red}${dir}${text_reset}"
# Get repository name
repository_name=$(basename "$dir")
# Get current branch
current_branch=$(git branch --show-current)
# Get origin head
if git symbolic-ref refs/remotes/origin/HEAD > /dev/null 2>&1; then
[[ $(git symbolic-ref refs/remotes/origin/HEAD) =~ refs/remotes/origin/(.*)$ ]] &>/dev/null && origin_head="${BASH_REMATCH[1]}"
else
origin_head="$current_branch"
fi
# Get number of additional local branches
num_branches=$(git branch | wc -l)
num_additional_branches=$(( num_branches - 1 ))
status_keys=""
# Check stash
if [[ $(git stash list) ]]; then
status_keys="${status_keys}\$"
fi
# Get status
remote_status_keys=""
has_upstream=true
while read -r status; do
if [[ "$status" =~ ^\#\#\ (.*) ]] && [ "$(git remote | wc -l)" -gt 0 ]; then
[[ "${BASH_REMATCH[1]}" != *"..."* ]] && has_upstream=false
fi
if [[ "$status" =~ ^\#\#.*\ \[(.*)\] ]]; then
[[ "${BASH_REMATCH[1]}" == *"ahead"* ]] && [[ "${BASH_REMATCH[1]}" == *"behind"* ]] && remote_status_keys="⇕" && continue
[[ "${BASH_REMATCH[1]}" == *"ahead"* ]] && remote_status_keys="⇡" && continue
[[ "${BASH_REMATCH[1]}" == *"behind"* ]] && remote_status_keys="⇣" && continue
[[ "${BASH_REMATCH[1]}" == *"gone"* ]] && remote_status_keys="✗" && continue
elif [[ "$status" != "#"* ]]; then
status_char="${status:0:1}"
[[ "$status_keys" != *"$status_char"* ]] && status_keys="${status_keys}${status_char}"
fi
done < <(git status --short --branch --porcelain)
status_keys="${status_keys}${remote_status_keys}"
# Beautify status
status_keys="${status_keys//A/+}"
status_keys="${status_keys//D/-}"
status_keys="${status_keys//M/!}"
status_keys="${status_keys//U/=}"
status_keys="${status_keys//R/»}"
# Update all status keys
all_status_keys="${all_status_keys}${status_keys}"
# Construct key output
if [[ "$status_keys" ]]; then
if [[ "$status_keys" == "\$" ]]; then
# Only stash symbol: yellow brackets and symbol
status_keys="${text_bold}${text_yellow}[${status_keys}]${text_reset}"
else
# Mixed status: only $ yellow, rest red
status_keys="${text_bold}${text_red}[${status_keys//\$/${text_yellow}\$${text_red}}]${text_reset}"
fi
else
status_keys="${text_bold}${text_green}[✓]${text_reset}"
fi
output="${output}${repository_name};${status_keys}"
# Construct branch output
if [ "$current_branch" == "$origin_head" ]; then
output="${output};${text_bold}${current_branch}${text_reset}"
else
output="${output};${text_bold}${text_yellow}${current_branch}${text_reset}"
fi
if [ "$has_upstream" == false ]; then
output="${output} ${text_blue}[no upstream]${text_reset}"
fi
if [ "$current_branch" != "$origin_head" ]; then
num_additional_branches=$(( num_additional_branches - 1 ))
output="${output} (${origin_head})"
fi
if [[ $num_additional_branches -gt 0 ]]; then
output="${output} (+${num_additional_branches})"
fi
output="${output}\n"
done
# Print keys
if [[ "$all_status_keys" ]]; then
echo "${text_bold}Status${text_reset}"
[[ "$all_status_keys" == *"\$"* ]] && echo " ${text_bold}${text_yellow}\$${text_reset} - Dirty stash"
[[ "$all_status_keys" == *"?"* ]] && echo " ${text_bold}${text_red}?${text_reset} - Untracked files"
[[ "$all_status_keys" == *"!"* ]] && echo " ${text_bold}${text_red}!${text_reset} - Local changes"
[[ "$all_status_keys" == *"+"* ]] && echo " ${text_bold}${text_red}+${text_reset} - Staged changes"
[[ "$all_status_keys" == *"-"* ]] && echo " ${text_bold}${text_red}-${text_reset} - File removed"
[[ "$all_status_keys" == *"»"* ]] && echo " ${text_bold}${text_red}»${text_reset} - File renamed"
[[ "$all_status_keys" == *"="* ]] && echo " ${text_bold}${text_red}=${text_reset} - Both modified"
[[ "$all_status_keys" == *"⇕"* ]] && echo " ${text_bold}${text_red}${text_reset} - Diverged from upstream"
[[ "$all_status_keys" == *"⇡"* ]] && echo " ${text_bold}${text_red}${text_reset} - Ahead upstream"
[[ "$all_status_keys" == *"⇣"* ]] && echo " ${text_bold}${text_red}${text_reset} - Behind upstream"
[[ "$all_status_keys" == *"✗"* ]] && echo " ${text_bold}${text_red}${text_reset} - Upstream gone"
echo
fi
echo -e "$output" | column -s ";" -t