summaryrefslogtreecommitdiff
path: root/git-coverage
blob: 102b6ca44861dd9044c162b16433eb973fdf5951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#!/usr/bin/env python

import fnmatch
import getopt
import os
import re
import subprocess
import sys
import tempfile

SKIP_PATTERNS = [
        'assert_not_reached',
        'return_val_if_reached',
        'return_if_reached',
        'UNREACHABLE:'
]

GIT_DIFF = [
        'git', 'diff', '--relative'
]

def subprocess_lines(argv):
    proc = subprocess.Popen(argv, stdout=subprocess.PIPE)
    while True:
        line = proc.stdout.readline()
        if line != "":
            yield line
        else:
            return

def match_any_re(regexps, line):
    for regexp in regexps:
        if regexp.search(line.strip()):
            return True
    return False

def warning(string):
    print >> sys.stderr, string

# ----------------------------------------------------------------------------
# PATCH PARSING
#
# The patch parsing code, heavily modified originated from:
#
# Copyright (C) 2005-2010 Aaron Bentley, Canonical Ltd
# <aaron.bentley@utoronto.ca>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#

class BadPatch(Exception):
    def __init(self, message):
        self.message = message

    def __str__(self):
        return self.message

class Hunk:
    def __init__(self, orig_pos, orig_range, mod_pos, mod_range, header=None):
        self.orig_pos = orig_pos
        self.orig_range = orig_range
        self.mod_pos = mod_pos
        self.mod_range = mod_range
        self.header = header
        self.lines = []

    @staticmethod
    def parse_range(textrange):
        tmp = textrange.split(',')
        if len(tmp) == 1:
            pos = tmp[0]
            range = "1"
        else:
            (pos, range) = tmp
        pos = int(pos)
        range = int(range)
        return (pos, range)

    @staticmethod
    def from_header(line):
        matches = re.match(r'\@\@ ([^@]*) \@\@( (.*))?\n', line)
        if matches is None:
            raise BadPatch("Does not match format.", line)
        try:
            (orig, mod) = matches.group(1).split(" ")
        except (ValueError, IndexError), e:
            raise BadPatch(str(e), line)
        if not orig.startswith('-') or not mod.startswith('+'):
            raise BadPatch("Positions don't start with + or -.", line)
        try:
            (orig_pos, orig_range) = Hunk.parse_range(orig[1:])
            (mod_pos, mod_range) = Hunk.parse_range(mod[1:])
        except (ValueError, IndexError), e:
            raise BadPatch(str(e), line)
        if mod_range < 0 or orig_range < 0:
            raise BadPatch("Hunk range is negative", line)
        tail = matches.group(3)
        return Hunk(orig_pos, orig_range, mod_pos, mod_range, line)

    @staticmethod
    def parse(lines):
        hunk = None
        lines = iter(lines)
        for line in lines:
            if line == "\n":
                if hunk is not None:
                    yield hunk
                    hunk = None
                continue
            if hunk is not None:
                yield hunk
            try:
                hunk = Hunk.from_header(line)
            except BadPatch:
                # If the line isn't a hunk header, then we've reached the end
                # of this patch and there's "junk" at the end.  Ignore the
                # rest of this patch.
                return
            orig_size = 0
            mod_size = 0
            offset = hunk.mod_pos
            while orig_size < hunk.orig_range or mod_size < hunk.mod_range:
                hunk_line = lines.next()
                if hunk_line.startswith("-"):
                    orig_size += 1
                elif hunk_line.startswith("\n"):
                    orig_size += 1
                    mod_size += 1
                elif hunk_line.startswith(" "):
                    orig_size += 1
                    mod_size += 1
                elif hunk_line.startswith("+"):
                    mod_size += 1
                else:
                    raise BadPatch("Unknown line type: %s" % hunk_line)
                hunk.lines.append((offset, hunk_line))
                if not hunk_line.startswith("-"):
                    offset += 1
        if hunk is not None:
            yield hunk


class Patch(object):
    BINARY_FILES_RE = re.compile('Binary files (.*) and (.*) differ\n')

    def __init__(self, oldname, newname):
        self.oldname = oldname.split("/", 1)[1]
        self.newname = newname.split("/", 1)[1]
        self.prefix = []
        self.hunks = []

    @staticmethod
    def parse_one(lines):
        try:
            first = lines.next()
            if not first.startswith("--- "):
                raise BadPatch("No orig name: %s" % first)
            else:
                orig_name = first[4:].rstrip("\n")
        except StopIteration:
            raise BadPatch("No orig line")
        try:
            second = lines.next()
            if not second.startswith("+++ "):
                raise BadPatch("No mod name")
            else:
                mod_name = second[4:].rstrip("\n")
        except StopIteration:
            raise BadPatch("No mod line")

        patch = Patch(orig_name, mod_name)
        for hunk in Hunk.parse(lines):
            patch.hunks.append(hunk)
        patch.prefix = [first, second]
        return patch

    @staticmethod
    def parse(lines):
        saved_lines = []
        orig_range = 0
        beginning = True
        for line in lines:
            if Patch.BINARY_FILES_RE.match(line):
                continue
            if line.startswith('=== ') or line.startswith('*** '):
                continue
            if line.startswith('#'):
                continue
            elif orig_range > 0:
                if line.startswith('-') or line.startswith(' '):
                    orig_range -= 1
            elif line.startswith('--- '):
                if beginning:
                    # Patches can have "junk" at the beginning
                    # Stripping junk from the end of patches is handled when we
                    # parse the patch
                    beginning = False
                elif len(saved_lines) > 0:
                    yield Patch.parse_one(iter(saved_lines))
                saved_lines = []
            elif line.startswith('@@'):
                hunk = Hunk.from_header(line)
                orig_range = hunk.orig_range
            saved_lines.append(line)
        if len(saved_lines) > 0:
            yield Patch.parse_one(iter(saved_lines))


