mirror of
https://github.com/firebase/firebase-ios-sdk
synced 2026-11-09 22:36:01 +05:45
150 lines
4.5 KiB
Bash
Executable File
150 lines
4.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Copyright 2026 Google LLC
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
set -u
|
|
|
|
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
|
|
echo "Usage: $0 [BASE_BRANCH]"
|
|
echo ""
|
|
echo "Finds newly added files compared to BASE_BRANCH (defaults to 'main')"
|
|
echo "and automatically prepends or updates the Google copyright header."
|
|
exit 0
|
|
fi
|
|
|
|
# Check if git is available.
|
|
if ! command -v git &> /dev/null; then
|
|
echo "git command could not be found."
|
|
exit 1
|
|
fi
|
|
|
|
# Move to the root of the repository to ensure paths are correct.
|
|
cd "$(git rev-parse --show-toplevel)" || exit 1
|
|
|
|
# Define the base branch (defaulting to main).
|
|
BASE_BRANCH=${1:-main}
|
|
|
|
# Check if BASE_BRANCH exists.
|
|
if ! git rev-parse --verify "$BASE_BRANCH" >/dev/null 2>&1; then
|
|
# Try origin/main if main doesn't exist locally
|
|
if git rev-parse --verify "origin/$BASE_BRANCH" >/dev/null 2>&1; then
|
|
BASE_BRANCH="origin/$BASE_BRANCH"
|
|
else
|
|
echo "Base branch '$BASE_BRANCH' not found. Please provide a valid branch name as an argument."
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
echo "Checking for new files against $BASE_BRANCH..."
|
|
|
|
# Get list of added files.
|
|
# git diff --name-only --diff-filter=A returns paths relative to repo root.
|
|
# We compare the working directory against the merge base of the current branch and the base branch.
|
|
FILES=$(git diff --name-only --diff-filter=A "$(git merge-base "$BASE_BRANCH" HEAD)")
|
|
|
|
if [ -z "$FILES" ]; then
|
|
echo "No new files found."
|
|
exit 0
|
|
fi
|
|
|
|
YEAR=$(date +%Y)
|
|
|
|
# Iterate over files
|
|
echo "$FILES" | while read -r file; do
|
|
if [ -z "$file" ]; then continue; fi
|
|
if [ ! -f "$file" ]; then continue; fi
|
|
|
|
# Determine extension
|
|
filename=$(basename -- "$file")
|
|
ext="${filename##*.}"
|
|
|
|
# Special case for CMakeLists.txt
|
|
if [ "$filename" == "CMakeLists.txt" ]; then
|
|
ext="cmake"
|
|
fi
|
|
|
|
prefix=""
|
|
case "$ext" in
|
|
c|cc|cpp|h|hpp|js|m|mm|swift)
|
|
prefix="//"
|
|
;;
|
|
cmake|py|rb|sh|yml|yaml)
|
|
prefix="#"
|
|
;;
|
|
*)
|
|
continue
|
|
;;
|
|
esac
|
|
|
|
# Check if file already has copyright.
|
|
if head -n 20 "$file" | grep -q "Copyright.*Google"; then
|
|
echo "Updating copyright year in $file"
|
|
tmp_file=$(mktemp)
|
|
# 1. Expand existing range or single year to range ending in current year.
|
|
# e.g. 2020 -> 2020-2026, 2020-2022 -> 2020-2026
|
|
# 2. Collapse same-year range.
|
|
# e.g. 2026-2026 -> 2026
|
|
sed -E "1,20s/Copyright ([0-9]{4})(-[0-9]{4})? Google/Copyright \1-${YEAR} Google/" "$file" | \
|
|
sed "1,20s/${YEAR}-${YEAR} Google/${YEAR} Google/" > "$tmp_file"
|
|
cat "$tmp_file" > "$file"
|
|
rm "$tmp_file"
|
|
continue
|
|
fi
|
|
|
|
echo "Adding copyright to $file"
|
|
|
|
# Create temporary file
|
|
tmp_file=$(mktemp)
|
|
|
|
# Read first line to check for shebang
|
|
first_line=$(head -n 1 "$file")
|
|
|
|
has_shebang=false
|
|
# Check if first line starts with #!
|
|
if [[ "$first_line" =~ ^#! ]]; then
|
|
has_shebang=true
|
|
echo "$first_line" > "$tmp_file"
|
|
echo "" >> "$tmp_file"
|
|
fi
|
|
|
|
# Append license
|
|
cat <<EOF >> "$tmp_file"
|
|
${prefix} Copyright ${YEAR} Google LLC
|
|
${prefix}
|
|
${prefix} Licensed under the Apache License, Version 2.0 (the "License");
|
|
${prefix} you may not use this file except in compliance with the License.
|
|
${prefix} You may obtain a copy of the License at
|
|
${prefix}
|
|
${prefix} http://www.apache.org/licenses/LICENSE-2.0
|
|
${prefix}
|
|
${prefix} Unless required by applicable law or agreed to in writing, software
|
|
${prefix} distributed under the License is distributed on an "AS IS" BASIS,
|
|
${prefix} WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
${prefix} See the License for the specific language governing permissions and
|
|
${prefix} limitations under the License.
|
|
|
|
EOF
|
|
|
|
# Append rest of file
|
|
if [ "$has_shebang" = true ]; then
|
|
tail -n +2 "$file" >> "$tmp_file"
|
|
else
|
|
cat "$file" >> "$tmp_file"
|
|
fi
|
|
|
|
cat "$tmp_file" > "$file"
|
|
rm "$tmp_file"
|
|
done
|