Last active
September 25, 2015 03:58
-
-
Save nbrew/859746 to your computer and use it in GitHub Desktop.
Commit an app's shared assets via svn and git.
This file contains 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 | |
# | |
# Depending on your system (and your user's tendencies to | |
# upload files with non-ascii characters), you may wish to set the | |
# default locale language to en_US.UTF-8. | |
# | |
# set system-wide on a centos box (with a stock i18n file): | |
# sed -i -e 's/^#\(LANG="en_US.UTF-8"\)$/\1/' -e 's/^\(LANG="C"\)/#\1/' /etc/sysconfig/i18n | |
# | |
# source: https://gist.github.com/859746 | |
# author: Nathan Hyde <[email protected]> | |
VERBOSE=true | |
SVN=/usr/local/bin/svn | |
GIT=/usr/local/bin/git | |
function debug() { | |
msg=$1 | |
if ( ${VERBOSE} ) && [ -n "${msg}" ]; then | |
echo ${msg}; | |
fi | |
} | |
function git_or_svn() { | |
dir=$1 | |
if [ -d "${dir}/.svn" ]; then | |
echo -n 'svn'; | |
elif [ -d "${dir}/.git" ]; then | |
b=`cd "${dir}" && git config --get svn-remote.svn.url`; | |
if [ $? -ne "0" ]; then | |
echo -n 'git'; | |
else | |
echo -n 'git-svn'; | |
fi | |
fi | |
} | |
function commit_directory_changes() { | |
local dir=$1 | |
local type_of_repo=$(git_or_svn ${dir}) | |
echo "Working in ${dir}" | |
echo "Repository type: ${type_of_repo}" | |
if [ 'svn' == "${type_of_repo}" ]; then | |
check_svn_repo $dir | |
elif [ 'git-svn' == "${type_of_repo}" ]; then | |
check_git_svn_repo $dir | |
elif [ 'git' == "${type_of_repo}" ]; then | |
check_git_repo $dir | |
else | |
echo 'ERROR: unmatched repository type (or not a repository)' | |
fi | |
} | |
function check_svn_repo() { | |
dir=$1 | |
result=`$SVN info ${dir}` | |
if [ $? -eq 0 ]; then | |
status_msg=$(${SVN} status ${dir}) | |
if [ '' != "$status_msg" ]; then | |
result=`echo "${status_msg}" | grep "^\!" | wc -l` | |
if [ 0 -lt $result ]; then | |
debug ' Removing yanked files.' | |
rm_cmd="echo \"${status_msg}\" | grep \"^\!\" | sed -e 's/! *//' | sed -e 's/ /\ /g' | tr \"\n\" \"\0\" | xargs -0 $SVN rm" | |
bash -c "$rm_cmd" | |
fi | |
result=`echo "${status_msg}" | grep "^\?" | wc -l` | |
if [ 0 -lt $result ]; then | |
debug ' Adding new files.' | |
add_cmd="echo \"${status_msg}\" | grep \"^\?\" | sed -e 's/? *//' | sed -e 's/ /\ /g' | tr \"\n\" \"\0\" | xargs -0 $SVN add" | |
bash -c "$add_cmd" | |
fi | |
debug ' Committing changes.' | |
bash -c "$SVN commit -m'Cron: Auto-checkin assets.' $dir" | |
else | |
debug 'No changes required.' | |
fi | |
else | |
debug "$dir is not a repository." | |
fi | |
} | |
function gitadd() { | |
local dir=$1 | |
local cmd="$GIT ls-files --modified -o --exclude-per-directory=.gitignore" | |
cd "${dir}" | |
if [ 0 -lt `${cmd} | wc -l` ]; then | |
debug ' Adding new files.' | |
${cmd} | xargs git add | |
fi | |
} | |
function gitrm() { | |
local dir=$1 | |
local cmd="$GIT ls-files --deleted" | |
cd "${dir}" | |
if [ 0 -lt `${cmd} | wc -l` ]; then | |
debug ' Removing yanked files.' | |
${cmd} | xargs git rm | |
fi | |
} | |
function gitcommit() { | |
local dir=$1 | |
cd "${dir}" | |
if [ 0 -lt `$GIT status -s | wc -l` ]; then | |
debug ' Committing changes.' | |
$GIT commit -m'Cron: Auto-checkin assets.' | |
else | |
debug 'No changes required.' | |
fi | |
} | |
function git_commit_changes() { | |
local dir=$1 | |
gitadd ${dir} | |
gitrm ${dir} | |
gitcommit ${dir} | |
} | |
function check_git_svn_repo() { | |
local dir=$1 | |
debug "Using untested check_git_svn_repo method." | |
git_commit_changes ${dir} | |
$GIT svn rebase | |
$GIT svn dcommit | |
} | |
function check_git_repo() { | |
local dir=$1 | |
local r=0 | |
git_commit_changes ${dir} | |
cd "${dir}" | |
r=`$GIT log origin/master..HEAD | wc -l | awk {'print $1'}` | |
if [ $r -ne 0 ]; then | |
$GIT push -f --mirror | |
fi | |
} | |
# from: http://stackoverflow.com/questions/3685970/bash-check-if-an-array-contains-a-value | |
function containsElement() { | |
local e | |
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done | |
return 1 | |
} | |
function commit_vhosts() { | |
local skip_dirs=( | |
'/var/www/vhosts/example.com' | |
'/var/www/vhosts/example.com/subdomains' | |
) | |
for d in `ls -d /var/www/vhosts/*`; do | |
containsElement $d "${skip_dirs[@]}" | |
if [ 1 -eq $? ]; then | |
dir=$d/deploy/shared/assets | |
if [ -d $dir ]; then | |
commit_directory_changes $dir | |
fi | |
fi | |
subdomains=$d/subdomains | |
containsElement $subdomains "${skip_dirs[@]}" | |
if [ 1 -eq $? ]; then | |
if [ -d $subdomains ]; then | |
for sub in `ls ${subdomains}`; do | |
dir=${subdomains}/$sub/deploy/shared/assets | |
if [ -d $dir ]; then | |
commit_directory_changes $dir | |
fi | |
done | |
fi | |
fi | |
done | |
} | |
commit_vhosts | |
exit 0 | |
# CHANGE HISTORY | |
# | |
# 27dec12 | |
# | |
# * Add ability to ignore certain directories | |
# * Only push with git when there are unpushed changes | |
# | |
# 21dec12 | |
# | |
# * Add functions for git and git_svn | |
# * Add notes and updates present in gist | |
# * Add similar output to svn when working with git repos | |
# * Test if add/rm/ci is necessary before running in git repos | |
# | |
# 23mar11 | |
# | |
# * Correct usage of ${status_msg} for pattern matching. | |
# * Verify grep found results before attempting the hand-off to xargs/svn. | |
# | |
# 21mar11 | |
# | |
# * Added note about setting default locale | |
# * Removed ineffective LC_CTYPE from add and rm commands. | |
# | |
# 15mar11 | |
# | |
# * Now check subdomain directories, too. | |
# * Refactored into check_svn_repo function | |
# * Use debug function to print status messages | |
# * Added setting LC_CTYPE to utf-8 when running commands (to permit svn to ci utf-8 file names) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment