From 83d63d5b5ca786f65361eb2ae0236e0ca1d6d69c Mon Sep 17 00:00:00 2001 From: PanGan21 Date: Mon, 24 Jun 2024 11:13:49 +0300 Subject: [PATCH 1/3] feat: add pre commit hook that checks if make proto-all should be run --- scripts/hooks/pre-commit.sh | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/scripts/hooks/pre-commit.sh b/scripts/hooks/pre-commit.sh index 8c7cf14ae9e..2d36083cb8d 100755 --- a/scripts/hooks/pre-commit.sh +++ b/scripts/hooks/pre-commit.sh @@ -30,5 +30,24 @@ function lint_and_add_modified_go_files() { done } +function is_proto_all_required() { + local before_files=$(git status --porcelain | awk '{print $2}') + make proto-all + local after_files=$(git status --porcelain | awk '{print $2}') + local changed_files=$(comm -13 <(echo "$before_files" | sort) <(echo "$after_files" | sort)) + if [[ -n "$changed_files" ]]; then + echo "Error: Please run 'make proto-all' and commit the updated files." + + # Revert the changes made by make proto-all + for file in $changed_files; do + echo "Reverting changes in $file" + git checkout -- "$file" + done + + exit 1 + fi +} + check_golangci_lint_version +is_proto_all_required lint_and_add_modified_go_files From 78480f0d23405396e02de43038b7ca4da660e753 Mon Sep 17 00:00:00 2001 From: PanGan21 Date: Tue, 2 Jul 2024 21:32:01 +0300 Subject: [PATCH 2/3] fix: run make proto-all and add the generated files to git index --- scripts/hooks/pre-commit.sh | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/scripts/hooks/pre-commit.sh b/scripts/hooks/pre-commit.sh index 2d36083cb8d..11a90638915 100755 --- a/scripts/hooks/pre-commit.sh +++ b/scripts/hooks/pre-commit.sh @@ -30,24 +30,34 @@ function lint_and_add_modified_go_files() { done } -function is_proto_all_required() { +function run_proto_all_if_needed() { local before_files=$(git status --porcelain | awk '{print $2}') + + # Run make proto-all make proto-all + local after_files=$(git status --porcelain | awk '{print $2}') local changed_files=$(comm -13 <(echo "$before_files" | sort) <(echo "$after_files" | sort)) + if [[ -n "$changed_files" ]]; then - echo "Error: Please run 'make proto-all' and commit the updated files." - - # Revert the changes made by make proto-all + echo "The following files have been modified by 'make proto-all' and have been added to the git index:" for file in $changed_files; do - echo "Reverting changes in $file" - git checkout -- "$file" + echo "$file" + git add "$file" done - - exit 1 + + # Add the modified .proto files as well + local modified_proto_files=$(echo "$before_files" "$after_files" | tr ' ' '\n' | grep '\.proto$' | sort | uniq) + if [[ -n "$modified_proto_files" ]]; then + echo "The following .proto files have been modified and have been added to the git index:" + for proto_file in $modified_proto_files; do + echo "$proto_file" + git add "$proto_file" + done + fi fi } check_golangci_lint_version -is_proto_all_required +run_proto_all_if_needed lint_and_add_modified_go_files From fccc001bf127cd83890a41ca13f6e14f5fdf46cb Mon Sep 17 00:00:00 2001 From: PanGan21 Date: Fri, 5 Jul 2024 13:54:55 +0300 Subject: [PATCH 3/3] udpate script to only run make proto-all and don't add to git --- scripts/hooks/pre-commit.sh | 40 +++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/scripts/hooks/pre-commit.sh b/scripts/hooks/pre-commit.sh index 11a90638915..4c32171e60c 100755 --- a/scripts/hooks/pre-commit.sh +++ b/scripts/hooks/pre-commit.sh @@ -31,29 +31,25 @@ function lint_and_add_modified_go_files() { } function run_proto_all_if_needed() { - local before_files=$(git status --porcelain | awk '{print $2}') - - # Run make proto-all - make proto-all - - local after_files=$(git status --porcelain | awk '{print $2}') - local changed_files=$(comm -13 <(echo "$before_files" | sort) <(echo "$after_files" | sort)) - - if [[ -n "$changed_files" ]]; then - echo "The following files have been modified by 'make proto-all' and have been added to the git index:" - for file in $changed_files; do - echo "$file" - git add "$file" - done - - # Add the modified .proto files as well - local modified_proto_files=$(echo "$before_files" "$after_files" | tr ' ' '\n' | grep '\.proto$' | sort | uniq) - if [[ -n "$modified_proto_files" ]]; then - echo "The following .proto files have been modified and have been added to the git index:" - for proto_file in $modified_proto_files; do - echo "$proto_file" - git add "$proto_file" + local proto_files_modified="$(git diff --name-only --diff-filter=d | grep '\.proto$')" + + if [[ -n "$proto_files_modified" ]]; then + echo "Detected changes in .proto files. Running 'make proto-all'." + local before_files=$(git status --porcelain | awk '{print $2}') + + # Run make proto-all + make proto-all + + local after_files=$(git status --porcelain | awk '{print $2}') + local changed_files=$(comm -13 <(echo "$before_files" | sort) <(echo "$after_files" | sort)) + + if [[ -n "$changed_files" ]]; then + echo "Running 'make proto-all' resulted in changes." + echo "The following files have been modified by 'make proto-all':" + for file in $changed_files; do + echo "$file" done + exit 1 fi fi }