-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit_gh.sh
More file actions
executable file
·172 lines (150 loc) · 4.77 KB
/
Copy pathcommit_gh.sh
File metadata and controls
executable file
·172 lines (150 loc) · 4.77 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
set -euo pipefail
# shellcheck disable=SC2034
VERSION="1.0.4"
QUIET=0
GENERATE_TREE=0
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--help|-h)
cat <<EOF
Usage: commit_gh [--quiet] [--tree true|false]
Automates common Git commit and push operations with smart handling:
• Detects and handles rebase/merge conflicts
• Stashes local changes before rebasing
• Adds/commits/pushes only when needed
• Auto-generates commit messages with timestamp
• Integrates with Dependabot if enabled
• Regenerates FOLDER_TREE.md if folder_tree is installed
Options:
--quiet, -q Suppress most output (still shows important errors)
--tree [true] Generate folder tree (default: false). If omitted, defaults to true.
--help, -h Show this help and exit
Examples:
./commit_gh.sh
./commit_gh.sh --quiet
./commit_gh.sh --tree # implicit true
./commit_gh.sh --tree true
./commit_gh.sh --tree false
EOF
exit 0
;;
--quiet|-q)
QUIET=1
;;
--tree)
# If the next argument is not another flag and exists, check its value
if [[ "${2:-}" =~ ^(true|false)$ ]]; then
[[ "$2" == "true" ]] && GENERATE_TREE=1
shift
else
# No value given → default to true
GENERATE_TREE=1
fi
;;
esac
shift
done
msg() { [[ $QUIET -eq 0 ]] && echo "$*"; }
always_msg() { echo "$*"; }
cd "$(git rev-parse --show-toplevel)" || exit 1
# --- Detect in-progress rebase or merge and exit if found ---
if [ -d ".git/rebase-merge" ] || [ -d ".git/rebase-apply" ]; then
echo -e "❌ \033[1;31mGit rebase is in progress.\033[0m"
echo " Please resolve conflicts and run 'git rebase --continue' before using this script."
exit 1
fi
if [ -f ".git/MERGE_HEAD" ]; then
echo -e "❌ \033[1;31mGit merge is in progress.\033[0m"
echo " Please resolve conflicts and run 'git merge --continue' (or abort) before using this script."
exit 1
fi
DD=$(date +'%d')
MM=$(date +'%m')
YYYY=$(date +'%Y')
COMMIT_MESSAGE="$DD/$MM/$YYYY - Updated configuration and fixed bugs"
# Ensure ssh-agent is running
if [ -z "${SSH_AUTH_SOCK:-}" ]; then
eval "$(ssh-agent -s)" >/dev/null
ssh-add -A >/dev/null 2>&1 || true
fi
# Remove tracked FOLDER_TREE.md if ignored
if git ls-files --error-unmatch FOLDER_TREE.md &>/dev/null; then
if grep -qF "FOLDER_TREE.md" .gitignore; then
msg "🧹 Removing FOLDER_TREE.md from Git tracking..."
git rm --cached FOLDER_TREE.md >/dev/null
fi
fi
git add . >/dev/null
# --- Commit staged changes if any ---
DID_COMMIT=0
if ! git diff --cached --quiet; then
msg "📦 Committing staged changes before pull/rebase..."
git commit -m "$COMMIT_MESSAGE" >/dev/null
DID_COMMIT=1
fi
# --- Stash unstaged changes if any, then rebase ---
if ! git diff --quiet; then
msg "💾 Stashing unstaged local changes before rebase..."
git stash -u >/dev/null
if ! git pull --rebase origin main >/dev/null 2>&1; then
echo "❌ Pull/rebase failed! Please resolve manually."
exit 1
fi
git stash pop >/dev/null 2>&1 || true
else
git pull --rebase origin main >/dev/null 2>&1
fi
git add . >/dev/null
if ! git diff --cached --quiet; then
msg "📦 Committing new staged changes..."
git commit -m "$COMMIT_MESSAGE" >/dev/null
DID_COMMIT=1
fi
# --- Smart Push Logic ---
DID_PUSH=0
try_push() {
local max_attempts=2
local attempt=1
while [[ $attempt -le $max_attempts ]]; do
# Only push if there are commits ahead
if [[ $(git log origin/main..HEAD --oneline | wc -l) -gt 0 ]]; then
if git push origin main >/dev/null 2>&1; then
DID_PUSH=1
msg "🚀 Successfully pushed to origin/main."
return 0
else
msg "⚠️ Push failed (maybe due to remote updates). Trying pull --rebase and re-push..."
if ! git pull --rebase origin main >/dev/null 2>&1; then
echo "❌ Pull/rebase failed! Please resolve manually."
exit 1
fi
((attempt++))
fi
else
# Nothing to push
return 0
fi
done
echo "❌ Push failed after rebase. Please resolve conflicts manually."
exit 1
}
try_push
# --- Output up-to-date message if nothing to commit or push ---
if [[ $DID_COMMIT -eq 0 && $DID_PUSH -eq 0 ]]; then
branch=$(git rev-parse --abbrev-ref HEAD)
always_msg "✅ Current branch $branch is up to date."
always_msg "🟢 No changes to commit."
fi
if [[ -f .github/dependabot.yml ]]; then
msg "🔐 Dependabot is enabled. Base image CVEs will be monitored automatically by GitHub."
fi
if [[ $GENERATE_TREE -eq 1 ]]; then
if command -v folder_tree &>/dev/null; then
msg "🌳 Generating updated folder tree..."
folder_tree --preset terraform,github --output markdown >/dev/null
else
msg "⚠️ 'folder_tree' command not found — skipping tree update."
fi
fi