Skip to content

Commit 160d714

Browse files
author
Fabiana Severin
committed
Making the add and check headers scripts use git diff and patches
1 parent 523de7c commit 160d714

File tree

2 files changed

+62
-38
lines changed

2 files changed

+62
-38
lines changed

scripts/add-headers.sh

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,45 @@
44

55
set -e
66

7-
# Find files missing copyright headers
8-
files=$(git ls-files 'bin/**' 'scripts/**' 'src/**' 'test/**' | \
9-
grep -E '\.(js|ts|mjs|mts|jsx|tsx|c|cpp|h)$' | \
10-
xargs grep -L 'Copyright.*Amazon\.com' || true)
11-
12-
if [ -z "$files" ]; then
7+
# Create patch file
8+
patch_file=$(mktemp)
9+
trap "rm -f $patch_file" EXIT
10+
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
36+
fi
37+
done
38+
39+
if [ ! -s "$patch_file" ]; then
1340
echo "✓ All files already have copyright headers"
1441
exit 0
1542
fi
1643

17-
echo "Found $(echo "$files" | wc -l) files missing headers"
18-
19-
# Add headers
20-
for file in $files; do
21-
if [[ "$file" == *.sh ]]; then
22-
header="#!/bin/bash
23-
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
24-
# SPDX-License-Identifier: Apache-2.0
25-
26-
"
27-
else
28-
header="/*
29-
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
30-
SPDX-License-Identifier: Apache-2.0
31-
*/
32-
33-
"
34-
fi
35-
36-
echo "$header$(cat "$file")" > "$file"
37-
echo "✓ Added header to $file"
38-
done
44+
echo "Applying copyright header patch..."
45+
git apply "$patch_file"
3946

40-
echo
41-
echo "✓ All copyright headers added successfully"
42-
echo "Run 'git diff' to review changes before committing"
47+
echo "✓ Copyright headers added successfully"
48+
echo "Run 'git diff --cached' or 'git diff' to review changes"

scripts/check-headers.sh

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

55
set -e
66

7-
# Find files missing copyright headers
8-
files=$(git ls-files 'bin/**' 'scripts/**' 'src/**' 'test/**' | \
7+
# Create temporary patch to see what would be added
8+
patch_file=$(mktemp)
9+
trap "rm -f $patch_file" EXIT
10+
11+
# Generate patch for missing headers
12+
git ls-files 'bin/**' 'scripts/**' 'src/**' 'test/**' | \
913
grep -E '\.(js|ts|mjs|mts|jsx|tsx|c|cpp|h|sh)$' | \
10-
xargs grep -L 'Copyright.*Amazon\.com' || true)
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
1129

12-
if [ -n "$files" ]; then
30+
if [ -s "$patch_file" ]; then
1331
echo "❌ Copyright header check failed."
14-
echo "Files missing required \"Copyright\" and \"Amazon.com\" keywords:"
15-
echo "$files" | sed 's/^/ - /'
32+
echo "Files missing headers (patch preview):"
33+
cat "$patch_file"
1634
echo
17-
echo "Run 'npm run add-headers' to fix these issues."
35+
echo "Run 'npm run add-headers' to apply these changes."
1836
exit 1
1937
fi
2038

0 commit comments

Comments
 (0)