Created
February 24, 2020 19:00
-
-
Save jamesthomasonjr/9d959d83fec52a63fedc4c9a6c699db4 to your computer and use it in GitHub Desktop.
Update the copyright only on changed files
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
#! /usr/bin/env bash | |
function get_error_text { | |
local readonly status="${1}" | |
local message | |
case "${status}" in | |
1) | |
message="Couldn't determine current year!" | |
;; | |
2) | |
message="Non-zero status from git grep command!" | |
;; | |
3) | |
message="Couldn't find any out of date files!" | |
;; | |
4) | |
message="Non-zero status when running sed!" | |
;; | |
*) | |
message="Unknown error" | |
;; | |
esac | |
echo "${message}" | |
return 0 | |
} | |
function error { | |
>&2 echo "${@}" | |
} | |
function run_script { | |
local year="$(date +"%Y")" | |
if [ -z "${year}" ]; then | |
error "Error: $(get_error_text 1)" | |
exit 1 | |
fi | |
# Can't get --null to work with any variety of IFS/read/for stuff. Luckily we don't have whitespace in our files | |
local out_of_date_files=( $(git grep --files-with-matches --perl-regexp '@copyright \(c\) (?!'"${year}"')\d{4}' $(git diff-tree --no-commit-id --name-only -r --diff-filter=ACMTUX HEAD..master)) ) | |
status="${?}" | |
if [ $status -ne 0 ]; then | |
error "Error: $(get_error_text 2)" | |
exit 2 | |
fi | |
if [ -z "${out_of_date_files}" ]; then | |
error "Error: $(get_error_text 3)" | |
exit 3 | |
fi | |
sed -i '' -e 's/@copyright (c) [[:digit:]]\{4\}/@copyright (c) '"${year}"'/g' ${out_of_date_files[@]} | |
status="${?}" | |
if [ $status -ne 0 ]; then | |
error "Error: $(get_error_text 4)" | |
exit 4 | |
fi | |
exit 0 | |
} | |
run_script "${@}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment