root / devel / check_copyright @ 5349519d
History | View | Annotate | Download (2.5 kB)
1 |
#!/bin/bash |
---|---|
2 |
|
3 |
# Copyright (C) 2014 Google Inc. |
4 |
# |
5 |
# This program is free software; you can redistribute it and/or modify |
6 |
# it under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 2 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# This program is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 |
# General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with this program; if not, write to the Free Software |
17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 |
# 02110-1301, USA. |
19 |
|
20 |
# Script to check whether the local dirty commits are changing files |
21 |
# which do not have an updated copyright. |
22 |
# |
23 |
# The script will determine your current remote branch and local |
24 |
# branch, from which it will extract the commits to analyze. |
25 |
# Afterwards, for each commit, it will see which files are being |
26 |
# modified and, for each file, it will check the copyright. |
27 |
|
28 |
function join { |
29 |
local IFS="$1" |
30 |
shift |
31 |
echo "$*" |
32 |
} |
33 |
|
34 |
# Determine the tracking branch for the current branch |
35 |
readonly REMOTE=$(git branch -vv | grep -e "^\*" | sed -e "s/ \+/ /g" | awk '{ print $4 }' | grep "\[" | tr -d ":[]") |
36 |
|
37 |
if [ -z "$REMOTE" ] |
38 |
then |
39 |
echo check_copyright: failed to get remote branch |
40 |
exit 1 |
41 |
fi |
42 |
|
43 |
# Determine which commits have no been pushed (i.e, diff between the |
44 |
# remote branch and the current branch) |
45 |
COMMITS=$(git log --pretty=format:'%h' ${REMOTE}..HEAD) |
46 |
|
47 |
if [ -z "$COMMITS" ] |
48 |
then |
49 |
echo check_copyright: there are no commits to check |
50 |
exit 0 |
51 |
fi |
52 |
|
53 |
# for each commit, check its files |
54 |
for commit in $(echo $COMMITS | tac -s " ") |
55 |
do |
56 |
FILES=$(git diff-tree --no-commit-id --name-only -r $commit) |
57 |
|
58 |
if [ -z "$FILES" ] |
59 |
then |
60 |
echo check_copyright: commit \"$commit\" has no files to check |
61 |
else |
62 |
# for each file, check if it is in the 'lib' or 'src' dirs |
63 |
# and, if so, check the copyright |
64 |
for file in $FILES |
65 |
do |
66 |
DIR=$(echo $file | cut -d "/" -f 1) |
67 |
|
68 |
if [ "$DIR" = lib -o "$DIR" = src ] |
69 |
then |
70 |
COPYRIGHT=$(grep "Copyright (C)" $file) |
71 |
YEAR=$(date +%G) |
72 |
|
73 |
if [ -z "$COPYRIGHT" ] |
74 |
then |
75 |
echo check_copyright: commit \"$commit\" misses \ |
76 |
copyright for \"$file\" |
77 |
elif ! echo $COPYRIGHT | grep -o $YEAR > /dev/null |
78 |
then |
79 |
echo check_copyright: commit \"$commit\" misses \ |
80 |
\"$YEAR\" copyright for \"$file\" |
81 |
fi |
82 |
fi |
83 |
done |
84 |
fi |
85 |
done |