简体   繁体   English

检测 mercurial 中的新行

[英]Detect new lines in mercurial

We are studying to add precommit hooks to detect coding standards compliance with our company policy.我们正在研究添加预提交挂钩来检测编码标准是否符合我们公司的政策。 We don't want to detect the legacy code, just the new lines added.我们不想检测遗留代码,只检测添加的新行。

In git it's possible to detect the line numbers that are added before committing, since they are shown with the hash number 00000000000 when invoking git blame.在 git 中,可以检测到在提交之前添加的行号,因为在调用 git blame 时,它们以散列号 00000000000 显示。

In mercurial the new lines are not shown in hg blame/annotate.在 mercurial 中,新行不会显示在 hg blame/annotate 中。 Is there another mechanism to figure out that?是否有另一种机制可以解决这个问题? Using hg diff can be really painful to do this.使用 hg diff 这样做真的很痛苦。

Edit your repository ( hg config --local ) or your user config ( hg config --edit ) and enable a pre-commit hook ( hg help config.hooks ):编辑您的存储库 ( hg config --local ) 或您的用户配置 ( hg config --edit ) 并启用预提交挂钩 ( hg help config.hooks ):

[hooks]
pre-commit=/path/to/check-added-lines

In the pre-commit hook, run the following version of the annotate command with the wdir() revset and the modified() + added() filesets: ( hg help revsets and hg help filesets ):在预提交挂钩中,使用wdir() revset 和modified() + added()文件集运行以下版本的注释命令:( hg help revsetshg help filesets ):

hg ann -r 'wdir()' 'set:modified() + added()'

That will give you annotation output where just-added lines (ie lines that only exist in your working directory) have a + after the revision number.这将为您提供注释输出,其中刚刚添加的行(即仅存在于您的工作目录中的行)在修订号之后有一个+ So in check-added-lines you can do something like, for example,所以在check-added-lines你可以做一些事情,例如,

#!/bin/bash
IFS=$'\n'
for line in $(hg ann -r 'wdir()' 'set:modified() + added()' | grep '^[0-9]*+' | sed 's/^[0-9]\++ *: //'); do
    if ! run_checker "$line"; then
        exit 1
    fi
done
exit 0

with your choice of run_checker program.使用您选择的run_checker程序。

Although I hesitate to recommend this, if you're willing to tolerate bugs and potential API breakage (Mercurial considers its behaviour not explicitly documented as experimental, its CLI, and its stdout to be API, which we try to keep very stable), you might also be interested in the new and experimental fix extension, which I think was built with your use-case in mind ( hg help --extension fix ).虽然我不太愿意推荐这个,但如果你愿意容忍错误和潜在的 API 破坏(Mercurial 认为它的行为没有明确记录为实验性的,它的 CLI 和它的标准输出是 API,我们试图保持非常稳定),你也可能对新的和实验性的修复扩展感兴趣,我认为它是根据您的用例构建的 ( hg help --extension fix )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM