[PATCH] guilt: improve patch header handling

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Greetings,

Below is a hacklet which made guilt more useful for my workflow.
Entirely disposable, just sharing.

	-Mike

Teach guilt to handle patch header Date Author AuthorDate etc lines,
to deal properly with git commits and tip-bot patches.  Previously,
a Date line in the patch header was emitted into the commit message
body, which seems to confuse git format-patch.

With this applied, git log emits..
   commit 40298016b331950394a4d6e12909506afc3cf54b
   Author: Mike Galbraith <efault@xxxxxx>
   Date:   Sun Feb 13 07:16:35 2011 +0100
..and gitk
   Author: Mike Galbraith <efault@xxxxxx>  2011-02-13 07:16:35
   Committer: Mike Galbraith <mgalbraith@xxxxxxx>  2011-02-14 05:09:05
..and git format-patch
   From 40298016b331950394a4d6e12909506afc3cf54b Mon Sep 17 00:00:00 2001
   From: Mike Galbraith <efault@xxxxxx>
   Date: Sun, 13 Feb 2011 07:16:35 +0100
   Subject: [PATCH] guilt: improve patch header handling

Whereas previously, git log emitted..
   commit 19e82183944cb645bb8f1f4c2fbcd3ee99698b05
   Author: Mike Galbraith <efault@xxxxxx>
   Date:   Mon Feb 14 05:24:10 2011 +0100  <== commit date

   guilt: improve patch header handling
   Date: Sun Feb 13 07:16:35 CET 2011  <== lands between subject and body
and gitk...
   Author: Mike Galbraith <efault@xxxxxx>  2011-02-14 05:24:10  <== authordate wrong
   Committer: Mike Galbraith <mgalbraith@xxxxxxx>  2011-02-14 05:24:10
and git format-patch
   From 19e82183944cb645bb8f1f4c2fbcd3ee99698b05 Mon Sep 17 00:00:00 2001
   From: Mike Galbraith <efault@xxxxxx>
   Date: Mon, 14 Feb 2011 05:24:10 +0100
   Subject: [PATCH] guilt: improve patch header handling
    Date: Sun Feb 13 07:16:35 CET 2011  <== lands in subject when imported into evolution

Signed-off-by: Mike Galbraith <efault@xxxxxx>
Cc: Josef 'Jeff' Sipek <jeffpc@xxxxxxxxxxxxxx>
---
 guilt |   54 +++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/guilt b/guilt
index ec4fb04..875fc06 100755
--- a/guilt
+++ b/guilt
@@ -342,17 +342,24 @@ BEGIN{}
 # usage: do_get_header patchfile
 do_get_header()
 {
-	# The complexity arises from the fact that we want to ignore the
-	# From line and the empty line after it if it exists
-
-	# 2nd line skips the From line
-	# 3rd line skips the empty line right after a From line
-	# 4th line terminates execution when we encounter the diff
+	# The complexity arises from the fact that we want to ignore all
+	# but the Subject line of the header, and any empty lines after it,
+	# if these exist, and inject only the Subject line as the first
+	# line of the commit message.
+
+	# 1st line prints first encountered Subject line plus empty line.
+	# 2nd line skips standard email/git patch header lines.
+	# 3rd line skips tip's additional header lines.
+	# 4th line skips any empty lines thereafter.
+	# 5th line turns off empty line skip upon seeing a non-empty line.
+	# 6th line terminates execution when we encounter the diff
 	cat "$1" | awk '
-BEGIN{skip=0}
-/^Subject:/ && (NR==1){print substr($0, 10); next}
-/^From:/{skip=1; next}
-/^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
+BEGIN{body=0; subj=0}
+/^Subject:/ && (body == 0 && subj == 0){subj=1; print substr($0, 10) "\n"; next}
+/^(Subject:|From:|Author:|Date:|commit)/ && (body == 0){next}
+/^(Commit-ID:|Gitweb:|AuthorDate:|Committer:CommitDate:)/ && (body == 0){next}
+/^[ \t\f\n\r\v]*$/ && (body==0){next}
+/^.*$/ && (body==0){body=1}
 /^(diff |---$|--- )/{exit}
 {print $0}
 END{}
@@ -526,13 +533,26 @@ commit()
 		# make a default commit message if patch doesn't contain one
 		[ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
 
-		# extract a From line from the patch header, and set
-		# GIT_AUTHOR_{NAME,EMAIL}
-		author_str=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
+		# extract author and date lines from the patch header, and set
+		# GIT_AUTHOR_{NAME,EMAIL,DATE}
+		# prefering Author/AuthorDate lines if available.
+		author_str=`sed -n -e '/^Author:/ { s/^Author: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
+		if [ -z "$author_str" ]; then
+			author_str=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
+		fi
+
 		if [ ! -z "$author_str" ]; then
 			GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
 			export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
                         export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
+
+			author_date_str=`sed -n -e '/^AuthorDate:/ { s/^AuthorDate: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
+			if [ -z "$author_date_str" ]; then
+				author_date_str=`sed -n -e '/^Date:/ { s/^Date: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
+			fi
+			if [ ! -z "$author_date_str" ]; then
+				export GIT_AUTHOR_DATE=`echo $author_date_str`
+			fi
 		fi
 
 		ct=`git log -1 --pretty=%ct`
@@ -549,9 +569,13 @@ commit()
 		# confused, and makes up strange timestamps from the past
 		# (chances are it decides to interpret it as a unix
 		# timestamp).
-		export GIT_AUTHOR_DATE="`stat -c %y "$p" | sed -e '
+		export GIT_COMMITTER_DATE="`stat -c %y "$p" | sed -e '
 s/^\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\) \([0-9]\{2\}\):\([0-9]\{2\}\):\([0-9]\{2\}\)\.[0-9]* \(.*\)$/\1-\2-\3 \4:\5:\6 \7/'`"
-		export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
+
+		# export GIT_AUTHOR_DATE only if a Date line was unavailable
+		if [ -z "$author_date_str" ]; then
+			export GIT_AUTHOR_DATE="$GIT_COMMITTER_DATE"
+		fi
 
 		# commit
 		treeish=`git write-tree`
-- 
1.7.4


--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]