用于监控git仓库中代码债务的仪表板。
项目描述
git-code-debt
用于监控git仓库中代码债务的仪表板。
安装
pip安装git-code-debt
用法
基本/简单用法
创建一个generate_config.yaml
# required: repository to clone (can be anything `git clone` understands) even
# a repository already on disk
repo: git@github.com:asottile/git-code-debt
# required: database generation path
database: database.db
# optional: default False
skip_default_metrics: false
# optional: default []
metric_package_names: []
# optional: default ^$ (python regex) to exclude paths such as '^vendor/'
exclude: ^$
调用cli
# Generate code metric data (substitute your own repo path)
$ git-code-debt-generate
# Start the server
$ git-code-debt-server database.db
更新现有数据库中的数据
向数据库添加数据就像再次运行generate一样简单。 git-code-debt
将从上次生成数据的地方获取git历史记录。
$ git-code-debt-generate
创建自己的指标
- 创建一个python项目,将
git-code-debt
作为依赖项。 - 创建一个包,你将在其中编写指标
- 将你的包添加到
generate_config.yaml
中的metric_package_names
编写自定义指标的最简单方法是扩展git_code_debt.metrics.base.SimpleLineCounterBase
。
以下是基类的外观
class SimpleLineCounterBase(DiffParserBase):
# ...
def should_include_file(self, file_diff_stat: FileDiffStat) -> bool:
"""Implement me to return whether a filename should be included.
By default, this returns True.
:param FileDiffStat file_diff_stat:
"""
return True
def line_matches_metric(self, line: bytes, file_diff_stat: FileDiffStat) -> bool:
"""Implement me to return whether a line matches the metric.
:param bytes line: Line in the file
:param FileDiffStat file_diff_stat:
"""
raise NotImplementedError
以下是一个示例指标
from git_code_debt.metrics.base import SimpleLineCounterBase
class Python__init__LineCount(SimpleLineCounterBase):
"""Counts the number of lines in __init__.py"""
def should_include_file(self, file_diff_stat: FileDiffStat) -> bool:
return file_diff_stat.filename == b'__init__.py'
def line_matches_metric(self, line: bytes, file_diff_stat -> FileDiffStat) -> bool:
# All lines in __init__.py match
return True
提供了一个额外的类,它将行作为文本提供(SimpleLineCounterBase
将它们作为bytes
提供):TextLineCounterBase
。以下是一个使用该基类的示例指标
from git_code_debt.metrics.base import TextLineCounterBase
class XXXLineCount(TextLineCounterBase):
"""Counts the number of lines which are XXX comments"""
def text_line_matches_metric(self, line: str, file_diff_stat: FileDiffStat) -> bool:
return '# XXX' in line
更复杂的指标可以扩展DiffParserBase
class DiffParserBase(object):
# Specify __metric__ = False to not be included (useful for base classes)
__metric__ = False
def get_metrics_from_stat(self, commit: Commit, file_diff_stats: Tuple[FileDiffStat, ...]) -> bool:
"""Implement me to yield Metric objects from the input list of
FileStat objects.
Args:
commit - Commit object
file_diff_stats - list of FileDiffStat objects
Returns:
generator of Metric objects
"""
raise NotImplementedError
def get_metrics_info(self) -> List[MetricInfo]:
"""Implement me to yield `MetricInfo` objects."""
raise NotImplementedError