#! /usr/bin/env bash # Update copyright in a git project. The fact that it is a git project is # used to avoid submodules as well as generated files. In other words, only # files tracked by git are updated. # # Run from the project root. # usage="usage: copyright " owd=`pwd` trap "{ cd $owd; exit 1; }" ERR set -o errtrace # Trap in functions. function info () { echo "$*" 1>&2; } function error () { info "$*"; exit 1; } while [ $# -gt 0 ]; do case $1 in *) break ;; esac done old="$1" new="$2" if [ -z "$old" -o -z "$new" ]; then error "$usage" fi # Load submodules into a map. # declare -A submodules for s in $(git submodule foreach -q 'echo $path'); do submodules["$s"]=true done # Get the list of files. # files=() while IFS= read -r -d '' f; do if [ "${submodules["$f"]}" = true ]; then info "skipping submodule $f" continue fi # Exclude symlinks (normally point into a submodule). # if test -L "$f"; then info "skipping symlink $f" continue fi # Exclude binary files. # if file -b --mime "$f" | grep -q "charset=binary"; then info "skipping binary $f" continue fi files+=("$f") done < <(git ls-tree -r HEAD -z --name-only ) # Process each file. # for f in "${files[@]}"; do info "processing $f" sed -i \ -re "s%(Copyright \(c\) [0-9]+)-$old (Code Synthesis)%\1-$new \2%" \ -re "s%(© [0-9]+)-$old (Code Synthesis)%\1-$new \2%" \ -re "s%(© [0-9]+)-$old (