-
Notifications
You must be signed in to change notification settings - Fork 0
/
gittool
executable file
·101 lines (88 loc) · 1.6 KB
/
gittool
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
#!/bin/sh
set -e
# set -x
gitRoot=''
# 获取git项目目录
getGitRoot(){
root=$(pwd)
if [ -d "./.git" ]; then
echo "$root"
else
[ "$root" != "/" ] && cd ../ && getGitRoot
fi
cd "$root"
}
# 拉取远程分支
fetchAll(){
git fetch --all
}
# 获取当前分支名
getCurrentBranch(){
branch=$(git rev-parse --abbrev-ref HEAD)
echo "$branch";
}
# 重制分支
resetHard(){
currentBranch=$(getCurrentBranch)
git reset --hard "origin/$currentBranch";
if [ -n "$1" ] && [ "$currentBranch" != "$1" ]; then
git checkout "$1";
if [ $? -eq 1 ]; then
return;
fi
fi
}
# 重制到最新的分支版本
resetToLast(){
fetchAll;
deleteChangeFiles;
resetHard "$1";
git pull
git diff;
git status;
}
# 获取未添加到git分支的文件
getChangeFiles(){
git status --porcelain 2>/dev/null | awk '{print $2}'
}
# 删除未添加到git分支的所有文件
deleteChangeFiles(){
getChangeFiles | xargs -n1 rm -rf
}
help() {
printf "resetToLast [branchName]\n"
printf "getChangeFiles\n"
printf "cleanFiles\n"
printf "getGitRoot\n"
}
main(){
# 切换至git主目录
gitRoot=$(getGitRoot)
if [ -z "$gitRoot" ]; then
printf "Not a git repository (or any of the parent directories): .git\n"
exit 1
fi
cd "$gitRoot" || exit 1
case $1 in
'resetToLast')
resetToLast "$2"
;;
'getChangeFiles')
getChangeFiles
;;
'cleanFiles')
deleteChangeFiles
;;
'getGitRoot')
echo "$gitRoot"
;;
'-h' | '--help')
help
;;
*)
printf 'no options!!\n'
help
;;
esac
}
main "$@";