Skip to content

Instantly share code, notes, and snippets.

@vedang
Last active November 8, 2019 06:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vedang/505adf41c45269ae0debccbf7af15455 to your computer and use it in GitHub Desktop.
Save vedang/505adf41c45269ae0debccbf7af15455 to your computer and use it in GitHub Desktop.
A small shell script to fetch code from git upstreams in all directories
#! /bin/bash
# find . -maxdepth 1 -type d \( ! -name . \) -print0 | xargs -0 -L1 /path/to/run-git-fetch.sh
fetch_file=".git-fetch"
echo "Working in $1"
cd "$1" || exit
fetch_upstream_and_merge()
{
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' ')"
echo "Trying to merge origin/$curr_branch into current branch."
if [[ "$(git diff)" ]]
then
echo "Working tree is not clean, not doing anything."
else
git merge origin/"$curr_branch" && echo "Local branch $curr_branch is now up-to-date."
fi
else
echo "git fetch failed, not doing anything."
fi
}
if [ -e $fetch_file ]
then
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 5 hours ago, don't do anything.
if [ $((current_time_in_secs - fetch_file_last_modified)) -gt 18000 ]
then
fetch_upstream_and_merge
else
echo "We have run git-fetch recently. Skipping"
fi
else
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