forked from bapt/svnstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstash
executable file
·175 lines (158 loc) · 4.73 KB
/
stash
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
169
170
171
172
173
174
175
#!/bin/sh -e
#
# Copyright (c) 2013 Baptiste Daroussin <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
err() {
out=$1
shift
echo "$@" >&2
exit $out
}
usage() {
echo "Usage: `basename $0` cmd
Available cmds:
ls - display all stashed patshes
show - view a given patch
save - save a new patch
apply - apply a given patch
rm - delete a given patch from the queue
pop - apply a patch and delete it
push - push patches to a remote site
sync - synchornize the stash directory is using a VCS
help - show help" >&2
}
export LC_ALL=C
[ -x `which svn` ] || err 1 "svn is not installed on this system"
svnrp=$(svn info 2>&1 | sed -n 's/Working Copy Root Path: \(.*\)/\1/p')
[ -d "${svnrp}" ] || err 1 "should be used inside a working copy"
[ -w "${svnrp}/.svn" ] || err 1 "no write access to ${svnrp}/.svn"
patchdir=${svnrp}/.svn/patches
mkdir -p "${patchdir}" || err 1 "Impossible to create the patch directory"
[ -d ${patchdir}/.svn ] && VCS="svn"
[ -f ${patchdir}/.fslckout ] && VCS="fossil"
[ -d ${patchdir}/.git ] && VCS="git"
[ -d ${patchdir}/.hg ] && VCS="hg"
save() {
[ $# -ge 1 ] || err 1 "Usage: `basename $0` save <name> [-uk] [files...]"
x=`echo $1|head -c1`
[ $x != "." ] || err 1 "The patch name must not start with '.'"
name=$1
patch="${patchdir}/$1.patch"
shift
while getopts 'uk' FLAG; do
case ${FLAG} in
u) update=yes ;;
k) keep=yes ;;
esac
done
shift $(( $OPTIND - 1))
if [ -z "${update}" -a -f "${patch}" ]; then
err 1 "A patch named ${name} already exists"
fi
files="$@"
changes=$(svn st -q ${files})
[ "${changes}" != "" ] || err 1 "No changes to be saved"
svn diff --git ${files:-.} > "${patch}"
if [ -z "${keep}" ]; then
svn revert --depth=infinity ${files:-.}
svn st ${files} | awk '/^?/ { print $2 }' | xargs rm -f
fi
if [ -n "${update}" -a -n "${VCS}" ]; then
(cd ${patchdir}; ${VCS} commit)
elif [ -n "${VCS}" ]; then
(cd ${patchdir}; ${VCS} add ${patch##*/}; ${VCS} commit)
fi
echo "stashed"
exit 0
}
list() {
for p in ${patchdir}/*.patch; do
[ "$p" = "${patchdir}/*.patch" ] && break
p=${p##*/}
echo ${p%.*}
done
exit 0
}
show() {
[ $# -eq 1 ] || err 1 "Usage: `basename $0` show <name>"
patch="${patchdir}/$1.patch"
[ -f "${patch}" ] || err 1 "$1: no such patch"
less "${patch}"
exit 0
}
apply() {
[ $# -eq 1 ] || err 1 "Usage: `basename $0` apply <name>"
patch="${patchdir}/$1.patch"
[ -f "${patch}" ] || err 1 "$1: no such patch"
cd ${svnrp}
svn patch --strip 1 "${patch}"
exit 0
}
remove() {
[ $# -eq 1 ] || err 1 "Usage: `basename $0` rm <name>"
patch="${patchdir}/$1.patch"
[ -f "${patch}" ] || err 1 "$1: no such patch"
rm -f "${patch}"
rm -f "${patch}"
if [ -n "${VCS}" ]; then
(cd ${patchdir}; ${VCS} rm ${patch##*/}; ${VCS} commit)
fi
exit 0
}
pop() {
[ $# -eq 1 ] || err 1 "Usage: `basename $0` pop <name>"
patch="${patchdir}/$1.patch"
[ -f "${patch}" ] || err 1 "$1: no such patch"
cd ${svnrp}
svn patch --strip 1 "${patch}"
remove $1
exit 0
}
push() {
[ $# -eq 1 ] || err 1 "Usage: `basename $0` push <name>"
patch="${patchdir}/$1.patch"
[ -f "${patch}" ] || err 1 "$1: no such patch"
cd ${svnrp}
scp "${patch}" freefall.freebsd.org:public_html
exit 0
}
sync() {
[ -n "${VCS}" ] || err 1 "the stash is not being versioned"
cd ${patchdir}
case ${VCS} in
hg) hg pull -u && hg push ;;
git) git pull -r && git push ;;
fossil) fossil sync && fossil update ;;
svn) svn update ;;
esac
exit 0
}
cmd=$1
shift
case $cmd in
ls) list "$@" ;;
show|save|apply|pop|push|sync) $cmd "$@" ;;
rm) remove "$@" ;;
*) usage ;;
esac