Created
January 4, 2023 23:58
-
-
Save wojtekmaj/d4c1ac8a3f5d810b2099ddf494aa5e68 to your computer and use it in GitHub Desktop.
Find all Git repositories recursively and update year in LICENSE if commit was made in current year
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
current_year=$(date +%Y) | |
# Find all Git repositories, skipping node_modules directories | |
find . -type d -name .git -not -path '*/node_modules/*' -print0 | while read -d $'\0' repo; do | |
# Go to the repository directory | |
cd "${repo%/.git}" | |
# Check the year of the last commit | |
year=$(git log -1 --format=%cd --date=format:%Y) | |
if [ "$year" != "$current_year" ] | |
then | |
# Last commit was not made in current year, skip this repository | |
cd - > /dev/null | |
continue | |
fi | |
if [ ! -f LICENSE ] | |
then | |
# LICENSE file not found, skip this repository | |
cd - > /dev/null | |
continue | |
fi | |
# Update the year in the LICENSE file | |
# Replace e.g. "(c) 2019 Wojciech Maj" with "(c) 2019–2023 Wojciech Maj" | |
sed -i '' "s/Copyright (c) \([[:digit:]]\{4\}\) /Copyright (c) \1–$current_year /g" LICENSE | |
# Replace e.g. "(c) 2019–2022 Wojciech Maj" with "(c) 2019–2023 Wojciech Maj" | |
sed -i '' "s/Copyright (c) \([[:digit:]]\{4\}\)\([-–]\)\([[:digit:]]\{4\}\)/Copyright (c) \1\2$current_year/g" LICENSE | |
# Commit all updates | |
git commit -am "Update year in LICENSE" | |
# Push changes to remote | |
git push | |
# Go back to the previous directory | |
cd - > /dev/null | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment