aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java
diff options
context:
space:
mode:
authorMarco Miller <marco.miller@ericsson.com>2016-05-06 13:18:38 -0400
committerMatthias Sohn <matthias.sohn@sap.com>2016-05-21 01:14:06 +0200
commit1f86350c5a97d8c6966fe1146d649eb5cbc60f53 (patch)
tree5f440bdc4216044e356d9fba904971f1b469dc08 /org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java
parentba0dfe1ae2c6d0e3370822eedb0ffd2eeef0d8e2 (diff)
downloadjgit-1f86350c5a97d8c6966fe1146d649eb5cbc60f53.tar.gz
jgit-1f86350c5a97d8c6966fe1146d649eb5cbc60f53.zip
Support git config [include] section with absolute path(s)
As per [1], but limited to absolute paths indeed. No support yet for tilde or $HOME expansion. Support for the --[no-]includes options ([1]) is not part of this commit scope either, but those options' defaults are in effect as described in [1]. [1] https://git-scm.com/docs/git-config Included path can be a config file that includes other path-s in turn. An exception is thrown if too many recursions (circular includes) happen because of ill-specified config files. Change-Id: I700bd7b7e1625eb7de0180f220c707d8e7b0930b Signed-off-by: Marco Miller <marco.miller@ericsson.com> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java43
1 files changed, 42 insertions, 1 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java
index 70c3997d52..b8eba3acc2 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java
@@ -51,6 +51,8 @@
package org.eclipse.jgit.lib;
+import java.io.File;
+import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
@@ -64,6 +66,8 @@ import org.eclipse.jgit.events.ConfigChangedListener;
import org.eclipse.jgit.events.ListenerHandle;
import org.eclipse.jgit.events.ListenerList;
import org.eclipse.jgit.internal.JGitText;
+import org.eclipse.jgit.util.IO;
+import org.eclipse.jgit.util.RawParseUtils;
import org.eclipse.jgit.util.StringUtils;
@@ -75,6 +79,7 @@ public class Config {
private static final long KiB = 1024;
private static final long MiB = 1024 * KiB;
private static final long GiB = 1024 * MiB;
+ private static final int MAX_DEPTH = 999;
/** the change listeners */
private final ListenerList listeners = new ListenerList();
@@ -1027,6 +1032,15 @@ public class Config {
* made to {@code this}.
*/
public void fromText(final String text) throws ConfigInvalidException {
+ state.set(newState(fromTextRecurse(text, 1)));
+ }
+
+ private List<ConfigLine> fromTextRecurse(final String text, int depth)
+ throws ConfigInvalidException {
+ if (depth > MAX_DEPTH) {
+ throw new ConfigInvalidException(
+ JGitText.get().tooManyIncludeRecursions);
+ }
final List<ConfigLine> newEntries = new ArrayList<ConfigLine>();
final StringReader in = new StringReader(text);
ConfigLine last = null;
@@ -1085,11 +1099,38 @@ public class Config {
} else
e.value = readValue(in, false, -1);
+ if (e.section.equals("include")) { //$NON-NLS-1$
+ addIncludedConfig(newEntries, e, depth);
+ }
} else
throw new ConfigInvalidException(JGitText.get().invalidLineInConfigFile);
}
- state.set(newState(newEntries));
+ 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);
+ 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));
+ } catch (IOException ioe) {
+ throw new ConfigInvalidException(MessageFormat.format(
+ JGitText.get().invalidIncludedPathInConfigFile, path));
+ }
}
private ConfigSnapshot newState() {