From bc2a231165ac40bc1fa12b05b8624029fe9af018 Mon Sep 17 00:00:00 2001 From: Aaron Jhaj Date: Tue, 9 Sep 2025 13:45:32 +0100 Subject: [PATCH 1/8] check only files tocuhed in branch --- .../samples-copyright-checker/samples-copyright-checker.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh index cae0811..541b72b 100644 --- a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh +++ b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh @@ -4,8 +4,10 @@ DIRECTORY="$1" FILE_EXTENSIONS="$2" BASE_COPYRIGHT="$3" +CHANGED_FILES=$(git fetch origin main && git diff --name-only origin/main...HEAD) + for ext in $FILE_EXTENSIONS; do - for file in $(find . -type f -name "$ext" -path "$DIRECTORY*"); do + echo "$CHANGED_FILES" | grep "$ext" | grep "^$DIRECTORY" | while read -r file; do echo "Processing file: $file" LAST_MODIFIED_YEAR=$(git log --follow -1 --format="%ad" --date=format:"%Y" -- "$file") @@ -15,7 +17,6 @@ for ext in $FILE_EXTENSIONS; do else # Extract existing copyright line CURRENT_COPYRIGHT=$(grep -o "Copyright IBM Corp. [0-9]\{4\}\(, [0-9]\{4\}\)\?" "$file") - ORIGINAL_YEAR=$(echo "$CURRENT_COPYRIGHT" | grep -o "[0-9]\{4\}" | head -1) # Check if LAST_MODIFIED_YEAR is anywhere in current copyright From c10876917b8ea5ae0c597b85bc972861d024d5dc Mon Sep 17 00:00:00 2001 From: Aaron Jhaj Date: Tue, 9 Sep 2025 13:57:51 +0100 Subject: [PATCH 2/8] retry to fetch git history --- .../samples-copyright-checker.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh index 541b72b..e4e29d3 100644 --- a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh +++ b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh @@ -4,7 +4,14 @@ DIRECTORY="$1" FILE_EXTENSIONS="$2" BASE_COPYRIGHT="$3" -CHANGED_FILES=$(git fetch origin main && git diff --name-only origin/main...HEAD) +# Ensure we have full git history to detect changes +git fetch origin "$GITHUB_BASE_REF" --depth=0 + +# Get list of files changed in the PR compared to the base branch +CHANGED_FILES=$(git diff --name-only origin/"$GITHUB_BASE_REF"...HEAD) + +echo "Files changed in this PR:" +echo "$CHANGED_FILES" for ext in $FILE_EXTENSIONS; do echo "$CHANGED_FILES" | grep "$ext" | grep "^$DIRECTORY" | while read -r file; do From 0a44dd31cbd3ac70d2e86a68e2074b1131728cd1 Mon Sep 17 00:00:00 2001 From: Aaron Jhaj Date: Tue, 9 Sep 2025 14:00:16 +0100 Subject: [PATCH 3/8] use unshallow fetch --- .../samples-copyright-checker/samples-copyright-checker.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh index e4e29d3..83c3fc9 100644 --- a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh +++ b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh @@ -5,7 +5,7 @@ FILE_EXTENSIONS="$2" BASE_COPYRIGHT="$3" # Ensure we have full git history to detect changes -git fetch origin "$GITHUB_BASE_REF" --depth=0 +git fetch --unshallow # Get list of files changed in the PR compared to the base branch CHANGED_FILES=$(git diff --name-only origin/"$GITHUB_BASE_REF"...HEAD) From 4e64dee568c2359d49898cfa223900b7a9bd3c8f Mon Sep 17 00:00:00 2001 From: Aaron Jhaj Date: Tue, 9 Sep 2025 14:02:02 +0100 Subject: [PATCH 4/8] retry fetch --- .../samples-copyright-checker/samples-copyright-checker.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh index 83c3fc9..12358d8 100644 --- a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh +++ b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh @@ -4,8 +4,6 @@ DIRECTORY="$1" FILE_EXTENSIONS="$2" BASE_COPYRIGHT="$3" -# Ensure we have full git history to detect changes -git fetch --unshallow # Get list of files changed in the PR compared to the base branch CHANGED_FILES=$(git diff --name-only origin/"$GITHUB_BASE_REF"...HEAD) From c1eba79669fa8ca0fa290b064b48e374c2c28180 Mon Sep 17 00:00:00 2001 From: Aaron Jhaj Date: Tue, 9 Sep 2025 14:05:57 +0100 Subject: [PATCH 5/8] fix loop --- .../samples-copyright-checker/samples-copyright-checker.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh index 12358d8..f08f1ab 100644 --- a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh +++ b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh @@ -4,15 +4,15 @@ DIRECTORY="$1" FILE_EXTENSIONS="$2" BASE_COPYRIGHT="$3" - # Get list of files changed in the PR compared to the base branch CHANGED_FILES=$(git diff --name-only origin/"$GITHUB_BASE_REF"...HEAD) echo "Files changed in this PR:" echo "$CHANGED_FILES" -for ext in $FILE_EXTENSIONS; do - echo "$CHANGED_FILES" | grep "$ext" | grep "^$DIRECTORY" | while read -r file; do +for file in $CHANGED_FILES; do + # Filter by directory and extension + if [[ "$file" == $DIRECTORY* ]] && [[ "$file" == $ext ]]; then echo "Processing file: $file" LAST_MODIFIED_YEAR=$(git log --follow -1 --format="%ad" --date=format:"%Y" -- "$file") @@ -22,6 +22,7 @@ for ext in $FILE_EXTENSIONS; do else # Extract existing copyright line CURRENT_COPYRIGHT=$(grep -o "Copyright IBM Corp. [0-9]\{4\}\(, [0-9]\{4\}\)\?" "$file") + ORIGINAL_YEAR=$(echo "$CURRENT_COPYRIGHT" | grep -o "[0-9]\{4\}" | head -1) # Check if LAST_MODIFIED_YEAR is anywhere in current copyright From 33f34a5f649d486600d3366f36da3a8d22ccc90e Mon Sep 17 00:00:00 2001 From: Aaron Jhaj Date: Tue, 9 Sep 2025 14:07:15 +0100 Subject: [PATCH 6/8] fix if --- .../samples-copyright-checker/samples-copyright-checker.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh index f08f1ab..a17a1c3 100644 --- a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh +++ b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh @@ -37,5 +37,5 @@ for file in $CHANGED_FILES; do fi fi fi - done + fi done \ No newline at end of file From 1f6b47883460834014381f9756e9b1f5cd8eea7b Mon Sep 17 00:00:00 2001 From: Aaron Jhaj Date: Tue, 9 Sep 2025 16:35:32 +0100 Subject: [PATCH 7/8] process files only in changes files --- .../samples-copyright-checker.sh | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh index a17a1c3..d0ef952 100644 --- a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh +++ b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh @@ -10,32 +10,36 @@ CHANGED_FILES=$(git diff --name-only origin/"$GITHUB_BASE_REF"...HEAD) echo "Files changed in this PR:" echo "$CHANGED_FILES" -for file in $CHANGED_FILES; do - # Filter by directory and extension - if [[ "$file" == $DIRECTORY* ]] && [[ "$file" == $ext ]]; then - echo "Processing file: $file" - - LAST_MODIFIED_YEAR=$(git log --follow -1 --format="%ad" --date=format:"%Y" -- "$file") - - if ! grep -q "Copyright" "$file"; then - echo -e "/**\n * $BASE_COPYRIGHT\n */\n$(cat "$file")" > "$file" - else - # Extract existing copyright line - CURRENT_COPYRIGHT=$(grep -o "Copyright IBM Corp. [0-9]\{4\}\(, [0-9]\{4\}\)\?" "$file") - - ORIGINAL_YEAR=$(echo "$CURRENT_COPYRIGHT" | grep -o "[0-9]\{4\}" | head -1) - - # Check if LAST_MODIFIED_YEAR is anywhere in current copyright - if [[ "$CURRENT_COPYRIGHT" != *"$LAST_MODIFIED_YEAR"* ]]; then - # Check if copyright has two years - if [[ "$CURRENT_COPYRIGHT" =~ ,\ [0-9]{4}$ ]]; then - # If there is already a second year, replace it - sed -i "s/Copyright IBM Corp. $ORIGINAL_YEAR, [0-9]\{4\}/Copyright IBM Corp. $ORIGINAL_YEAR, $LAST_MODIFIED_YEAR/" "$file" - else - # If there is no second year, add it - sed -i "s/$CURRENT_COPYRIGHT/$CURRENT_COPYRIGHT, $LAST_MODIFIED_YEAR/" "$file" +for ext in $FILE_EXTENSIONS; do + for file in $(find . -type f -name "$ext" -path "$DIRECTORY*"); do + file=${file#./} + + if echo "$CHANGED_FILES" | grep -qx "$file"; then + + echo "Processing file: $file" + + LAST_MODIFIED_YEAR=$(git log --follow -1 --format="%ad" --date=format:"%Y" -- "$file") + + if ! grep -q "Copyright" "$file"; then + echo -e "/**\n * $BASE_COPYRIGHT\n */\n$(cat "$file")" > "$file" + else + # Extract existing copyright line + CURRENT_COPYRIGHT=$(grep -o "Copyright IBM Corp. [0-9]\{4\}\(, [0-9]\{4\}\)\?" "$file") + + ORIGINAL_YEAR=$(echo "$CURRENT_COPYRIGHT" | grep -o "[0-9]\{4\}" | head -1) + + # Check if LAST_MODIFIED_YEAR is anywhere in current copyright + if [[ "$CURRENT_COPYRIGHT" != *"$LAST_MODIFIED_YEAR"* ]]; then + # Check if copyright has two years + if [[ "$CURRENT_COPYRIGHT" =~ ,\ [0-9]{4}$ ]]; then + # If there is already a second year, replace it + sed -i "s/Copyright IBM Corp. $ORIGINAL_YEAR, [0-9]\{4\}/Copyright IBM Corp. $ORIGINAL_YEAR, $LAST_MODIFIED_YEAR/" "$file" + else + # If there is no second year, add it + sed -i "s/$CURRENT_COPYRIGHT/$CURRENT_COPYRIGHT, $LAST_MODIFIED_YEAR/" "$file" + fi fi fi - fi - fi + fi + done done \ No newline at end of file From c05ebcccf4c54dd50d5eaa259f32bdc5f2d5b9f8 Mon Sep 17 00:00:00 2001 From: Aaron Jhaj Date: Wed, 10 Sep 2025 10:37:06 +0100 Subject: [PATCH 8/8] added comments --- .../samples-copyright-checker/samples-copyright-checker.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh index d0ef952..b602047 100644 --- a/.github/actions/samples-copyright-checker/samples-copyright-checker.sh +++ b/.github/actions/samples-copyright-checker/samples-copyright-checker.sh @@ -12,10 +12,11 @@ echo "$CHANGED_FILES" for ext in $FILE_EXTENSIONS; do for file in $(find . -type f -name "$ext" -path "$DIRECTORY*"); do + # Remove ./ from start of file string file=${file#./} + # Process file if it has been changed in PR if echo "$CHANGED_FILES" | grep -qx "$file"; then - echo "Processing file: $file" LAST_MODIFIED_YEAR=$(git log --follow -1 --format="%ad" --date=format:"%Y" -- "$file")