Created
January 14, 2021 15:00
-
-
Save ndevenish/ee781f91336083dc794d4e980f774b63 to your computer and use it in GitHub Desktop.
Find and list commits that are not present upstream
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 | |
# | |
# Finds and lists commits that are not present upstream. | |
# | |
# Searches in and under the current directory for all .git repositories. | |
# These are then checked for any local commits that are not present in | |
# any of the upstream repositories, e.g. commits that have been made | |
# locally and do not have a known backup anywhere else. | |
# | |
# This is helpful to know if e.g. a repository can be safely deleted | |
# without manually checking every branch for upstream matches. | |
# | |
print_usage() { | |
echo "Usage: find_local.sh [-h | --help] [search_dir]" | |
} | |
if [[ "$1" == "-h" || "$1" == "--help" ]]; then | |
print_usage | |
echo | |
echo "Find all local repositories, then list changes that are not upstream." | |
exit 0 | |
fi | |
RED=$(tput setaf 1) | |
BOLD=$(tput bold) | |
NC=$(tput sgr0) | |
if [[ -n "$1" ]]; then | |
SEARCHDIR=${1%/} | |
if [[ ! -d "$SEARCHDIR" ]]; then | |
print_usage | |
echo "Error: $SEARCHDIR is not a directory" | |
exit 1 | |
fi | |
else | |
SEARCHDIR="." | |
fi | |
printf "Finding repos...." | |
repo_dirs=$(find -L $SEARCHDIR -name "*.git" -type d) | |
count=$(echo "$repo_dirs" | wc -l) | |
printf "${count//[[:space:]]/} found\n" | |
repos=$(echo $repo_dirs | xargs -n 1 dirname) | |
get_remote_diff_log() { | |
git --no-pager log --branches --not --remotes --oneline --decorate --graph --color=always | |
} | |
changed_repos=0 | |
for repo in $repos; do | |
( cd $repo | |
log_text=$(get_remote_diff_log) | |
stashes=$(git stash list) | |
no_up="" | |
workchange="" | |
changed_repos=0 | |
if [[ -n "$log_text" ]]; then | |
no_up=yes | |
fi | |
if ! git diff-index --quiet HEAD --; then | |
workchange=yes | |
fi | |
# Output, if needed | |
if [[ -n "$no_up" || -n "$workchange" || -n "$stashes" ]]; then | |
changed_repos=1 | |
echo "$BOLD$RED$repo$NC:" | |
fi | |
if [[ -n "$no_up" ]]; then | |
get_remote_diff_log | |
fi | |
if [[ -n "$workchange" ]]; then | |
echo "- Uncomitted changes to working directory" | |
fi | |
if [[ -n "$stashes" ]]; then | |
echo "- Stashes present" | |
fi | |
exit $changed_repos | |
) | |
# Use exit code as counter adder | |
changed_repos=$((changed_repos+$?)) | |
done | |
if [[ $changed_repos -eq 0 ]]; then | |
echo "No local-only commits found." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment