generated from Chemaclass/bash-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract compare,release,validate functions
- Loading branch information
1 parent
8f85821
commit b6bd800
Showing
6 changed files
with
167 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
|
||
|
||
function main::compare_branch_with_target() { | ||
local source=$1 | ||
local target=$2 | ||
local changed_files=$3 | ||
|
||
echo -e "Comparing ${COLOR_ORANGE}$source${COLOR_RESET} with ${COLOR_ORANGE}$target${COLOR_RESET}" | ||
echo -e "${COLOR_BLUE}============================================${COLOR_RESET}" | ||
echo -e "Commits to include in the release (into ${COLOR_ORANGE}$target${COLOR_RESET}):" | ||
echo "$(git log --color --oneline origin/"$target".."$source")" | ||
echo -e "${COLOR_BLUE}============================================${COLOR_RESET}" | ||
echo -e "Changed files between '${COLOR_ORANGE}$target${COLOR_RESET}' and '${COLOR_ORANGE}$source${COLOR_RESET}':" | ||
compare::render_changed_files "$source" "$target" | ||
echo -e "${COLOR_PURPLE}============================================${COLOR_RESET}" | ||
} | ||
|
||
|
||
function compare::render_changed_files() { | ||
local source=$1 | ||
local target=$2 | ||
|
||
local added_files=() | ||
local modified_files=() | ||
local deleted_files=() | ||
|
||
# Collect files based on their status | ||
while read status file; do | ||
case "$status" in | ||
A) # Added (created) | ||
added_files+=("$file") | ||
;; | ||
M) # Modified (updated) | ||
modified_files+=("$file") | ||
;; | ||
D) # Deleted | ||
deleted_files+=("$file") | ||
;; | ||
esac | ||
done < <(git diff --name-status "$target".."$source") | ||
|
||
# Output the files, sorted by status | ||
|
||
# Added (created) files | ||
for file in "${added_files[@]}"; do | ||
echo -e "${COLOR_GREEN}+ $file${COLOR_RESET}" | ||
done | ||
|
||
# Modified (updated) files | ||
for file in "${modified_files[@]}"; do | ||
echo -e "${COLOR_YELLOW}~ $file${COLOR_RESET}" | ||
done | ||
|
||
# Deleted files | ||
for file in "${deleted_files[@]}"; do | ||
echo -e "${COLOR_RED}- $file${COLOR_RESET}" | ||
done | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
|
||
function release::generate_new_tag() { | ||
local latest_tag=$1 | ||
local changed_files=$2 | ||
|
||
local tag_number | ||
if [[ $latest_tag =~ ^v([0-9]+)$ ]]; then | ||
tag_number="${BASH_REMATCH[1]}" | ||
tag_number=$((tag_number + 1)) | ||
else | ||
tag_number=1 | ||
fi | ||
|
||
echo "v$tag_number" | ||
} | ||
|
||
function release::create_tag() { | ||
local new_tag=$1 | ||
local changed_files=$2 | ||
git tag -a "$new_tag" -m "Release $new_tag | ||
Changes: | ||
$changed_files" | ||
|
||
git push origin --tags --no-verify | ||
} | ||
|
||
# shellcheck disable=SC2155 | ||
function release::create_github_release() { | ||
if [ "$GH_CLI_INSTALLED" = false ]; then | ||
return | ||
fi | ||
|
||
local previous_tag=$1 | ||
local new_tag=$2 | ||
|
||
if [ "$previous_tag" = "v0" ]; then | ||
previous_tag="main" | ||
fi | ||
|
||
local commits=$(git log --oneline "$previous_tag".."$new_tag") | ||
local release_name=$(release::generate_release_name) | ||
local remote_url=$(git remote get-url origin) | ||
local repo_info=$(echo "$remote_url" | sed -E 's|git@github\.com:||; s|\.git$||') | ||
|
||
local changelog_url="https://github.com/$repo_info/compare/$previous_tag...$new_tag" | ||
local full_changelog="**Full Changelog**: $changelog_url" | ||
gh release create "$new_tag" \ | ||
--title "$release_name" \ | ||
--notes "$(echo -e "$commits\n\n$full_changelog")" | ||
} | ||
|
||
# shellcheck disable=SC2155 | ||
function release::generate_release_name() { | ||
local current_date=$(date +"%Y-%m-%d") | ||
# Use xargs to trim leading/trailing whitespace | ||
local latest_release=$(gh release list | head -n 1 | awk -F 'Latest' '{print $1}' | xargs) | ||
|
||
local release_number | ||
if [[ "$latest_release" =~ ^$current_date\ #([0-9]+)$ ]]; then | ||
release_number=$((BASH_REMATCH[1] + 1)) | ||
else | ||
release_number=1 | ||
fi | ||
|
||
echo "$current_date #$release_number" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
|
||
function validate::no_diff_between_local_and_origin() { | ||
local SOURCE_BRANCH=$1 | ||
local target_branch=$2 | ||
# Check the status of the local branch compared to origin/main | ||
ahead_commits=$(git rev-list --count HEAD ^origin/main) | ||
|
||
if [[ "$ahead_commits" -gt 0 && "$FORCE_DEPLOY" == false ]]; then | ||
echo -e "${COLOR_RED}Error: Your $SOURCE_BRANCH is ahead of 'origin/main' by $ahead_commits commit(s)${COLOR_RESET}." | ||
echo -e "${COLOR_YELLOW}Please push your changes or reset your${COLOR_RESET} ${COLOR_ORANGE}$SOURCE_BRANCH${COLOR_RESET}." | ||
exit 1 | ||
fi | ||
if [[ "$ahead_commits" -gt 0 && "$FORCE_DEPLOY" == true ]]; then | ||
echo -e "Your local ${COLOR_ORANGE}$SOURCE_BRANCH${COLOR_RESET} is ahead of ${COLOR_ORANGE}origin/$SOURCE_BRANCH${COLOR_RESET} by ${COLOR_RED}$ahead_commits${COLOR_RESET} commit(s)${COLOR_RESET}." | ||
io::confirm_or_exit "${COLOR_YELLOW}Are you sure you want to push them to 'origin/$target_branch' as part of the release?${COLOR_RESET}" | ||
fi | ||
} |