Skip to content

Commit 3a4b50a

Browse files
committed
Introduce keyfile check scripts
1 parent a8afa74 commit 3a4b50a

File tree

2 files changed

+340
-0
lines changed

2 files changed

+340
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
@echo off
2+
rem keystore_file_check.bat
3+
rem Find files with the same name as given targets in any other local branch.
4+
5+
rem Defaults for wolfBoot if no args are given
6+
set DEF1=wolfboot_signing_private_key.der
7+
set DEF2=keystore.der
8+
set DEF3=keystore.c
9+
10+
if "%~1"=="" (
11+
set T1=%DEF1%
12+
set T2=%DEF2%
13+
set T3=%DEF3%
14+
set TARGET_COUNT=3
15+
) else (
16+
set T1=%~1
17+
set T2=%~2
18+
set T3=%~3
19+
set TARGET_COUNT=0
20+
if not "%~1"=="" set TARGET_COUNT=1
21+
if not "%~2"=="" set TARGET_COUNT=2
22+
if not "%~3"=="" set TARGET_COUNT=3
23+
)
24+
25+
:: -------------------------------------------------------------------------------------------
26+
:: Begin common section to start at repo root
27+
:: -------------------------------------------------------------------------------------------
28+
setlocal EnableExtensions EnableDelayedExpansion
29+
30+
rem === Resolve script path/dir ===
31+
set "SCRIPT_PATH=%~f0"
32+
for %%I in ("%~dp0") do set "SCRIPT_DIR=%%~fI"
33+
34+
rem === Repo root is parent of /tools/scripts ===
35+
for %%I in ("%SCRIPT_DIR%\..\..") do set "REPO_ROOT=%%~fI"
36+
37+
rem === Caller's current directory ===
38+
for %%I in ("%CD%") do set "CALLER_CWD=%%~fI"
39+
40+
rem === (Optional) Normalize to physical paths via PowerShell to resolve junctions/symlinks ===
41+
rem call :physpath "%SCRIPT_DIR%" SCRIPT_DIR_P
42+
rem call :physpath "%REPO_ROOT%" REPO_ROOT_P
43+
rem call :physpath "%CALLER_CWD%" CALLER_CWD_P
44+
set "SCRIPT_DIR_P=%SCRIPT_DIR%"
45+
set "REPO_ROOT_P=%REPO_ROOT%"
46+
set "CALLER_CWD_P=%CALLER_CWD%"
47+
48+
rem === Print only if caller CWD is neither <root> nor <root>\scripts ===
49+
if /I "%CALLER_CWD_P%"=="%REPO_ROOT_P%" goto after_print
50+
if /I "%CALLER_CWD_P%"=="%REPO_ROOT_P%\scripts" goto after_print
51+
52+
echo Caller CWD = %CALLER_CWD_P%
53+
echo SCRIPT_DIR = %SCRIPT_DIR_P%
54+
echo REPO_ROOT = %REPO_ROOT_P%
55+
56+
:after_print
57+
rem === Always work from repo root ===
58+
pushd "%REPO_ROOT_P%" || goto :err
59+
echo Starting %~nx0 from %CD%
60+
:: -------------------------------------------------------------------------------------------
61+
:: End common section to start at repo root
62+
:: -------------------------------------------------------------------------------------------
63+
64+
65+
rem Ensure we are in a git repo
66+
git rev-parse --git-dir >nul 2>&1
67+
if errorlevel 1 (
68+
echo Error: not inside a git repository.
69+
exit /b 2
70+
)
71+
72+
rem Determine current branch (may be detached)
73+
for /f "usebackq tokens=*" %%B in (`git rev-parse --abbrev-ref HEAD 2^>nul`) do set CUR_BRANCH=%%B
74+
if "%CUR_BRANCH%"=="" set CUR_BRANCH=HEAD
75+
76+
rem Build list of local branches (excluding current, unless detached)
77+
set "BR_LIST="
78+
for /f "usebackq tokens=*" %%R in (`git for-each-ref --format^="%%(refname:short)" refs/heads/`) do (
79+
if /i "%CUR_BRANCH%"=="HEAD" (
80+
set "BR_LIST=!BR_LIST!;;%%R"
81+
) else (
82+
if /i not "%%R"=="%CUR_BRANCH%" (
83+
set "BR_LIST=!BR_LIST!;;%%R"
84+
)
85+
)
86+
)
87+
88+
set EXIT_CODE=0
89+
90+
call :PROCESS_TARGET "%T1%"
91+
if %TARGET_COUNT% LSS 2 goto DONE
92+
call :PROCESS_TARGET "%T2%"
93+
if %TARGET_COUNT% LSS 3 goto DONE
94+
call :PROCESS_TARGET "%T3%"
95+
goto DONE
96+
97+
:PROCESS_TARGET
98+
set "TARGET=%~1"
99+
if "%TARGET%"=="" goto :eof
100+
101+
rem Normalize and get basename
102+
set "TMPP=%TARGET:/=\%"
103+
for %%G in ("%TMPP%") do set "BASENAME=%%~nxG"
104+
105+
echo === Searching for name: %BASENAME% ===
106+
107+
rem -------- FAST CURRENT WORKING TREE SCAN (tracked + untracked + ignored) --------
108+
set CCNT=0
109+
110+
rem Tracked + staged + untracked (excluding ignored)
111+
for /f "usebackq delims=" %%P in (`
112+
git ls-files -co --exclude-standard
113+
`) do (
114+
set "PTH=%%P"
115+
set "PTHB=!PTH:/=\!"
116+
for %%Q in ("!PTHB!") do set "NM=%%~nxQ"
117+
if /i "!NM!"=="%BASENAME%" (
118+
if !CCNT! EQU 0 echo Paths in current branch:
119+
echo .\!PTHB!
120+
set /a CCNT+=1
121+
)
122+
)
123+
124+
rem Ignored files too (in case the file is generated and ignored)
125+
for /f "usebackq delims=" %%P in (`
126+
git ls-files -i --others --exclude-standard
127+
`) do (
128+
set "PTH=%%P"
129+
set "PTHB=!PTH:/=\!"
130+
for %%Q in ("!PTHB!") do set "NM=%%~nxQ"
131+
if /i "!NM!"=="%BASENAME%" (
132+
if !CCNT! EQU 0 echo Paths in current branch:
133+
echo .\!PTHB!
134+
set /a CCNT+=1
135+
)
136+
)
137+
138+
echo Current branch %CUR_BRANCH% has %CCNT% file(s) named %BASENAME%
139+
140+
rem -------- OTHER BRANCHES (tracked only) --------
141+
set FOUND_ANY=0
142+
for %%B in (%BR_LIST:;;= %) do (
143+
for /f "usebackq delims=" %%F in (`
144+
git ls-tree -r --name-only "%%B"
145+
`) do (
146+
set "OP=%%F"
147+
set "OPB=!OP:/=\!"
148+
for %%H in ("!OPB!") do set "ON=%%~nxH"
149+
if /i "!ON!"=="%BASENAME%" (
150+
if "!FOUND_ANY!"=="0" (
151+
echo Matches in other branches:
152+
set FOUND_ANY=1
153+
)
154+
echo %%B:%%F
155+
)
156+
)
157+
)
158+
159+
if "%FOUND_ANY%"=="0" (
160+
echo No matches in other branches.
161+
) else (
162+
set EXIT_CODE=1
163+
)
164+
echo.
165+
goto :eof
166+
167+
:DONE
168+
endlocal & exit /b %EXIT_CODE%
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#!/usr/bin/env bash
2+
#
3+
# keystore_file_check.sh
4+
# Find files with the same name as given targets in any other local branch.
5+
6+
echo "$0 v1.0"
7+
echo ""
8+
9+
# Specify the executable shell checker you want to use:
10+
MY_SHELLCHECK="shellcheck"
11+
12+
# Check if the executable is available in the PATH
13+
if command -v "$MY_SHELLCHECK" >/dev/null 2>&1; then
14+
# Run your command here
15+
shellcheck "$0" || exit 1
16+
else
17+
echo "$MY_SHELLCHECK is not installed. Please install it if changes to this script have been made."
18+
fi
19+
20+
if git --version >/dev/null 2>&1; then
21+
# Run your command here
22+
echo "Confirmed git is installed: $(git --version)"
23+
else
24+
echo "git is not installed. Please install it to use this script." >&2
25+
exit 1
26+
fi
27+
28+
29+
set -euo pipefail
30+
31+
# Defaults for wolfBoot, used when no args are given
32+
DEFAULTS=(
33+
"wolfboot_signing_private_key.der"
34+
"keystore.der"
35+
"keystore.c"
36+
)
37+
38+
usage() {
39+
echo "Usage: $0 [file1 [file2 ...]]"
40+
echo "If no files are provided, the defaults are:"
41+
printf " %s\n" "${DEFAULTS[@]}"
42+
}
43+
44+
45+
# --------------------------------------------------------------------------------------------
46+
# Begin common section to start at repo root, for /tools/scripts
47+
# --------------------------------------------------------------------------------------------
48+
# Resolve this script's absolute path and its directories
49+
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
50+
SCRIPT_PATH="${SCRIPT_DIR}/$(basename -- "${BASH_SOURCE[0]}")"
51+
REPO_ROOT="$(cd -- "${SCRIPT_DIR}/../.." && pwd)"
52+
53+
# Normalize to physical paths (no symlinks, no trailing slashes)
54+
# SCRIPT_DIR_P="$(cd -- "$SCRIPT_DIR" && pwd -P)"
55+
REPO_ROOT_P="$(cd -- "$REPO_ROOT" && pwd -P)"
56+
CALLER_CWD_P="$(pwd -P)" # where the user ran the script from
57+
58+
# Print only if caller's cwd is neither REPO_ROOT nor REPO_ROOT/scripts
59+
case "$CALLER_CWD_P" in
60+
"$REPO_ROOT_P" | "$REPO_ROOT_P"/scripts)
61+
: # silent
62+
;;
63+
*)
64+
echo "Script paths:"
65+
echo "-- SCRIPT_PATH =$SCRIPT_PATH"
66+
echo "-- SCRIPT_DIR =$SCRIPT_DIR"
67+
echo "-- REPO_ROOT =$REPO_ROOT"
68+
;;
69+
esac
70+
71+
# Always work from the repo root, regardless of where the script was invoked
72+
cd -- "$REPO_ROOT_P" || { printf 'Failed to cd to: %s\n' "$REPO_ROOT_P" >&2; exit 1; }
73+
echo "Starting $0 from $(pwd -P)"
74+
# --------------------------------------------------------------------------------------------
75+
# End common section to start at repo root, for /tools/scripts
76+
# --------------------------------------------------------------------------------------------
77+
78+
79+
80+
81+
82+
# Resolve targets
83+
if [ "$#" -eq 0 ]; then
84+
TARGETS=("${DEFAULTS[@]}")
85+
else
86+
TARGETS=("$@")
87+
fi
88+
89+
# Ensure we are in a git repo
90+
if ! git rev-parse --git-dir >/dev/null 2>&1; then
91+
echo "Error: not inside a git repository."
92+
exit 2
93+
fi
94+
95+
# Determine current branch (may be detached)
96+
CURRENT_BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")"
97+
98+
# Get list of local branches
99+
mapfile -t BRANCHES < <(git for-each-ref --format="%(refname:short)" refs/heads/)
100+
101+
# Filter out current branch if named
102+
OTHER_BRANCHES=()
103+
for b in "${BRANCHES[@]}"; do
104+
if [ "$CURRENT_BRANCH" != "HEAD" ] && [ -n "$CURRENT_BRANCH" ]; then
105+
if [ "$b" != "$CURRENT_BRANCH" ]; then
106+
OTHER_BRANCHES+=("$b")
107+
fi
108+
else
109+
# Detached HEAD, consider all branches
110+
OTHER_BRANCHES+=("$b")
111+
fi
112+
done
113+
114+
# Helper to list and count files with this basename in the current working tree
115+
# Includes tracked, untracked, and ignored files (fast: no disk crawl)
116+
list_and_count_in_current_branch() {
117+
local base="$1"
118+
local count=0
119+
local printed=0
120+
# Collect working-tree paths: tracked + untracked + ignored, unique
121+
while IFS= read -r path; do
122+
name="${path##*/}"
123+
if [ "$name" = "$base" ]; then
124+
if [ $printed -eq 0 ]; then
125+
echo "Paths in current branch ${CURRENT_BRANCH:-(detached)}:"
126+
printed=1
127+
fi
128+
echo " ./$path"
129+
count=$((count + 1))
130+
fi
131+
done < <( { git ls-files -co --exclude-standard; git ls-files -i --others --exclude-standard; } | sort -u )
132+
echo "$count"
133+
}
134+
135+
136+
exit_code=0
137+
138+
for target in "${TARGETS[@]}"; do
139+
base="${target##*/}"
140+
141+
echo "=== Searching for name: ${base} ==="
142+
143+
# Report presence count on current branch
144+
cur_count="$(list_and_count_in_current_branch "$base")"
145+
echo "Current branch ${CURRENT_BRANCH:-(detached)} has ${cur_count} file(s) named ${base}"
146+
147+
# Search other branches
148+
found_any=0
149+
for br in "${OTHER_BRANCHES[@]}"; do
150+
# List all names in branch and print matches by basename
151+
while IFS= read -r path; do
152+
name="${path##*/}"
153+
if [ "$name" = "$base" ]; then
154+
if [ "$found_any" -eq 0 ]; then
155+
echo "Matches in other branches:"
156+
found_any=1
157+
fi
158+
echo " ${br}:${path}"
159+
fi
160+
done < <(git ls-tree -r --name-only "$br")
161+
done
162+
163+
if [ "$found_any" -eq 0 ]; then
164+
echo "No matches in other branches."
165+
else
166+
exit_code=1
167+
fi
168+
169+
echo
170+
done
171+
172+
exit "$exit_code"

0 commit comments

Comments
 (0)