#/bin/bash # This script was indeed self-updated! # Catch signals so we don't leave zombie tasks behind if we Ctrl^C # No idea why but it seems to prevent the script from working? Disable! #trap "exit" INT TERM ERR #trap "kill 0" EXIT BASEDIR="$HOME/.git-build" run() { p_name="$1" echo "[$p_name] RUN" # Run in background and redirect output to $p_name.log (GITBUILDDIR="$BASEDIR" nohup $BASEDIR/$p_name $p_name > $BASEDIR/$p_name.log 2> $BASEDIR/$p_name.err) & } # So scripts can know we're still running (for autoupdater) touch $BASEDIR/.LOCK # Find targeted projects from args PROJECTS=() for arg in "$@"; do [ -f $BASEDIR/$arg.source ] && PROJECTS+=("$arg") # use elseif to match other arguments, so we can do else print arg not found done # If no project argument passed, default to all projects if [[ ${#PROJECTS[*]} = 0 ]]; then for project in $BASEDIR/*.source; do # Extract the project name from path PROJECTS+=("$(basename $project .source)") done fi for p_name in ${PROJECTS[*]}; do echo "[$p_name] START" [ -f $BASEDIR/$p_name.branch ] && p_branch="$(cat $BASEDIR/$p_name.branch)" || p_branch="master" p_dir="$BASEDIR/.$p_name" if [ ! -d $p_dir ]; then source="$(cat $BASEDIR/$p_name.source)" echo "[$p_name] CLONE $source" # Don't forget the git submodules! git clone --recursive "$source" "$p_dir" [[ $? != 0 ]] && echo "[$p_name] CLONE FAILED" && exit 1 cd $p_dir if [[ "$p_branch" != "master" ]]; then echo "[$p_name] CHECKOUT BRANCH $p_branch" git checkout "$p_branch" [[ $? != 0 ]] && echo "[$p_name] CHECKOUT FAILED" && exit 1 fi run $p_name fi cd "$p_dir" # Refresh remote before comparing with local git fetch --quiet origin git diff --quiet remotes/origin/$p_branch if [[ $? != 0 ]]; then echo "[$p_name] PULL" # Update all submodules, for now only when the main repo changed (TODO) git pull --quiet --recurse-submodules [[ $? != 0 ]] && echo "[$p_name] PULL FAILED" && exit 1 run $p_name fi done rm $BASEDIR/.LOCK