# ----------------------------------------------------------------------------
# COVERAGE PARSERS

class GccCoverage:
    extensions = [".c", ".cpp", ".cc"]

    def __init__(self, skips):
        self._gcno_cache = []
        self._creating_re = re.compile("^.*'(.+\.gcov)'$")
        self._file_re = re.compile("^File.*'(.+)'$")
        self._skips = skips

        def visit(paths, dirname, names):
            for name in names:
                path = os.path.normpath(os.path.join(dirname, name))
                if os.path.isdir(path):
                    continue
                if not fnmatch.fnmatch(name, "*.gcno"):
                    continue
                # Skip if no gcda file for this gcno file
                (base, ext) = os.path.splitext(path)
                if os.path.exists(base + ".gcda"):
                    paths.append(path)
        os.path.walk(".", visit, self._gcno_cache)

    def _match_gcno_files(self, filename):
        matches = []
        (directory, base) = os.path.split(filename)
        (base, ext) = os.path.splitext(base)
        if directory:
            match = "%s/*%s.gcno" % (directory, base)
        else:
            match = "*%s.gcno" % (base, )
        bad_mtime = False
        mtime = os.path.getmtime(filename)
        for gcno in self._gcno_cache:
            if fnmatch.fnmatch(gcno, match):
                if os.path.getctime(gcno) < mtime:
                    bad_mtime = True
                else:
                    matches.append(gcno)

        if not matches:
            if bad_mtime:
                warning("%s: Found old coverage data, likely not built" % filename)
            else:
                warning("%s: Found no coverage data" % filename)
        return matches

    def _find_directory_gcno_compiled_in(self, gcno, filename):
        cmd = ['gcov', '--preserve-paths', '--relative-only', '--no-output']
        for line in subprocess_lines(cmd + gcno):
            match = self._file_re.match(line.strip())
            if not match:
                continue
            expected = match.group(1)
            if filename.endswith(expected):
                extra = filename[:-len(expected)]
                if os.path.exists(extra):
                    return extra
            elif expected.endswith(filename):
                extra = expected[:-len(filename)]
                up = "../" * len(extra.strip(os.sep).split(os.sep))
                if os.path.exists(up):
                    return up
        return None

    def _gcov_lines_for_files(self, filename):
        gcno = self._match_gcno_files(filename)
        if not gcno:
            return

        absgcno = [os.path.abspath(path) for path in gcno]

        # gcov wants to be in the directory that gcc was executed
        # from. We don't know which directory that is. So we run
        # gcov once to figure out the file path it thinks the source
        # is at.

        directory = self._find_directory_gcno_compiled_in(absgcno, filename)

        oldcwd = None
        if directory:
            oldcwd = os.getcwd()
            os.chdir(directory)

        # We scrape the output of the command for the names of the
        # gcov files created, which we process, and then remove
        gcovs = []

        cmd = ['gcov', '--preserve-paths', '--relative-only']
        for line in subprocess_lines(cmd + absgcno):
            match = self._creating_re.match(line.strip())
            if not match:
                continue
            gcov = match.group(1)
            if os.path.exists(gcov):
                gcovs.append(os.path.abspath(gcov))

        # Because we change the directory, we have to take care not
        # to yield while the current directory is changed

        if oldcwd:
            os.chdir(oldcwd)

        for gcov in gcovs:
            with open(gcov, 'r') as f:
                for l in f:
                    yield l
                os.unlink(gcov)

    def coverage(self, filename):
        coverage = { }
        for line in self._gcov_lines_for_files(filename):
            # Each gcov coverage output line looks something like this
            #  coverage:   lineno:  remainder is actual line content
            parts = line.split(':', 2)
            if len(parts) != 3:
                continue

            covered = parts[0].strip()
            try:
                no = int(parts[1].strip())
                if covered == '-':
                    count = 0
                else:
                    count = int(covered)
                coverage[no] = parts[2]
            except ValueError:
                if match_any_re(self._skips, parts[2]):
                    coverage[no] = parts[2]
        return coverage

    def usage(self, output):
        string = """GCC gcov C code coverage

             Used with: %s

             The program should be (re)built with the specicial GCC options
             '-fprofile-arcs -ftest-coverage'. Run the C applications as you
             normally would to create test coverage data.
        """

        message = string % ", ".join(self.extensions)
        message = message.replace("\t", "")
        output.write(message)


