惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
V
Visual Studio Blog
雷峰网
雷峰网
J
Java Code Geeks
博客园 - 三生石上(FineUI控件)
人人都是产品经理
人人都是产品经理
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog RSS Feed
C
Check Point Blog
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
D
Docker
IT之家
IT之家
博客园 - 聂微东
腾讯CDC
U
Unit 42
Microsoft Security Blog
Microsoft Security Blog
The Cloudflare Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Check Trailing White Spaces in Your Project
2025-12-20 · via jdhao's digital space

When I am working on a Python project, I am using black to format the code, so that we have a unified format across the code base.

One pain point, however, is the super annoying trailing white spaces:

  1. black can remove trailing spaces for doc string, code and comment, but it will not touch trailing spaces in, e.g., multi-line strings.
my_multiline_str = """
this is a string that
spans multiple line.
<space><space>
The above line has trailing spaces that black won't touch
"""
  1. For other files such as YAML, black can not format those
  2. Not everyone is using the same tool, which makes enforcement of removing trailing white spaces locally hard

So I want to check if any of the files tracked by git has trailing spaces, and if yes, break the pipeline. This will force everyone to be aware of the issue and fix them on their side.

How do we do this then?

with grep#

We can achieve this with grep:

if grep --recursive --line-number  --exclude-dir="*cache*" '[[:blank:]]$' src/; then
    echo "Trailing whitespaces found"
    exit 1
fi

In the above, option --exclude-dir is used to exclude some directories to reduce noise. The exit 1 is used to break the pipeline run.

Or, you can combine grep with git:

if git ls-files | xargs grep --recursive --line-number '[[:blank:]]$'; then
    echo "Trailing whitespaces found"
    exit 1
fi

with git#

The git diff --check command can check if there are trailing white spaces in your current diff, however, if you have committed the code changes, this command is useless. This means that in the pipeline, this command does not work since the code is already committed.

Base on this post here, we can instead use the git diff-tree command.

git diff-tree --check $(git hash-object -t tree /dev/null) HEAD

The command git hash-object -t tree /dev/null just compute an empty hash, so that we can compare the current code against “EMPTY”. With this trick, we can check the trailing white spaces even if the code is committed.

references#