3 # Copyright (c) 2020 ARM Limited
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.
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.
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.
43 import git_filter_repo
45 import update_copyright
47 parser
= argparse
.ArgumentParser(description
=
48 """Update copyright headers on files of a range of commits.
50 This can be used to easily update copyright headers at once on an entire
51 patchset before submitting.
53 Only files touched by the selected commits are updated.
55 Only existing copyrights for the selected holder are updated, new
56 notices are never automatically added if not already present.
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
63 Example usage with an organization alias such as `arm`:
66 python3 -m pip install --user --requirement \
67 gem5/util/update_copyright/requirements.txt
68 ./update-copyright.py -o arm HEAD~3
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:
75 - * Copyright (c) 2010, 2012-2013, 2015,2017-2019 ARM Limited
76 + * Copyright (c) 2010, 2012-2013, 2015,2017-2020 ARM Limited
79 If the organization is not in the alias list, you can also explicitly give
80 the organization string as in:
83 ./update-copyright.py HEAD~3 'ARM Limited'
86 which is equivalent to the previous invocation.
88 formatter_class
=argparse
.RawTextHelpFormatter
,
90 parser
.add_argument('start',
92 help="The commit before the last commit to be modified")
93 parser
.add_argument('org-string',
95 help="Copyright holder name")
96 parser
.add_argument('-o', '--org', choices
=('arm',),
97 help="Alias for known organizations")
98 args
= parser
.parse_args()
101 print('error: ' + msg
, file=sys
.stderr
)
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
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")
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
]
123 org_bytes
= getattr(args
, 'org-string').encode()
125 # Call git_filter_repo.
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(
140 blob_callback
=lambda x
, y
: blob_callback( x
, y
, org_bytes
)