避免成为垃圾邮件_如何避免犯垃圾

避免成为垃圾邮件

by Yoel Zeldes

由Yoel Zeldes

如何避免犯垃圾 (How to avoid committing junk)

In the development process, every developer writes stuff they don’t intend to commit and push to the remote server, things like debug prints. It happens to all of us every now and then: we forget to remove this temporary stuff before committing…

在开发过程中,每个开发人员都会编写他们不打算提交并推送到远程服务器的内容,例如调试打印。 它时不时地发生在我们所有人身上:我们忘记在提交之前删除这些临时性内容…

I solved this somewhat embarrassing situation using a simple approach: to every line I don’t want to accidentally commit, I add the magic characters sequence xxx. This sequence can be in any part of the line: inside a comment, as a variable name, as a function name, you name it. A few usage examples:

我使用一种简单的方法解决了这种令人尴尬的情况:在我不想意外提交的每一行中,我添加了魔术字符序列xxx 。 该序列可以在行的任何部分:在注释内,作为变量名,函数名,您可以对其进行命名。 一些用法示例:

  • debug print: print 'xxx reached this line'.

    调试打印: print 'xxx reached this line'

  • variable used for debugging: xxx_counter = 0.

    用于调试的变量: xxx_counter = 0

  • temporary function: def xxx_print_debug_info():.

    临时功能: def xxx_print_debug_info():

  • TODO which must be attended before committing: #TODO: don't forget to refactor this function xxx.

    提交前必须要处理的TODO: #TODO: don't forget to refactor this function xxx

I implemented it using Git hooks. A hook is Git’s mechanism to fire off custom scripts when certain important actions occur. I used the pre-commit hook for validating the commit’s content.

我使用Git钩子实现了它。 挂钩是Git的机制,可在发生某些重要操作时触发自定义脚本。 我使用了pre-commit钩子来验证提交的内容。

Just create a file with the name <YOUR-REPO-FOLDER>/.git/hooks/pre-commit with the following content:

只需创建一个名称为<YOUR-REPO-FOLDER>/.git/hooks/pre- commit的文件,其内容如下:

#!/bin/sh
marks=xxx,aaamarksRegex=`echo "($marks)" | sed -r 's/,/|/g'`marksMessage=`echo "$marks" | sed -r 's/,/ or /g'`if git diff --staged | egrep -q "^\+.*$marksRegex"; then    echo "you forgot to remove a line containing $marksMessage!"    echo "you can forcefully commit using \"commit -n\""    exit 1fi
  1. marks contains the characters sequences which are not allowed to be committed.

    marks包含不允许提交的字符序列。

  2. git diff --staged shows the changes which will be committed. The changes are passed to a regular expression that searches for any forbidden mark (using egrep).

    git diff --staged显示将要提交的更改。 所做的更改将传递给正则表达式,该正则表达式搜索任何禁止的标记(使用egrep )。

  3. If a forbidden mark is found, the script exits with an error code, causing the commit to fail.

    如果找到了禁止标记,脚本将退出并显示错误代码,从而导致提交失败。

You want to bypass the hook? Executecommit -n. This can be useful when you want to commit a binary file such as an image, which may contain a forbidden mark.

您想绕过钩子吗? 执行commit -n 。 当您要提交二进制文件(例如图像)时,这可能很有用,例如可能包含禁止标记的图像。

Any tricks you’ve implemented in your daily git workflow? Share your magic in the comments :)

您在日常git工作流程中实施了什么技巧? 在评论中分享您的魔力:)

This post was originally posted by me at www.anotherdatum.com.

该帖子最初由我在www.anotherdatum.com上发布。

翻译自: https://www.freecodecamp.org/news/how-to-avoid-committing-junk-dae1277c1433/

避免成为垃圾邮件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值