class PythonCoverage:
    extensions = [".py"]

    def __init__(self, skips):
        self._temp_dir = tempfile.mkdtemp(prefix='git-coverage')
        self._skips = skips

    def __del__(self):
        for path in self._list_files():
            os.unlink(path)
        os.rmdir(self._temp_dir)

    def _list_files(self):
        for name in os.listdir(self._temp_dir):
            if fnmatch.fnmatch(name, "*,cover"):
                yield os.path.join(self._temp_dir, name)

    def _read_coverage(self, filename):
        coverage = { }
        no = 1
        for line in open(filename, 'r'):
            if not line.startswith("!"):
                coverage[no] = line
            elif match_any_re(self._skips, line[1:]):
                covarege[no] = line
            no += 1
        return coverage

    def coverage(self, filename):
        cmd = ["coverage", "annotate", "--directory", self._temp_dir, filename]
        subprocess.check_call(cmd)

        coverage = { }
        base = os.path.basename(filename)

        for path in self._list_files():
            if not coverage and fnmatch.fnmatch(path, "*_%s,cover" % base):
                coverage = self._read_coverage(path)
            os.unlink(path)

        return coverage

    def usage(self, output):
        string = """Python code coverage

             Used with: %s

             This requires the python-coverage module. The program should be
             run with 'coverage run my_program.py' which produces test coverage
             data in the current directory.
        """

        message = string % ", ".join(self.extensions)
        message = message.replace("\t", "")
        output.write(message)


class Output:
    defaults = {
            'diff.new': 'green',
            'diff.meta': 'bold',
            'diff.plain': 'normal',
            'diff.frag': 'cyan',
            'diff.old': 'red',
            'diff.whitespace': 'normal red',
            'diff.problem': 'normal red'
    }

    def __init__(self, output):
        self.output = output
        self.escapes = { }

        cmd = ['git', 'config', '--get-colorbool',
               'color.diff', output.isatty() and 'true' or 'false']
        self.with_colors = subprocess.check_output(cmd).strip() == 'true'

    def write_meta(self, data, meta):
        if not self.with_colors:
            pass
        elif not meta:
            self.output.write('\033[0m')
        elif meta in self.escapes:
            self.output.write(self.escapes[meta])
        else:
            default = self.defaults.get(meta, 'normal')
            cmd = ['git', 'config', '--get-color', "color.%s" % meta, default]
            escape = subprocess.check_output(cmd)
            self.output.write(escape)
            self.escapes[meta] = escape
        self.write(data)
        if self.with_colors:
            self.output.write('\033[0m')

    def write(self, data):
        self.output.write(data)


# ----------------------------------------------------------------------------

def print_patch_hunks(patch, hunks, coverage, output):
    for line in patch.prefix:
        output.write_meta(line, 'diff.meta')
    for hunk in hunks:
        output.write_meta(hunk.header, 'diff.frag')
        for (no, line) in hunk.lines:
            if no in coverage:
                output.write(" ")
            else:
                output.write_meta("!", 'diff.problem')
            if line.startswith("-"):
                output.write_meta(line, 'diff.old')
            elif line.startswith("+"):
                output.write_meta(line, 'diff.new')
            else:
                output.write_meta(line, 'diff.plain')

def is_hunk_covered(hunk, coverage):
    for (no, contents) in hunk.lines:
        if no not in coverage:
            return False
    return True


def usage(parsers, output=sys.stdout):
    string = """usage: git coverage [diff-options] commit

    Shows the code coverage for code changed between the specified commit and
    the latest code. Use standard git diff options to specify which commits
    to include in the code.
    """
    message = string.replace("\t", "")
    output.write(message)
    for parser in parsers:
        output.write("\n")
        parser.usage(output=output)


def main(argv):
    skips = [re.compile(p) for p in SKIP_PATTERNS]

    parsers = (
            GccCoverage(skips),
            PythonCoverage(skips)
    )

    have_target = False
    for arg in argv[1:]:
        if arg in ('-h', '--help'):
            usage(parsers)
            return 0

        if not arg.startswith("-"):
            have_target = True
            break

    cmd = GIT_DIFF + argv[1:]
    if not have_target:
        cmd += ['HEAD']

    output = Output(sys.stdout)

    printed_any = 0
    patches_by_filename = { }

    # Pull all the patches appart into the hunks that we need
    for patch in Patch.parse(subprocess_lines(cmd)):
        filename = os.path.normpath(patch.newname)
        if filename not in patches_by_filename:
            patches_by_filename[filename] = []
        patches_by_filename[filename].append(patch)

    # Now go through and calculate coverage
    for (filename, patches) in patches_by_filename.items():
        (name, ext) = os.path.splitext(filename)
        for parser in parsers:
            if ext in parser.extensions:
                coverage = parser.coverage(filename)
                break
        else:
            continue

        for patch in patches:
            to_print = []
            for hunk in patch.hunks:
                if not is_hunk_covered(hunk, coverage):
                    to_print.append(hunk)
            if to_print:
                print_patch_hunks(patch, to_print, coverage, output)
                printed_any = 1

    return printed_any


if __name__ == '__main__':
    sys.exit(main(sys.argv))