From 1b5e13e2758c0c5eb79201abaa36b9a47df947fe Mon Sep 17 00:00:00 2001 From: godcong Date: Fri, 15 Nov 2024 18:39:16 +0800 Subject: [PATCH] feat(scripts): improve tag.sh script and related functionality - Update tag.sh to handle stashed changes and branch switching - Refactor test.sh and upm.sh scripts for better readability and maintainability - Improve error handling and add more informative output in all scripts --- scripts/tag.sh | 43 ++++++++++++------- scripts/test.sh | 108 ++++++++++++++++++++++++------------------------ scripts/upm.sh | 20 ++++----- 3 files changed, 92 insertions(+), 79 deletions(-) diff --git a/scripts/tag.sh b/scripts/tag.sh index ea5ceae..4b286ef 100644 --- a/scripts/tag.sh +++ b/scripts/tag.sh @@ -13,20 +13,20 @@ check_for_go_mod() { local dir="$1" local go_mod_name="go.mod" local updated=1 # Assume not updated by default - + # Change to the directory cd "$dir" || return 1 - + # Check if the go.mod file exists in the directory if [ -f "$go_mod_name" ]; then echo "Find dependencies go.mod in directories:" echo " ->DIR: $dir" updated=0 fi - + # Return to the original working directory for the next iteration cd "$ORIGINAL_DIR" || return 1 - + # Return the update status return $updated } @@ -37,22 +37,22 @@ function_checks() { echo "Error: get_latest_tag function is not defined" exit 1 fi - + if ! declare -f get_matching_tags >/dev/null; then echo "Error: get_matching_tags function is not defined" exit 1 fi - + if ! declare -f get_head_version_tag >/dev/null; then echo "Error: get_head_version_tag function is not defined" exit 1 fi - + if ! declare -f get_next_module_version >/dev/null; then echo "Error: get_next_module_version function is not defined" exit 1 fi - + if ! declare -f get_latest_module_tag >/dev/null; then echo "Error: get_latest_module_tag function is not defined" exit 1 @@ -65,11 +65,11 @@ handle_go_mod_directory() { local module="main" local module_name module_name="$(echo "$dir" | sed "s/^.\///")" # Drop the beginning './' - + if [ "$module_name" != "." ]; then module="$module_name" fi - + if check_for_go_mod "$dir"; then local HEAD_TAG local LATEST_TAG @@ -85,15 +85,15 @@ handle_go_mod_directory() { echo " ->LATEST_TAG: $LATEST_TAG" NEXT_TAG=$(get_next_module_version "$module_name") echo " ->NEXT_TAG: $NEXT_TAG" - + if [ -z "$HEAD_TAG" ]; then echo "Creating new tag: $NEXT_TAG" create_new_tag "$NEXT_TAG" fi - + echo "" fi - + } # Define a function to traverse directories and apply the handle_go_mod_directory function @@ -101,15 +101,24 @@ add_go_mod_tag() { echo "Checking for go.mod files..." function_checks local module_name="$1" - + echo "" echo "COMMIT_HASH: $(get_current_commit_hash "$module_name")" echo "" + local current_branch="main" + current_branch=$(git branch --show-current) + echo "Current branch: $current_branch" + + # Save all local changes to the Git stash + echo "Saving all local changes and change to the main branch..." + git stash save --include-untracked + git checkout -B "main" "original/main" + # If a module_name is specified, process only that directory if [ "$module_name" == "." ]; then handle_go_mod_directory "." - elif [ -n "$module_name" ]; then + elif [ -n "$module_name" ]; then handle_go_mod_directory "$module_name" else # Skip the root directory ('.') @@ -120,6 +129,10 @@ add_go_mod_tag() { handle_go_mod_directory "$dir" done fi + + # Restore the original branch and pop the stash + git checkout "$current_branch" + git stash pop } add_go_mod_tag "$1" diff --git a/scripts/test.sh b/scripts/test.sh index bc7adde..65475e0 100644 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -6,64 +6,64 @@ ROOT_DIR=$1 # Define a function to check for the existence of a go.mod file in a directory and perform corresponding actions check_go_mod_and_test() { - local dir="$1" - local go_mod_name="go.mod" - local need_test=1 # Assume not need_test by default - - # Change to the directory - cd "$dir" || return 1 - - # Check if the go.mod file exists in the directory - if [ -f "$go_mod_name" ]; then - # If it exists, perform specific operations - - # go get -u ./... - # go mod tidy - -# local test_status=$? - # Only mark as need_test if tests pass -# if [ $test_status -eq 0 ]; then - need_test=0 -# fi - fi - - # Return to the original working directory - cd "$ORIGINAL_DIR" || return 1 - # Return the update status - return $need_test + local dir="$1" + local go_mod_name="go.mod" + local need_test=1 # Assume not need_test by default + + # Change to the directory + cd "$dir" || return 1 + + # Check if the go.mod file exists in the directory + if [ -f "$go_mod_name" ]; then + # If it exists, perform specific operations + + # go get -u ./... + # go mod tidy + + # local test_status=$? + # Only mark as need_test if tests pass + # if [ $test_status -eq 0 ]; then + need_test=0 + # fi + fi + + # Return to the original working directory + cd "$ORIGINAL_DIR" || return 1 + # Return the update status + return $need_test } # Define a function to traverse directories and apply the check_go_mod_and_act function check_all_go_mod() { - local start_dir="$1" - - # If a module_name is specified, process only that directory - if [ "$start_dir" != "." ]; then - # if check_go_mod_and_test "./$module_name"; then - # echo "Processing directories in: ./$module_name" - # local need_test=$? - # if [ $need_test -eq 0 ]; then - # git_commit_changes "./$module_name" - # fi - start_dir="./$start_dir" - else - start_dir="." - fi - - # Skip the root directory ('.') - find "$start_dir" -mindepth 1 -type d -not -path "./.*" -print0 | while IFS= read -r -d '' dir; do - - cd "$ORIGINAL_DIR" || return 1 - if check_go_mod_and_test "$dir"; then - local need_test=$? - if [ $need_test -eq 0 ]; then - echo "Testing packages in: $dir" - cd "$dir" || continue - go mod tidy || return 1 - go test -race ./... || return 1 - fi - fi - done + local start_dir="$1" + + # If a module_name is specified, process only that directory + if [ "$start_dir" != "." ]; then + # if check_go_mod_and_test "./$module_name"; then + # echo "Processing directories in: ./$module_name" + # local need_test=$? + # if [ $need_test -eq 0 ]; then + # git_commit_changes "./$module_name" + # fi + start_dir="./$start_dir" + else + start_dir="." + fi + + # Skip the root directory ('.') + find "$start_dir" -mindepth 1 -type d -not -path "./.*" -print0 | while IFS= read -r -d '' dir; do + + cd "$ORIGINAL_DIR" || return 1 + if check_go_mod_and_test "$dir"; then + local need_test=$? + if [ $need_test -eq 0 ]; then + echo "Testing packages in: $dir" + cd "$dir" || continue + go mod tidy || return 1 + go test -race ./... || return 1 + fi + fi + done } check_all_go_mod "$ROOT_DIR" diff --git a/scripts/upm.sh b/scripts/upm.sh index e19518a..2d98819 100644 --- a/scripts/upm.sh +++ b/scripts/upm.sh @@ -9,17 +9,17 @@ check_go_mod_and_act() { local dir="$1" local go_mod_name="go.mod" local updated=1 # Assume not updated by default - + # Change to the directory cd "$dir" || return 1 - + # Check if the go.mod file exists in the directory if [ -f "$go_mod_name" ]; then # If it exists, perform specific operations echo "Updating packages in: $dir" go get -u ./... go mod tidy - + go test -race ./... || true local test_status=$? # Only mark as updated if tests pass @@ -27,7 +27,7 @@ check_go_mod_and_act() { updated=0 fi fi - + # Return to the original working directory cd "$ORIGINAL_DIR" || return 1 # Return the update status @@ -39,20 +39,20 @@ git_commit_changes() { local dir="$1" local module_name="" local commit_message="" - + # Change to the directory cd "$dir" || return - + # Construct the commit message, including the module name # the module name must be the directory name without the beginning './' # otherwise, the commit message will be incorrect module_name=$(echo "$dir" | sed "s/^.\///") # Drop the beginning './' - + echo "Checking update ${module_name}/(go.mod|go.sum)" if git status --porcelain | grep -q -E "^[ ]?(M)[ ]? ${module_name}/(go\.mod|go\.sum)$"; then # Add go.mod and go.sum to the Git staging area git add go.mod go.sum - + commit_message="feat($module_name): Update go.mod and go.sum for ${module_name}" echo "Committing changes in [$module_name] with message: $commit_message" # Commit the changes @@ -60,7 +60,7 @@ git_commit_changes() { else echo "No changes to commit in [$module_name]" fi - + # Return to the original working directory cd "$ORIGINAL_DIR" || return } @@ -68,7 +68,7 @@ git_commit_changes() { # Define a function to traverse directories and apply the check_go_mod_and_act function update_go_mod() { local module_name="$1" - + # If a module_name is specified, process only that directory if [ -n "$module_name" ]; then if check_go_mod_and_act "./$module_name"; then