Meta/Reintegrate: work on a detached head

Junio C Hamano committed Mar 8, 2024 at 21:47 UTC 34cee3057e4c77fa71e370cd0d37c4d148a16bb3
1 file changed +40 -13
Reintegrate
+40 -13
@@ -164,7 +164,33 @@ no)
164 prev_cut=
165 }
166
167 + detach () {
168 + if original_branch=$(git symbolic-ref HEAD 2>/dev/null)
169 + then
170 + original_branch=${original_branch#refs/heads/}
171 + git checkout --quiet --detach
172 + into="--into $original_branch"
173 + else
174 + original_branch=
175 + into=
176 + fi
177 +
178 + }
179 +
180 + leave () {
181 + if test -n "$original_branch" && ! git symbolic-ref HEAD 2>/dev/null
182 + then
183 + git checkout --quiet -B "$original_branch"
184 + fi
185 + if test -n "$1"
186 + then
187 + exit "$1"
188 + fi
189 + }
190 +
191 + detach
192 cut_seen=0 prev_cut= count_since_last_cut=0 cocci_count=0
193 +
194 while read branch eh
195 do
196 case "$branch" in '###') cut_seen=$(( $cut_seen + 1 )) ;; esac
@@ -206,13 +232,13 @@ no)
232 cat contrib/coccinelle/*.patch >cocci.patch
233 if ! test -s cocci.patch
234 then
209 - exit 0
235 + leave 0
236 fi
211 - git apply --index -3 cocci.patch || exit
237 + git apply --index -3 cocci.patch || leave $?
238 rm cocci.patch
239 git diff --quiet HEAD && continue
240 fi
215 - git commit -m "$cocci_mark" || exit
241 + git commit -m "$cocci_mark" || leave $?
242
243 mark_cut
244 continue
@@ -228,7 +254,7 @@ no)
254 save=$(git rev-parse --verify HEAD) &&
255 tip=$(git rev-parse --verify "$branch^0") &&
256 mb=$(git merge-base "$tip" "$save") ||
231 - exit
257 + leave $?
258
259 test "$mb" = "$tip" && continue
260
@@ -237,13 +263,13 @@ no)
263
264 rebuild=$(git config "branch.$branch.rebuild" || :)
265
240 - GIT_EDITOR=: git merge --no-ff $rebuild $accept_rerere --edit "$branch" ||
266 + GIT_EDITOR=: git merge --no-ff $into $rebuild $accept_rerere --edit "$branch" ||
267 accept_rerere ||
242 - exit
268 + leave $?
269
244 - annotate_merge "$branch" || exit
270 + annotate_merge "$branch" || leave $?
271 test -z "$edit" ||
246 - git commit --amend || exit
272 + git commit --amend || leave $?
273
274 this=$(git rev-parse --verify HEAD)
275 if test "$this" = "$save"
@@ -254,7 +280,7 @@ no)
280 echo >&2 "Fixing up the merge"
281 git cherry-pick --no-commit "refs/merge-fix/$branch" &&
282 git diff --stat HEAD &&
257 - GIT_EDITOR=: git commit --amend -a || exit
283 + GIT_EDITOR=: git commit --amend -a || leave $?
284 fi
285 ;;
286 pick" "*)
@@ -262,13 +288,14 @@ no)
288
289 mark_cut
290
265 - git cherry-pick "$branch" || exit ;;
266 - *) echo >&2 "Eh? $branch $eh"; exit ;;
291 + git cherry-pick "$branch" || leave $? ;;
292 + *) echo >&2 "Eh? $branch $eh"; leave $? ;;
293 esac
294
269 - eval "$exec" || exit
295 + eval "$exec" || leave $?
296 done
271 - exit
297 + leave $?
298 + ;;
299 esac
300
301 if test -n "$update" && test $# = 0