Add Makefile with target to add git submodules and usage instructions

This commit is contained in:
2026-01-18 20:59:11 -05:00
parent 21a5de903e
commit 805dca9310

48
Makefile Normal file
View File

@@ -0,0 +1,48 @@
.PHONY: add-submodule help
help:
@echo "Available targets:"
@echo " add-submodule - Add a git submodule"
@echo " Usage: make add-submodule <git-url> [directory-name]"
%:
@:
add-submodule:
@ARGS="$(filter-out $@,$(MAKECMDGOALS))"; \
URL=$$(echo $$ARGS | awk '{print $$1}'); \
DIR=$$(echo $$ARGS | awk '{print $$2}'); \
if [ -z "$$URL" ]; then \
echo "Error: URL is required. Usage: make add-submodule <git-url> [directory-name]"; \
exit 1; \
fi; \
if [ -z "$$DIR" ]; then \
REPO_NAME=$$(echo $$URL | awk -F'/' '{print $$NF}' | sed 's/\.git$$//'); \
SUBMODULE_DIR=$$REPO_NAME; \
else \
SUBMODULE_DIR=$$DIR; \
fi; \
ADD_OUTPUT=$$(git submodule add $$URL $$SUBMODULE_DIR 2>&1); \
ADD_EXIT=$$?; \
if [ $$ADD_EXIT -ne 0 ]; then \
if echo "$$ADD_OUTPUT" | grep -q "A git directory for.*is found locally"; then \
echo "Warning: Submodule directory already exists. Using --force to reuse it."; \
git submodule add --force $$URL $$SUBMODULE_DIR 2>&1 || { \
echo "Error: Failed to add submodule with --force"; \
exit 1; \
}; \
else \
echo "$$ADD_OUTPUT"; \
exit $$ADD_EXIT; \
fi; \
fi; \
UPDATE_OUTPUT=$$(git submodule update --init --recursive $$SUBMODULE_DIR 2>&1); \
UPDATE_EXIT=$$?; \
if [ $$UPDATE_EXIT -ne 0 ]; then \
if echo "$$UPDATE_OUTPUT" | grep -q "empty repository\|yet to be born"; then \
echo "Warning: Empty repository detected. Submodule added but not checked out."; \
else \
echo "$$UPDATE_OUTPUT"; \
exit $$UPDATE_EXIT; \
fi; \
fi