Skip to content

Instantly share code, notes, and snippets.

@luismbo
Created September 17, 2011 16:59
Show Gist options
  • Save luismbo/1224133 to your computer and use it in GitHub Desktop.
Save luismbo/1224133 to your computer and use it in GitHub Desktop.
cron.root.update-git-mirrors
#!/bin/bash
GIT=/usr/bin/git
GIT_FETCH_TIMEOUT="5m"
warn() {
echo "${1}" >&2
}
die() {
warn "${1}"
exit 1
}
bare-git-dir-p() {
[[ $# = 1 ]] || die "${FUNCNAME[0]} must receive exactly one argument"
[[ -d ${1} ]] && [[ -d ${1}/refs ]] && [[ -f ${1}/HEAD ]]
}
get_remote_mirror() {
local repo_dir=${1}
if [[ -f ${repo_dir}/clnet_remote ]]; then
cat ${repo_dir}/clnet_remote
else
env GIT_DIR="${repo_dir}" ${GIT} config clnet.mirror 2>/dev/null
fi
}
git_mirror_enabled_p() {
local repo_dir=${1}
local disabled_p=$(env GIT_DIR="${repo_dir}" ${GIT} config clnet.mirrorDisabled)
if [[ ${disabled_p} == 'true' ]]; then
return 1;
else
return 0;
fi
}
update-git-dir() {
[[ $# = 1 ]] || die "${FUNCNAME[0]} must receive exactly one argument"
local repo_dir=${1}
local user=$(ls -ld ${repo_dir} | cut -d " " -f 3)
local remote_mirror=$(get_remote_mirror "${repo_dir}")
if [[ -n ${remote_mirror} ]] && git_mirror_enabled_p "${repo_dir}"; then
if [[ -n ${DEBUG} ]]; then
local url
if ! [[ "${remote_mirror}" =~ '://' ]]; then
url=" = $(env GIT_DIR="${repo_dir}" ${GIT} config remote.${remote_mirror}.url)"
fi
echo "Updating ${repo_dir}"
echo " from remote ${remote_mirror}${url}"
fi
sudo -u ${user} \
env GIT_DIR="${repo_dir}" \
timeout $GIT_FETCH_TIMEOUT ${GIT} fetch --force --tags --prune ${remote_mirror} master:master &>/dev/null
fi
}
update-repos-by-type() {
[[ $# = 1 ]] || die "${FUNCNAME[0]} must receive exactly one argument"
local base_dir=${1} # a glob pattern
for repo_dir in ${base_dir} ; do
if bare-git-dir-p ${repo_dir} ; then
# single repo
update-git-dir ${repo_dir}
else
# multiple repo
for sub_repo in ${repo_dir}/* ; do
if bare-git-dir-p ${sub_repo} ; then
update-git-dir ${sub_repo}
elif bare-git-dir-p ${sub_repo}/.git ; then
update-git-dir ${sub_repo}
fi
done
fi
done
}
[[ ${1} == '-d' ]] && DEBUG=yes
source /custom/conf/functions
PROCESS=$(basename $0)
lock_script ${PROCESS}
on_exit() { release_script ${PROCESS}; }
trap on_exit EXIT
update-repos-by-type '/project/*/public_html/git'
update-repos-by-type '/home/*/public_html/git'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment