git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Shawn O. Pearce" <spearce@spearce.org>
To: Robin Rosenberg <robin.rosenberg@dewire.com>
Cc: git@vger.kernel.org
Subject: [JGIT PATCH 16/19] Refactor author/committer lookup to use cached data
Date: Sat, 25 Jul 2009 11:52:59 -0700	[thread overview]
Message-ID: <1248547982-4003-17-git-send-email-spearce@spearce.org> (raw)
In-Reply-To: <1248547982-4003-16-git-send-email-spearce@spearce.org>

The author and committer don't typically change when processing,
so we can maintain them in a cached entity just like we do with
the TransferConfig and the CoreConfig.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../org/spearce/jgit/lib/RepositoryConfigTest.java |   29 ++--
 .../src/org/spearce/jgit/lib/RepositoryConfig.java |   56 ++------
 .../src/org/spearce/jgit/lib/UserConfig.java       |  149 ++++++++++++++++++++
 3 files changed, 173 insertions(+), 61 deletions(-)
 create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/UserConfig.java

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 d4a6dd2..67d9964 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
@@ -109,55 +109,56 @@ public void test007_readUserConfig() {
 		SystemReader.setInstance(mockSystemReader);
 		final String hostname = mockSystemReader.getHostname();
 		final Config userGitConfig = mockSystemReader.userGitConfig;
-		final RepositoryConfig localConfig = new RepositoryConfig(userGitConfig, null);
-		localConfig.create();
+		final Config localConfig = new Config(userGitConfig);
 		mockSystemReader.values.clear();
 
 		String authorName;
 		String authorEmail;
 
 		// no values defined nowhere
-		authorName = localConfig.getAuthorName();
-		authorEmail = localConfig.getAuthorEmail();
+		authorName = localConfig.get(UserConfig.KEY).getAuthorName();
+		authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
 		assertEquals(Constants.UNKNOWN_USER_DEFAULT, authorName);
 		assertEquals(Constants.UNKNOWN_USER_DEFAULT + "@" + hostname, authorEmail);
 
 		// the system user name is defined
 		mockSystemReader.values.put(Constants.OS_USER_NAME_KEY, "os user name");
-		authorName = localConfig.getAuthorName();
+		localConfig.uncache(UserConfig.KEY);
+		authorName = localConfig.get(UserConfig.KEY).getAuthorName();
 		assertEquals("os user name", authorName);
 
 		if (hostname != null && hostname.length() != 0) {
-			authorEmail = localConfig.getAuthorEmail();
+			authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
 			assertEquals("os user name@" + hostname, authorEmail);
 		}
 
 		// the git environment variables are defined
 		mockSystemReader.values.put(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
 		mockSystemReader.values.put(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
-		authorName = localConfig.getAuthorName();
-		authorEmail = localConfig.getAuthorEmail();
+		localConfig.uncache(UserConfig.KEY);
+		authorName = localConfig.get(UserConfig.KEY).getAuthorName();
+		authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
 		assertEquals("git author name", authorName);
 		assertEquals("author@email", authorEmail);
 
 		// the values are defined in the global configuration
 		userGitConfig.setString("user", null, "name", "global username");
 		userGitConfig.setString("user", null, "email", "author@globalemail");
-		authorName = localConfig.getAuthorName();
-		authorEmail = localConfig.getAuthorEmail();
+		authorName = localConfig.get(UserConfig.KEY).getAuthorName();
+		authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
 		assertEquals("global username", authorName);
 		assertEquals("author@globalemail", authorEmail);
 
 		// the values are defined in the local configuration
 		localConfig.setString("user", null, "name", "local username");
 		localConfig.setString("user", null, "email", "author@localemail");
-		authorName = localConfig.getAuthorName();
-		authorEmail = localConfig.getAuthorEmail();
+		authorName = localConfig.get(UserConfig.KEY).getAuthorName();
+		authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
 		assertEquals("local username", authorName);
 		assertEquals("author@localemail", authorEmail);
 
-		authorName = localConfig.getCommitterName();
-		authorEmail = localConfig.getCommitterEmail();
+		authorName = localConfig.get(UserConfig.KEY).getCommitterName();
+		authorEmail = localConfig.get(UserConfig.KEY).getCommitterEmail();
 		assertEquals("local username", authorName);
 		assertEquals("author@localemail", authorEmail);
 	}
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 c6a13b6..15fe9de 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 org.spearce.jgit.util.SystemReader;
-
 /**
  * An object representing the Git config file.
  *
@@ -83,13 +81,18 @@ public TransferConfig getTransfer() {
 		return get(TransferConfig.KEY);
 	}
 
+	/** @return standard user configuration data */
+	public UserConfig getUserConfig() {
+		return get(UserConfig.KEY);
+	}
+
 	/**
 	 * @return the author name as defined in the git variables
 	 *         and configurations. If no name could be found, try
 	 *         to use the system user name instead.
 	 */
 	public String getAuthorName() {
-		return getUsernameInternal(Constants.GIT_AUTHOR_NAME_KEY);
+		return getUserConfig().getAuthorName();
 	}
 
 	/**
@@ -98,26 +101,7 @@ public String getAuthorName() {
 	 *         to use the system user name instead.
 	 */
 	public String getCommitterName() {
-		return getUsernameInternal(Constants.GIT_COMMITTER_NAME_KEY);
-	}
-
-	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");
-
-		if (username == null) {
-			// try to get the user name for the system property GIT_XXX_NAME
-			username = systemReader.getenv(gitVariableKey);
-		}
-		if (username == null) {
-			// get the system user name
-			username = systemReader.getProperty(Constants.OS_USER_NAME_KEY);
-		}
-		if (username == null) {
-			username = Constants.UNKNOWN_USER_DEFAULT;
-		}
-		return username;
+		return getUserConfig().getCommitterName();
 	}
 
 	/**
@@ -127,7 +111,7 @@ private String getUsernameInternal(String gitVariableKey) {
 	 *         host name.
 	 */
 	public String getAuthorEmail() {
-		return getUserEmailInternal(Constants.GIT_AUTHOR_EMAIL_KEY);
+		return getUserConfig().getAuthorEmail();
 	}
 
 	/**
@@ -137,29 +121,7 @@ public String getAuthorEmail() {
 	 *         host name.
 	 */
 	public String getCommitterEmail() {
-		return getUserEmailInternal(Constants.GIT_COMMITTER_EMAIL_KEY);
-	}
-
-	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");
-
-		if (email == null) {
-			// try to get the email for the system property GIT_XXX_EMAIL
-			email = systemReader.getenv(gitVariableKey);
-		}
-
-		if (email == null) {
-			// try to construct an email
-			String username = systemReader.getProperty(Constants.OS_USER_NAME_KEY);
-			if (username == null){
-				username = Constants.UNKNOWN_USER_DEFAULT;
-			}
-			email = username + "@" + systemReader.getHostname();
-		}
-
-		return email;
+		return getUserConfig().getCommitterEmail();
 	}
 
 	/**
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/UserConfig.java b/org.spearce.jgit/src/org/spearce/jgit/lib/UserConfig.java
new file mode 100644
index 0000000..27c7d69
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/UserConfig.java
@@ -0,0 +1,149 @@
+/*
+ * Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.com>
+ * Copyright (C) 2009, Google Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ *   copyright notice, this list of conditions and the following
+ *   disclaimer in the documentation and/or other materials provided
+ *   with the distribution.
+ *
+ * - Neither the name of the Git Development Community nor the
+ *   names of its contributors may be used to endorse or promote
+ *   products derived from this software without specific prior
+ *   written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.spearce.jgit.lib;
+
+import org.spearce.jgit.lib.Config.SectionParser;
+import org.spearce.jgit.util.SystemReader;
+
+/** The standard "user" configuration parameters. */
+public class UserConfig {
+	/** Key for {@link Config#get(SectionParser)}. */
+	public static final Config.SectionParser<UserConfig> KEY = new SectionParser<UserConfig>() {
+		public UserConfig parse(final Config cfg) {
+			return new UserConfig(cfg);
+		}
+	};
+
+	private final String authorName;
+
+	private final String authorEmail;
+
+	private final String committerName;
+
+	private final String committerEmail;
+
+	private UserConfig(final Config rc) {
+		authorName = getNameInternal(rc, Constants.GIT_AUTHOR_NAME_KEY);
+		authorEmail = getEmailInternal(rc, Constants.GIT_AUTHOR_EMAIL_KEY);
+
+		committerName = getNameInternal(rc, Constants.GIT_COMMITTER_NAME_KEY);
+		committerEmail = getEmailInternal(rc, Constants.GIT_COMMITTER_EMAIL_KEY);
+	}
+
+	/**
+	 * @return the author name as defined in the git variables and
+	 *         configurations. If no name could be found, try to use the system
+	 *         user name instead.
+	 */
+	public String getAuthorName() {
+		return authorName;
+	}
+
+	/**
+	 * @return the committer name as defined in the git variables and
+	 *         configurations. If no name could be found, try to use the system
+	 *         user name instead.
+	 */
+	public String getCommitterName() {
+		return committerName;
+	}
+
+	/**
+	 * @return the author email as defined in git variables and
+	 *         configurations. If no email could be found, try to
+	 *         propose one default with the user name and the
+	 *         host name.
+	 */
+	public String getAuthorEmail() {
+		return authorEmail;
+	}
+
+	/**
+	 * @return the committer email as defined in git variables and
+	 *         configurations. If no email could be found, try to
+	 *         propose one default with the user name and the
+	 *         host name.
+	 */
+	public String getCommitterEmail() {
+		return committerEmail;
+	}
+
+	private static String getNameInternal(Config rc, String envKey) {
+		// try to get the user name from the local and global configurations.
+		String username = rc.getString("user", null, "name");
+
+		if (username == null) {
+			// try to get the user name for the system property GIT_XXX_NAME
+			username = system().getenv(envKey);
+		}
+		if (username == null) {
+			// get the system user name
+			username = system().getProperty(Constants.OS_USER_NAME_KEY);
+		}
+		if (username == null) {
+			username = Constants.UNKNOWN_USER_DEFAULT;
+		}
+		return username;
+	}
+
+	private static String getEmailInternal(Config rc, String envKey) {
+		// try to get the email from the local and global configurations.
+		String email = rc.getString("user", null, "email");
+
+		if (email == null) {
+			// try to get the email for the system property GIT_XXX_EMAIL
+			email = system().getenv(envKey);
+		}
+
+		if (email == null) {
+			// try to construct an email
+			String username = system().getProperty(Constants.OS_USER_NAME_KEY);
+			if (username == null){
+				username = Constants.UNKNOWN_USER_DEFAULT;
+			}
+			email = username + "@" + system().getHostname();
+		}
+
+		return email;
+	}
+
+	private static SystemReader system() {
+		return SystemReader.getInstance();
+	}
+}
-- 
1.6.4.rc2.216.g769fa

  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           ` [JGIT PATCH 06/19] Move SystemReader out of RepositoryConfig Shawn O. Pearce
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                               ` Shawn O. Pearce [this message]
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-17-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).