Skip to content

Instantly share code, notes, and snippets.

@ffflorian
Last active March 20, 2018 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ffflorian/b2586d13509bca9bcc909e357c2ea572 to your computer and use it in GitHub Desktop.
Save ffflorian/b2586d13509bca9bcc909e357c2ea572 to your computer and use it in GitHub Desktop.
Update all git projects in folder
#!/usr/bin/env bash
ERROR_FILE="$(mktemp)"
ERRORS=""
SEPERATOR="\\n\\e[90m-----------------------\\e[0m\\n"
# Change to script's directory to ensure we're in the correct folder.
# http://stackoverflow.com/questions/3349105/how-to-set-current-working-directory-to-the-directory-of-the-script
cd "${0%/*}" || exit 1
if ! command -v "git" > /dev/null; then
echo "git not found. Exiting."
exit 1
fi
_exit_cleanup() {
echo -e "$(< "${ERROR_FILE}")"
rm "${ERROR_FILE}"
}
trap _exit_cleanup 0
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
parse_default_git_branch() {
git symbolic-ref refs/remotes/origin/HEAD 2> /dev/null | sed 's@^refs/remotes/origin/@@'
}
printf "\\e[1mUpdating all git projects in\\e[0m \\e[1;93m%s\\e[0m ...\\n\\n" "$(pwd -P)"
for DIR in *; do
if [ -d "${DIR}/.git" ]; then
cd "${DIR}" || exit 1
BRANCH="$(parse_git_branch)"
DEFAULT_BRANCH="$(parse_default_git_branch)"
printf "#### Updating \\e[1m%s\\e[0m (\\e[1;93m%s\\e[0m) ... " "${DIR}" "${BRANCH}"
RESULT="$(git pull 2>&1 >/dev/null)"
if [ "${?}" != "0" ]; then
printf "\\e[1;91merror\\e[0m\\n"
if [ "${ERRORS}" == "" ]; then
printf "%s\\e[1mResults\\e[0m%s" "${SEPERATOR}" "${SEPERATOR}" >> "${ERROR_FILE}"
ERRORS="errors"
fi
printf "\\e[1m%s\\e[0m:\\n\\e[91m%s\\e[0m%s" "${DIR}" "${RESULT}" "${SEPERATOR}" >> "${ERROR_FILE}"
else
printf "\\e[1;32msuccess\\e[0m\\n"
fi
if [ "${DEFAULT_BRANCH}" == "" ]; then
printf "\\e[1;33mWarning:\\e[0m No default branch found. Repository broken?\\n\\n"
elif [ "${BRANCH}" != "${DEFAULT_BRANCH}" ]; then
printf "\\e[33mWarning:\\e[0m Not on default branch \\e[1m%s\\e[0m.\\n\\n" "${DEFAULT_BRANCH}"
fi
cd .. || exit 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment