summaryrefslogtreecommitdiff
path: root/git-coverage
blob: ac837c9e755009558ef94849c1785f93ab477e51 (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
#!/usr/bin/env python

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

COVERAGE_EXTENSIONS = [".c", ".cpp", ".cc"]

# Portions of the code:
#
# 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
#

BINARY_FILES_RE = re.compile('Binary files (.*) and (.*) differ\n')

class MalformedHunkHeader(Exception):
    def __init__(self, desc, line):
        self.desc = desc
        self.line = line

    def __str__(self):
        return "Malformed hunk header.  %(desc)s\n%(line)r" % self.__dict__

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

    def __str__(self):
        return self.message


def get_patch_names(iter_lines):
    try:
        line = iter_lines.next()
        if not line.startswith("--- "):
            raise BadPatch("No orig name: %s" % line)
        else:
            orig_name = line[4:].rstrip("\n")
    except StopIteration:
        raise BadPatch("No orig line")
    try:
        line = iter_lines.next()
        if not line.startswith("+++ "):
            raise BadPatch("No mod name")
        else:
            mod_name = line[4:].rstrip("\n")
    except StopIteration:
        raise BadPatch("No mod line")
    return (orig_name, mod_name)


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)


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


class HunkLine:
    def __init__(self, contents):
        self.contents = contents


class ContextLine(HunkLine):
    def __init__(self, contents):
        HunkLine.__init__(self, contents)


class InsertLine(HunkLine):
    def __init__(self, contents):
        HunkLine.__init__(self, contents)


class RemoveLine(HunkLine):
    def __init__(self, contents):
        HunkLine.__init__(self, contents)


def parse_line(line):
    if line.startswith("\n"):
        return ContextLine(line)
    elif line.startswith(" "):
        return ContextLine(line[1:])
    elif line.startswith("+"):
        return InsertLine(line[1:])
    elif line.startswith("-"):
        return RemoveLine(line[1:])
    else:
        raise BadPatch("Unknown line type" % line)


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


def iter_hunks(iter_lines, allow_dirty=False):
    hunk = None
    for line in iter_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 MalformedHunkHeader:
            if allow_dirty:
                # 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
            raise
        orig_size = 0
        mod_size = 0
        while orig_size < hunk.orig_range or mod_size < hunk.mod_range:
            hunk_line = parse_line(iter_lines.next())
            hunk.lines.append(hunk_line)
            if isinstance(hunk_line, (RemoveLine, ContextLine)):
                orig_size += 1
            if isinstance(hunk_line, (InsertLine, ContextLine)):
                mod_size += 1
    if hunk is not None:
        yield hunk


class Patch(object):
    def __init__(self, oldname, newname):
        self.oldname = oldname
        self.newname = newname
        self.hunks = []


def parse_patch(iter_lines, allow_dirty=False):
    (orig_name, mod_name) = get_patch_names(iter_lines)
    patch = Patch(orig_name, mod_name)
    for hunk in iter_hunks(iter_lines, allow_dirty):
        patch.hunks.append(hunk)
    return patch


def iter_file_patch(iter_lines, allow_dirty=False):
    saved_lines = []
    orig_range = 0
    beginning = True
    for line in iter_lines:
        if 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 allow_dirty and 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 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 saved_lines


#
# End of code from bzr
# ---------------------------------------------------------------------------

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

def iter_desired_coverage(iter_lines):
    for fil in iter_file_patch(iter_lines, allow_dirty=True):
        patch = parse_patch(fil.__iter__(), allow_dirty=True)
        filename = os.path.normpath(patch.newname.split("/", 1)[1])
        (root, ext) = os.path.splitext(filename)
        if ext not in COVERAGE_EXTENSIONS:
            continue
        for hunk in patch.hunks:
            offset = hunk.mod_pos
            for line in hunk.lines:
                if isinstance(line, RemoveLine):
                    yield (filename, offset, line.contents)
                elif isinstance(line, InsertLine):
                    yield (filename, offset, line.contents)
                    offset += 1
                else:
                    offset += 1

def find_all_gcno_files(directory):
    paths = []
    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
            (base, ext) = os.path.splitext(path)
            if not os.path.exists(base + ".gcda"):
                continue
            paths.append(path)
    os.path.walk(".", visit, paths)
    return paths


def match_gcno_files(filename, gcno_cache):
    matches = []
    (directory, base) = os.path.split(filename)
    (base, ext) = os.path.splitext(base)
    match = "%s/*%s.gcno" % (directory, base)
    for gcno in gcno_cache:
        if fnmatch.fnmatch(gcno, match):
            matches.append(gcno)
    return matches

CREATING_RE = re.compile(".*'(.+\.gcov)'.*")

def gcov_lines_for_files(filename, gcno_cache):
    gcno = match_gcno_files(filename, gcno_cache)
    if not gcno:
        return

    # gcov wants to be in the directory with the source files
    # so we make all the gcno paths absolute and change to that
    # directory

    absgcno = [os.path.abspath(path) for path in gcno]
    (directory, base) = os.path.split(filename)

    oldpwd = 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 iter_process_lines(cmd + absgcno):
        match = CREATING_RE.match(line)
        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

    os.chdir(oldpwd)

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


def gcov_coverage_lines(gcov_iter):
    coverage = { }
    for line in gcov_iter:

        # 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:
            pass
    return coverage


def main(argv):
    try:
        opts, args = getopt.getopt(argv[1:], "h", ["help"])
    except getopt.GetoptError, err:
        print str(err)
        return 2
    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            return 0
        else:
            assert False, "unhandled option"

    if len(args) == 0:
        cmd = ['git', 'diff', 'HEAD']
    else:
        cmd = ['git', 'show'] + args

    # First process all the lines that we need
    needed_coverage = { }
    iterator = iter_process_lines(cmd)
    for (filename, no, content) in iter_desired_coverage(iterator):
        if filename not in needed_coverage:
            needed_coverage[filename] = { }
        needed_coverage[filename][no] = content

    gcno_cache = find_all_gcno_files(".")

    for (filename, needed) in needed_coverage.items():
        gcov = gcov_lines_for_files(filename, gcno_cache)
        coverage = gcov_coverage_lines(gcov)

        for (no, content) in needed.items():
            if no not in coverage:
                print filename, no, content,

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