git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH 06/19] Move SystemReader out of RepositoryConfig

From
Shawn O. Pearce <spearce@spearce.org>
Date
Jul 25, 2009, 18:52 UTC
Message-ID
<1248547982-4003-7-git-send-email-spearce@spearce.org>
In-Reply-To
<1248547982-4003-6-git-send-email-spearce@spearce.org>

Reading basic properties of the JVM has nothing to do with reading a Git style configuration file for a repository, or for the current user account. Instead pull all of that logic into its own abstract class, and provide a default implementation available through a singleton pattern.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../org/spearce/jgit/lib/RepositoryConfigTest.java |   11 +---
 .../org/spearce/jgit/lib/RepositoryTestCase.java   |    9 ++-
 .../src/org/spearce/jgit/lib/RepositoryConfig.java |   50 +--------------
 .../src/org/spearce/jgit/util/SystemReader.java    |   64 ++++++++++++++++++-
 4 files changed, 72 insertions(+), 62 deletions(-)
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java
index 5e2328b..5bb9afb 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java
@@ -42,8 +42,6 @@
 
 import java.io.File;
 import java.io.IOException;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
 import java.util.Arrays;
 import java.util.LinkedList;
 
@@ -116,14 +114,7 @@ public void test006_readCaseInsensitive() throws IOException {
 	}
 
 	public void test007_readUserInfos() throws IOException {
-		String hostname;
-		try {
-			InetAddress localMachine = InetAddress.getLocalHost();
-			hostname = localMachine.getCanonicalHostName();
-		} catch (UnknownHostException e) {
-			hostname = "localhost";
-		}
-
+		final String hostname = FAKE_HOSTNAME;
 		final File localConfig = writeTrashFile("local.config", "");
 		System.clearProperty(Constants.OS_USER_NAME_KEY);
 
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
index 2783180..9dfaeef 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
@@ -81,6 +81,8 @@
 
 	protected static final PersonIdent jcommitter;
 
+	protected static final String FAKE_HOSTNAME = "fake.host.example.com";
+
 	static {
 		jauthor = new PersonIdent("J. Author", "jauthor@example.com");
 		jcommitter = new PersonIdent("J. Committer", "jcommitter@example.com");
@@ -88,7 +90,7 @@
 
 	protected boolean packedGitMMAP;
 
-	protected static class FakeSystemReader implements SystemReader {
+	protected static class FakeSystemReader extends SystemReader {
 		Map<String, String> values = new HashMap<String, String>();
 		RepositoryConfig userGitConfig;
 		public String getenv(String variable) {
@@ -103,6 +105,9 @@ public RepositoryConfig openUserConfig() {
 		public void setUserGitConfig(RepositoryConfig userGitConfig) {
 			this.userGitConfig = userGitConfig;
 		}
+		public String getHostname() {
+			return FAKE_HOSTNAME;
+		}
 	}
 
 	/**
@@ -114,7 +119,7 @@ public void setUserGitConfig(RepositoryConfig userGitConfig) {
 
 	static {
 		fakeSystemReader = new FakeSystemReader();
-		RepositoryConfig.setSystemReader(fakeSystemReader);
+		SystemReader.setInstance(fakeSystemReader);
 	}
 
 	/**
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
index c80db00..9be7c1b 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
@@ -43,8 +43,6 @@
 
 import java.io.File;
 import java.io.IOException;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
 
 import org.spearce.jgit.util.FS;
 import org.spearce.jgit.util.SystemReader;
@@ -63,7 +61,7 @@
 	 *         configuration file from their home directory.
 	 */
 	public static RepositoryConfig openUserConfig() {
-		return systemReader.openUserConfig();
+		return SystemReader.getInstance().openUserConfig();
 	}
 
 	/** Section name for a branch configuration. */
@@ -73,21 +71,6 @@ public static RepositoryConfig openUserConfig() {
 
 	TransferConfig transfer;
 
-	private static String hostname;
-
-	// default system reader gets the value from the system
-	private static SystemReader systemReader = new SystemReader() {
-		public String getenv(String variable) {
-			return System.getenv(variable);
-		}
-		public String getProperty(String key) {
-			return System.getProperty(key);
-		}
-		public RepositoryConfig openUserConfig() {
-			return new RepositoryConfig(null, new File(FS.userHome(), ".gitconfig"));
-		}
-	};
-
 	RepositoryConfig(final Repository repo) {
 		this(openUserConfig(), FS.resolve(repo.getDirectory(), "config"));
 	}
@@ -139,6 +122,7 @@ public String getCommitterName() {
 	}
 
 	private String getUsernameInternal(String gitVariableKey) {
+		SystemReader systemReader = SystemReader.getInstance();
 		// try to get the user name from the local and global configurations.
 		String username = getString("user", null, "name");
 
@@ -177,6 +161,7 @@ public String getCommitterEmail() {
 	}
 
 	private String getUserEmailInternal(String gitVariableKey) {
+		SystemReader systemReader = SystemReader.getInstance();
 		// try to get the email from the local and global configurations.
 		String email = getString("user", null, "email");
 
@@ -191,7 +176,7 @@ private String getUserEmailInternal(String gitVariableKey) {
 			if (username == null){
 				username = Constants.UNKNOWN_USER_DEFAULT;
 			}
-			email = username + "@" + getHostname();
+			email = username + "@" + systemReader.getHostname();
 		}
 
 		return email;
@@ -216,31 +201,4 @@ public void load() throws IOException {
 		core = new CoreConfig(this);
 		transfer = new TransferConfig(this);
 	}
-
-	/**
-	 * Gets the hostname of the local host.
-	 * If no hostname can be found, the hostname is set to the default value "localhost".
-	 * @return the canonical hostname
-	 */
-	private static String getHostname() {
-		if (hostname == null) {
-			try {
-				InetAddress localMachine = InetAddress.getLocalHost();
-				hostname = localMachine.getCanonicalHostName();
-			} catch (UnknownHostException e) {
-				// we do nothing
-				hostname = "localhost";
-			}
-			assert hostname != null;
-		}
-		return hostname;
-	}
-
-	/**
-	 * Overrides the default system reader by a custom one.
-	 * @param newSystemReader new system reader
-	 */
-	public static void setSystemReader(SystemReader newSystemReader) {
-		systemReader = newSystemReader;
-	}
 }
diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
index 32c2e20..36c188c 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
@@ -37,6 +37,10 @@
 
 package org.spearce.jgit.util;
 
+import java.io.File;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
 import org.spearce.jgit.lib.RepositoryConfig;
 
 /**
@@ -47,21 +51,73 @@
  * permits to control the user's global configuration.
  * </p>
  */
-public interface SystemReader {
+public abstract class SystemReader {
+	private static SystemReader INSTANCE = new SystemReader() {
+		private volatile String hostname;
+
+		public String getenv(String variable) {
+			return System.getenv(variable);
+		}
+
+		public String getProperty(String key) {
+			return System.getProperty(key);
+		}
+
+		public RepositoryConfig openUserConfig() {
+			final File home = FS.userHome();
+			return new RepositoryConfig(null, new File(home, ".gitconfig"));
+		}
+
+		public String getHostname() {
+			if (hostname == null) {
+				try {
+					InetAddress localMachine = InetAddress.getLocalHost();
+					hostname = localMachine.getCanonicalHostName();
+				} catch (UnknownHostException e) {
+					// we do nothing
+					hostname = "localhost";
+				}
+				assert hostname != null;
+			}
+			return hostname;
+		}
+	};
+
+	/** @return the live instance to read system properties. */
+	public static SystemReader getInstance() {
+		return INSTANCE;
+	}
+
+	/**
+	 * @param newReader
+	 *            the new instance to use when accessing properties.
+	 */
+	public static void setInstance(SystemReader newReader) {
+		INSTANCE = newReader;
+	}
+
+	/**
+	 * Gets the hostname of the local host. If no hostname can be found, the
+	 * hostname is set to the default value "localhost".
+	 *
+	 * @return the canonical hostname
+	 */
+	public abstract String getHostname();
+
 	/**
 	 * @param variable system variable to read
 	 * @return value of the system variable
 	 */
-	String getenv(String variable);
+	public abstract String getenv(String variable);
 
 	/**
 	 * @param key of the system property to read
 	 * @return value of the system property
 	 */
-	String getProperty(String key);
+	public abstract String getProperty(String key);
 
 	/**
 	 * @return the git configuration found in the user home
 	 */
-	RepositoryConfig openUserConfig();
+	public abstract RepositoryConfig openUserConfig();
 }
-- 
1.6.4.rc2.216.g769fa
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 7 of 26 in “More Config class cleanup work”
  1. 00/19 More Config class cleanup workShawn O. Pearce, Jul 25, 2009
  2. 01/19 Cleanup nonstandard references to encoding strings to bytesShawn O. Pearce, Jul 25, 2009
  3. 02/19 Delete incorrect Javadoc from Config's getRawString methodShawn O. Pearce, Jul 25, 2009
  4. 03/19 Make Config.escapeValue a private methodShawn O. Pearce, Jul 25, 2009
  5. 04/19 Allow a RemoteConfig to use the more generic Config classShawn O. Pearce, Jul 25, 2009
  6. 05/19 Use type specific sets when creating a new RepositoryConfigShawn O. Pearce, Jul 25, 2009
  7. 06/19 Move SystemReader out of RepositoryConfigShawn O. Pearce, Jul 25, 2009
  8. 07/19 Correct user config to be of type FileBasedConfigShawn O. Pearce, Jul 25, 2009
  9. 08/19 Extract the test specific SystemReader out of RepositoryTestCaseShawn O. Pearce, Jul 25, 2009
  10. 09/19 Refactor Config hierarchy to make IO more explicitShawn O. Pearce, Jul 25, 2009
  11. 10/19 Test for the config file when creating a new repositoryShawn O. Pearce, Jul 25, 2009
  12. 11/19 Remove the map lookup for values in ConfigShawn O. Pearce, Jul 25, 2009
  13. 12/19 Return base values first from Config.getStringList()Shawn O. Pearce, Jul 25, 2009
  14. 13/19 Make Config thread safe by using copy-on-write semanticsShawn O. Pearce, Jul 25, 2009
  15. 14/19 Support cached application models in a ConfigShawn O. Pearce, Jul 25, 2009
  16. 15/19 Cache Config subsection names when requested by application codeShawn O. Pearce, Jul 25, 2009
  17. 16/19 Refactor author/committer lookup to use cached dataShawn O. Pearce, Jul 25, 2009
  18. 17/19 Move repository config creation fully into Repository classShawn O. Pearce, Jul 25, 2009
  19. 18/19 Use Config SectionParser cache to store daemon enable statesShawn O. Pearce, Jul 25, 2009
  20. 19/19 Use Config cache for fetch and receive configuration parsingShawn O. Pearce, Jul 25, 2009
  21. Robin RosenbergJul 25, 2009
  22. Shawn O. PearceJul 25, 2009
  23. Robin RosenbergJul 25, 2009
  24. Shawn O. PearceJul 25, 2009
  25. Robin RosenbergJul 25, 2009
  26. Shawn O. PearceJul 25, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.