-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathupdate_git_repository
46 lines (42 loc) · 1.99 KB
/
update_git_repository
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
update_git_repository() {
local repo_name="$1"
local repo_url="$2"
local _venv="$3" # Accepting the new _venv argument
local repo_dir="/opt/$repo_name"
if [ -d "$repo_dir/.git" ]; then
echo "Updating existing repository $repo_name..."
cd "$repo_dir" || return
# Clean up any unstaged changes or state that could block our actions
echo $key | sudo -S git reset --hard
echo $key | sudo -S git clean -fdx
# Attempt to pull the latest changes
if ! echo $key | sudo -S git pull; then
echo "git pull encountered conflicts or errors. Removing and re-cloning the repository."
# Ensure we are not in the directory we're about to delete
cd /opt || return
echo $key | sudo -S rm -rf "$repo_dir"
echo $key | sudo -S git clone "$repo_url" "$repo_dir"
echo $key | sudo -S chown -R $USER:$USER "$repo_dir"
fi
else
echo "Cloning new repository $repo_name..."
echo $key | sudo -S git clone "$repo_url" "$repo_dir"
echo $key | sudo -S chown -R $USER:$USER "$repo_dir"
fi
# After cloning or pulling changes, handle Python dependencies
echo $key | sudo -S chown -R $USER:$USER "$repo_dir"
if [ -f "$repo_dir/requirements.txt" ]; then
if [[ -n $_venv ]]; then
echo "Setting up Python virtual environment and installing dependencies..."
python -m venv "${repo_dir}/${repo_name}-venv" && \
source "${repo_dir}/${repo_name}-venv/bin/activate" && \
pip install -r "${repo_dir}/requirements.txt" && \
echo "Dependencies installed successfully in the virtual environment." || echo "Failed to install dependencies in the virtual environment."
deactivate
else
echo "Installing dependencies globally..."
sudo pip install -r "${repo_dir}/requirements.txt" && \
echo "Dependencies installed successfully globally." || echo "Failed to install dependencies globally."
fi
fi
}