-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpam.sh
executable file
·92 lines (79 loc) · 1.94 KB
/
pam.sh
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
#!/bin/bash
if [ ! -d ".git" ]; then
echo "ERROR: This is not a git repo. Please run the command in a git repository."
exit 1
fi
# VARIABLES
REPO_BRANCHES=$(git branch)
REPO_BRANCHES=${REPO_BRANCHES/\*/}
INITIAL_BRANCH=$(git branch --show-current)
REPO_REMOTES=$(git remote -v)
REMOTE=${3:-origin}
reset_to_normal() {
echo
echo "Reseting to inital state before running command ..."
git switch "$1"
}
pull_and_merge() {
trap "reset_to_normal $INITIAL_BRANCH" SIGINT
git switch "$1" &&
git pull "$REMOTE" "$1" &&
git switch "$2" &&
git merge "$1" &&
echo "Pulled branch \"$1\" from \"$REMOTE\" and merge it to branch \"$2\". Done :)"
}
show_help() {
echo "Pam is here to help you ;)"
echo
echo "Syntax:"
echo "pam PULLING_BRANCH MERGING_BRANCH [ORIGIN]"
echo "pam -h"
echo
echo "options:"
echo "h Print this Help."
echo
}
check_existance() {
for B in $1; do
if [[ "$B" == "$2" ]]; then
return 0
fi
done
return 1
}
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
show_help
exit 0
fi
if [[ -z $1 || -z $2 ]]; then
echo "ERROR: You have to provide two branches as parameters. See \"pam -h\""
exit 1
fi
check_existance "$REPO_BRANCHES" "$1"
if [ "$?" -ne "0" ]; then
echo "ERROR: There is no branch called \"$1\""
exit 1
fi
check_existance "$REPO_BRANCHES" "$2"
if [ "$?" -ne "0" ]; then
echo "ERROR: There is no branch called \"$2\""
exit 1
fi
if [[ ! -z "$3" ]]; then
check_existance "$REPO_REMOTES" "$3"
if [ "$?" -ne "0" ]; then
echo "ERROR: There is no remote called \"$REMOTE\""
exit 1
fi
fi
if [[ -z $3 ]]; then
echo "No remote has been provided. Default remote is \"origin\""
fi
while true; do
read -p "Pull branch \"$1\" from \"$REMOTE\" and merge it to branch \"$2\". right? [Yes, No] " USER_ANSWER
case $USER_ANSWER in
[Yy]* ) pull_and_merge $1 $2; exit 0;;
[Nn]* ) reset_to_normal $INITIAL_BRANCH; exit 1;;
* ) echo "Please answer yes or no.";;
esac
done