forked from macrocosm-os/prompting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromote_changes.sh
executable file
·117 lines (97 loc) · 2.77 KB
/
promote_changes.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
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
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
function echo_info {
echo -e "${CYAN}[INFO ]${NC} $1"
}
function echo_good_news {
echo -e "${GREEN}[GOOD ]${NC} $1"
}
function echo_warning {
echo -e "${YELLOW}[OOPS ]${NC} $1"
}
function echo_error {
echo -e "${RED}[ERROR]${NC} $1"
}
REMOTE_ORIGIN_SSH='SSH'
REMOTE_ORIGIN_HTTP='HTTP'
REMOTE_ORIGIN_UNEXPECTED='UNEXPECTED'
function get_remote_origin_type {
case "$1" in
https:*)
echo $REMOTE_ORIGIN_HTTP
;;
echo $REMOTE_ORIGIN_SSH
;;
*)
echo $REMOTE_ORIGIN_UNEXPECTED
;;
esac
}
REMOTE_HTTP='https://github.com/opentensor/prompting.git'
REMOTE_SSH='[email protected]:opentensor/prompting.git'
REMOTE_NAME='tmp-promote-changes'
function check_if_remote_exists {
URL_COUNT="$(git remote -v | grep -c $REMOTE_NAME)"
if (( URL_COUNT > 0 )); then
echo "yes"
else
echo "no"
fi
}
echo_info 'Checking git remote origin...'
EXPECTE_REMOTE_ORIGIN='private'
ORIGIN_BRANCH="$(git remote -v | grep $EXPECTE_REMOTE_ORIGIN | grep push | awk '{ print $2 }')"
case "$ORIGIN_BRANCH" in
*opentensor/prompting-private*)
echo_good_news 'Correct remote origin'
;;
*)
echo_error 'Unexpected remote origin'
echo_info 'finishing execution...'
exit 1
;;
esac
ORIGIN_TYPE="$(get_remote_origin_type "$ORIGIN_BRANCH")"
case $ORIGIN_TYPE in
"$REMOTE_ORIGIN_HTTP")
REMOTE_URL=$REMOTE_HTTP
;;
"$REMOTE_ORIGIN_SSH")
REMOTE_URL=$REMOTE_SSH
;;
esac
echo_info "Creating git remote '$REMOTE_NAME' '$REMOTE_URL' if not exists"
REMOTE_ALREADY_EXIST="$(check_if_remote_exists)"
if [ "$REMOTE_ALREADY_EXIST" == "no" ]; then
git remote add "$REMOTE_NAME" "$REMOTE_URL"
fi
REMOTE_ALREADY_EXIST="$(check_if_remote_exists)"
if [ "$REMOTE_ALREADY_EXIST" == "no" ]; then
echo_error "Something went wrong, unable to create git remote $REMOTE_NAME"
fi
echo_good_news "Remote exists '$REMOTE_NAME' -> $REMOTE_URL"
USER_NAME="$(git config user.name)"
if [ -z "$USER_NAME" ]; then
USER_NAME='unknown'
fi
TS=$(date +%Y_%m_%d)
BRANCH_TO_PUSH="promote-changes/$TS/$USER_NAME"
echo_info "Pushing changes to remote branch '$BRANCH_TO_PUSH'"
git push "$REMOTE_NAME" "main:$BRANCH_TO_PUSH"
case $OSTYPE in
"linux-gnu"*)
xdg-open "https://github.com/opentensor/prompting/pull/new/$BRANCH_TO_PUSH"
;;
"darwin"*)
open "https://github.com/opentensor/prompting/pull/new/$BRANCH_TO_PUSH"
;;
*)
echo_warning "OS type '$OSTYPE' not supported."
echo_good_news "You can open the PR with the following URL: https://github.com/opentensor/prompting/pull/new/$BRANCH_TO_PUSH"
;;
esac