From: "Shawn O. Pearce" <spearce@spearce.org>
To: Robin Rosenberg <robin.rosenberg@dewire.com>
Cc: git@vger.kernel.org
Subject: [JGIT PATCH 06/19] Move SystemReader out of RepositoryConfig
Date: Sat, 25 Jul 2009 11:52:49 -0700 [thread overview]
Message-ID: <1248547982-4003-7-git-send-email-spearce@spearce.org> (raw)
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
next prev parent reply other threads:[~2009-07-25 18:54 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-07-25 18:52 [JGIT PATCH 00/19] More Config class cleanup work Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 01/19] Cleanup nonstandard references to encoding strings to bytes Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 02/19] Delete incorrect Javadoc from Config's getRawString method Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 03/19] Make Config.escapeValue a private method Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 04/19] Allow a RemoteConfig to use the more generic Config class Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 05/19] Use type specific sets when creating a new RepositoryConfig Shawn O. Pearce
2009-07-25 18:52 ` Shawn O. Pearce [this message]
2009-07-25 18:52 ` [JGIT PATCH 07/19] Correct user config to be of type FileBasedConfig Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 08/19] Extract the test specific SystemReader out of RepositoryTestCase Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 09/19] Refactor Config hierarchy to make IO more explicit Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 10/19] Test for the config file when creating a new repository Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 11/19] Remove the map lookup for values in Config Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 12/19] Return base values first from Config.getStringList() Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 13/19] Make Config thread safe by using copy-on-write semantics Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 14/19] Support cached application models in a Config Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 15/19] Cache Config subsection names when requested by application code Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 16/19] Refactor author/committer lookup to use cached data Shawn O. Pearce
2009-07-25 18:53 ` [JGIT PATCH 17/19] Move repository config creation fully into Repository class Shawn O. Pearce
2009-07-25 18:53 ` [JGIT PATCH 18/19] Use Config SectionParser cache to store daemon enable states Shawn O. Pearce
2009-07-25 18:53 ` [JGIT PATCH 19/19] Use Config cache for fetch and receive configuration parsing Shawn O. Pearce
2009-07-25 22:54 ` [JGIT PATCH 09/19] Refactor Config hierarchy to make IO more explicit Robin Rosenberg
2009-07-25 22:55 ` Shawn O. Pearce
2009-07-25 23:34 ` Robin Rosenberg
2009-07-25 23:38 ` Shawn O. Pearce
2009-07-25 20:32 ` [JGIT PATCH 02/19] Delete incorrect Javadoc from Config's getRawString method Robin Rosenberg
2009-07-25 20:33 ` Shawn O. Pearce
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1248547982-4003-7-git-send-email-spearce@spearce.org \
--to=spearce@spearce.org \
--cc=git@vger.kernel.org \
--cc=robin.rosenberg@dewire.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).