Skip to content

Commit 5b388c8

Browse files
Charles Baylisbarisione
authored andcommitted
Don't recommend reformatting merge commits
1 parent ec2b01d commit 5b388c8

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

git-pre-commit-format

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,37 @@ else
313313
cat "$patch"
314314
fi
315315

316+
# We don't want to suggest applying clang-format after merge resolution
317+
if git rev-parse MERGE_HEAD >/dev/null 2>&1; then
318+
readonly this_is_a_merge=true
319+
else
320+
readonly this_is_a_merge=false
321+
fi
322+
316323
echo
317324
echo "${b}The staged content is not formatted correctly.${n}"
318325
echo "The patch shown above can be applied automatically to fix the formatting."
319326
echo
320327

328+
if $this_is_a_merge; then
329+
echo "${b}You appear to be committing the result of a merge. It is not${n}"
330+
echo "${b}recommended to apply the patch if it will reformat any code you${n}"
331+
echo "${b}did not modify in your branch.${n}"
332+
echo
333+
readonly recommend_apply=" (not recommended for merge!)"
334+
readonly recommend_force=""
335+
readonly bold_apply=""
336+
readonly bold_force="${b}"
337+
else
338+
readonly recommend_apply=""
339+
readonly recommend_force=" (not recommended!)"
340+
readonly bold_apply="${b}"
341+
readonly bold_force=""
342+
fi
343+
321344
echo "You can:"
322-
echo " [a]: Apply the patch"
323-
echo " [f]: Force and commit anyway (not recommended!)"
345+
echo " ${bold_apply}[a]: Apply the patch${recommend_apply}${n}"
346+
echo " ${bold_force}[f]: Force and commit anyway${recommend_force}${n}"
324347
echo " [c]: Cancel the commit"
325348
echo " [?]: Show help"
326349
echo
@@ -344,15 +367,26 @@ while true; do
344367
$'Cannot apply patch to git staged changes.\n' \
345368
$'This may happen if you have some overlapping unstaged changes. To solve\n' \
346369
$'you need to stage or reset changes manually.'
370+
371+
if $this_is_a_merge; then
372+
echo
373+
echo "Applied the patch to reformat the merge commit."
374+
echo "You can always abort by quitting your editor with no commit message."
375+
echo
376+
echo -n "Press return to continue."
377+
read -r < "$tty"
378+
fi
347379
;;
348380

349381
[fF] )
350382
echo
351-
echo "Will commit anyway!"
352-
echo "You can always abort by quitting your editor with no commit message."
353-
echo
354-
echo -n "Press return to continue."
355-
read -r < "$tty"
383+
if ! $this_is_a_merge; then
384+
echo "Will commit anyway!"
385+
echo "You can always abort by quitting your editor with no commit message."
386+
echo
387+
echo -n "Press return to continue."
388+
read -r < "$tty"
389+
fi
356390
exit 0
357391
;;
358392

0 commit comments

Comments
 (0)