Add unified diff to newchange mail template.

This change add a new macro to the NewChange mail template. The
$email.UnifiedDiff is expanded to a unified diff of the patch.

Change-Id: I9c72cc7129adb9aae4f8168e7bba9215aa6ced3c
This commit is contained in:
Gal Hammer
2011-07-13 14:50:45 +03:00
committed by Shawn O. Pearce
parent dedc2b2f30
commit 4a80e99da5
2 changed files with 55 additions and 0 deletions

View File

@@ -16,10 +16,22 @@ package com.google.gerrit.server.mail;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.server.patch.PatchList;
import com.google.gerrit.server.patch.PatchListNotAvailableException;
import com.google.gerrit.server.ssh.SshInfo;
import com.jcraft.jsch.HostKey;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.util.RawParseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@@ -28,6 +40,9 @@ import java.util.Set;
/** Sends an email alerting a user to a new change for them to review. */
public abstract class NewChangeSender extends ChangeEmail {
private static final Logger log =
LoggerFactory.getLogger(NewChangeSender.class);
private final SshInfo sshInfo;
private final Set<Account.Id> reviewers = new HashSet<Account.Id>();
private final Set<Account.Id> extraCC = new HashSet<Account.Id>();
@@ -85,4 +100,41 @@ public abstract class NewChangeSender extends ChangeEmail {
}
return host;
}
/** Show patch set as unified difference. */
public String getUnifiedDiff() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Repository repo = getRepository();
if (repo != null) {
DiffFormatter df = new DiffFormatter(out);
try {
PatchList patchList = getPatchList();
if (patchList.getOldId() != null) {
df.setRepository(repo);
df.setDetectRenames(true);
df.format(patchList.getOldId(), patchList.getNewId());
}
} catch (PatchListNotAvailableException e) {
log.error("Cannot format patch", e);
} catch (IOException e) {
log.error("Cannot format patch", e);
} finally {
df.release();
repo.close();
}
}
return RawParseUtils.decode(out.toByteArray());
}
private Repository getRepository() {
try {
return args.server.openRepository(change.getProject());
} catch (RepositoryNotFoundException e) {
log.error("Cannot open repository", e);
return null;
} catch (IOException e) {
log.error("Cannot open repository", e);
return null;
}
}
}

View File

@@ -52,3 +52,6 @@ $email.changeDetail
#if($email.sshHost)
git pull ssh://$email.sshHost/$projectName $patchSet.refName
#end
## It is possible to include the patch as a unified diff in the email:
#$email.UnifiedDiff