Skip to content

Commit c4bbad4

Browse files
jaykoreanmeta-codesync[bot]
authored andcommitted
Update format-diff script to add text to new files (facebook#14143)
Summary: Fixing internal validator failure ``` Every project specific source file must contain a doc block with an appropriate copyright header. Unrelated files must be listed as exceptions in the Copyright Headers Exceptions page in the repo dashboard. A copyright header clearly indicates that the code is owned by Meta. Every open source file must start with a comment containing "Meta Platforms, Inc. and affiliates" https://github.com/facebook/rocksdb/blob/main/buckifier/targets_cfg.py: The first 16 lines of 'buckifier/targets_cfg.py' do not contain the patterns: (Meta Platforms, Inc. and affiliates)|(Facebook, Inc(\.|,)? and its affiliates)|([0-9]{4}-present(\.|,)? Facebook)|([0-9]{4}(\.|,)? Facebook) ``` While fixing the text to pass the linter, I took the opportunity to modify `format-diff.sh` script to add the copyright header automatically if missing in new files. Pull Request resolved: facebook#14143 Test Plan: ``` $> make format ``` **new python file** ``` build_tools/format-diff.sh Checking format of uncommitted changes... Checking for copyright headers in new files... Added copyright header to build_tools/test.py Copyright headers were added to new files. Nothing needs to be reformatted! ``` **new header file** ``` build_tools/format-diff.sh Checking format of uncommitted changes... Checking for copyright headers in new files... Added copyright header to db/db_impl/db_impl_jewoongh.h Copyright headers were added to new files. Nothing needs to be reformatted! ``` Reviewed By: hx235 Differential Revision: D87653124 Pulled By: jaykorean fbshipit-source-id: 164322cfcd2c162bb3b41bb8f3bafefa3f20b695
1 parent dc33c1a commit c4bbad4

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

buckifier/targets_cfg.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Copyright (c) Meta Platforms, Inc. and its affiliates. All Rights Reserved.
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# This source code is licensed under both the GPLv2 (found in the COPYING file in the root directory)
3+
# and the Apache 2.0 License (found in the LICENSE.Apache file in the root directory).
24

35
rocksdb_target_header_template = """# This file \100generated by:
46
#$ python3 buckifier/buckify_rocksdb.py{extra_argv}

build_tools/format-diff.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,70 @@ else
148148
echo "Checking format of uncommitted changes..."
149149
fi
150150

151+
# Check for missing copyright in new files
152+
echo "Checking for copyright headers in new files..."
153+
154+
# Get list of new files (added, not just modified)
155+
if [ -z "$uncommitted_code" ]; then
156+
# Post-commit: check files added since merge base
157+
new_files=$(git diff --name-only --diff-filter=A "$FORMAT_UPSTREAM_MERGE_BASE" -- '*.h' '*.cc' '*.py' $EXCLUDE)
158+
else
159+
# Pre-commit: check staged new files
160+
new_files=$(git diff --name-only --diff-filter=A --cached HEAD -- '*.h' '*.cc' '*.py' $EXCLUDE)
161+
fi
162+
163+
if [ -n "$new_files" ]; then
164+
files_missing_copyright=""
165+
166+
for file in $new_files; do
167+
if [ -f "$file" ]; then
168+
# Check if file is missing copyright
169+
# For .py files, check for Python-style comment
170+
# For .h and .cc files, check for C++-style comment
171+
if [[ "$file" == *.py ]]; then
172+
if ! grep -q "Copyright (c) Meta Platforms, Inc. and affiliates" "$file"; then
173+
files_missing_copyright="$files_missing_copyright $file"
174+
# Add copyright header to Python file
175+
temp_file=$(mktemp)
176+
{
177+
echo "# Copyright (c) Meta Platforms, Inc. and affiliates."
178+
echo "# This source code is licensed under both the GPLv2 (found in the COPYING file in the root directory)"
179+
echo "# and the Apache 2.0 License (found in the LICENSE.Apache file in the root directory)."
180+
echo
181+
cat "$file"
182+
} > "$temp_file"
183+
mv "$temp_file" "$file"
184+
echo "Added copyright header to $file"
185+
fi
186+
elif [[ "$file" == *.h ]] || [[ "$file" == *.cc ]]; then
187+
if ! grep -q "Copyright (c) Meta Platforms, Inc. and affiliates" "$file"; then
188+
files_missing_copyright="$files_missing_copyright $file"
189+
# Add copyright header to C++ file
190+
temp_file=$(mktemp)
191+
{
192+
echo "// Copyright (c) Meta Platforms, Inc. and affiliates. "
193+
echo "// This source code is licensed under both the GPLv2 (found in the "
194+
echo "// COPYING file in the root directory) and Apache 2.0 License "
195+
echo "// (found in the LICENSE.Apache file in the root directory)."
196+
echo
197+
cat "$file"
198+
} > "$temp_file"
199+
mv "$temp_file" "$file"
200+
echo "Added copyright header to $file"
201+
fi
202+
fi
203+
fi
204+
done
205+
206+
if [ -n "$files_missing_copyright" ]; then
207+
echo "Copyright headers were added to new files."
208+
else
209+
echo "All new files have copyright headers."
210+
fi
211+
else
212+
echo "No new files to check for copyright headers."
213+
fi
214+
151215
if [ -z "$diffs" ]
152216
then
153217
echo "Nothing needs to be reformatted!"

0 commit comments

Comments
 (0)