forked from red-panda-ci/git-promote
/
git-promote
executable file
·152 lines (128 loc) · 3.1 KB
/
git-promote
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
# POSIX
# Credits
# - Forked from soulrebel git-promote gist https://gist.github.com/soulrebel/9c47ee936cfce9dcb725
# - Parse arguments howto http://mywiki.wooledge.org/BashFAQ/035
git_promote="$(basename "$0" | sed -e 's/-/ /')"
HELP="Usage: $git_promote [options...] [upstream] <downstream>
Promote an \"upstream\" Git branch by merging it into a \"downstream\" branch.
When omitted, the currently checked out branch is used as upstream.
Options:
-m <message> # merge commit message, asked otherwise
--dry-run # echo git commands, do not execute them
--nopull # do not pull from remote
--nopush # do not push from remote
--no-edit # use --no-edit in the merge
--local # same as --nopull and --nopush
--help # prints this
Examples:
$git_promote quality # promotes current branch to \"qality\"
$git_promote quality master # promotes \"quality\" branch to \"master\"
"
# to install copy to /usr/local/bin and
# sudo chmod +x /usr/local/bin/git-promote
# to install man page so that git(no dash)promote --help works:
# mkdir -p /usr/local/man/man1
# git-promote --help | txt2man -t git-promote | gzip | sudo tee /usr/local/man/man1/git-promote.1.gz >/dev/null
git='git'
pull_upstream='y'
pull_downstream='y'
push='y'
no_edit=''
commit_message=''
while :; do
case $1 in
-m|--message)
shift
commit_message="$1"
;;
--dry-run)
git="echo git"
;;
--no-edit)
no_edit='--no-edit'
;;
--nopull)
pull_upstream='n'
pull_downstream='n'
;;
--nopush)
push='n'
;;
--local)
pull_upstream='n'
pull_downstream='n'
push='n'
;;
-h|\?|--help)
echo "$HELP"
exit 0
;;
--) # End of all options.
shift
break
;;
-?*)
printf 'WARN: Unknown option: %s\n' "$1" >&2
echo "$HELP"
exit 1
;;
*) # Default case: If no more options then break out of the loop.
break
esac
shift
done
current="$(git branch | awk '/^[*]/{print $2;}')"
if [ "$current" == "(HEAD" ]
then
current="$(git rev-parse HEAD)"
fi
case "$#" in
1)
upstream="$current"
downstream="$1"
;;
2)
upstream="$1"
downstream="$2"
;;
*)
echo "$HELP" >&2
exit 1
;;
esac
if [ "$commit_message" == "" ]
then
$commit_message="Merge from $upstream"
fi
if [ -z "$current" ]; then
exit 1
fi
function fail {
$git checkout "$current" 2>/dev/null || true
exit 1
}
trap 'fail' ERR INT TERM
if [ "$upstream" != "$current" ]; then
$git checkout "$upstream"
else
echo "Promoting $upstream to $downstream"
fi
if [ "$pull_upstream" == 'y' ]; then
$git pull --ff-only
fi
$git checkout "$downstream"
if [ "$pull_downstream" == 'y' ]; then
$git pull --ff-only
fi
if [ "$git" == "echo git" ]
then
commit_message="\"$commit_message\""
fi
$git merge --no-ff $no_edit -m "$commit_message" "$upstream"
if [ "$push" == 'y' ]; then
$git push
fi
$git checkout "$current"
exit 0
# vim: set expandtab ts=2 sw=2