throw new Exception("Can't parse an empty diff!");
}
+ // Detect `git-format-patch`, by looking for a "---" line somewhere in
+ // the file and then a footer with Git version number, which looks like
+ // this:
+ //
+ // --
+ // 1.8.4.2
+ //
+ // Note that `git-format-patch` adds a space after the "--", but we don't
+ // require it when detecting patches, as trailing whitespace can easily be
+ // lost in transit.
+ $detect_patch = '/^---$.*^-- ?[\s\d.]+\z/ms';
+ $message = null;
+ if (preg_match($detect_patch, $diff)) {
+ list($message, $diff) = $this->stripGitFormatPatch($diff);
+ }
+
$this->didStartParse($diff);
// Strip off header comments. While `patch` allows comments anywhere in the
$line = $this->nextLine();
}
+ if (strlen($message)) {
+ // If we found a message during pre-parse steps, add it to the resulting
+ // changes here.
+ $change = $this->buildChange(null)
+ ->setType(ArcanistDiffChangeType::TYPE_MESSAGE)
+ ->setMetadata('message', $message);
+ }
+
do {
$patterns = array(
// This is a normal SVN text change, probably from "svn diff".
return array($old, $new);
}
+
+ /**
+ * Strip the header and footer off a `git-format-patch` diff.
+ *
+ * Returns a parseable normal diff and a textual commit message.
+ */
+ private function stripGitFormatPatch($diff) {
+
+ // We can parse this by splitting it into two pieces over and over again
+ // along different section dividers:
+ //
+ // 1. Mail headers.
+ // 2. ("\n\n")
+ // 3. Mail body.
+ // 4. ("---")
+ // 5. Diff stat section.
+ // 6. ("\n\n")
+ // 7. Actual diff body.
+ // 8. ("--")
+ // 9. Patch footer.
+
+ list($head, $tail) = preg_split("/^---$/m", $diff, 2);
+ list($mail_headers, $mail_body) = explode("\n\n", $head, 2);
+ list($body, $foot) = preg_split('/^-- ?$/m', $tail, 2);
+ list($stat, $diff) = explode("\n\n", $body, 2);
+
+ // Rebuild the commit message by putting the subject line back on top of it,
+ // if we can find one.
+ $matches = null;
+ $pattern = '/^Subject: (?:\[PATCH\] )?(.*)$/mi';
+ if (preg_match($pattern, $mail_headers, $matches)) {
+ $mail_body = $matches[1]."\n\n".$mail_body;
+ $mail_body = rtrim($mail_body);
+ }
+
+ return array($mail_body, $diff);
+ }
+
}
case 'svnlook-delete.svndiff':
$this->assertEqual(1, count($changes));
break;
+ case 'git-format-patch.gitdiff':
+ $this->assertEqual(2, count($changes));
+
+ $change = array_shift($changes);
+ $this->assertEqual(
+ ArcanistDiffChangeType::TYPE_MESSAGE,
+ $change->getType());
+ $this->assertEqual("WIP", $change->getMetadata('message'));
+
+ $change = array_shift($changes);
+ $this->assertEqual(
+ ArcanistDiffChangeType::TYPE_CHANGE,
+ $change->getType());
+ break;
default:
throw new Exception("No test block for diff file {$diff_file}.");
break;