Coverage for quibble/gitchangedinhead.py: 42%
18 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-08-07 07:21 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2026-08-07 07:21 +0000
1# Copyright 2017-2018, Antoine "hashar" Musso
2# Copyright 2017, Kunal Mehta
3# Copyright 2017-2018, Wikimedia Foundation Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
17"""
18git-changed-in-head : list changed files in HEAD matching a file extension
20This script list any added, copied or modified file in the current directory
21and filters out by extension.
23The current directory must be a git repository.
25USAGE:
26 git-changed-in-head [extension..]
28EXAMPLE:
30 List files ending with .php, .php5 or .inc
31 git-changed-in-head php php5 inc
33 List any file changed (no filtering):
34 git-changed-in-head
37Copyright © 2013-2018, Antoine Musso
38Copyright © 2013, Wikimedia Foundation Inc.
39Copyright © 2017, Kunal Mehta
41This program is free software; you can redistribute it and/or modify
42it under the terms of the GNU General Public License version 2 as
43published by the Free Software Foundation.
45This program is distributed in the hope that it will be useful,
46but WITHOUT ANY WARRANTY; without even the implied warranty of
47MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48GNU General Public License for more details.
50You should have received a copy of the GNU General Public License along
51with this program; if not, write to the Free Software Foundation, Inc.,
5251 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
53"""
55import subprocess
58class GitChangedInHead:
59 def __init__(self, args, cwd=None):
60 self.cwd = cwd
61 self.path_args = []
62 for arg in args:
63 # Put a dot in front for file extensions
64 self.path_args.append('.{}'.format(arg))
66 def changedFiles(self):
67 return [f for f in self._get_changed_files()]
69 def _get_changed_files(self):
70 # Some explanations for the git command below:
71 # HEAD^ will not exist for an initial commit, we thus need `git show`
72 # --name-only: strip patch payload, only report the file being altered
73 # --diff-filter=ACM: only care about files Added, Copied or Modified
74 # --find-renames=100%: renamed files that had a slight change would be
75 # considered modified and thus included.
76 # -m: show differences for merge commits ...
77 # --first-parent: ... but only follow the first parent commit
78 # --format=format: : strip out the commit summary
79 cmd = [
80 'git',
81 'show',
82 'HEAD',
83 '--name-only',
84 '--diff-filter=ACM',
85 '--find-renames=100%',
86 '-m',
87 '--first-parent',
88 '--format=format:',
89 ]
90 out = subprocess.check_output(cmd, cwd=self.cwd).decode()
91 for line in out.splitlines():
92 # If matching on file extensions, filter that out
93 if self.path_args and not line.endswith(tuple(self.path_args)):
94 continue
95 elif line.endswith('autoload_static.php'):
96 # T136021: Don't lint composer/autoload_static.php.
97 continue
98 yield line