-
Notifications
You must be signed in to change notification settings - Fork 7
/
sync.sh
executable file
·168 lines (136 loc) · 3.69 KB
/
sync.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env bash
set -eu
for i in "$@"
do
case $i in
-p=*|--plugin-name=*)
readonly PLUGIN_NAME="${i#*=}"
shift # past argument=value
;;
-g=*|--git-repo=*)
readonly GIT_REPO="${i#*=}"
shift # past argument=value
;;
-u=*|--svn-user=*)
readonly SVN_USER="${i#*=}"
shift # past argument=value
;;
-a=*|--assets-dir=*)
readonly ASSETS_DIR="${i#*=}"
shift # past argument=value
;;
*)
echo "Unknown option '${i#*=}', aborting..."
exit
;;
esac
done
readonly GIT_DIR=$(pwd)/git
readonly GIT_ASSETS_DIR="$GIT_DIR/${ASSETS_DIR:=.wordpress.org}"
readonly SVN_DIR=$(pwd)/svn
readonly SVN_ASSETS_DIR="$SVN_DIR/assets"
readonly SVN_TAGS_DIR="$SVN_DIR/tags"
readonly SVN_TRUNK_DIR="$SVN_DIR/trunk"
readonly SVN_REPO="https://plugins.svn.wordpress.org/$PLUGIN_NAME"
fetch_svn_repo () {
rm -rf "$SVN_DIR"
echo "Fetch clean SVN repository."
if ! svn co "$SVN_REPO" "$SVN_DIR" > /dev/null; then
echo "Unable to fetch content from SVN repository at URL $SVN_REPO."
exit
fi
echo
}
fetch_git_repo () {
rm -rf "$GIT_DIR"
echo "Fetch clean GIT repository."
if ! git clone "$GIT_REPO" "$GIT_DIR"; then
echo "Unable to fetch content from GIT repository at URL $GIT_REPO."
exit
fi
echo
}
stage_and_commit_changes () {
local message=$1
svn add --force --quiet .
if [ -d "$ASSETS_DIR" ]; then
svn del --force --quiet "$ASSETS_DIR"
fi
find . -type f -name "*.png" \
| awk '{print $0 "@"}' \
| xargs svn propset --quiet --force svn:mime-type image/png
find . -type f -name "*.jpg" \
| awk '{print $0 "@"}' \
| xargs svn propset --quiet --force svn:mime-type image/jpeg
# Untrack files that have been deleted.
# We add an at symbol to every name.
# See http://stackoverflow.com/questions/1985203/why-subversion-skips-files-which-contain-the-symbol#1985366
svn status \
| grep -v "^[ \t]*\..*" \
| grep "^\!" \
| awk '{print $2 "@"}' \
| xargs svn del --force --quiet
changes=$(svn status -q)
if [[ $changes ]]; then
echo "Detected changes in $(pwd), about to commit them."
svn commit --username="$SVN_USER" -m "$message"
else
echo "No changes detected changes in $(pwd)."
fi
echo
}
sync_files () {
local source=$1/
local destination=$2/
local excludeFrom="$source.distignore"
if [ -f "$excludeFrom" ]; then
rsync --compress --recursive --delete --delete-excluded --force --archive --exclude-from "$excludeFrom" "$source" "$destination"
else
rsync --compress --recursive --delete --delete-excluded --force --archive "$source" "$destination"
fi
}
sync_tag () {
local tag=$1
if [ -d "$SVN_DIR/tags/$tag" ]; then
# Tag is already part of the SVN repository, stop here.
return
fi
cd "$GIT_DIR" || exit
echo "Checking out 'tags/$tag'."
git checkout "tags/$tag" > /dev/null 2>&1
echo "Copying files over to svn repository in folder $SVN_DIR/tags/$tag."
mkdir "$SVN_DIR/tags/$tag"
sync_files . "$SVN_DIR/tags/$tag"
cd "$SVN_DIR/tags/$tag" || exit
stage_and_commit_changes "Release tag $tag"
}
sync_all_tags () {
cd "$GIT_DIR" || exit
for tag in $(git tag); do
sync_tag "$tag"
done
}
sync_trunk () {
cd "$GIT_DIR" || exit
echo "Checking out master branch."
git checkout master > /dev/null 2>&1
echo "Copying files over to svn repository in folder $SVN_TRUNK_DIR."
sync_files . "$SVN_TRUNK_DIR"
cd "$SVN_TRUNK_DIR" || exit
stage_and_commit_changes "Updating trunk"
}
sync_assets () {
cd "$GIT_DIR" || exit
git checkout master > /dev/null 2>&1
if [ -d "$GIT_ASSETS_DIR" ]; then
sync_files "$GIT_ASSETS_DIR" "$SVN_ASSETS_DIR"
fi
cd "$SVN_ASSETS_DIR" || exit
stage_and_commit_changes "Updating assets"
}
fetch_svn_repo
fetch_git_repo
sync_assets
sync_all_tags
sync_trunk
exit 0