aboutsummaryrefslogtreecommitdiff
path: root/copyright
blob: 14e439e9867cf7dc8484b97a9c6a8d6902fc7d42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#! /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 <old-year> <new-year>"

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%(&#169; [0-9]+)-$old (Code Synthesis)%\1-$new \2%" \
    -re "s%(&#169; [0-9]+)-$old (<a href=\"http://(www.)?codesynthesis.com\")%\1-$new \2%" \
    "$f"

  # Grep for matching copyrights that didn't end with $old (or have other
  # discrepancies like single year only, etc).
  #
  grep -vE \
    -e "Copyright \(c\) [0-9]+-$new Code Synthesis" \
    -e "&#169; [0-9]+-$new Code Synthesis" \
    -e "&#169; [0-9]+-$new <a href=\"http://(www.)?codesynthesis.com\"" \
    "$f" | \
  grep -iE --color=auto "Copyright.*Code Synthesis" || true

  # Grep for the old year (for review).
  #
  grep --color=auto --with-filename "$old" "$f" || true
done