def addCommit()

in Allura/allura/model/stats.py [0:0]


    def addCommit(self, newcommit, commit_datetime, project):
        def _computeLines(newblob, oldblob=None):
            if oldblob:
                listold = list(oldblob)
            else:
                listold = []
            if newblob:
                listnew = list(newblob)
            else:
                listnew = []

            if oldblob is None:
                lines = len(listnew)
            elif newblob and newblob.has_html_view:
                # py2 unified_diff can handle some unicode but not consistently, so best to do ensure_str (can drop it on py3)
                diff = difflib.unified_diff(
                    [six.ensure_str(h.really_unicode(line)) for line in listold],
                    [six.ensure_str(h.really_unicode(line)) for line in listnew],
                    six.ensure_str('old' + oldblob.path()),
                    six.ensure_str('new' + newblob.path()))
                lines = len(
                    [l for l in diff if len(l) > 0 and l[0] == '+']) - 1
            else:
                lines = 0
            return lines

        def _addCommitData(stats, topics, languages, lines):
            lt = topics + [None]
            ll = languages + [None]
            for t in lt:
                i = getElementIndex(stats.general, category=t)
                if i is None:
                    newstats = dict(
                        category=t,
                        commits=[],
                        messages=[],
                        tickets=dict(
                            assigned=0,
                            solved=0,
                            revoked=0,
                            totsolvingtime=0))
                    stats.general.append(newstats)
                    i = getElementIndex(stats.general, category=t)
                for lang in ll:
                    j = getElementIndex(
                        stats.general[i]['commits'], language=lang)
                    if j is None:
                        stats.general[i]['commits'].append(dict(
                            language=lang, lines=lines, number=1))
                    else:
                        stats.general[i]['commits'][j].lines += lines
                        stats.general[i]['commits'][j].number += 1

        topics = [t for t in project.trove_topic if t]
        languages = [l for l in project.trove_language if l]

        d = newcommit.diffs
        if len(newcommit.parent_ids) > 0:
            oldcommit = newcommit.repo.commit(newcommit.parent_ids[0])

        totlines = 0
        if asbool(config.get('userstats.count_lines_of_code', True)):
            for changed in d.changed:
                newblob = newcommit.tree.get_blob_by_path(changed)
                oldblob = oldcommit.tree.get_blob_by_path(changed)
                totlines += _computeLines(newblob, oldblob)

            for copied in d.copied:
                newblob = newcommit.tree.get_blob_by_path(copied['new'])
                oldblob = oldcommit.tree.get_blob_by_path(copied['old'])
                totlines += _computeLines(newblob, oldblob)

            for added in d.added:
                newblob = newcommit.tree.get_blob_by_path(added)
                totlines += _computeLines(newblob)

        _addCommitData(self, topics, languages, totlines)

        self.lastmonth.commits.append(dict(
            datetime=commit_datetime,
            categories=topics,
            programming_languages=languages,
            lines=totlines))
        self.checkOldArtifacts()