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

[JGIT PATCH 16/19] Refactor author/committer lookup to use cached data

From
Shawn O. Pearce <spearce@spearce.org>
Date
Jul 25, 2009, 18:52 UTC
Message-ID
<1248547982-4003-17-git-send-email-spearce@spearce.org>
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
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 17 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.