Files
git.taskfile.build/bin/add-git-folder
T
Janik von Rotz db90606f7f feat(add-git-folder): validate branch before adding submodule
- Check if the target repository is on the expected branch before proceeding
- Exit with error if branch mismatch is detected
- Improve reliability of submodule addition by enforcing branch consistency
2026-06-16 09:30:51 +02:00

33 lines
823 B
Bash
Executable File

#!/bin/bash
add-git-folder() {
if [[ -z "$1" ]]; then
echo '$1/url is empty.'
exit
fi
if [[ -z "$2" ]]; then
echo '$2/branch is empty.'
exit
fi
if [[ -z "$3" ]]; then
echo '$3/path is empty.'
exit
fi
local url="$1"
local branch="$2"
local path="$3"
local current_branch=$(git -C "$path" rev-parse --abbrev-ref HEAD)
if [[ "$current_branch" != "$branch" ]]; then
echo "Error: Repository is on branch '$current_branch', expected '$branch'"
exit 1
fi
# Add entry to .gitmodules
git config -f .gitmodules submodule."$path".path "$path"
git config -f .gitmodules submodule."$path".url "$url"
git config -f .gitmodules submodule."$path".branch "$branch"
# Save commit ref
save-git-folder
}