Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or https://opensource.org/license/mit/.5use std::fs::File;6use std::io::{Read, Seek, SeekFrom};8use crate::util::{check_output, commit_range, get_pathspecs_default_excludes, git, LintResult};10/// Return the pathspecs for whitespace related excludes11fn get_pathspecs_exclude_whitespace() -> Vec<String> {12let mut list = get_pathspecs_default_excludes();13list.extend(14[15// Permanent excludes16"*.patch",17"src/qt/locale",18"contrib/windeploy/win-codesign.cert",19"doc/README_windows.txt",20// Temporary excludes, or existing violations21"src/qt/res/src/*.svg",22"test/functional/test_framework/crypto/ellswift_decode_test_vectors.csv",23"test/functional/test_framework/crypto/xswiftec_inv_test_vectors.csv",24]25.iter()26.map(|s| format!(":(exclude){s}")),27);28list29}31pub fn lint_trailing_whitespace() -> LintResult {32let trailing_space = git()33.args(["grep", "-I", "--line-number", "\\s$", "--"])34.args(get_pathspecs_exclude_whitespace())35.status()36.expect("command error")37.success();38if trailing_space {39Err(r#"40Trailing whitespace (including Windows line endings [CR LF]) is problematic, because git may warn41about it, or editors may remove it by default, forcing developers in the future to either undo the42changes manually or spend time on review.44Thus, it is best to remove the trailing space now.46Please add any false positives, such as subtrees, Windows-related files, patch files, or externally47sourced files to the exclude list.48"#49.trim()50.to_string())51} else {52Ok(())53}54}56pub fn lint_trailing_newline() -> LintResult {57let files = check_output(58git()59.args([60"ls-files", "--", "*.py", "*.cpp", "*.h", "*.md", "*.rs", "*.sh", "*.cmake",61])62.args(get_pathspecs_default_excludes()),63)?;64let mut missing_newline = false;65for path in files.lines() {66let mut file = File::open(path).expect("must be able to open file");67if file.seek(SeekFrom::End(-1)).is_err() {68continue; // Allow fully empty files69}70let mut buffer = [0u8; 1];71file.read_exact(&mut buffer)72.expect("must be able to read the last byte");73if buffer[0] != b'\n' {74missing_newline = true;75println!("{path}");76}77}78if missing_newline {79Err(r#"80A trailing newline is required, because git may warn about it missing. Also, it can make diffs81verbose and can break git blame after appending lines.83Thus, it is best to add the trailing newline now.85Please add any false positives to the exclude list.86"#87.trim()88.to_string())89} else {90Ok(())91}92}94pub fn lint_tabs_whitespace() -> LintResult {95let tabs = git()96.args(["grep", "-I", "--line-number", "--perl-regexp", "^\\t", "--"])97.args(["*.cpp", "*.h", "*.md", "*.py", "*.sh"])98.args(get_pathspecs_exclude_whitespace())99.status()100.expect("command error")101.success();102if tabs {103Err(r#"104Use of tabs in this codebase is problematic, because existing code uses spaces and tabs will cause105display issues and conflict with editor settings.107Please remove the tabs.109Please add any false positives, such as subtrees, or externally sourced files to the exclude list.110"#111.trim()112.to_string())113} else {114Ok(())115}116}118pub fn lint_commit_msg() -> LintResult {119let mut good = true;120let commit_hashes = check_output(git().args([121"-c",122"log.showSignature=false",123"log",124&commit_range(),125"--format=%H",126]))?;127for hash in commit_hashes.lines() {128let commit_info = check_output(git().args([129"-c",130"log.showSignature=false",131"log",132"--format=%B",133"-n",134"1",135hash,136]))?;137if let Some(line) = commit_info.lines().nth(1) {138if !line.is_empty() {139println!(140"The subject line of commit hash {hash} is followed by a non-empty line. Subject lines should always be followed by a blank line."141);142good = false;143}144}145}146if good {147Ok(())148} else {149Err("".to_string())150}151}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.