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#!/bin/sh2# Copyright (c) 2015-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.6export LC_ALL=C.UTF-88check_remote=09while getopts "?hr" opt; do10case $opt in11'?' | h)12echo "Usage: $0 [-r] DIR [COMMIT]"13echo " $0 -?"14echo ""15echo "Checks that a certain prefix is pure subtree, and optionally whether the"16echo "referenced commit is present in any fetched remote."17echo ""18echo "DIR is the prefix within the repository to check."19echo "COMMIT is the commit to check, if it is not provided, HEAD will be used."20echo ""21echo "-r Check that subtree commit is present in repository."22echo " To do this check, fetch the subtreed remote first. Example:"23echo ""24echo " git fetch https://github.com/bitcoin-core/secp256k1.git"25echo " test/lint/git-subtree-check.sh -r src/secp256k1"26exit 127;;28r)29check_remote=130;;31esac32done33shift $((OPTIND-1))35if [ -z "$1" ]; then36echo "Need to provide a DIR, see $0 -?"37exit 138fi40# Strip trailing / from directory path (in case it was added by autocomplete)41DIR="${1%/}"42COMMIT="$2"43if [ -z "$COMMIT" ]; then44COMMIT=HEAD45fi47# Taken from git-subtree (Copyright (C) 2009 Avery Pennarun <[email protected]>)48find_latest_squash()49{50dir="$1"51sq=52main=53sub=54git log --grep="^git-subtree-dir: $dir/*\$" \55--pretty=format:'START %H%n%s%n%n%b%nEND%n' "$COMMIT" |56while read a b _; do57case "$a" in58START) sq="$b" ;;59git-subtree-mainline:) main="$b" ;;60git-subtree-split:) sub="$b" ;;61END)62if [ -n "$sub" ]; then63if [ -n "$main" ]; then64# a rejoin commit?65# Pretend its sub was a squash.66sq="$sub"67fi68echo "$sq" "$sub"69break70fi71sq=72main=73sub=74;;75esac76done77}79# find latest subtree update80latest_squash="$(find_latest_squash "$DIR")"81if [ -z "$latest_squash" ]; then82echo "ERROR: $DIR is not a subtree" >&283exit 284fi85# shellcheck disable=SC208686set $latest_squash87old=$188rev=$290# get the tree in the current commit91tree_actual=$(git ls-tree -d "$COMMIT" "$DIR" | head -n 1)92if [ -z "$tree_actual" ]; then93echo "FAIL: subtree directory $DIR not found in $COMMIT" >&294exit 195fi96# shellcheck disable=SC208697set $tree_actual98tree_actual_type=$299tree_actual_tree=$3100echo "$DIR in $COMMIT currently refers to $tree_actual_type $tree_actual_tree"101if [ "d$tree_actual_type" != "dtree" ]; then102echo "FAIL: subtree directory $DIR is not a tree in $COMMIT" >&2103exit 1104fi106# get the tree at the time of the last subtree update107tree_commit=$(git show -s --format="%T" "$old")108echo "$DIR in $COMMIT was last updated in commit $old (tree $tree_commit)"110# ... and compare the actual tree with it111if [ "$tree_actual_tree" != "$tree_commit" ]; then112git diff "$tree_commit" "$tree_actual_tree" >&2113echo "FAIL: subtree directory was touched without subtree merge" >&2114exit 1115fi117if [ "$check_remote" != "0" ]; then118# get the tree in the subtree commit referred to119if [ "d$(git cat-file -t "$rev" 2>/dev/null)" != dcommit ]; then120echo "subtree commit $rev unavailable: cannot compare. Did you add and fetch the remote?" >&2121exit 1122fi123tree_subtree=$(git show -s --format="%T" "$rev")124echo "$DIR in $COMMIT was last updated to upstream commit $rev (tree $tree_subtree)"126# ... and compare the actual tree with it127if [ "$tree_actual_tree" != "$tree_subtree" ]; then128echo "FAIL: subtree update commit differs from upstream tree!" >&2129exit 1130fi131fi133echo "GOOD"
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.