]> Pileus Git - ~andy/sunrise/blob - scripts/sunrise-commit
ebca8cdae1a369eb1791ae67642d7369e73a9c02
[~andy/sunrise] / scripts / sunrise-commit
1 #!/bin/bash
2 # sunrise-commit - Automates the Gentoo Sunrise Overlay commit process
3 # Released into the public domain
4
5 source /sbin/functions.sh
6
7 BLUE=$BRACKET
8 BOLD=$'\e[0;01m'
9 DARKGREEN=$'\e[32m'
10 GREEN=$GOOD
11 LIGHTBLUE=$HILITE
12 RED=$BAD
13 YELLOW=$WARN
14
15 commit_category="$(pwd | awk -F/ '{ print $(NF-1) }')"
16 commit_package="$(pwd | awk -F/ '{ print $NF }')"
17 num_new_dirs=0
18 opt_changelog=0
19 opt_noformat=0
20 opt_norepoman=0
21 opt_noupdate=0
22 opt_quiet=0
23 opt_verbose=0
24
25 changelog_append() {
26         if [[ "$opt_changelog" == "1" ]] ; then
27                 if [[ "$(svn status)" =~ 'ChangeLog' ]] ; then
28                         echo "!!! Error: Only one ChangeLog entry should be made per commit, and you have"
29                         echo "!!! already modified your ChangeLog locally since your last commit. To keep the"
30                         echo "!!! pre-existing modifications please run sunrise-commit again without the -c"
31                         echo "!!! option. To discard the pre-existing modifications run:"
32                         echo "!!!   svn revert ChangeLog"
33                         exit 1
34                 fi
35                 ebegin "Appending/creating ChangeLog"
36                 echangelog "$*"
37                 eend $?
38         fi
39 }
40
41 create_digests() {
42         if [[ "$(ls)" =~ '\.ebuild' ]] ; then
43                 ebegin "Digesting ebuilds"
44                 for i in *.ebuild ; do
45                         ebuild $i digest
46                 done
47                 eend $?
48         fi
49 }
50
51
52 repoman_check() {
53         if [[ "$opt_norepoman" == "0" ]] ; then
54                 if [[ "$(ls)" =~ '\.ebuild' ]] ; then
55                         ebegin "Running repoman"
56                         export PORTDIR_OVERLAY="$(dirname $(dirname $(pwd)))"
57                         repoman
58                         eend $?
59                 fi
60                 return $?
61         fi
62 }
63
64 svn_add() {
65         ebegin "Adding local changes to working copy"
66         if [[ "$opt_verbose" == "1" ]] ; then
67                 svn add ../$(basename `pwd`) --force
68         else
69                 svn add ../$(basename `pwd`) --force -q
70         fi
71         eend $?
72 }
73
74 svn_commit() {
75         local commit_message="$*"
76
77         for (( i=num_new_dirs ; i > 0 ; i-- )) ; do
78                 cd ..
79         done
80
81         if [[ "$opt_noformat" == "0" ]] ; then
82                 if [[ "$(ls)" =~ '\.ebuild' ]] ; then
83                         commit_message="${commit_category}/${commit_package}: ${commit_message}"
84                 else
85                         commit_message="${commit_package}/$(svn status | awk '{ print $2 }'): ${commit_message}"
86                 fi
87         fi
88
89         echo
90         echo "${DARKGREEN}The following local changes will be committed to the repository:${NORMAL}"
91         echo
92         svn status
93         echo
94         echo "${DARKGREEN}The following commit message will be used:${NORMAL}"
95         echo
96         echo "$commit_message"
97
98         if [[ "$opt_quiet" == "0" ]] ; then
99                 echo
100                 echo -n "${BOLD}Commit changes?${NORMAL} [${GREEN}Yes${NORMAL}/${RED}No${NORMAL}] "
101                 read choice
102                 echo
103
104                 case "$choice" in
105                         y*|Y*|"")
106                                 ;;
107
108                         *)
109                                 echo "Quitting."
110                                 echo
111                                 return 1 ;;
112                 esac
113         fi
114
115         ebegin "Committing working copy to repository"
116         svn commit -m "$commit_message"
117         eend $?
118 }
119
120 svn_up() {
121         if [[ "$opt_noupdate" == "0" ]] ; then
122                 while [[ "$(echo `svn status`)" =~ 'A.+? \.' ]] ; do
123                         pushd .. >/dev/null
124                         (( num_new_dirs++ ))
125                 done
126
127                 ebegin "Updating working copy to latest version from repository"
128
129                 if [[ "$opt_verbose" == "1" ]] ; then
130                         svn update || set $?  
131                 else
132                         svn update -q || set $?
133                 fi
134
135                 eend ${1:-0}
136
137                 for (( i=num_new_dirs ; i > 0 ; i-- )) ; do
138                         popd >/dev/null
139                 done
140
141                 local conflict_files=$(svn status | sed -rn 's/^C.+ ([^ ]+)$/\1/p')
142                 if [[ -n "$conflict_files" ]] ; then
143                         echo "!!! Error: Some local files have changes that conflict with the latest"
144                         echo "!!! revisions in the repository. Please contact the previous committer(s) to"
145                         echo "!!! resolve the conflicts manually before running sunrise-commit again:"
146                         for filename in $conflict_files ; do
147                                 echo "!!!"
148                                 echo "!!!        file: ${filename}"
149                                 echo "!!!   committer: $(svn info ${filename} | sed -rn 's/Last Changed Author\: (.*)$/\1/p')"
150                         done
151                         exit 1
152                 fi
153         fi
154         return ${1:-0}
155 }
156
157 usage() {
158 cat << EOF
159 ${BOLD}Usage:${NORMAL} ${LIGHTBLUE}sunrise-commit${NORMAL} [ ${GREEN}options${NORMAL} ] ${BLUE}message${NORMAL}
160
161 ${GREEN}options${NORMAL}:
162   ${BOLD}--changelog, -c${NORMAL}  Create a ChangeLog entry using ${BLUE}message${NORMAL}
163   ${BOLD}--help, -h${NORMAL}       Show help
164   ${BOLD}--noformat, -m${NORMAL}   Disable automatic formatting of ${BLUE}message${NORMAL}
165   ${BOLD}--norepoman, -p${NORMAL}  Skip repoman check
166   ${BOLD}--noupdate, -d${NORMAL}   Don't update from repository before committing
167   ${BOLD}--quiet, -q${NORMAL}      Don't ask for confirmation
168   ${BOLD}--verbose, -v${NORMAL}    Show detailed information during commit
169
170 ${BLUE}message${NORMAL}:
171   Commit message describing changes and listing names/emails of anyone (other
172   than the commiter) who contributed.
173 EOF
174         exit ${1:-0}
175 }
176
177 [[ -z "$1" ]] && usage 1
178
179 while [[ $# > 0 ]] ; do
180         case "$1" in
181                 --changelog|-c)
182                         if [[ -z "$ECHANGELOG_USER" ]] ; then
183                                 echo "!!! Error: --changelog option requires ECHANGELOG_USER to be set:"
184                                 echo "!!!   export ECHANGELOG_USER=\"Your Name <your@mail.org>\""
185                                 exit 1
186                         fi
187                         opt_changelog=1
188                         shift ;;
189
190                 --help|-h)
191                         usage ;;
192
193                 --noformat|-m)
194                         opt_noformat=1
195                         shift ;;
196
197                 --norepoman|-p)
198                         opt_norepoman=1
199                         shift ;;
200
201                 --noupdate|-d)
202                         opt_noupdate=1
203                         shift ;;
204
205                 --quiet|-q)
206                         opt_quiet=1
207                         shift ;;
208
209                 --verbose|-v)
210                         opt_verbose=1
211                         shift ;;
212
213                 -*)
214                         echo "!!! Error: Unknown option ${1}. See: sunrise-commit -h"
215                         exit 1 ;;
216
217                 *)
218                         break ;;
219         esac
220 done
221
222 if [[ -z "$*" ]] ; then
223         echo "!!! Error: You must supply a commit message. See: sunrise-commit -h"
224         exit 1
225 fi
226
227 svn_up || exit $?
228 [[ ! -e metadata.xml ]] && cp ../../skel.metadata.xml metadata.xml >/dev/null 2>&1
229 changelog_append "$*" || exit $?
230 create_digests || exit $?
231 svn_add || exit $?
232
233 if [[ -z "$(svn status)" ]] ; then
234         echo "!!! Error: No changes found in current directory tree. Aborting commit."
235         exit 1
236 fi
237
238 repoman_check || exit $?
239 svn_commit "$*" || exit $?