Created
March 31, 2022 13:57
-
-
Save vedang/9b0bf440d35dca26e8363bf39a58591a to your computer and use it in GitHub Desktop.
Run git fetch and merge on all repos inside a directory.
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 | |
# find . -maxdepth 1 -type d \( ! -name . \) -print0 | xargs -0 -L1 ~/Tresors/Documents/private-dotfiles/scripts/run-git-fetch.sh | |
fetch_file=".git-fetch" | |
debug_mode="false" | |
master_branch="main" | |
echo "[git-fetch-dir] Working in $1" | |
cd "$1" || exit | |
debug_echo() | |
{ | |
if [ "$debug_mode" = "true" ] | |
then | |
echo "$1" | |
fi | |
} | |
fetch_upstream_and_merge() | |
{ | |
debug_echo "Running git fetch origin in this dir" | |
if git fetch origin 2> /dev/null; | |
then | |
touch $fetch_file | |
curr_branch="$(git branch | grep \* | cut -f2 -d' ')" | |
if [ "$curr_branch" != "$master_branch" ] | |
then | |
echo "[git-fetch] Current branch is $curr_branch" | |
fi | |
debug_echo "Trying to merge origin/$curr_branch into current branch." | |
if [[ "$(git diff)" ]] | |
then | |
echo "[git-fetch] Working tree is not clean, not doing anything." | |
else | |
git merge origin/"$curr_branch" && echo "[git-fetch] Local branch $curr_branch is now up-to-date." | |
fi | |
else | |
echo "[git-fetch] Failed, not doing anything." | |
fi | |
} | |
if [ -e $fetch_file ] | |
then | |
debug_echo "Fetch File exists in this dir" | |
current_time_in_secs="$(date +%s)"; | |
# Where am I? Stat works differently on Linux and Mac :( | |
machine_info="$(uname -s)" | |
case "$machine_info" in | |
Linux*) fetch_file_last_modified=$(stat -c "%Y" $fetch_file);; | |
Darwin*) fetch_file_last_modified=$(stat -f "%m" $fetch_file);; | |
# Unknown Machine, go with Linux default | |
*) fetch_file_last_modified=$(stat -c "%Y" $fetch_file) | |
esac | |
# If we ran a git-fetch less than 50 hours ago, don't do anything. | |
if [ $((current_time_in_secs - fetch_file_last_modified)) -gt 180000 ] | |
then | |
fetch_upstream_and_merge | |
else | |
debug_echo "We have run git-fetch recently. Skipping" | |
fi | |
else | |
debug_echo "Fetch File does not exist in this dir." | |
fetch_upstream_and_merge | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment