-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-merge
executable file
·60 lines (44 loc) · 1.17 KB
/
git-merge
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
#!/usr/bin/env bash
###
### git-merge: Add convenience options to git-merge(1).
###
BASENAME="$(basename $0)"
ERR_NOTREPO="Not a Git repository."
ERR_NO_PREV_BRANCH="Unable to merge, no previous branch could be found."
ARGV=()
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
exec $GIT merge --help
exit 0
;;
-T|--trim)
shift
if [[ ! "$($GIT rev-parse --is-inside-work-tree)" ]] 2>/dev/null; then
printf '%s: %s\n' "$BASENAME" "$ERR_NOTREPO"
exit 1
fi
LASTBRANCH="$($GIT check-ref-format --branch @{-1} 2>/dev/null)"
if [[ $? -gt 0 ]]; then
printf '%s: %s\n' "$BASENAME" "$ERR_NO_PREV_BRANCH"
exit 1
fi
;;
*)
ARGV+=("$1")
shift
;;
esac
done
if [[ ! "$($GIT rev-parse --is-inside-work-tree)" ]] 2>/dev/null; then
printf '%s: %s\n' "$BASENAME" "$ERR_NOTREPO"
exit 1
fi
$GIT merge "${ARGV[@]}"
# On a successful merge where --trim was specified and a previous branch (ref) exists.
if [[ $? -eq 0 ]] && \
[[ -n "$LASTBRANCH" ]]; then
$(echo $($GIT branch --list) | grep "$LASTBRANCH" >/dev/null 2>&1)
# If $LASTBRANCH is a valid, local branch, attempt soft delete.
[[ $? -eq 0 ]] && exec $GIT branch -d $LASTBRANCH
fi