-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathchop_tmp.sh
executable file
·85 lines (76 loc) · 1.79 KB
/
chop_tmp.sh
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
#!/bin/bash
# Copyright (c) 2020 Ran Panfeng. All rights reserved.
# Author: satanson
# Email: ranpanf@gmail.com
# Github repository: https://github.com/satanson/cpp_etudes.git
#
#basedir=$(cd $(dirname ${BASH_SOURCE:-$0});pwd);
#cd ${basedir}
branch_exists(){
local br=${1:?"branch"};shift
local res=$(git branch -v |perl -lne "print qq/ok/ if /^\\s*(\\*\\s*)?$br\\s+/"|head -1)
if [ -n "$res" ];then
return 0;
else
return 1;
fi
}
ensure_branch_exists(){
if !(branch_exists $1);then
echo "ERROR: branch $1 not exists " >&2;
exit 1
fi
}
ensure_branch_not_exists(){
if (branch_exists $1);then
echo "ERROR: branch $1 exists " >&2
exit 1
fi
}
branch_current(){
git branch -v |perl -lne "print \$1 if /^\\s*\\*\\s*(\\S+)\\s+/"|head -1
}
current_branch_is(){
local br=${1:?"branch"};shift
local cbr=$(branch_current)
if [ "x${br}x" != "x${cbr}x" ];then
return 1
else
return 0
fi
}
ensure_current_branch(){
local br=${1:?"branch"};shift
if !(current_branch_is $br);then
echo "ERROR: not on branch '${br}', current branch is '$(branch_current)'" >&2
exit 1
fi
}
set -e -o pipefail
notDryRun=${1}
cbr=$(branch_current)
cbr0=$(perl -le "print \$1 if qq/${cbr}/=~/^(.*)\\.tmp\\.\\d{8}_\\d{6}/")
if [ -s "${cbr0}" ];then
echo "# ${cbr} is not a temp branch"
exit 1
fi
echo "# mv ${cbr} ${cbr0}"
if (! branch_exists ${cbr0});then
echo git checkout -b ${cbr0}
if [ -n "${notDryRun}" ];then
git checkout -b ${cbr0}
fi
else
echo git branch -D ${cbr0}
echo git checkout -b ${cbr0}
if [ -n "${notDryRun}" ];then
git branch -D ${cbr0}
git checkout -b ${cbr0}
fi
fi
echo git branch -D ${cbr}
echo git push --force satanson ${cbr0}:${cbr0}
if [ -n "${notDryRun}" ];then
git branch -D ${cbr}
git push --force satanson ${cbr0}:${cbr0}
fi