-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-trash-branch
executable file
·96 lines (81 loc) · 2.6 KB
/
git-trash-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
#!/usr/bin/env bash
function error() {
echo "$@" >> /dev/stderr
exit 1
}
USAGE=`cat <<'EOD'
NAME
git-branch-trash - Move a branch to trash box
SYNOPSIS
git branch-trash <branch>
git branch-trash -h
DESCRIPTION
Append "trash/" to the branch name.
OPTIONS
-h, --help
Show this description.
EXAMPLES
When
$ git branch-trash branch1
executed, branch1 is deleted and trash/branch1 is created.
Each HEAD is exactly the same.
If you want to pick up that branch from trash box, simply move it as
below command.
$ git branch -m trash/branch1 branch1
EOD`
OPTIONS=()
for OPTION in "$@"; do
case "$OPTION" in
"-h" )
# --helpはgit本体のコマンドがmanコマンドへ変換してしまうようで
# このようなオリジナルのコマンドでは使用できない(git側の微妙な仕様)
echo "$USAGE"
exit 0
;;
* )
OPTIONS+=("$OPTION")
;;
esac
done
function check_git_command_existence() {
which git > /dev/null 2>&1
if [ $? != 0 ]; then
error "Git command does not exists."
fi
}
function check_git_repository_existence() {
git status > /dev/null 2>&1
if [ $? != 0 ]; then
error "Here is not a git repository."
fi
}
# FIXME: 本当はここで受け取るブランチ名をエスケープする必要がある
# ※git branch --list <pattern>はpatternで受け取った文字列を
# shellのワイルドカードとして処理してしまうため
function exist_given_branch() {
if [ -z "$(git branch --list $@)" ]; then
echo 0
else
echo 1
fi
}
if [ ${#OPTIONS[@]} == 0 ]; then
error "$USAGE"
else
check_git_command_existence
check_git_repository_existence
SRC_BRANCH_NAME=${OPTIONS[0]}
if [ $(exist_given_branch $SRC_BRANCH_NAME) == 0 ]; then
error "Branch '$@' does not exists."
fi
DST_BRANCH_NAME="trash/${SRC_BRANCH_NAME}"
# TODO: 移動後のブランチ名がすでに使われている場合、
# コマンドを中止するのではなく自動的に.1や.2をつける、
# もしくはプロンプトでそうするか聞く、などの対応をするべき
# (理想的にはそれらをコマンドオプションで制御できればいい。
# イメージはyum install -y ~~~の-yのような感じ)
if [ $(exist_given_branch $DST_BRANCH_NAME) == 1 ]; then
error "Branch '$DST_BRANCH_NAME' already exists."
fi
git branch -m $SRC_BRANCH_NAME $DST_BRANCH_NAME
fi