-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgt
116 lines (102 loc) · 2.9 KB
/
gt
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
#! /bin/bash
cur_branch() {
echo $(git branch | sed -n '/\*/p' | awk '{print $2}')
}
# example:
# confirm && hg push ssh://..
# confirm "Would you really like to do a push?" && hg push ssh://..
confirm () {
# call with a prompt string or use a default
read -r -p "${1:-Are you sure? [y/N]} " response
case $response in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}
help() {
echo -e "example: gitutils [-s] [-a] [-l] [-h] [-c] [-b branch] [-r repository] [-m commit message]"
echo -e " \t\t -s git push current branch"
echo -e " \t\t -l git pull current branch"
echo -e " \t\t -a git add -A | git commit | git push, for current branch to all repositories"
echo -e " \t\t -c only operate on origin repository, as of now, only affects -a option"
echo -e " \t\t -r repository, not mandatory, provides repository"
echo -e " \t\t -b branch, not mandatory, provides branch"
echo -e " \t\t -m commit repository, not mandatory, replace boilerplate commit message"
echo -e " \t\t -h command help"
}
## $1: repository, default to origin
## $2: branch, default to current branch
push_yeah() {
local rep=${1:-origin}
local branch=${2:-$(cur_branch)}
cmd="git push $rep $branch"
confirm "about to run [$cmd]" && eval "$cmd"
}
## $1: repository, default to origin
## $2: branch, default to current branch
pull_yeah() {
local rep=${1:-origin}
local branch=${2:-$(cur_branch)}
cmd="git pull $rep $branch"
confirm "about to run [$cmd]" && eval "$cmd"
}
all_yeah() {
local cmd="git add -A"
echo "about to run [$cmd]"
eval "$cmd"
commit_msg=${commit_msg:-$boilerplate}
cmd="git commit -m '$commit_msg'"
echo "about to run [$cmd]"
eval "$cmd"
local branch=$(cur_branch)
if [[ -z $cur_flag ]]; then
echo -e "$(git remote)" | while read rep
do
cmd="git push $rep $branch"
echo "about to run [$cmd]"
eval "$cmd"
done
else
rep='origin'
cmd="git push $rep $branch"
echo "about to run [$cmd]"
eval "$cmd"
fi
}
# set -x
if [[ -z "$@" ]]; then
help
fi
while getopts ":sSlLaAcChHm:M:r:R:b:B:" opt
do
case $opt in
[sS] ) push_flag='Y'
;;
[lL] ) pull_flag='Y'
;;
[aA] ) all_flag='Y';;
[cC] ) cur_flag='Y';;
[hH] ) help;;
[bB] ) branch=$OPTARG;;
[rR] ) repo=$OPTARG;;
[mM] ) commit_msg="$OPTARG";;
[rR] ) repo=$OPTARG;;
* ) help;;
esac
done
repo=${repo:-origin}
branch=${branch:-$(cur_branch)}
boilerplate='progressing'
if [[ -n $pull_flag ]]; then
pull_yeah $repo $branch
fi
if [[ -n $push_flag ]]; then
push_yeah $repo $branch
fi
if [[ -n $all_flag ]]; then
all_yeah
fi