-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-backup
executable file
·61 lines (48 loc) · 2.31 KB
/
git-backup
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
#!/bin/bash
#Copyright 2023 Johannes Zink <opensource@johannes-zink.de>
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
function printUsage {
echo "git backup - create a backup copy of the branch you're working on"
echo "Usage: "
echo ""
echo "git backup"
echo ""
echo "this will automagically create a tag. Each time you call"
echo "this command, a new backup tag is created, using the"
echo "current date and incrementing numbers to distinguish each of them."
}
DEFAULT_BACKUP_NAME="backup"
BACKUP_NAME="${BACKUP:-$DEFAULT_BACKUP_NAME}"
function gb_backup_branch_basename {
echo "$1-$BACKUP_NAME-$(date +%Y%m%d)-"
}
function gb_generate_backup_tag {
CURRENT_BRANCH=$(git branch --show-current)
CURRENT_BASENAME=$(gb_backup_branch_basename "$CURRENT_BRANCH")
#get all branches matching <current-branch-name>-<today>-*
CANDIDATES=$(git tag | grep "$CURRENT_BASENAME")
#get highest index in these branches
HIGHEST_INDEX=`echo $CANDIDATES | sort -n -r | head -n 1 | grep -o '[^-]*$'`
#calculate the new branch name. if no highest index exists, it evaluates to 0
let "NEW_INDEX=$HIGHEST_INDEX + 1"
NEW_TAG_NAME="$CURRENT_BASENAME$NEW_INDEX"
#do the actual branch
git tag $NEW_TAG_NAME
echo "ok, done. backup is in $NEW_TAG_NAME"
}
case "$1" in
"--help")
printUsage
;;
"-h")
printUsage
;;
*)
gb_generate_backup_tag
;;
esac
exit 0