From cbcb2f00b9daaa8f75694d40e40950c2607603d3 Mon Sep 17 00:00:00 2001 From: Daniel Frederico Lins Leite Date: Wed, 10 Jul 2024 06:20:50 +0100 Subject: [PATCH] uncomment update contract update check (#6247) ## Description - Uncomment clean git status - allow interactive update with the `-i` flag. ## Checklist - [ ] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [ ] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: IGI-111 Co-authored-by: Joshua Batty --- test/update-contract-ids.sh | 68 ++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 8 deletions(-) diff --git a/test/update-contract-ids.sh b/test/update-contract-ids.sh index 23c82db18a9..591a2a7052d 100755 --- a/test/update-contract-ids.sh +++ b/test/update-contract-ids.sh @@ -1,10 +1,23 @@ #! /bin/bash -# CHANGES=$(git status --porcelain | wc -l) -# if [ "$CHANGES" != "0" ]; then -# echo "git state is not clean. commit or restore first." -# exit -# fi +print_help() { + echo " + -i run interactively +"; +} + +INTERACTIVELY="0" + +while [[ "$#" -gt 0 ]]; do + case $1 in + -i) INTERACTIVELY="1"; shift ;; + *) print_help; exit 1 ;; + esac + shift +done + + +CHANGES=$(git status --porcelain | wc -l) BOLD_RED='\033[1;31m' BOLD_GREEN="\033[1;32m" @@ -19,13 +32,27 @@ function join_by { fi } +ask_confirmation() { + echo -e "${BOLD_WHITE}Command below needs confirmation before running.${NC} $2" + echo "> $1" + read -p "Run the command above? (y/n) " -n 1 -r > /dev/null - sed -i "${LINE}s/0x[a-zA-Z0-9]*/$CONTRACT_ID/g" $FILE - echo -e " ${BOLD_GREEN}ok${NC} ($CONTRACT_ID)" + + SED_EXPR="${LINE}s/0x[a-zA-Z0-9]*/$CONTRACT_ID/g" + + # check if there is a diff + diff -s --color <(cat $FILE) <(cat $FILE | sed --expression="$SED_EXPR") > /dev/null + if [ $? -eq 0 ]; then + # no diff, continue + echo -e " ${BOLD_GREEN}no changes needed${NC} ($CONTRACT_ID)" + else + # diff detected, check we are clean to update files, if not abort + if [[ "$INTERACTIVELY" == "0" ]]; then + # DonĀ“t change anything if git is dirty + if [ "$CHANGES" != "0" ]; then + echo -e " ${BOLD_RED}Aborting${NC} This contract id needs update, but git state is not clean. commit, restore first or run with \"-i\"." + echo $FILE + diff -s --color <(cat $FILE) <(cat $FILE | sed --expression="$SED_EXPR") + exit + fi + # we are clean and can update files + sed -i "$SED_EXPR" $FILE + else + # ask confirmation before applying the change + diff -s --color <(cat $FILE) <(cat $FILE | sed --expression="$SED_EXPR") + ask_confirmation "sed -i \"$SED_EXPR\" $FILE" "Update contract id" + fi + echo -e " ${BOLD_GREEN}updated${NC} ($CONTRACT_ID)" + fi else echo -e " ${BOLD_RED}error${NC}" cargo r -p forc --release -- contract-id --release