Debian Codegraph @

scripts/update-copyrights

raw
1 #!/bin/bash
2
3 if [ $# -lt 2 ]; then
4 echo "Usage: $0 <company> <domain>">&2
5 echo "This script updates copyright lines for <company> for files modified">&2
6 echo "this year by authors whose email addresses are at <domain>.">&2
7 echo "Usage example: $0 \"Red Hat\" redhat.com">&2
8 exit 1
9 fi
10
11 company=$1
12 domain=$2
13
14 # Switch to root directory of repo
15 rootdir=$(git rev-parse --show-toplevel)
16 if [ $? -ne 0 ]; then
17 exit 1
18 fi
19 cd $rootdir
20
21 # This script has the potential to create a massive diff, so to help going back
22 # for some reason, enforce a clean working directory.
23 git diff --exit-code --quiet
24 if [ $? -ne 0 ]; then
25 echo "Working directory should be clean">&2
26 echo "Please stash or commit changes">&2
27 fi
28
29 set -e
30
31 year=$(date +%Y)
32 lastyear=$(expr $year - 1)
33
34 echo -n "Fetching in-tree copyrighted files... "
35
36 # Collect all the files in the tree with copyrights older than this year
37 old_copyrighted_files=
38 for file in $(git ls-tree --full-tree --name-only -r HEAD); do
39
40 # Exclude po dir
41 if [ ! -d $file ] && \
42 [[ $file != po/* ]] && \
43 grep -q -E 'Copyright \(C\) .*[0-9]{4}\s+'"$company" $file; then
44
45 # Add to the list if it's not copyrighted for this year
46 if ! grep -q "Copyright (C) .*$year.*$company" $file; then
47 old_copyrighted_files="$old_copyrighted_files $file"
48 fi
49 fi
50 done
51
52 echo "done"
53 echo -n "Checking year of last modification... "
54
55 # For each of those files, do a git log to determine if it has been modified by
56 # an author at domain this year
57 needs_updating=
58 for file in $old_copyrighted_files; do
59 authors_this_year=$(git log --format='%ae' --after="$lastyear-12-31" -- "$file")
60 for author in $authors_this_year; do
61 if [[ $author == *@$domain ]]; then
62 needs_updating="$needs_updating $file"
63 break
64 fi
65 done
66 done
67
68 echo "done"
69 echo -n "Updating copyright years... "
70
71 # Update all the copyright lines to the current year
72 n=0
73 for file in $needs_updating; do
74
75 # Modify the XXXX copyright line to XXXX-YYYY
76 sed -i -r 's/(.*Copyright \(C\).* )([0-9]{4})\s+('"$company"'.*)/\1\2-'$year' \3/' $file
77 # Modify the XXXX-YYYY copyright line
78 sed -i -r 's/(.*Copyright \(C\).*-)([0-9]{4})\s+('"$company"'.*)/\1'$year' \3/' $file
79
80 # If it wasn't modified, output warning
81 git diff --exit-code --quiet $file && \
82 echo "File $file could not be updated. Continuing...">&2 || \
83 n=$(expr $n + 1)
84 done
85
86 echo "done ($n files)"
87
88 if [ $n -gt 0 ]; then
89 echo "Please carefully review modifications"
90 fi