<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
 <xsl:param name="DARCS" select="'/usr/local/bin/darcs'"/>
 <xsl:param name="GIT" select="'/usr/local/bin/git'"/>
 <xsl:param name="TMPDIR" select="'/tmp/darcs2git/'"/>

 <xsl:output method="text"/>

 <xsl:template match="/patches|/changelog">
  <xsl:text>DARCS="</xsl:text>
  <xsl:value-of select="$DARCS"/>
  <xsl:text>"
</xsl:text>
  <xsl:text>GIT="</xsl:text>
  <xsl:value-of select="$GIT"/>
  <xsl:text>"
</xsl:text>
  <xsl:text>TMPDIR="</xsl:text>
  <xsl:value-of select="$TMPDIR"/>
  <xsl:text>"
ulimit -c 0
</xsl:text>
  <xsl:apply-templates select="patch" />
  <xsl:text>
rm -rf $TMPDIR
</xsl:text>
 </xsl:template>

 <xsl:template match="patch">
  <xsl:text>$DARCS pull --all --quiet --matches="hash </xsl:text>
  <xsl:value-of select="@hash"/>
  <xsl:text>" --posthook=': &gt; '"${TMPDIR}darcs_files.txt"' ; for fnl in "$DARCS_FILES" ; do echo -n "$fnl" &gt;&gt; '"${TMPDIR}darcs_files.txt"' ; done' || exit 1
sed -E -e 's%^\./%%' ${TMPDIR}darcs_files.txt | \
sed -E -e $'s%(\\$|`|"|\\\\)%\\\\\\1%g' \
    -e $'s%\'%\"\'\"%g' | \
awk '{ printf " \"%s\"", $0 }' \
&gt; ${TMPDIR}darcs_files.args
{ echo -n "$GIT add --all" ; cat ${TMPDIR}darcs_files.args ; } &gt; ${TMPDIR}git-add.sh
sh -x ${TMPDIR}git-add.sh || exit 1
echo -n "$GIT commit --allow-empty -m " &gt; ${TMPDIR}git_commit.cmd
cat &gt;&gt; ${TMPDIR}git_commit.cmd &lt;&lt; '-EOF-'
!:&amp;#_</xsl:text>
  <xsl:value-of select="name"/>
  <xsl:text>!:&amp;#_ -m !:&amp;#_</xsl:text>
  <xsl:value-of select="comment"/>
  <xsl:text>!:&amp;#_ --author=!:&amp;#_</xsl:text>
  <xsl:choose>
   <xsl:when test="normalize-space(@author) = ''">
    <xsl:text>darcs2git &lt;&gt;</xsl:text>
   </xsl:when>
   <xsl:when test="not(contains(@author,'&lt;'))">
    <xsl:choose>
     <xsl:when test="contains(@author,'@') and not(contains(substring-after(@author,'@'),'@'))">
      <xsl:value-of select="normalize-space(substring-before(@author,'@'))"/>
      <xsl:text> &lt;</xsl:text>
      <xsl:value-of select="normalize-space(@author)"/>
      <xsl:text>&gt;</xsl:text>
     </xsl:when>
     <xsl:otherwise>
      <xsl:value-of select="@author"/>
      <xsl:text> &lt;&gt;</xsl:text>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:when>
   <xsl:when test="not(normalize-space(substring-before(@author,'&lt;')))">
    <xsl:text>darcs2git </xsl:text>
    <xsl:value-of select="@author"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="@author"/>
   </xsl:otherwise>
  </xsl:choose>
  <xsl:text>!:&amp;#_ --date=!:&amp;#_</xsl:text>
  <xsl:value-of select="@local_date"/>
  <xsl:text>!:&amp;#_
-EOF-
sed -E -e $'s%(\\$|`|"|\\\\)%\\\\\\1%g' \
    -e 's%!:&amp;#_%"%g' ${TMPDIR}git_commit.cmd \
    &gt; ${TMPDIR}git-commit.sh
truncate -s -1 ${TMPDIR}git-commit.sh
cat ${TMPDIR}darcs_files.args &gt;&gt; ${TMPDIR}git-commit.sh
sh -x ${TMPDIR}git-commit.sh || exit 1
</xsl:text>
 </xsl:template>

</xsl:stylesheet>
