-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit-commit.sh
More file actions
executable file
·247 lines (216 loc) · 6.67 KB
/
Copy pathedit-commit.sh
File metadata and controls
executable file
·247 lines (216 loc) · 6.67 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env bash
# ============================================================
# edit-commit.sh — edit a commit's metadata / file list
#
# HEAD fast path (target == HEAD):
# - working tree may be dirty
# - add/modify/remove files freely, zero downstream conflict risk
# - direct git commit --amend, no rebase
#
# Old-commit path (target != HEAD):
# - working tree must be clean
# - good for: change message, add new file (untracked), remove file
# - NOT for: modifying existing file contents → use the fixup menu
# Reason: this script requires a clean tree, so your edits have
# nowhere to land; staging "+:tracked-file" only stages the
# "target commit's version of it", a no-op. Use fixup to apply
# your delta to the old commit.
# ============================================================
set -euo pipefail
SHA="${1:?missing SHA}"
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$DIR/lib.sh"
tmpdir="" # lib's EXIT trap cleans this
# ============ Shared: prompt for file +/- list and execute ============
do_file_ops() {
echo
echo "$MSG_EDIT_COMMIT_FILE_OPS_HEADER"
echo "$MSG_EDIT_COMMIT_FILE_OPS_ADD"
echo "$MSG_EDIT_COMMIT_FILE_OPS_REMOVE"
echo "$MSG_EDIT_COMMIT_FILE_OPS_DONE"
echo
while IFS= read -er line; do
[[ "$line" == "Q" ]] && break
[[ -z "$line" ]] && continue
if [[ ! "$line" =~ ^([-+]):(.+)$ ]]; then
printf "$MSG_EDIT_COMMIT_FILE_FMT_ERR_FMT" "$line"
continue
fi
local op="${BASH_REMATCH[1]}"
local path="${BASH_REMATCH[2]}"
case "$op" in
+)
if [[ ! -e "$path" ]]; then
printf "$MSG_EDIT_COMMIT_FILE_NOT_EXIST_FMT" "$path"
elif git add -- "$path" 2>/dev/null; then
printf "$MSG_EDIT_COMMIT_FILE_ADD_OK_FMT" "$path"
else
printf "$MSG_EDIT_COMMIT_FILE_ADD_FAIL_FMT" "$path"
fi
;;
-)
if git rm --cached -- "$path" >/dev/null 2>&1; then
printf "$MSG_EDIT_COMMIT_FILE_RM_OK_FMT" "$path"
else
printf "$MSG_EDIT_COMMIT_FILE_RM_FAIL_FMT" "$path"
fi
;;
esac
done
}
# ============ Shared: ask for new message ============
ask_new_message() {
echo "$MSG_EDIT_COMMIT_ASK_MSG" >&2
local msg="" line
while IFS= read -er line; do
[[ "$line" == "Q" ]] && break
msg+="${line}"$'\n'
done
printf '%s' "$msg"
}
show_intro "$MSG_EDIT_COMMIT_TITLE" \
"$MSG_EDIT_COMMIT_HEAD_PATH" \
"$MSG_EDIT_COMMIT_OLD_PATH" \
"$MSG_EDIT_COMMIT_NOT_SUITED"
print_header "$SHA"
require_clean_state
enable_failure_rollback
HEAD_SHA="$(git rev-parse HEAD)"
TARGET_SHA="$(git rev-parse "$SHA")"
# ============================================================
# HEAD fast path
# ============================================================
if [[ "$HEAD_SHA" == "$TARGET_SHA" ]]; then
echo "$MSG_EDIT_COMMIT_HEAD_HEADER"
echo "$MSG_EDIT_COMMIT_HEAD_NOTE_TARGET"
echo "$MSG_EDIT_COMMIT_HEAD_NOTE_DIRTY"
echo "$MSG_EDIT_COMMIT_HEAD_NOTE_CHANGES"
echo
echo "$MSG_EDIT_COMMIT_HEAD_CUR_MSG"
git --no-pager log -1 --format='%B' | sed 's/^/ /'
echo
if ! git diff --quiet || ! git diff --cached --quiet; then
echo "$MSG_EDIT_COMMIT_HEAD_CUR_CHANGES"
git --no-pager status --short | sed 's/^/ /'
echo
fi
new_msg=""
read -erp "$MSG_EDIT_COMMIT_HEAD_ASK_MSG" ans
if [[ "$ans" =~ ^[yY] ]]; then
new_msg="$(ask_new_message)"
fi
read -erp "$MSG_EDIT_COMMIT_HEAD_ASK_FILES" ans
if [[ "$ans" =~ ^[yY] ]]; then
do_file_ops
fi
have_msg=0
have_staged=0
[[ -n "$new_msg" ]] && have_msg=1
git diff --cached --quiet || have_staged=1
if (( ! have_msg && ! have_staged )); then
echo
echo "$MSG_EDIT_COMMIT_NO_CHANGES"
exit_ok
fi
if ! git diff --quiet; then
echo
echo "$MSG_EDIT_COMMIT_UNSTAGED_HINT"
fi
tmpdir="$(mktemp -d)"
echo
if (( have_msg )); then
msg_file="$tmpdir/msg"
printf '%s' "$new_msg" > "$msg_file"
git commit --amend -F "$msg_file" >/dev/null
if (( have_staged )); then
echo "$MSG_EDIT_COMMIT_AMEND_MSG_FILES"
else
echo "$MSG_EDIT_COMMIT_AMEND_MSG"
fi
else
git commit --amend --no-edit >/dev/null
echo "$MSG_EDIT_COMMIT_AMEND_FILES"
fi
git --no-pager log -1 --oneline
exit 0
fi
# ============================================================
# Old-commit path (rebase)
# ============================================================
if ! git diff --quiet || ! git diff --cached --quiet; then
echo "$MSG_EDIT_COMMIT_OLD_DIRTY_TREE_BLOCK" >&2
exit 1
fi
if ! git merge-base --is-ancestor "$SHA" HEAD; then
printf "$MSG_EDIT_COMMIT_OLD_NOT_ANCESTOR_FMT" "$SHA" >&2
exit 1
fi
if ! git rev-parse --verify --quiet "${SHA}^" >/dev/null 2>&1; then
printf "$MSG_COMMON_ROOT_COMMIT_FMT" "$SHA" >&2
echo "$MSG_COMMON_ROOT_HINT" >&2
exit 1
fi
echo "$MSG_EDIT_COMMIT_OLD_HEADER"
echo "$MSG_EDIT_COMMIT_OLD_NOTE_APPLIES"
echo "$MSG_EDIT_COMMIT_OLD_NOTE_NOT_APPLIES"
echo
confirm "$MSG_EDIT_COMMIT_OLD_CONTINUE"
tmpdir="$(mktemp -d)"
cat > "$tmpdir/seq" <<EOF
#!/bin/sh
sed -i.bak '1s/^pick /edit /' "\$1" && rm -f "\$1.bak"
EOF
chmod +x "$tmpdir/seq"
export GIT_SEQUENCE_EDITOR="$tmpdir/seq"
# `git rebase -i` returning non-zero here is the EXPECTED path: our
# sequence editor changed 'pick' → 'edit', so rebase stops on that
# commit. But the same non-zero could also mean GIT_SEQUENCE_EDITOR
# itself failed (permissions, missing tmpdir, …). Distinguish the two
# by inspecting whether rebase-merge/rebase-apply was created — only
# the success-stopped-at-edit path leaves those directories behind.
if ! git rebase -i "${SHA}^"; then
gitdir="$(git rev-parse --git-dir)"
if [ ! -d "$gitdir/rebase-merge" ] && [ ! -d "$gitdir/rebase-apply" ]; then
echo "$MSG_EDIT_COMMIT_OLD_REBASE_NOT_EDIT" >&2
exit 1
fi
fi
echo
echo "$MSG_EDIT_COMMIT_OLD_CUR_MSG"
git --no-pager log -1 --format='%B' | sed 's/^/ /'
echo
new_msg=""
read -erp "$MSG_EDIT_COMMIT_OLD_ASK_MSG" ans
if [[ "$ans" =~ ^[yY] ]]; then
new_msg="$(ask_new_message)"
fi
read -erp "$MSG_EDIT_COMMIT_OLD_ASK_FILES" ans
if [[ "$ans" =~ ^[yY] ]]; then
do_file_ops
fi
have_msg=0
have_staged=0
[[ -n "$new_msg" ]] && have_msg=1
git diff --cached --quiet || have_staged=1
echo
if (( have_msg )); then
msg_file="$tmpdir/msg"
printf '%s' "$new_msg" > "$msg_file"
git commit --amend -F "$msg_file" >/dev/null
if (( have_staged )); then
echo "$MSG_EDIT_COMMIT_AMEND_MSG_FILES"
else
echo "$MSG_EDIT_COMMIT_AMEND_MSG"
fi
elif (( have_staged )); then
git commit --amend --no-edit >/dev/null
echo "$MSG_EDIT_COMMIT_AMEND_FILES"
else
echo "$MSG_EDIT_COMMIT_OLD_NO_CHANGES"
fi
if ! git rebase --continue; then
echo >&2
echo "$MSG_EDIT_COMMIT_OLD_CONTINUE_FAIL" >&2
exit 1
fi
echo "$MSG_EDIT_COMMIT_OLD_REBASE_DONE"