-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommit.sh
More file actions
executable file
·132 lines (108 loc) · 4.74 KB
/
commit.sh
File metadata and controls
executable file
·132 lines (108 loc) · 4.74 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
# Script to commit changes to all submodules recursively and the main repo
# Usage: ./commit.sh "commit message"
#
# Features:
# - Commits to ALL submodules recursively
# - Commits to main repo
# - Runs "git add ." before committing
# - Includes timestamp in commit message
# - Allows empty commits for synchronization
set -e # Exit on error
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Get commit message from parameter or use default with timestamp
if [ -z "$1" ]; then
TIMESTAMP=$(date "+%Y-%m-%d-%H-%M-%S")
COMMIT_MSG="sync at $TIMESTAMP"
else
COMMIT_MSG="$1"
fi
# Use commit message directly
FULL_MSG="$COMMIT_MSG"
echo -e "${GREEN}╔═══════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ Committing to all submodules recursively + main repo ║${NC}"
echo -e "${GREEN}╚═══════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e "${GREEN}Message: ${NC}$FULL_MSG"
echo ""
# Get the root directory
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# First, commit all submodules recursively
echo -e "${GREEN}=== Step 1: Committing submodules recursively ===${NC}"
echo ""
# Use git submodule foreach to process all submodules recursively
git submodule foreach --recursive "
printf '\033[1;33mProcessing submodule:\033[0m %s at %s\n' \"\$name\" \"\$sm_path\"
# Add all changes including nested submodule references
# Use --all to capture nested submodule pointer updates
git add --all
# Commit with message (allow empty commits)
if git commit --allow-empty -m '$FULL_MSG'; then
printf '\033[0;32m✓ Committed:\033[0m %s\n' \"\$name\"
else
printf '\033[0;31m✗ Commit failed:\033[0m %s\n' \"\$name\"
fi
printf '\n'
"
# Second pass: Update parent submodule references to nested submodules
echo -e "${GREEN}=== Step 2: Updating parent submodule references ===${NC}"
echo ""
# This ensures parent submodules commit the updated nested submodule pointers
git submodule foreach --recursive "
# Check if there are any submodule changes to commit
if ! git diff-index --quiet HEAD -- 2>/dev/null || git status --porcelain | grep -q '^M'; then
printf '\033[1;33mUpdating references in:\033[0m %s\n' \"\$name\"
git add --all
if git commit --allow-empty -m '$FULL_MSG (submodule ref update)'; then
printf '\033[0;32m✓ Updated:\033[0m %s\n' \"\$name\"
fi
printf '\n'
fi
"
# Then commit the main repo
echo -e "${GREEN}=== Step 3: Committing main repository ===${NC}"
echo ""
cd "$ROOT_DIR"
echo -e "${YELLOW}Processing:${NC} Main repository"
# Add all changes including submodule pointer updates
# Use --all to ensure everything is staged, including submodule reference updates
git add --all
# Commit with message (allow empty commits)
if git commit --allow-empty -m "$FULL_MSG"; then
echo -e "${GREEN}✓ Committed:${NC} Main repository"
else
echo -e "${RED}✗ Commit failed:${NC} Main repository"
fi
echo ""
# Verify clean state
echo -e "${GREEN}=== Step 4: Verifying clean state ===${NC}"
echo ""
# Check if there are any uncommitted changes left
if ! git diff-index --quiet HEAD -- 2>/dev/null; then
echo -e "${YELLOW}⚠ Warning: There are still uncommitted changes in the main repo${NC}"
echo -e "${YELLOW}This might be expected if you made changes after starting commit.sh${NC}"
echo ""
echo -e "${YELLOW}Run 'git status' to see what's uncommitted${NC}"
echo ""
fi
# Check submodules for uncommitted changes
SUBMODULE_STATUS=$(git submodule foreach --recursive --quiet "
if ! git diff-index --quiet HEAD -- 2>/dev/null; then
printf '\033[1;33m⚠ Uncommitted changes in submodule:\033[0m %s\n' \"\$name\"
echo 'HAS_CHANGES'
fi
")
if echo "$SUBMODULE_STATUS" | grep -q "HAS_CHANGES"; then
echo ""
echo -e "${YELLOW}Run 'git status' in each submodule to see details${NC}"
fi
echo ""
echo -e "${GREEN}╔═══════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ All commits complete! ║${NC}"
echo -e "${GREEN}╚═══════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e "${YELLOW}Next step:${NC} Run ./push.sh to push all changes"