239 lines
7.7 KiB
Bash
Executable file
239 lines
7.7 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 [COMMANDS] [OPTIONS]
|
|
|
|
Show a status summary of all Git repositories which are found recursively in
|
|
current work directory. If the colon-separated environment variable \$GIS_PATH
|
|
is set, the declared directories will be used instead.
|
|
|
|
COMMANDS
|
|
fetch Execute 'git fetch --prune --all' for all found repositories
|
|
pull Execute 'git pull' for all found repositories which are behind upstream
|
|
|
|
OPTIONS
|
|
-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
|
|
}
|
|
|
|
# Parse arguments
|
|
fetch=false
|
|
pull=false
|
|
while (( "$#" )); do
|
|
case "$1" in
|
|
-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_bold}"
|
|
;;
|
|
fetch)
|
|
fetch=true
|
|
shift
|
|
;;
|
|
f*)
|
|
error "Unsupported command ${text_bold}$1${text_reset}, did you mean ${text_bold}fetch${text_reset}?"
|
|
;;
|
|
pull)
|
|
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 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
|
|
readarray -t found_git_dirs < <(find "$path" -type d -name ".git" -exec dirname {} \; | sort)
|
|
git_dirs+=("${found_git_dirs[@]}")
|
|
done
|
|
|
|
# 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}"
|
|
|
|
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")
|
|
|
|
# Fetch all Git repositories in background
|
|
git fetch --prune --all 1> /dev/null 2> >(trap "" INT TERM; sed "s/^/${text_bold}${text_blue}${repository_name}${text_reset} /" >&2) &
|
|
fetch_pids+=("$!")
|
|
done
|
|
|
|
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}"
|
|
|
|
# Pull all Git repositories which are behind upstream in background
|
|
for dir in "${pull_dirs[@]}"; do
|
|
cd "$dir" || echo "Failed to cd into ${text_bold}${text_red}${dir}${text_reset}"
|
|
|
|
# Get repository name
|
|
repository_name=$(basename "$dir")
|
|
|
|
git pull 1> /dev/null 2> >(trap "" INT TERM; sed "s/^/${text_bold}${text_magenta}${repository_name}${text_reset} /" >&2) &
|
|
pull_pids+=("$!")
|
|
done
|
|
|
|
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) =~ \/([^\/]*)$ ]] &>/dev/null && origin_head="${BASH_REMATCH[1]}"
|
|
else
|
|
origin_head="$current_branch"
|
|
fi
|
|
|
|
status_keys=""
|
|
|
|
# Check stash
|
|
if [[ $(git stash list) ]]; then
|
|
status_keys="${status_keys}\$"
|
|
fi
|
|
|
|
# Get status
|
|
remote_status_keys=""
|
|
while read -r status; do
|
|
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/=}"
|
|
|
|
# Update all status keys
|
|
all_status_keys="${all_status_keys}${status_keys}"
|
|
|
|
# Construct key output
|
|
if [[ "$status_keys" ]]; then
|
|
status_keys="${text_bold}${text_red}[${status_keys}]${text_reset}"
|
|
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}\n"
|
|
else
|
|
output="${output};${text_bold}${text_yellow}${current_branch}${text_reset} (${origin_head}${text_reset})\n"
|
|
fi
|
|
|
|
done
|
|
|
|
# Print keys
|
|
if [[ "$all_status_keys" ]]; then
|
|
echo "${text_bold}Status${text_reset}"
|
|
[[ "$all_status_keys" == *"\$"* ]] && echo " ${text_bold}${text_red}\$${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} - 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
|