Skip to content

Commit 2e6e3e8

Browse files
torvaldsgitster
authored andcommitted
git-am: cope better with an empty Subject: line
When the Subject: line is empty for whatever reason, git-am was fooled by it and left an empty line at the beginning of the resulting commit log message. This moves the logic around so that we do not keep $SUBJECT in a separate variable. Instead, $dotest/msg-clean, which used to be the log message body extracted from the message and then trailing whitespaces cleansed out, now contains the subject line followed by a blank line at the beginning for normal messages, and we use the first line from the file as the summary line throughout the program. Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6a28518 commit 2e6e3e8

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

git-am.sh

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ It does not apply to blobs recorded in its index."
107107
# patch did not touch, so recursive ends up canceling them,
108108
# saying that we reverted all those changes.
109109

110-
eval GITHEAD_$his_tree='"$SUBJECT"'
110+
eval GITHEAD_$his_tree='"$FIRSTLINE"'
111111
export GITHEAD_$his_tree
112112
git-merge-recursive $orig_tree -- HEAD $his_tree || {
113113
git rerere
@@ -117,10 +117,6 @@ It does not apply to blobs recorded in its index."
117117
unset GITHEAD_$his_tree
118118
}
119119

120-
reread_subject () {
121-
git stripspace <"$1" | sed -e 1q
122-
}
123-
124120
prec=4
125121
dotest=.dotest sign= utf8=t keep= skip= interactive= resolved= binary=
126122
resolvemsg= resume=
@@ -303,7 +299,11 @@ do
303299
echo "Patch is empty. Was it split wrong?"
304300
stop_here $this
305301
}
306-
git stripspace < "$dotest/msg" > "$dotest/msg-clean"
302+
SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
303+
case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
304+
305+
(echo "$SUBJECT" ; echo ; cat "$dotest/msg") |
306+
git stripspace > "$dotest/msg-clean"
307307
;;
308308
esac
309309

@@ -319,9 +319,6 @@ do
319319

320320
export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
321321

322-
SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
323-
case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
324-
325322
case "$resume" in
326323
'')
327324
if test '' != "$SIGNOFF"
@@ -340,10 +337,8 @@ do
340337
ADD_SIGNOFF=
341338
fi
342339
{
343-
printf '%s\n' "$SUBJECT"
344340
if test -s "$dotest/msg-clean"
345341
then
346-
echo
347342
cat "$dotest/msg-clean"
348343
fi
349344
if test '' != "$ADD_SIGNOFF"
@@ -360,6 +355,7 @@ do
360355
;;
361356
esac
362357
esac
358+
FIRSTLINE=$(head -1 "$dotest/final-commit")
363359

364360
resume=
365361
if test "$interactive" = t
@@ -380,7 +376,7 @@ do
380376
[aA]*) action=yes interactive= ;;
381377
[nN]*) action=skip ;;
382378
[eE]*) git_editor "$dotest/final-commit"
383-
SUBJECT=$(reread_subject "$dotest/final-commit")
379+
FIRSTLINE=$(head -1 "$dotest/final-commit")
384380
action=again ;;
385381
[vV]*) action=again
386382
LESS=-S ${PAGER:-less} "$dotest/patch" ;;
@@ -403,7 +399,7 @@ do
403399
stop_here $this
404400
fi
405401

406-
printf 'Applying %s\n' "$SUBJECT"
402+
printf 'Applying %s\n' "$FIRSTLINE"
407403

408404
case "$resolved" in
409405
'')
@@ -461,7 +457,7 @@ do
461457
tree=$(git write-tree) &&
462458
parent=$(git rev-parse --verify HEAD) &&
463459
commit=$(git commit-tree $tree -p $parent <"$dotest/final-commit") &&
464-
git update-ref -m "$GIT_REFLOG_ACTION: $SUBJECT" HEAD $commit $parent ||
460+
git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
465461
stop_here $this
466462

467463
if test -x "$GIT_DIR"/hooks/post-applypatch

0 commit comments

Comments
 (0)