Prune change-management.rst and update links.
[alexxy/gromacs.git] / admin / copyright.sh
1 #!/bin/bash
2 #
3 # This file is part of the GROMACS molecular simulation package.
4 #
5 # Copyright (c) 2019,2020, by the GROMACS development team, led by
6 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 # and including many others, as listed in the AUTHORS file in the
8 # top-level source directory and at http://www.gromacs.org.
9 #
10 # GROMACS is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public License
12 # as published by the Free Software Foundation; either version 2.1
13 # of the License, or (at your option) any later version.
14 #
15 # GROMACS is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # Lesser General Public License for more details.
19 #
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with GROMACS; if not, see
22 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24 #
25 # If you want to redistribute modifications to GROMACS, please
26 # consider that scientific software is very special. Version
27 # control is crucial - bugs must be traceable. We will be happy to
28 # consider code for inclusion in the official distribution, but
29 # derived work must not be called official GROMACS. Details are found
30 # in the README & COPYING files - if they are missing, get the
31 # official version at http://www.gromacs.org.
32 #
33 # To help us fund GROMACS development, we humbly ask that you cite
34 # the research papers on the package. Check out http://www.gromacs.org.
35
36 # This script runs copyright header checks on modified files and
37 # reports/applies the necessary changes.
38 #
39 # See `copyright.sh -h` for a brief usage, and docs/dev-manual/code-formatting.rst
40 # for more details.
41
42 # Parse command-line arguments
43 function usage() {
44     echo "usage: copyright.sh [-f|--force] [--rev=REV]"
45     echo "           [--copyright=<cmode>]"
46     echo "           [--warnings=<file>] [<action>]"
47     echo "<action>: (check*|diff|update)[-(index|workdir*)] (*=default)"
48     echo "<cmode>:  off|add|update*|replace|full"
49 }
50
51 action="check-workdir"
52 declare -a diffargs
53 baserev="origin/master"
54 force=
55 copyright_mode=update
56 warning_file=
57 for arg in "$@" ; do
58     if [[ "$arg" == "check-index" || "$arg" == "check-workdir" || \
59           "$arg" == "diff-index" || "$arg" == "diff-workdir" || \
60           "$arg" == "update-index" || "$arg" == "update-workdir" ]]
61     then
62         action=$arg
63     elif [[ "$arg" == "check" || "$arg" == "diff" || "$arg" == "update" ]] ; then
64         action=$arg-workdir
65     elif [[ "$action" == diff-* ]] ; then
66         diffargs+=("$arg")
67     elif [[ "$arg" == --copyright=* ]] ; then
68         copyright_mode=${arg#--copyright=}
69     elif [[ "$arg" == "-f" || "$arg" == "--force" ]] ; then
70         force=1
71     elif [[ "$arg" == --rev=* ]] ; then
72         baserev=${arg#--rev=}
73     elif [[ "$arg" == --warnings=* ]] ; then
74         warning_file=${arg#--warnings=}
75     elif [[ "$arg" == "-h" || "$arg" == "--help" ]] ; then
76         usage
77         exit 0
78     else
79         echo "Unknown option: $arg"
80         echo
81         usage
82         exit 2
83     fi
84 done
85
86 # Switch to the root of the source tree and check the config file
87 srcdir=`git rev-parse --show-toplevel`
88 pushd $srcdir >/dev/null
89 admin_dir=$srcdir/admin
90
91 # Actual processing starts: create a temporary directory
92 tmpdir=`mktemp -d -t gmxuncrust.XXXXXX`
93
94 # Produce a list of changed files
95 # Only include files that have proper filter set in .gitattributes
96 internal_diff_args=
97 if [[ $action == *-index ]]
98 then
99     internal_diff_args="--cached"
100 fi
101 git diff-index $internal_diff_args --diff-filter=ACMR $baserev >$tmpdir/difflist
102 cut -f2 <$tmpdir/difflist | \
103     git check-attr --stdin filter | \
104     sed -e 's/.*: filter: //' | \
105     paste $tmpdir/difflist - | \
106     grep -E '(complete_formatting|uncrustify|copyright|includesort)$' >$tmpdir/filtered
107 cut -f2 <$tmpdir/filtered >$tmpdir/filelist_all
108 grep -E '(complete_formatting|copyright)$' <$tmpdir/filtered | \
109     cut -f2 >$tmpdir/filelist_copyright
110 git diff-files --name-only | grep -Ff $tmpdir/filelist_all >$tmpdir/localmods
111
112 # Extract changed files to a temporary directory
113 mkdir $tmpdir/org
114 if [[ $action == *-index ]] ; then
115     git checkout-index --prefix=$tmpdir/org/ --stdin <$tmpdir/filelist_all
116 else
117     rsync --files-from=$tmpdir/filelist_all $srcdir $tmpdir/org
118 fi
119 # Duplicate the original files to a separate directory, where all changes will
120 # be made.
121 cp -r $tmpdir/org $tmpdir/new
122
123 # Create output file for what was done (in case no messages get written)
124 touch $tmpdir/messages
125
126 # Run uncrustify on the temporary directory
127 cd $tmpdir/new
128
129 # Update the copyright headers using the requested mode
130 if [[ $copyright_mode != "off" ]] ; then
131     cpscript_args="--update-year"
132     case "$copyright_mode" in
133         year)
134             ;;
135         add)
136             cpscript_args+=" --add-missing"
137             ;;
138         update)
139             cpscript_args+=" --add-missing --update-header"
140             ;;
141         replace)
142             cpscript_args+=" --replace-header"
143             ;;
144         full)
145             cpscript_args+=" --add-missing --update-header --replace-header"
146             ;;
147         *)
148             echo "Unknown copyright mode: $copyright_mode"
149             exit 2
150     esac
151     if [[ $action == check-* ]] ; then
152         cpscript_args+=" --check"
153     fi
154     # TODO: Probably better to invoke python explicitly through a customizable
155     # variable.
156     if ! $admin_dir/copyright.py -F $tmpdir/filelist_copyright $cpscript_args >>$tmpdir/messages
157     then
158         echo "Copyright checking failed!"
159         rm -rf $tmpdir
160         exit 2
161     fi
162 fi
163
164 cd $tmpdir
165
166 # If a diff was requested, show it and we are done
167 if [[ $action == diff-* ]] ; then
168     git diff --no-index --no-prefix "${diffargs[@]}" org/ new/
169     rm -rf $tmpdir
170     exit 0
171 fi
172
173 # Find the changed files
174 git diff --no-index --name-only --exit-code org/ new/ | \
175     sed -e 's#new/##' > $tmpdir/changed
176 changes=
177 if [[ -s $tmpdir/changed ]]
178 then
179     changes=1
180 fi
181
182 # Check if changed files have changed outside the index
183 if grep -Ff $tmpdir/localmods $tmpdir/changed > $tmpdir/conflicts
184 then
185     awk '{print $0 ": has changes in work tree"}' $tmpdir/conflicts \
186         >> $tmpdir/messages
187     if [[ ! $force && $action == update-* ]] ; then
188         echo "Modified files found in work tree, skipping update. Use -f to override."
189         echo "The following would have been done:"
190         sort $tmpdir/messages
191         rm -rf $tmpdir
192         exit 2
193     fi
194 fi
195
196 # Update the index/work tree if requested
197 if [[ $action == update-index ]] ; then
198     grep -Ff $tmpdir/changed $tmpdir/filtered > $tmpdir/tohash
199     cd $tmpdir/new
200     IFS='
201 '
202     for change in `cut -f2 $tmpdir/tohash | \
203                    git --git-dir=$srcdir/.git hash-object -w --stdin-paths --no-filters | \
204                    paste - $tmpdir/tohash`
205     do
206         # NOTE: the patterns below contain literal tabs
207         sha1=${change%% *}
208         rest=${change#* }
209         mode=${rest:8:6}
210         path=${rest#*   }
211         path=${path%%   *}
212         # Contains a literal tab
213         echo "$mode $sha1       $path" >> $tmpdir/toindex
214     done
215     unset IFS
216     git --git-dir=$srcdir/.git update-index --index-info < $tmpdir/toindex
217 elif [[ $action == update-workdir ]] ; then
218     rsync --files-from=$tmpdir/changed $tmpdir/new/ $srcdir/
219 fi
220
221 # Get back to the original directory
222 popd >/dev/null
223
224 # Report what was done
225 sort $tmpdir/messages | tee $warning_file
226
227 rm -rf $tmpdir
228 exit $changes