-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathlibrary.sh
More file actions
76 lines (76 loc) · 2.12 KB
/
library.sh
File metadata and controls
76 lines (76 loc) · 2.12 KB
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
#!/usr/bin/env bash
# Common functions for Kubeflow manifest synchronization scripts
setup_error_handling() {
set -euxo pipefail
IFS=$'\n\t'
}
# Check if the git repository has uncommitted changes
check_uncommitted_changes() {
if [ -n "$(git status --porcelain)" ]; then
echo "WARNING: You have uncommitted changes"
fi
}
# Create a new git branch if it doesn't exist
create_branch() {
local branch="$1"
check_uncommitted_changes
if [ $(git branch --list "$branch") ]; then
echo "WARNING: Branch $branch already exists."
fi
if ! git show-ref --verify --quiet refs/heads/$branch; then
git checkout -b "$branch"
else
echo "Branch $branch already exists."
fi
}
clone_and_checkout() {
local source_directory="$1"
local repository_url="$2"
local repository_directory="$3"
local commit="$4"
mkdir -p "$source_directory"
cd "$source_directory"
# Clone repository if it doesn't exist
if [ ! -d "$repository_directory/.git" ]; then
git clone "$repository_url" "$repository_directory"
fi
# Checkout to specific commit
cd "$source_directory/$repository_directory"
if ! git rev-parse --verify --quiet "$commit"; then
git checkout -b "$commit"
else
git checkout "$commit"
fi
check_uncommitted_changes
}
# Copy manifests from source to destination
copy_manifests() {
local source="$1"
local destination="$2"
if [ -d "$destination" ]; then
rm -r "$destination"
fi
cp "$source" "$destination" -r
}
# Update README with new commit reference
update_readme() {
local manifests_directory="$1"
local source_text="$2"
local destination_text="$3"
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i "" "s|$source_text|$destination_text|g" "${manifests_directory}/README.md" # BSD sed of Mac OSX
else
sed -i "s|$source_text|$destination_text|g" "${manifests_directory}/README.md" # GNU sed of Linux
fi
}
# Commit changes to git repository
commit_changes() {
local manifests_directory="$1"
local commit_message="$2"
local paths_to_add=("${@:3}")
cd "$manifests_directory"
for path in "${paths_to_add[@]}"; do
git add "$path"
done
git commit -s -m "$commit_message"
}