diff --git a/multi-fetch.bashrc b/multi-fetch.bashrc new file mode 100644 index 0000000..e9c9014 --- /dev/null +++ b/multi-fetch.bashrc @@ -0,0 +1,69 @@ +#!/bin/bash +# these two functions will recursively look for repos when they exist, and try to update their +# head pointers. +function ___doFetchCheck() { + export IFS=$'\n' + FMT_GRN="$(tput rev)$(tput setaf 2)" + FMT_YLW="$(tput rev)$(tput setaf 3)" + FMT_END="$(tput sgr0)" + + DIR="$1" + LEVEL=${2:-1} + + DIR_BN="${DIR##*/}" + git -C $DIR log -n0 2>/dev/null + if [[ $? -ne 0 ]]; then + # First try it's children in case it is a container + FOUND=0 + if [[ $LEVEL -lt 2 ]]; then + for DIR in $(find $DIR -mindepth 1 -maxdepth 1 -type d); do + ___doFetchCheck "$DIR" $(($LEVEL+1)) + if [[ $? -eq 0 ]]; then FOUND=1; fi + done + fi + if [[ $FOUND -ne 0 ]]; then + echo "${FMT_YLW} Skipping '$DIR_BN', not a git repo. ${FMT_END} " + fi + return 1 + # skipping as this isn't a valid GIT directory. + fi + + echo "${FMT_GRN} Fetching '$DIR_BN' ${FMT_END} " + git -C $DIR fetch 2>/dev/null + #BR_NAME="$(git -C $DIR branch --show-current)" + # Check to see if we're sync'ed with the remote. + # Test if HEAD exists + git -C $DIR branch -r | grep /HEAD -q + if [[ $? -eq 0 ]]; then + TARGET_BRANCHES=($(git -C $DIR branch -r | grep -e /HEAD | sed 's_[^a-zA-Z0-9_]\+\([a-zA-Z0-9_]\+/HEAD\) .\+_\1_')) + if [[ ${#TARGET_BRANCHES[@]} -eq 1 ]]; then + TARGET_BRANCH=${TARGET_BRANCHES[0]} + else + TARGET_BRANCH=$(printf "%s\n" ${TARGET_BRANCHES[*]} | grep origin) + fi + else + TARGET_BRANCH=$(git -C $DIR branch -r | grep -v -e ' ->' | \ + grep -e /main -e /develop | \ + head -1 | tr -d ' ') + fi + + git -C $DIR diff-index $TARGET_BRANCH --quiet + if [[ $? -eq 1 ]]; then + # Test to see if the tree is dirty or clean + CHANGE_COUNT_INDEX=$(git -C $DIR diff --stat | wc -l) + CHANGE_COUNT_TREE=$(git -C $DIR ls-files -o --exclude-standard | wc -l) + if [[ $CHANGE_COUNT_INDEX -eq 0 && $CHANGE_COUNT_TREE -eq 0 ]]; then + # Pull the updates if we're ready for them + git -C $DIR pull -ff #&>/dev/null + fi + fi + + return 0 +} + +function -multi-fetch() { + export IFS=$'\n' + for DIR in $(find $REPOSITORY_SEARCH_BASE -mindepth 1 -maxdepth 1 -type d); do + ___doFetchCheck "$DIR" + done +} \ No newline at end of file