Patches created using gitk's "write commit to file" functionality (which
uses 'git diff-tree -p --pretty' under the hood) need some massaging in
order to apply cleanly. This consists of dropping the 'commit' line
automatically determining the subject and removing leading whitespace.

Signed-off-by: Chris Packham <judge.pack...@gmail.com>
---
 Documentation/git-am.txt |  3 ++-
 git-am.sh                | 36 ++++++++++++++++++++++++++++++++++++
 t/t4150-am.sh            | 13 +++++++++++++
 3 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/Documentation/git-am.txt b/Documentation/git-am.txt
index 9adce37..b59d2b3 100644
--- a/Documentation/git-am.txt
+++ b/Documentation/git-am.txt
@@ -101,7 +101,8 @@ default.   You can use `--no-utf8` to override this.
        By default the command will try to detect the patch format
        automatically. This option allows the user to bypass the automatic
        detection and specify the patch format that the patch(es) should be
-       interpreted as. Valid formats are mbox, stgit, stgit-series and hg.
+       interpreted as. Valid formats are mbox, stgit, stgit-series, hg and
+       gitk.
 
 -i::
 --interactive::
diff --git a/git-am.sh b/git-am.sh
index ee61a77..f979925 100755
--- a/git-am.sh
+++ b/git-am.sh
@@ -227,6 +227,9 @@ check_patch_format () {
                "# HG changeset patch")
                        patch_format=hg
                        ;;
+               'commit '*)
+                       patch_format=gitk
+                       ;;
                *)
                        # if the second line is empty and the third is
                        # a From, Author or Date entry, this is very
@@ -357,6 +360,39 @@ split_patches () {
                this=
                msgnum=
                ;;
+       gitk)
+               # These patches are generates with 'git diff-tree -p --pretty'
+               # we discard the 'commit' line, after that the first line not
+               # starting with 'Author:' or 'Date:' is the subject. We also
+               # need to strip leading whitespace from the message body.
+               this=0
+               for gitk in "$@"
+               do
+                       this=$(expr "$this" + 1)
+                       msgnum=$(printf "%0${prec}d" $this)
+                       @@PERL@@ -ne 'BEGIN { $subject = 0; $diff = 0; }
+                               if (!$diff) { s/^    // ; }
+                               if ($subject > 1) { print ; }
+                               elsif (/^commit\s.*$/) { next ; }
+                               elsif (/^\s+$/) { next ; }
+                               elsif (/^Author:/) { s/Author/From/ ; print ; }
+                               elsif (/^Date:/) { print ;}
+                               elsif (/^diff --git/) { $diff = 1 ; print ;}
+                               elsif ($subject) {
+                                       $subject = 2 ;
+                                       print "\n" ;
+                                       print ;
+                               } else {
+                                       print "Subject: ", $_ ;
+                                       $subject = 1;
+                               }
+                       ' <"$gitk" >"$dotest/$msgnum" || clean_abort
+
+               done
+               echo "$this" >"$dotest/last"
+               this=
+               msgnum=
+               ;;
        *)
                if test -n "$patch_format"
                then
diff --git a/t/t4150-am.sh b/t/t4150-am.sh
index 5edb79a..e36cd0b 100755
--- a/t/t4150-am.sh
+++ b/t/t4150-am.sh
@@ -103,6 +103,7 @@ test_expect_success setup '
                echo "X-Fake-Field: Line Three" &&
                git format-patch --stdout first | sed -e "1d"
        } > patch1-ws.eml &&
+       git diff-tree -p --pretty second >patch1-gitk.eml &&
 
        sed -n -e "3,\$p" msg >file &&
        git add file &&
@@ -186,6 +187,18 @@ test_expect_success 'am applies patch e-mail with 
preceding whitespace' '
        test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
 '
 
+test_expect_success 'am applies patch generated by gitk' '
+       cat patch1-gitk.eml &&
+       rm -fr .git/rebase-apply &&
+       git reset --hard &&
+       git checkout first &&
+       git am patch1-gitk.eml &&
+       test_path_is_missing .git/rebase-apply &&
+       git diff --exit-code second &&
+       test "$(git rev-parse second)" = "$(git rev-parse HEAD)" &&
+       test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
+'
+
 test_expect_success 'setup: new author and committer' '
        GIT_AUTHOR_NAME="Another Thor" &&
        GIT_AUTHOR_EMAIL="a.t...@example.com" &&
-- 
2.0.4.2.gadd452d

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

Reply via email to