-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgit-archive-branch
executable file
·314 lines (261 loc) · 7.85 KB
/
git-archive-branch
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
#!/bin/bash -u
#
# git-archive-branch: move branchname to archive/2013/branchname and tag it
# as archive-2013-03-04-branchname.
#
# optional git-config hooks:
# branch-tools.archive ... prefix for branch names, default "archive"
# branch-tools.no-description-branches ... list of branches that do
# not need descriptions
readonly DEFAULT_ARCHIVE="archive"
ARCHIVE=`git config branch-tools.archive`
if [ -z "$ARCHIVE" ]; then
ARCHIVE=$DEFAULT_ARCHIVE
fi
# Strip archive off the branch name, store in $2
normalize_branch() {
local branch=$1
local __normalized_name=$2
branch=${branch/$ARCHIVE\/20*\//}
eval $__normalized_name="'$branch'"
}
# return 0 if $branch exists, or non-zero otherwise
branch_exists() {
local readonly branch=$1
git log -n1 $branch -- > /dev/null 2>&1
[[ $? -eq 0 ]] && return 0 || return 1
}
# return 0 if $tag exists, or non-zero otherwise
tag_exists() {
local readonly tag=$1
git log -n1 $tag -- > /dev/null 2>&1
[[ $? -eq 0 ]] && return 0 || return 1
}
# Create archived branch name from $branch and store in $2
make_archive_branch_name() {
local readonly branch=$1
local __archived_branch=$2
local normalized_branch=''
local readonly year=`date +%Y`
normalize_branch $branch normalized_branch
local archive_branch_name="$ARCHIVE/$year/$normalized_branch"
eval $__archived_branch="'$archive_branch_name'"
}
# return tag name for $branch and store it in $2
make_tag() {
local readonly branch=$1
local readonly date=`date +%Y-%m-%d`
local normalized_branch=''
local __tag=$2
normalize_branch $branch normalized_branch
tagname=archive-$date-$normalized_branch
eval $__tag="'$tagname'"
}
# Archive $branch to $ARCHIVES and tag last commit
archive() {
local readonly branch=$1
local archive_branch=''
local tag=''
case $branch in
$ARCHIVE*)
echo "Branch $branch appears to be archived already. Aborting."
exit 1
;;
*)
if ! ( branch_exists $branch ); then
echo "Branch $branch does not exist."
exit 1
fi
;;
esac
make_archive_branch_name $branch archive_branch
if ( branch_exists $archive_branch ); then
echo "Error: Branch $archive_branch already exists."
echo "Move out of the way first."
exit 1
fi
make_tag $branch tag
if ( tag_exists $tag ); then
echo "Error: Tag '$tag' already exists."
echo "Move out of the way first."
exit 1
fi
echo -n "Move branch $branch to $archive_branch? [y/n] "
read confirm
if ! test "$confirm" = "y" && ! test "$confirm" = "Y"; then
exit 0;
fi
# add description unless branch is in no-description-branches
local description=`git config branch.$branch.description`
if [ -z "$description" ]; then
local readonly no_desc_branches=`git config branch-tools.no-description-branches`
local skip_desc=0
for ndb in $no_desc_branches; do
if [ "$ndb" = "$branch" ]; then
skip_desc=1
break
fi
done
if [ $skip_desc -eq 0 ]; then
echo "Archiving a branch without a description is not recommended."
echo "Use git branch --edit-description before archiving."
echo -n "Continue anyway? [y/n] "
read confirm
if ! test "$confirm" = "y" && ! test "$confirm" = "Y"; then
exit 0;
fi
fi
fi
local readonly tmpfile=`mktemp`
cat > $tmpfile << EOF
Branch: $branch.
Archived: `date`.
$description
Top commits:
`git log -n 10 --pretty=oneline $branch --`
EOF
echo "Tag message:"
echo "------------------------------------------------------------------------"
cat $tmpfile
echo "------------------------------------------------------------------------"
echo -n "Edit message? [y/n] "
read confirm
if test "$confirm" = "y" || test "$confirm" = "Y"; then
$EDITOR $tmpfile
fi
git branch -m $branch $archive_branch
git tag -a -F $tmpfile $tag $archive_branch
rm $tmpfile
echo "Archived and tagged as $tag."
}
# Search for $branch in the archive and move it back, leaving the tags
# intact.
restore() {
branch=$1
case $branch in
$ARCHIVE*)
if ! ( branch_exists $branch ); then
echo "Branch $branch does not exist in the archives"
exit 1
fi
branch_to_restore=$branch
;;
*)
if ( branch_exists $branch ); then
echo "Branch $branch looks like a normal, existing branch"
exit 1
fi
echo "Searching for $branch in the archives"
for b in `git branch | grep $ARCHIVE | sed -e "s/\*\? \+//"`; do
local normalized_b=''
normalize_branch $b normalized_b
if [ "$normalized_b" = "$branch" ]; then
echo "Found matching archived branch $b"
branch_to_restore=$b
break;
fi
done
;;
esac
if [ -z "$branch_to_restore" ]; then
echo "Failed to find branch to restore"
exit 1
fi
local normalized_branch=''
normalize_branch $branch_to_restore normalized_branch
if ( branch_exists $normalized_branch ); then
echo "Attempting to restore to branch $normalized_branch, but it already exists."
echo "Move out of the way first"
exit 1
fi
echo -n "Restoring archived branch $branch_to_restore as $normalized_branch. [y/n] "
read confirm
if ! test "$confirm" = "y" && ! test "$confirm" = "Y"; then
exit 0;
fi
git branch -m $branch_to_restore $normalized_branch
}
# restore $branch
undo() {
local branch=$branch
case $branch in
$ARCHIVE*)
if ! ( branch_exists $branch ); then
echo "Branch $branch does not exist in the archives."
exit 1
fi
;;
*)
echo "Specify full archived branch name for undo."
exit 1
;;
esac
local normalized_branch=''
normalize_branch $branch normalized_branch
if ( branch_exists $normalized_branch ); then
echo "Branch $normalized_branch already exists, cannot undo."
exit 1
fi
local tag=''
make_tag $branch tag
if ! ( tag_exists $tag ); then
echo "Tag $tag does not exist, cannot undo."
exit 1
fi
echo -n "Undoing archive of branch $branch? [y/n] "
read confirm
if ! test "$confirm" = "y" && ! test "$confirm" = "Y"; then
exit 0;
fi
git tag -d $tag
git branch -m $branch $normalized_branch
}
usage() {
cat <<- EOF
`basename $0` [--restore|--undo] [branch]
`basename $0` [branch]
moves the current or given branch to $ARCHIVE
`basename $0` --restore [branch]
restores the branch from the archive
`basename $0` --undo $ARCHIVE/<year>/branch
restores the branch and deletes archived tags
This will only work immediately after a archive operation
EOF
}
restore=''
undo=''
branch=''
while [ $# -gt 0 ]; do
case $1 in
--restore)
restore=1
shift
;;
--undo)
undo=1
shift
;;
-h|--help)
usage
exit
;;
*)
branch=$1
shift
;;
esac
done
if [ -n "$restore" ] && [ -n "$undo" ]; then
echo "Undo or restore? Pick one only."
exit 1
fi
if test -z "$branch"; then
branch=`git symbolic-ref --short HEAD`
fi
if [ -n "$restore" ]; then
restore $branch
elif [ -n "$undo" ]; then
undo $branch
else
archive $branch
fi