]> source.dussan.org Git - jgit.git/commitdiff
Config: extract method to read included config 47/111847/5
authorMarc Strapetz <marc.strapetz@syntevo.com>
Mon, 20 Nov 2017 09:21:30 +0000 (10:21 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Mon, 4 Dec 2017 22:38:24 +0000 (23:38 +0100)
Change-Id: I2c55af3e5fe29e0ce8e3d05e4b54ec961d3f863a
Signed-off-by: Marc Strapetz <marc.strapetz@syntevo.com>
org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java

index 77dfce10826a0c95bb132ef7431a0360c2eccbd5..f655f062b49d0b79833c1de8cfabadb2271847f9 100644 (file)
@@ -63,6 +63,7 @@ import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicReference;
 
+import org.eclipse.jgit.annotations.Nullable;
 import org.eclipse.jgit.errors.ConfigInvalidException;
 import org.eclipse.jgit.events.ConfigChangedEvent;
 import org.eclipse.jgit.events.ConfigChangedListener;
@@ -1100,29 +1101,29 @@ public class Config {
                return newEntries;
        }
 
-       private void addIncludedConfig(final List<ConfigLine> newEntries,
-                       ConfigLine line, int depth) throws ConfigInvalidException {
-               if (!line.name.equals("path") || //$NON-NLS-1$
-                               line.value == null || line.value.equals(MAGIC_EMPTY_VALUE)) {
-                       throw new ConfigInvalidException(
-                                       JGitText.get().invalidLineInConfigFile);
-               }
-               File path = new File(line.value);
+       /**
+        * Read the included config from the specified (possibly) relative path
+        *
+        * @param relPath
+        *            possibly relative path to the included config, as specified in
+        *            this config
+        * @return the read bytes, or null if the included config should be ignored
+        * @throws ConfigInvalidException
+        *             if something went wrong while reading the config
+        * @since 4.10
+        */
+       @Nullable
+       protected byte[] readIncludedConfig(String relPath)
+                       throws ConfigInvalidException {
+               File path = new File(relPath);
                try {
-                       byte[] bytes = IO.readFully(path);
-                       String decoded;
-                       if (isUtf8(bytes)) {
-                               decoded = RawParseUtils.decode(RawParseUtils.UTF8_CHARSET,
-                                               bytes, 3, bytes.length);
-                       } else {
-                               decoded = RawParseUtils.decode(bytes);
-                       }
-                       newEntries.addAll(fromTextRecurse(decoded, depth + 1));
+                       return IO.readFully(path);
                } catch (FileNotFoundException fnfe) {
                        if (path.exists()) {
                                throw new ConfigInvalidException(MessageFormat
                                                .format(JGitText.get().cannotReadFile, path), fnfe);
                        }
+                       return null;
                } catch (IOException ioe) {
                        throw new ConfigInvalidException(
                                        MessageFormat.format(JGitText.get().cannotReadFile, path),
@@ -1130,6 +1131,28 @@ public class Config {
                }
        }
 
+       private void addIncludedConfig(final List<ConfigLine> newEntries,
+                       ConfigLine line, int depth) throws ConfigInvalidException {
+               if (!line.name.equals("path") || //$NON-NLS-1$
+                               line.value == null || line.value.equals(MAGIC_EMPTY_VALUE)) {
+                       throw new ConfigInvalidException(
+                                       JGitText.get().invalidLineInConfigFile);
+               }
+               byte[] bytes = readIncludedConfig(line.value);
+               if (bytes == null) {
+                       return;
+               }
+
+               String decoded;
+               if (isUtf8(bytes)) {
+                       decoded = RawParseUtils.decode(RawParseUtils.UTF8_CHARSET, bytes, 3,
+                                       bytes.length);
+               } else {
+                       decoded = RawParseUtils.decode(bytes);
+               }
+               newEntries.addAll(fromTextRecurse(decoded, depth + 1));
+       }
+
        private ConfigSnapshot newState() {
                return new ConfigSnapshot(Collections.<ConfigLine> emptyList(),
                                getBaseState());