import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.AnyObjectId;
+import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode;
private final OutputStream out;
- private Repository db;
-
private ObjectReader reader;
+ private boolean closeReader;
+
private DiffConfig diffCfg;
private int context = 3;
* source repository holding referenced objects.
*/
public void setRepository(Repository repository) {
- if (reader != null)
- reader.close();
+ setReader(repository.newObjectReader(), repository.getConfig(), true);
+ }
- db = repository;
- reader = db.newObjectReader();
- diffCfg = db.getConfig().get(DiffConfig.KEY);
+ /**
+ * Set the repository the formatter can load object contents from.
+ *
+ * @param reader
+ * source reader holding referenced objects. Caller is responsible
+ * for closing the reader.
+ * @param cfg
+ * config specifying diff algorithm and rename detection options.
+ * @since 4.5
+ */
+ public void setReader(ObjectReader reader, Config cfg) {
+ setReader(reader, cfg, false);
+ }
+
+ private void setReader(ObjectReader reader, Config cfg, boolean closeReader) {
+ close();
+ this.closeReader = closeReader;
+ this.reader = reader;
+ this.diffCfg = cfg.get(DiffConfig.KEY);
ContentSource cs = ContentSource.create(reader);
source = new ContentSource.Pair(cs, cs);
- DiffConfig dc = db.getConfig().get(DiffConfig.KEY);
- if (dc.isNoPrefix()) {
+ if (diffCfg.isNoPrefix()) {
setOldPrefix(""); //$NON-NLS-1$
setNewPrefix(""); //$NON-NLS-1$
}
- setDetectRenames(dc.isRenameDetectionEnabled());
+ setDetectRenames(diffCfg.isRenameDetectionEnabled());
- diffAlgorithm = DiffAlgorithm.getAlgorithm(db.getConfig().getEnum(
+ diffAlgorithm = DiffAlgorithm.getAlgorithm(cfg.getEnum(
ConfigConstants.CONFIG_DIFF_SECTION, null,
ConfigConstants.CONFIG_KEY_ALGORITHM,
SupportedAlgorithm.HISTOGRAM));
-
}
/**
*/
public void setDetectRenames(boolean on) {
if (on && renameDetector == null) {
- assertHaveRepository();
- renameDetector = new RenameDetector(db);
+ assertHaveReader();
+ renameDetector = new RenameDetector(reader, diffCfg);
} else if (!on)
renameDetector = null;
}
*/
@Override
public void close() {
- if (reader != null)
+ if (reader != null && closeReader) {
reader.close();
+ }
}
/**
*/
public List<DiffEntry> scan(AnyObjectId a, AnyObjectId b)
throws IOException {
- assertHaveRepository();
+ assertHaveReader();
try (RevWalk rw = new RevWalk(reader)) {
RevTree aTree = a != null ? rw.parseTree(a) : null;
* trees cannot be read or file contents cannot be read.
*/
public List<DiffEntry> scan(RevTree a, RevTree b) throws IOException {
- assertHaveRepository();
+ assertHaveReader();
AbstractTreeIterator aIterator = makeIteratorFromTreeOrNull(a);
AbstractTreeIterator bIterator = makeIteratorFromTreeOrNull(b);
*/
public List<DiffEntry> scan(AbstractTreeIterator a, AbstractTreeIterator b)
throws IOException {
- assertHaveRepository();
+ assertHaveReader();
TreeWalk walk = new TreeWalk(reader);
walk.addTree(a);
}
private String format(AbbreviatedObjectId id) {
- if (id.isComplete() && db != null) {
+ if (id.isComplete() && reader != null) {
try {
id = reader.abbreviate(id.toObjectId(), abbreviationLength);
} catch (IOException cannotAbbreviate) {
type = PatchType.UNIFIED;
} else {
- assertHaveRepository();
+ assertHaveReader();
byte[] aRaw, bRaw;
return diffAlgorithm.diff(comparator, a, b);
}
- private void assertHaveRepository() {
- if (db == null)
- throw new IllegalStateException(JGitText.get().repositoryIsRequired);
+ private void assertHaveReader() {
+ if (reader == null) {
+ throw new IllegalStateException(JGitText.get().readerIsRequired);
+ }
}
private byte[] open(DiffEntry.Side side, DiffEntry entry)