|
| 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