1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-02-16 00:38:38 +01:00

Add an option to inline diffs up to a certain size in emails

Summary:
We already generate patches, but currently attach them. Allow them to be inlined instead (optionally, up to a certain size).

Also allow selection between unified and git patches.

Test Plan: Set these options in my local config, sent out a diff.

Reviewers: btrahan, Makinde

Reviewed By: Makinde

CC: aran, epriestley

Maniphest Tasks: T874

Differential Revision: https://secure.phabricator.com/D1759
This commit is contained in:
epriestley 2012-03-03 11:05:19 -08:00
parent b5897ec9c0
commit 0962980fef
2 changed files with 59 additions and 26 deletions

View file

@ -231,10 +231,20 @@ return array(
'metamta.differential.subject-prefix' => '[Differential]',
// Set this to true if you want patches to be attached to mail from
// Differential. This won't work if you are using SendGrid as your mail
// Differential. This won't work if you are using SendGrid as your mail
// adapter.
'metamta.differential.attach-patches' => false,
// To include patches in email bodies, set this to a positive integer. Patches
// will be inlined if they are at most that many lines. For instance, a value
// of 100 means "inline patches if they are no longer than 100 lines". By
// default, patches are not inlined.
'metamta.differential.inline-patches' => 0,
// If you enable either of the options above, you can choose what format
// patches are sent in. Valid options are 'unified' (like diff -u) or 'git'.
'metamta.differential.patch-format' => 'unified',
// Prefix prepended to mail sent by Diffusion.
'metamta.diffusion.subject-prefix' => '[Diffusion]',

View file

@ -77,41 +77,64 @@ abstract class DifferentialReviewRequestMail extends DifferentialMail {
$body[] = null;
}
$inline_key = 'metamta.differential.inline-patches';
$inline_max_length = PhabricatorEnv::getEnvConfig($inline_key);
if ($inline_max_length) {
$patch = $this->buildPatch();
if (count(explode("\n", $patch)) <= $inline_max_length) {
$body[] = 'CHANGE DETAILS';
$body[] = $patch;
}
}
return implode("\n", $body);
}
protected function buildAttachments() {
$attachments = array();
if (PhabricatorEnv::getEnvConfig('metamta.differential.attach-patches')) {
$revision = $this->getRevision();
$revision_id = $revision->getID();
$diffs = $revision->loadDiffs();
$diff_number = count($diffs);
$diff = array_pop($diffs);
$filename = "D{$revision_id}.{$diff_number}.patch";
$diff->attachChangesets($diff->loadChangesets());
// TODO: We could batch this to improve performance.
foreach ($diff->getChangesets() as $changeset) {
$changeset->attachHunks($changeset->loadHunks());
}
$diff_dict = $diff->getDiffDict();
$changes = array();
foreach ($diff_dict['changes'] as $changedict) {
$changes[] = ArcanistDiffChange::newFromDictionary($changedict);
}
$bundle = ArcanistBundle::newFromChanges($changes);
$unified_diff = $bundle->toUnifiedDiff();
$attachments[] = new PhabricatorMetaMTAAttachment(
$unified_diff,
$filename,
$this->buildPatch(),
"D{$revision_id}.{$diff_number}.patch",
'text/x-patch; charset=utf-8'
);
}
return $attachments;
}
private function buildPatch() {
$revision = $this->getRevision();
$revision_id = $revision->getID();
$diffs = $revision->loadDiffs();
$diff_number = count($diffs);
$diff = array_pop($diffs);
$diff->attachChangesets($diff->loadChangesets());
// TODO: We could batch this to improve performance.
foreach ($diff->getChangesets() as $changeset) {
$changeset->attachHunks($changeset->loadHunks());
}
$diff_dict = $diff->getDiffDict();
$changes = array();
foreach ($diff_dict['changes'] as $changedict) {
$changes[] = ArcanistDiffChange::newFromDictionary($changedict);
}
$bundle = ArcanistBundle::newFromChanges($changes);
$format = PhabricatorEnv::getEnvConfig('metamta.differential.patch-format');
switch ($format) {
case 'git':
return $bundle->toGitPatch();
break;
case 'unified':
default:
return $bundle->toUnifiedDiff();
break;
}
}
}