util: add update-copyright utility to update copyright on commits
[gem5.git] / util / update-copyright.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2020 ARM Limited
4 # All rights reserved
5 #
6 # The license below extends only to copyright in the software and shall
7 # not be construed as granting a license to any other intellectual
8 # property including but not limited to intellectual property relating
9 # to a hardware implementation of the functionality of the software
10 # licensed hereunder. You may use the software subject to the license
11 # terms below provided that you ensure that this notice is replicated
12 # unmodified and in its entirety in all distributions of the software,
13 # modified or unmodified, in source code or in binary form.
14 #
15 # Redistribution and use in source and binary forms, with or without
16 # modification, are permitted provided that the following conditions are
17 # met: redistributions of source code must retain the above copyright
18 # notice, this list of conditions and the following disclaimer;
19 # redistributions in binary form must reproduce the above copyright
20 # notice, this list of conditions and the following disclaimer in the
21 # documentation and/or other materials provided with the distribution;
22 # neither the name of the copyright holders nor the names of its
23 # contributors may be used to endorse or promote products derived from
24 # this software without specific prior written permission.
25 #
26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 import argparse
39 import datetime
40 import subprocess
41 import sys
42
43 import git_filter_repo
44
45 import update_copyright
46
47 parser = argparse.ArgumentParser(description=
48 """Update copyright headers on files of a range of commits.
49
50 This can be used to easily update copyright headers at once on an entire
51 patchset before submitting.
52
53 Only files touched by the selected commits are updated.
54
55 Only existing copyrights for the selected holder are updated, new
56 notices are never automatically added if not already present.
57
58 The size of the changes is not taken into account, every touched file gets
59 updated. If you want to undo that for a certain file because the change to
60 it is trivial, you need to manually rebase and undo the copyright change
61 for that file.
62
63 Example usage with an organization alias such as `arm`:
64
65 ```
66 python3 -m pip install --user --requirement \
67 gem5/util/update_copyright/requirements.txt
68 ./update-copyright.py -o arm HEAD~3
69 ```
70
71 The above would act on the 3 last commits (HEAD~2, HEAD~ and HEAD),
72 leaving HEAD~3 unchanged, and doing updates such as:
73
74 ```
75 - * Copyright (c) 2010, 2012-2013, 2015,2017-2019 ARM Limited
76 + * Copyright (c) 2010, 2012-2013, 2015,2017-2020 ARM Limited
77 ```
78
79 If the organization is not in the alias list, you can also explicitly give
80 the organization string as in:
81
82 ```
83 ./update-copyright.py HEAD~3 'ARM Limited'
84 ```
85
86 which is equivalent to the previous invocation.
87 """,
88 formatter_class=argparse.RawTextHelpFormatter,
89 )
90 parser.add_argument('start',
91 nargs='?',
92 help="The commit before the last commit to be modified")
93 parser.add_argument('org-string',
94 nargs='?',
95 help="Copyright holder name")
96 parser.add_argument('-o', '--org', choices=('arm',),
97 help="Alias for known organizations")
98 args = parser.parse_args()
99
100 def error(msg):
101 print('error: ' + msg, file=sys.stderr)
102 sys.exit(1)
103
104 # The existing safety checks are too strict, so we just disable them
105 # with force, and do our own checks to not overwrite uncommited changes
106 # checks.
107 # https://github.com/newren/git-filter-repo/issues/159
108 if subprocess.call(['git', 'diff', '--staged', '--quiet']):
109 error("uncommitted changes")
110 if subprocess.call(['git', 'diff', '--quiet']):
111 error("unstaged changes")
112
113 # Handle CLI arguments.
114 if args.start is None:
115 error("the start argument must be given")
116 if args.org is None and getattr(args, 'org-string') is None:
117 error("either --org or org-string must be given")
118 if args.org is not None and getattr(args, 'org-string') is not None:
119 error("both --org and org-string given")
120 if args.org is not None:
121 org_bytes = update_copyright.org_alias_map[args.org]
122 else:
123 org_bytes = getattr(args, 'org-string').encode()
124
125 # Call git_filter_repo.
126 # Args deduced from:
127 # print(git_filter_repo.FilteringOptions.parse_args(['--refs', 'HEAD',
128 # '--force'], error_on_empty=False))
129 filter_repo_args = git_filter_repo.FilteringOptions.default_options()
130 filter_repo_args.force = True
131 filter_repo_args.partial = True
132 filter_repo_args.refs = ['{}..HEAD'.format(args.start)]
133 filter_repo_args.repack=False
134 filter_repo_args.replace_refs='update-no-add'
135 def blob_callback(blob, callback_metadata, org_bytes):
136 blob.data = update_copyright.update_copyright(blob.data,
137 datetime.datetime.now().year, org_bytes)
138 git_filter_repo.RepoFilter(
139 filter_repo_args,
140 blob_callback=lambda x, y: blob_callback( x, y, org_bytes)
141 ).run()