|
|
@ -73,6 +73,7 @@ import org.eclipse.jgit.errors.MissingObjectException; |
|
|
|
import org.eclipse.jgit.internal.JGitText; |
|
|
|
import org.eclipse.jgit.internal.JGitText; |
|
|
|
import org.eclipse.jgit.lib.AbbreviatedObjectId; |
|
|
|
import org.eclipse.jgit.lib.AbbreviatedObjectId; |
|
|
|
import org.eclipse.jgit.lib.AnyObjectId; |
|
|
|
import org.eclipse.jgit.lib.AnyObjectId; |
|
|
|
|
|
|
|
import org.eclipse.jgit.lib.Config; |
|
|
|
import org.eclipse.jgit.lib.ConfigConstants; |
|
|
|
import org.eclipse.jgit.lib.ConfigConstants; |
|
|
|
import org.eclipse.jgit.lib.Constants; |
|
|
|
import org.eclipse.jgit.lib.Constants; |
|
|
|
import org.eclipse.jgit.lib.FileMode; |
|
|
|
import org.eclipse.jgit.lib.FileMode; |
|
|
@ -117,10 +118,10 @@ public class DiffFormatter implements AutoCloseable { |
|
|
|
|
|
|
|
|
|
|
|
private final OutputStream out; |
|
|
|
private final OutputStream out; |
|
|
|
|
|
|
|
|
|
|
|
private Repository db; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ObjectReader reader; |
|
|
|
private ObjectReader reader; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean closeReader; |
|
|
|
|
|
|
|
|
|
|
|
private DiffConfig diffCfg; |
|
|
|
private DiffConfig diffCfg; |
|
|
|
|
|
|
|
|
|
|
|
private int context = 3; |
|
|
|
private int context = 3; |
|
|
@ -172,28 +173,42 @@ public class DiffFormatter implements AutoCloseable { |
|
|
|
* source repository holding referenced objects. |
|
|
|
* source repository holding referenced objects. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setRepository(Repository repository) { |
|
|
|
public void setRepository(Repository repository) { |
|
|
|
if (reader != null) |
|
|
|
setReader(repository.newObjectReader(), repository.getConfig(), true); |
|
|
|
reader.close(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
db = repository; |
|
|
|
/** |
|
|
|
reader = db.newObjectReader(); |
|
|
|
* Set the repository the formatter can load object contents from. |
|
|
|
diffCfg = db.getConfig().get(DiffConfig.KEY); |
|
|
|
* |
|
|
|
|
|
|
|
* @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); |
|
|
|
ContentSource cs = ContentSource.create(reader); |
|
|
|
source = new ContentSource.Pair(cs, cs); |
|
|
|
source = new ContentSource.Pair(cs, cs); |
|
|
|
|
|
|
|
|
|
|
|
DiffConfig dc = db.getConfig().get(DiffConfig.KEY); |
|
|
|
if (diffCfg.isNoPrefix()) { |
|
|
|
if (dc.isNoPrefix()) { |
|
|
|
|
|
|
|
setOldPrefix(""); //$NON-NLS-1$
|
|
|
|
setOldPrefix(""); //$NON-NLS-1$
|
|
|
|
setNewPrefix(""); //$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_DIFF_SECTION, null, |
|
|
|
ConfigConstants.CONFIG_KEY_ALGORITHM, |
|
|
|
ConfigConstants.CONFIG_KEY_ALGORITHM, |
|
|
|
SupportedAlgorithm.HISTOGRAM)); |
|
|
|
SupportedAlgorithm.HISTOGRAM)); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -330,8 +345,8 @@ public class DiffFormatter implements AutoCloseable { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setDetectRenames(boolean on) { |
|
|
|
public void setDetectRenames(boolean on) { |
|
|
|
if (on && renameDetector == null) { |
|
|
|
if (on && renameDetector == null) { |
|
|
|
assertHaveRepository(); |
|
|
|
assertHaveReader(); |
|
|
|
renameDetector = new RenameDetector(db); |
|
|
|
renameDetector = new RenameDetector(reader, diffCfg); |
|
|
|
} else if (!on) |
|
|
|
} else if (!on) |
|
|
|
renameDetector = null; |
|
|
|
renameDetector = null; |
|
|
|
} |
|
|
|
} |
|
|
@ -387,8 +402,9 @@ public class DiffFormatter implements AutoCloseable { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void close() { |
|
|
|
public void close() { |
|
|
|
if (reader != null) |
|
|
|
if (reader != null && closeReader) { |
|
|
|
reader.close(); |
|
|
|
reader.close(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -412,7 +428,7 @@ public class DiffFormatter implements AutoCloseable { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public List<DiffEntry> scan(AnyObjectId a, AnyObjectId b) |
|
|
|
public List<DiffEntry> scan(AnyObjectId a, AnyObjectId b) |
|
|
|
throws IOException { |
|
|
|
throws IOException { |
|
|
|
assertHaveRepository(); |
|
|
|
assertHaveReader(); |
|
|
|
|
|
|
|
|
|
|
|
try (RevWalk rw = new RevWalk(reader)) { |
|
|
|
try (RevWalk rw = new RevWalk(reader)) { |
|
|
|
RevTree aTree = a != null ? rw.parseTree(a) : null; |
|
|
|
RevTree aTree = a != null ? rw.parseTree(a) : null; |
|
|
@ -441,7 +457,7 @@ public class DiffFormatter implements AutoCloseable { |
|
|
|
* trees cannot be read or file contents cannot be read. |
|
|
|
* trees cannot be read or file contents cannot be read. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public List<DiffEntry> scan(RevTree a, RevTree b) throws IOException { |
|
|
|
public List<DiffEntry> scan(RevTree a, RevTree b) throws IOException { |
|
|
|
assertHaveRepository(); |
|
|
|
assertHaveReader(); |
|
|
|
|
|
|
|
|
|
|
|
AbstractTreeIterator aIterator = makeIteratorFromTreeOrNull(a); |
|
|
|
AbstractTreeIterator aIterator = makeIteratorFromTreeOrNull(a); |
|
|
|
AbstractTreeIterator bIterator = makeIteratorFromTreeOrNull(b); |
|
|
|
AbstractTreeIterator bIterator = makeIteratorFromTreeOrNull(b); |
|
|
@ -476,7 +492,7 @@ public class DiffFormatter implements AutoCloseable { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public List<DiffEntry> scan(AbstractTreeIterator a, AbstractTreeIterator b) |
|
|
|
public List<DiffEntry> scan(AbstractTreeIterator a, AbstractTreeIterator b) |
|
|
|
throws IOException { |
|
|
|
throws IOException { |
|
|
|
assertHaveRepository(); |
|
|
|
assertHaveReader(); |
|
|
|
|
|
|
|
|
|
|
|
TreeWalk walk = new TreeWalk(reader); |
|
|
|
TreeWalk walk = new TreeWalk(reader); |
|
|
|
walk.addTree(a); |
|
|
|
walk.addTree(a); |
|
|
@ -674,7 +690,7 @@ public class DiffFormatter implements AutoCloseable { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String format(AbbreviatedObjectId id) { |
|
|
|
private String format(AbbreviatedObjectId id) { |
|
|
|
if (id.isComplete() && db != null) { |
|
|
|
if (id.isComplete() && reader != null) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
id = reader.abbreviate(id.toObjectId(), abbreviationLength); |
|
|
|
id = reader.abbreviate(id.toObjectId(), abbreviationLength); |
|
|
|
} catch (IOException cannotAbbreviate) { |
|
|
|
} catch (IOException cannotAbbreviate) { |
|
|
@ -940,7 +956,7 @@ public class DiffFormatter implements AutoCloseable { |
|
|
|
type = PatchType.UNIFIED; |
|
|
|
type = PatchType.UNIFIED; |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
assertHaveRepository(); |
|
|
|
assertHaveReader(); |
|
|
|
|
|
|
|
|
|
|
|
byte[] aRaw, bRaw; |
|
|
|
byte[] aRaw, bRaw; |
|
|
|
|
|
|
|
|
|
|
@ -987,9 +1003,10 @@ public class DiffFormatter implements AutoCloseable { |
|
|
|
return diffAlgorithm.diff(comparator, a, b); |
|
|
|
return diffAlgorithm.diff(comparator, a, b); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void assertHaveRepository() { |
|
|
|
private void assertHaveReader() { |
|
|
|
if (db == null) |
|
|
|
if (reader == null) { |
|
|
|
throw new IllegalStateException(JGitText.get().repositoryIsRequired); |
|
|
|
throw new IllegalStateException(JGitText.get().readerIsRequired); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private byte[] open(DiffEntry.Side side, DiffEntry entry) |
|
|
|
private byte[] open(DiffEntry.Side side, DiffEntry entry) |
|
|
|