-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathinstall.sh
executable file
·162 lines (125 loc) · 5.18 KB
/
install.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
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
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
#script for installing gitpmoji. oneliner
# to run it as one liner you can use this command:
# curl -s https://raw.githubusercontent.com/Fl0p/gitpmoji/main/install.sh | bash
CURRENT_DIR=$(pwd)
#check if jq is installed
if ! command -v jq &> /dev/null
then
echo "jq could not be found, installing it"
brew install jq
fi
echo "Current dir: $CURRENT_DIR"
TOP_LEVEL_GIT_DIR=$(git rev-parse --show-toplevel 2>/dev/null || echo ".")
echo "Top level project dir: $TOP_LEVEL_GIT_DIR"
ls -la $TOP_LEVEL_GIT_DIR
echo "Enter dir name where gitpmoji scripts will be installed. use '.' for current dir. just press enter for default 'gitpmoji'"
read -p "GITPMOJI_DIR=" GITPMOJI_DIR
if [ -z "$GITPMOJI_DIR" ]; then
GITPMOJI_DIR="gitpmoji"
fi
GITPMOJI_INSTALL_DIR="$TOP_LEVEL_GIT_DIR/$GITPMOJI_DIR"
echo "gitpmoji will be installed in $GITPMOJI_INSTALL_DIR"
mkdir -p $GITPMOJI_INSTALL_DIR
cd $GITPMOJI_INSTALL_DIR
pwd
#download from github
curl -o prepare-commit-msg.sh https://raw.githubusercontent.com/Fl0p/gitpmoji/main/prepare-commit-msg.sh
curl -o gpt.sh https://raw.githubusercontent.com/Fl0p/gitpmoji/main/gpt.sh
#make executable
chmod +x prepare-commit-msg.sh
chmod +x gpt.sh
echo "Do you want to add '$GITPMOJI_DIR' directory to gitignore? (y/n)"
read GITPMOJI_ADD_TO_GITIGNORE
if [ "$GITPMOJI_ADD_TO_GITIGNORE" = "y" ]; then
echo "" >> $TOP_LEVEL_GIT_DIR/.gitignore
echo "# ignore gitpmoji directory" >> $TOP_LEVEL_GIT_DIR/.gitignore
echo "$GITPMOJI_DIR" >> $TOP_LEVEL_GIT_DIR/.gitignore
fi
#check if .gitpmoji.env exists
if [ -f .gitpmoji.env ]; then
echo "$GITPMOJI_DIR/.gitpmoji.env already exists, skipping setup environment variables"
echo "--- start of .gitpmoji.env ---"
cat .gitpmoji.env
echo "--- end of .gitpmoji.env ---"
else
echo ".gitpmoji.env does not exist, creating it"
echo "Enter your OpenAI API key (https://platform.openai.com/account/api-keys):"
read -p "GITPMOJI_API_KEY=" api_key
echo "Enter prefix for commit messages which will be untouched as first keyword for each message"
echo "In format of sed RegExp use double backslash (\\\\) for escaping special symbols like {, }, ?, etc."
read -p "GITPMOJI_PREFIX_RX=" prefix
echo "Enter base url for OpenAI API (leave empty for default 'https://api.openai.com/v1')"
read -p "GITPMOJI_API_BASE_URL=" base_url
if [ -z "$base_url" ]; then
base_url="https://api.openai.com/v1"
fi
echo "Enter model for OpenAI API (leave empty for default 'gpt-4o')"
read -p "GITPMOJI_API_MODEL=" model
if [ -z "$model" ]; then
model="gpt-4o"
fi
echo "GITPMOJI_API_KEY=\"$api_key\""
echo "GITPMOJI_PREFIX_RX=\"$prefix\""
echo "GITPMOJI_API_BASE_URL=\"$base_url\""
echo "GITPMOJI_API_MODEL=\"$model\""
cat << EOF > .gitpmoji.env
# Your api key you can get one here https://platform.openai.com/account/api-keys
export GITPMOJI_API_KEY="$api_key"
# Regex for sed command. emoji will be placed after it if found
export GITPMOJI_PREFIX_RX="$prefix"
export GITPMOJI_API_BASE_URL="$base_url"
export GITPMOJI_API_MODEL="$model"
EOF
fi
if [ "$GITPMOJI_ADD_TO_GITIGNORE" != "y" ]; then
echo -e "\033[0;31m Do you want to add environment file '$GITPMOJI_DIR/.gitpmoji.env' to .gitignore to keep your API key secret? (y/n)\033[0m"
read GITPMOJI_ADD_ENV_TO_GITIGNORE
if [ "$GITPMOJI_ADD_ENV_TO_GITIGNORE" = "y" ]; then
echo "" >> $TOP_LEVEL_GIT_DIR/.gitignore
echo "# ignore environment file for gitpmoji" >> $TOP_LEVEL_GIT_DIR/.gitignore
echo "$GITPMOJI_DIR/.gitpmoji.env" >> $TOP_LEVEL_GIT_DIR/.gitignore
fi
fi
cd $TOP_LEVEL_GIT_DIR
echo "Gitpmoji files installed in: $GITPMOJI_INSTALL_DIR"
HOOKS_DIR="$TOP_LEVEL_GIT_DIR/.git/hooks"
echo "git hooks dir: $HOOKS_DIR"
echo "Going to install git hook for prepare-commit-msg"
relative_path() {
local common_part="$1" # for now
local result="." # for now
while [[ "${2#"$common_part"}" == "${2}" ]]; do
# no match, means that candidate common part is not correct
common_part="$(dirname "$common_part")"
result="${result}/.." # move to parent dir in relative path
done
if [[ "$common_part" == "/" ]]; then
# special case for root (no common path)
result="$result/"
fi
# since we now have identified the common part,
# compute the non-common part
local forward_part="${2#"$common_part"}"
# and now stick all parts together
result="${result}${forward_part}"
echo "$result"
}
echo "Looking for relative path between:"
# Get absolute path of gitpmoji install dir
TARGET="$(cd "$GITPMOJI_INSTALL_DIR"; pwd)"
# Get absolute path of the hooks directory
SOURCE="$(cd "$HOOKS_DIR"; pwd)"
echo "TARGET: $TARGET"
echo "SOURCE: $SOURCE"
RELATIVE_PATH=$(relative_path "$SOURCE" "$TARGET")
echo "RELATIVE_PATH: $RELATIVE_PATH"
cd $HOOKS_DIR
echo "Creating symlink for prepare-commit-msg"
echo "ln -sf $RELATIVE_PATH/prepare-commit-msg.sh prepare-commit-msg"
ln -sf $RELATIVE_PATH/prepare-commit-msg.sh prepare-commit-msg
cd $TOP_LEVEL_GIT_DIR
echo "Git hooks successfully installed"
echo "You can now commit with gitpmoji. 🚀"
echo "To uninstall just remove $HOOKS_DIR/prepare-commit-msg and $GITPMOJI_INSTALL_DIR"
exit 0