Skip to content

Commit 595e7e8

Browse files
author
Fabiana Severin
committed
Making headers scripts use patch files
1 parent 160d714 commit 595e7e8

File tree

4 files changed

+39
-63
lines changed

4 files changed

+39
-63
lines changed

scripts/add-headers.sh

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,23 @@
44

55
set -e
66

7-
# Create patch file
8-
patch_file=$(mktemp)
9-
trap "rm -f $patch_file" EXIT
7+
script_dir="$(dirname "$0")"
8+
files_modified=0
109

11-
# Generate patch for missing headers
12-
files_count=0
13-
git ls-files 'bin/**' 'scripts/**' 'src/**' 'test/**' | \
14-
grep -E '\.(js|ts|mjs|mts|jsx|tsx|c|cpp|h|sh)$' | \
15-
while read file; do
16-
if ! grep -q 'Copyright.*Amazon\.com' "$file"; then
17-
files_count=$((files_count + 1))
18-
19-
if [[ "$file" == *.sh ]]; then
20-
header="#!/bin/bash\n# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n# SPDX-License-Identifier: Apache-2.0\n\n"
21-
else
22-
header="/*\nCopyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\nSPDX-License-Identifier: Apache-2.0\n*/\n\n"
23-
fi
24-
25-
# Get first line for context
26-
first_line=$(head -n1 "$file" 2>/dev/null || echo "")
27-
28-
# Create patch entry
29-
echo "--- a/$file" >> "$patch_file"
30-
echo "+++ b/$file" >> "$patch_file"
31-
echo "@@ -1,1 +1,$(echo -e "$header" | wc -l) @@" >> "$patch_file"
32-
echo -e "$header" | sed 's/^/+/' >> "$patch_file"
33-
if [ -n "$first_line" ]; then
34-
echo " $first_line" >> "$patch_file"
35-
fi
10+
while IFS= read -r file; do
11+
if ! grep -q 'Copyright.*Amazon\.com' "$file"; then
12+
if [[ "$file" == *.sh ]]; then
13+
sed "s|PLACEHOLDER|$file|" "$script_dir/patches/sh-files.patch" | git apply
14+
else
15+
sed "s|PLACEHOLDER|$file|" "$script_dir/patches/js-files.patch" | git apply
3616
fi
37-
done
17+
files_modified=$((files_modified + 1))
18+
fi
19+
done < <(git ls-files 'bin/**' 'scripts/**' 'src/**' 'test/**' | grep -E '\.(js|ts|mjs|mts|jsx|tsx|c|cpp|h|sh)$')
3820

39-
if [ ! -s "$patch_file" ]; then
21+
if [ "$files_modified" -eq 0 ]; then
4022
echo "✓ All files already have copyright headers"
41-
exit 0
42-
fi
43-
44-
echo "Applying copyright header patch..."
45-
git apply "$patch_file"
46-
47-
echo "✓ Copyright headers added successfully"
48-
echo "Run 'git diff --cached' or 'git diff' to review changes"
23+
else
24+
echo "✓ Copyright headers added to $files_modified files"
25+
echo "Run 'git diff' to review changes"
26+
fi

scripts/check-headers.sh

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,20 @@
44

55
set -e
66

7-
# Create temporary patch to see what would be added
8-
patch_file=$(mktemp)
9-
trap "rm -f $patch_file" EXIT
7+
missing_files=()
108

11-
# Generate patch for missing headers
12-
git ls-files 'bin/**' 'scripts/**' 'src/**' 'test/**' | \
13-
grep -E '\.(js|ts|mjs|mts|jsx|tsx|c|cpp|h|sh)$' | \
14-
while read file; do
15-
if ! grep -q 'Copyright.*Amazon\.com' "$file"; then
16-
if [[ "$file" == *.sh ]]; then
17-
header="#!/bin/bash\n# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n# SPDX-License-Identifier: Apache-2.0\n\n"
18-
else
19-
header="/*\nCopyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\nSPDX-License-Identifier: Apache-2.0\n*/\n\n"
20-
fi
21-
22-
# Create patch entry
23-
echo "--- a/$file" >> "$patch_file"
24-
echo "+++ b/$file" >> "$patch_file"
25-
echo "@@ -1,1 +1,$(echo -e "$header" | wc -l) @@" >> "$patch_file"
26-
echo -e "$header" | sed 's/^/+/' >> "$patch_file"
27-
fi
28-
done
9+
while IFS= read -r file; do
10+
if ! grep -q 'Copyright.*Amazon\.com' "$file"; then
11+
missing_files+=("$file")
12+
fi
13+
done < <(git ls-files 'bin/**' 'scripts/**' 'src/**' 'test/**' | grep -E '\.(js|ts|mjs|mts|jsx|tsx|c|cpp|h|sh)$')
2914

30-
if [ -s "$patch_file" ]; then
15+
if [ ${#missing_files[@]} -gt 0 ]; then
3116
echo "❌ Copyright header check failed."
32-
echo "Files missing headers (patch preview):"
33-
cat "$patch_file"
17+
echo "Files missing headers:"
18+
printf ' %s\n' "${missing_files[@]}"
3419
echo
35-
echo "Run 'npm run add-headers' to apply these changes."
20+
echo "Run 'npm run add-headers' to fix these files."
3621
exit 1
3722
fi
3823

scripts/patches/js-files.patch

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--- /dev/null
2+
+++ b/PLACEHOLDER
3+
@@ -0,0 +1,4 @@
4+
+/*
5+
+Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6+
+SPDX-License-Identifier: Apache-2.0
7+
+*/

scripts/patches/sh-files.patch

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--- /dev/null
2+
+++ b/PLACEHOLDER
3+
@@ -0,0 +1,3 @@
4+
+#!/bin/bash
5+
+# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6+
+# SPDX-License-Identifier: Apache-2.0

0 commit comments

Comments
 (0)