-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvn-cip
executable file
·327 lines (296 loc) · 8.57 KB
/
svn-cip
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/bin/bash
# svn-cip, an svn equivalent for git add --patch
# Usage:
# svn-cip <file> [<file> [...]]
function usage(){
sed 's/^........//' <<USAGE
Usage: $(basename "$0") [--changelist <ARG>] [file [file...]]
Options:
--cl,--changelist <ARG>
Use the specified SVN changelist in addition to any specified files
--
Treat all further arguments as filenames
--help
Display this message and exit
USAGE
}
function fully_qualify(){
local file="$1"
readlink -fn "$([ "${file#-}" = "$file" ]||echo -n './')$file"
}
if [ "$1" = "help" ]; then
if [ $# -gt 1 ]; then
echo "Subcommand 'help' doesn't accept option '$2'" >&2
echo "Type '$(basename "$0") help' for usage." >&2
exit 1
fi
usage
exit 0
fi
no_args=0
changelists=()
files=()
while [ $# -gt 0 ]; do
if [ "$no_args" = "1" ]; then
files[${#files[@]}]="$(fully_qualify "$1")"
shift
continue
fi
case "$1" in
--cl|--changelist)
shift
changelist="$1"
if [ -z "$changelist" ]; then
echo "Invalid changelist name '$changelist'" >&2
exit 1
fi
changelists[${#changelists[@]}]="$changelist"
;;
--cl=|--changelist=)
changelist="$(sed '1s/^[^=]*=//')"
if [ -z "$changelist" ]; then
echo "Invalid changelist name '$changelist'" >&2
exit 1
fi
changelists[${#changelists[@]}]="$changelist"
;;
--help)
usage
exit 0
;;
--)
no_args=1
;;
-*)
echo "Unknown argument: $1" >&2
exit 1
;;
*)
files[${#files[@]}]="$(fully_qualify "$1")"
;;
esac
shift
done
# look through all files and determine topmost directory
function longest_common_prefix(){
perl /dev/fd/3 "$1" 3<<-'PERL'
#!/usr/bin/perl
use strict;
my $delim = $ARGV[0];
my $common = undef;
binmode STDIN;
$/ = "\0";
STRING: while(<STDIN>){ chomp;
my @parts = split( $delim );
my $string = '';
my @candidate_prefixes = ($delim);
foreach my $part (@parts) {
if( $part eq "" ){ next; }
$string = $string . $delim . $part;
push @candidate_prefixes, $string;
}
foreach my $candidate_prefix (reverse @candidate_prefixes){
if(
!defined($common) ||
$candidate_prefix eq $delim ||
$candidate_prefix eq $common ||
(
length($candidate_prefix.$delim) <= length($common) &&
substr($common, 0, length($candidate_prefix)+1) eq $candidate_prefix.$delim
)
){
$common = $candidate_prefix;
next STRING;
}
}
}
if( !defined($common) ){
exit(1);
}
print $common;
exit(0);
PERL
}
if [ ${#files[@]} -eq 0 -o ${#changelists[@]} -gt 0 ]; then
parent="$(fully_qualify .)"
[ -d "$parent/.svn" ] || {
echo "Not an SVN directory." >&2
exit 1
}
changelist_args=()
for changelist in "${changelists[@]}"; do
changelist_args[${#changelist_args[@]}]="--cl"
changelist_args[${#changelist_args[@]}]="$changelist"
done
while [ -d "$parent/.svn" ]; do
top="$parent"
parent="$(dirname "$parent")"
done
cd "$top"
oldIFS="$IFS"
IFS=$'\n'
echo -n "Generating changelist from 'svn st' output..." >&2
status_files=( $(svn st "${changelist_args[@]}" | sed -n 's/^M.......//p') ) || exit 1
IFS="$oldIFS"
for file in "${status_files[@]}"; do
files[${#files[@]}]="$(fully_qualify "$file")"
done
echo " done." >&2
fi
for file in "${files[@]}"; do
if [ -d "$file" ]; then
[ -d "$file/.svn" ] || {
echo "'$file' is not a working copy" >&2
exit 1
}
else
[ -d "$(dirname "$file")/.svn" ] || {
echo "'$file' is not controlled by svn" >&2
exit 1
}
fi
done
for file in "${files[@]}"; do
files[${#files[@]}]="$(fully_qualify "$file")"
done
common="$(for file in "${files[@]}"; do
echo -n "$file"
echo -en '\0'
done | longest_common_prefix /)"
common="$(
parent="$(dirname "$common")"
while [ -d "$parent/.svn" ]; do
common="$parent"
parent="$(dirname "$common")"
done
echo "$common"
)"
[ -d $common ] || common="$(dirname "$common")"
GIT_DIR="$(mktemp -d --tmpdir "svn-cip$([ "$common" = "/" ] || echo ".$(basename "$common")" ).XXXXXXXXXX")" || exit 1
function _cleanup(){
rm -rf "$GIT_DIR"
}
trap _cleanup EXIT SIGHUP SIGINT SIGQUIT SIGTERM || exit 1
GIT_WORK_TREE="$common"
export GIT_DIR
export GIT_WORK_TREE
git init --quiet || exit 1
function filepaths(){
local file
local prefix
for file in "$@"; do
file="$(fully_qualify "$file")"
if [ "$common" = "/" ]; then
file="${file:1}"
else
prefix="$common/"
file="${file:${#prefix}}"
fi
echo -n "$file"
echo -en '\0'
done | sort -rz
}
function hash_svn_base(){
local filepath="$1"
svn cat -rBASE "$filepath" | git hash-object -t blob -w --stdin
}
function hash_file(){
local filepath="$1"
cat "$filepath" | git hash-object -t blob -w --stdin
}
function update_index(){
local callback="$1"
local file="$2"
hash="$("$callback" "$common/$file")"
git update-index --add --cacheinfo '100644' "$hash" "$file" || exit 1
}
function commit(){
local tree="$1"
local parent="$2"
local message="$3"
local date="$(date +'%s %z')"
[ -n "$parent" ] && parent="$(echo; echo "parent $parent")"
{
sed 's/^\s*//' <<COMMIT
tree $tree$parent
author nobody <nobody@nowhere> $date
committer nobody <nobody@nowhere> $date
$message
COMMIT
} | git hash-object -t commit -w --stdin
}
filepaths "${files[@]}" | while read -d $'\0' file; do
update_index hash_svn_base "$file" || exit 1
done &&
git reset -q "$(commit "$(git write-tree)" '' 'BASE')" &&
git tag base &&
git add --patch &&
git reset -q "$(commit "$(git write-tree)" "$(git rev-parse refs/tags/base)" 'PARTIAL')" &&
git tag partial &&
filepaths "${files[@]}" | while read -d $'\0' file; do
update_index hash_file "$file" || exit 1
done &&
git reset -q "$(commit "$(git write-tree)" "$(git rev-parse refs/tags/base)" 'FULL')" &&
git tag full || exit 1
if git diff --quiet refs/tags/base refs/tags/partial; then
echo "No changes to commit." >&2
exit 1
fi
#confirm with user that this is ok, eg: output diffs which will actually be sent
while true; do
read -p 'Are you sure you wish to continue [y,n,a,r,q,e,?]? ' response
case "$response" in
y|ye|yes)
if git diff --quiet refs/tags/base refs/tags/partial; then
echo "No changes to commit." >&2
exit 1
fi
break
;;
n|no|q|qu|qui|quit)
exit 0
;;
e|ex|exa|exam|exami|examin|examine)
if git diff --quiet refs/tags/base refs/tags/partial; then
echo "No changes to commit."
else
git diff refs/tags/base refs/tags/partial
fi
continue
;;
r|re|rem|remo|remov|remove)
git reset -q refs/tags/partial &&
git reset -q --soft refs/tags/base &&
git reset --patch &&
git reset -q "$(commit "$(git write-tree)" "$(git rev-parse refs/tags/base)" 'PARTIAL')" &&
git update-ref refs/tags/partial HEAD || exit 1
continue
;;
a|ad|add)
git reset -q refs/tags/partial &&
git reset -q --soft refs/tags/base &&
git add --patch &&
git reset -q "$(commit "$(git write-tree)" "$(git rev-parse refs/tags/base)" 'PARTIAL')" &&
git update-ref refs/tags/partial HEAD || exit 1
continue
;;
*)
sed 's/^\s*//' <<HELP
y - yes, commit changes to svn
n - no, exit
a - add more changes to the commit
r - remove changes from the commit
q - quit (same as "no")
e - examine changes to be committed
? - print help
HELP
esac
done
function _restore(){
git reset -q --hard refs/tags/full &&
_cleanup
}
trap _restore EXIT SIGHUP SIGINT SIGQUIT SIGTERM || exit 1
git reset -q --hard refs/tags/partial || exit 1
svn commit "${files[@]}" || exit 1
exit 0