]> source.dussan.org Git - jgit.git/commitdiff
DiffFormatter: Support setting a reader without a repo 09/78409/2
authorDave Borowitz <dborowitz@google.com>
Wed, 3 Aug 2016 20:19:05 +0000 (16:19 -0400)
committerDave Borowitz <dborowitz@google.com>
Wed, 3 Aug 2016 20:24:37 +0000 (16:24 -0400)
Change-Id: I575cdb9c0a9a341b79ef5e3c7a35e68cde142540

org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties
org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java

index 7bf3d1e3243597a155f026be531a09cf0923c6ec..5dcadd38584965726ae716968bf54bfaa31f32d8 100644 (file)
@@ -501,6 +501,7 @@ pushIsNotSupportedForBundleTransport=Push is not supported for bundle transport
 pushNotPermitted=push not permitted
 pushOptionsNotSupported=Push options not supported; received {0}
 rawLogMessageDoesNotParseAsLogEntry=Raw log message does not parse as log entry
+readerIsRequired=Reader is required
 readingObjectsFromLocalRepositoryFailed=reading objects from local repository failed: {0}
 readTimedOut=Read timed out after {0} ms
 receivePackObjectTooLarge1=Object too large, rejecting the pack. Max object size limit is {0} bytes.
@@ -529,7 +530,6 @@ renamesFindingExact=Finding exact renames
 renamesRejoiningModifies=Rejoining modified file pairs
 repositoryAlreadyExists=Repository already exists: {0}
 repositoryConfigFileInvalid=Repository config file {0} invalid {1}
-repositoryIsRequired=Repository is required.
 repositoryNotFound=repository not found: {0}
 repositoryState_applyMailbox=Apply mailbox
 repositoryState_bare=Bare
index fc701f3a54044678f725fae1add0cd3354b2283f..819442cbea6b6e21985e3af4981261f9c8dd3480 100644 (file)
@@ -73,6 +73,7 @@ import org.eclipse.jgit.errors.MissingObjectException;
 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;
@@ -117,10 +118,10 @@ public class DiffFormatter implements AutoCloseable {
 
        private final OutputStream out;
 
-       private Repository db;
-
        private ObjectReader reader;
 
+       private boolean closeReader;
+
        private DiffConfig diffCfg;
 
        private int context = 3;
@@ -172,28 +173,42 @@ public class DiffFormatter implements AutoCloseable {
         *            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));
-
        }
 
        /**
@@ -330,8 +345,8 @@ public class DiffFormatter implements AutoCloseable {
         */
        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;
        }
@@ -387,8 +402,9 @@ public class DiffFormatter implements AutoCloseable {
         */
        @Override
        public void close() {
-               if (reader != null)
+               if (reader != null && closeReader) {
                        reader.close();
+               }
        }
 
        /**
@@ -412,7 +428,7 @@ public class DiffFormatter implements AutoCloseable {
         */
        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;
@@ -441,7 +457,7 @@ public class DiffFormatter implements AutoCloseable {
         *             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);
@@ -476,7 +492,7 @@ public class DiffFormatter implements AutoCloseable {
         */
        public List<DiffEntry> scan(AbstractTreeIterator a, AbstractTreeIterator b)
                        throws IOException {
-               assertHaveRepository();
+               assertHaveReader();
 
                TreeWalk walk = new TreeWalk(reader);
                walk.addTree(a);
@@ -674,7 +690,7 @@ public class DiffFormatter implements AutoCloseable {
        }
 
        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) {
@@ -940,7 +956,7 @@ public class DiffFormatter implements AutoCloseable {
                        type = PatchType.UNIFIED;
 
                } else {
-                       assertHaveRepository();
+                       assertHaveReader();
 
                        byte[] aRaw, bRaw;
 
@@ -987,9 +1003,10 @@ public class DiffFormatter implements AutoCloseable {
                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)
index 3c7dfb4c1bee39a423125eca0c7c1cff8a848830..fc54fd80a669489f777c702ba4f27e325bb73361 100644 (file)
@@ -560,6 +560,7 @@ public class JGitText extends TranslationBundle {
        /***/ public String pushNotPermitted;
        /***/ public String pushOptionsNotSupported;
        /***/ public String rawLogMessageDoesNotParseAsLogEntry;
+       /***/ public String readerIsRequired;
        /***/ public String readingObjectsFromLocalRepositoryFailed;
        /***/ public String readTimedOut;
        /***/ public String receivePackObjectTooLarge1;
@@ -588,7 +589,6 @@ public class JGitText extends TranslationBundle {
        /***/ public String renamesRejoiningModifies;
        /***/ public String repositoryAlreadyExists;
        /***/ public String repositoryConfigFileInvalid;
-       /***/ public String repositoryIsRequired;
        /***/ public String repositoryNotFound;
        /***/ public String repositoryState_applyMailbox;
        /***/ public String repositoryState_bare;