-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-libft-submodule.sh
More file actions
executable file
·104 lines (100 loc) · 3.7 KB
/
fix-libft-submodule.sh
File metadata and controls
executable file
·104 lines (100 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env bash
# repair lib/libft submodule metadata (safe, makes backups)
# This script can be run from any directory; it will locate the repo root.
set -euo pipefail
# determine script dir and repository root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
GIT_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || true)"
if [ -n "$GIT_ROOT" ]; then
ROOT="$GIT_ROOT"
else
# fallback: assume the script is in <repo>/scripts/
ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
fi
SUBMODULE_REL="lib/libft"
SUBMODULE="$ROOT/$SUBMODULE_REL"
MODULE_PATH="$ROOT/.git/modules/libft"
BACKUP_SUFFIX=".bak_by_fixscript_$(date +%s)"
if [ ! -d "$SUBMODULE" ]; then
echo "Submodule directory '$SUBMODULE_REL' not found (resolved to '$SUBMODULE'). Run from repo root or ensure submodule exists."
exit 1
fi
echo "== Repairing submodule $SUBMODULE_REL (root: $ROOT) =="
# Show current .git inside submodule (file or dir)
if [ -e "$SUBMODULE/.git" ]; then
if [ -d "$SUBMODULE/.git" ]; then
echo "Detected $SUBMODULE/.git is a directory (contains git metadata)."
echo "Will move its contents into $MODULE_PATH and replace with gitdir file."
echo "Creating backup of $SUBMODULE -> ${SUBMODULE}${BACKUP_SUFFIX}"
mv "$SUBMODULE" "${SUBMODULE}${BACKUP_SUFFIX}"
mkdir -p "$SUBMODULE"
# restore working tree files except .git from backup
shopt -s dotglob
for f in "${SUBMODULE}${BACKUP_SUFFIX}"/*; do
name="$(basename "$f")"
if [ "$name" = ".git" ]; then
continue
fi
mv "$f" "$SUBMODULE/" || true
done
shopt -u dotglob
# create modules dir and move .git contents
mkdir -p "$(dirname "$MODULE_PATH")"
echo "Moving git metadata to $MODULE_PATH"
mkdir -p "$MODULE_PATH"
# prefer rsync if available
if command -v rsync >/dev/null 2>&1; then
rsync -a "${SUBMODULE}${BACKUP_SUFFIX}/.git/" "$MODULE_PATH/"
else
cp -a "${SUBMODULE}${BACKUP_SUFFIX}/.git/." "$MODULE_PATH/" || true
fi
rm -rf "${SUBMODULE}${BACKUP_SUFFIX}/.git"
printf "gitdir: ../.git/modules/libft\n" > "$SUBMODULE/.git"
echo "Repair complete. Backup of original submodule at ${SUBMODULE}${BACKUP_SUFFIX}"
exit 0
elif [ -f "$SUBMODULE/.git" ]; then
echo "Detected $SUBMODULE/.git is a file. Contents:"
cat "$SUBMODULE/.git"
# read target
target=$(sed -n 's/^gitdir: *//p' "$SUBMODULE/.git" || true)
if [ -z "$target" ]; then
echo "Unknown .git file format. Manual fix required."
exit 2
fi
# make target absolute if relative
case "$target" in
/*) target_abs="$target" ;;
*) target_abs="$SUBMODULE/$target" ;;
esac
if [ -d "$target_abs" ]; then
echo "Target directory $target_abs exists. Submodule metadata seems OK."
exit 0
else
echo "Target $target_abs does not exist. Attempting to recover from embedded metadata..."
# try to find any stray metadata under submodule backup locations
if [ -d "${SUBMODULE}/.git/" ]; then
echo "Found ${SUBMODULE}/.git/ directory unexpectedly. Moving to $MODULE_PATH"
mkdir -p "$(dirname "$MODULE_PATH")"
mkdir -p "$MODULE_PATH"
if command -v rsync >/dev/null 2>&1; then
rsync -a "${SUBMODULE}/.git/" "$MODULE_PATH/"
else
cp -a "${SUBMODULE}/.git/." "$MODULE_PATH/" || true
fi
printf "gitdir: ../.git/modules/libft\n" > "$SUBMODULE/.git"
echo "Repaired by copying metadata into $MODULE_PATH"
exit 0
else
echo "No embedded metadata found. Suggested manual steps:"
echo " git submodule deinit -f -- $SUBMODULE_REL"
echo " rm -rf $ROOT/.git/modules/libft"
echo " git submodule update --init --recursive $SUBMODULE_REL"
exit 2
fi
fi
fi
else
echo "No $SUBMODULE/.git found. Likely not a valid submodule layout."
echo "Try: (from repo root) git submodule update --init --recursive $SUBMODULE_REL"
exit 1
fi