aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2011-03-16 16:23:14 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2011-03-16 16:23:14 +0100
commit157a996e1d48b504df922f995bf1104dc2c57c7b (patch)
tree0fb85806b1f86b0ea9d2520197213070b60f8b6d
parent8aef29c114910b8212306880d914c085e94a2fcd (diff)
downloadjgit-157a996e1d48b504df922f995bf1104dc2c57c7b.tar.gz
jgit-157a996e1d48b504df922f995bf1104dc2c57c7b.zip
Expose if name or email is based on a guess
This enables applications to differentiate between explicitly set configuration parameters and best effort attempts to guess these parameters from the operating system. Change-Id: I67cc4099238a40c6dca795e64f0155ced6008ef1 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java12
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/UserConfig.java106
2 files changed, 99 insertions, 19 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
index d5da16ad80..b9f62177cc 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
@@ -148,17 +148,21 @@ public class ConfigTest {
authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
assertEquals(Constants.UNKNOWN_USER_DEFAULT, authorName);
assertEquals(Constants.UNKNOWN_USER_DEFAULT + "@" + hostname, authorEmail);
+ assertTrue(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
+ assertTrue(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
// the system user name is defined
mockSystemReader.setProperty(Constants.OS_USER_NAME_KEY, "os user name");
localConfig.uncache(UserConfig.KEY);
authorName = localConfig.get(UserConfig.KEY).getAuthorName();
assertEquals("os user name", authorName);
+ assertTrue(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
if (hostname != null && hostname.length() != 0) {
authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
assertEquals("os user name@" + hostname, authorEmail);
}
+ assertTrue(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
// the git environment variables are defined
mockSystemReader.setProperty(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
@@ -168,6 +172,8 @@ public class ConfigTest {
authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
assertEquals("git author name", authorName);
assertEquals("author@email", authorEmail);
+ assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
+ assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
// the values are defined in the global configuration
userGitConfig.setString("user", null, "name", "global username");
@@ -176,6 +182,8 @@ public class ConfigTest {
authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
assertEquals("global username", authorName);
assertEquals("author@globalemail", authorEmail);
+ assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
+ assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
// the values are defined in the local configuration
localConfig.setString("user", null, "name", "local username");
@@ -184,11 +192,15 @@ public class ConfigTest {
authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
assertEquals("local username", authorName);
assertEquals("author@localemail", authorEmail);
+ assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
+ assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
authorName = localConfig.get(UserConfig.KEY).getCommitterName();
authorEmail = localConfig.get(UserConfig.KEY).getCommitterEmail();
assertEquals("local username", authorName);
assertEquals("author@localemail", authorEmail);
+ assertFalse(localConfig.get(UserConfig.KEY).isCommitterNameImplicit());
+ assertFalse(localConfig.get(UserConfig.KEY).isCommitterEmailImplicit());
}
@Test
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/UserConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/UserConfig.java
index 28b3cd0277..da44a2ef31 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/UserConfig.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/UserConfig.java
@@ -1,6 +1,7 @@
/*
* Copyright (C) 2009, Google Inc.
* Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.com>
+ * Copyright (C) 2011, Matthias Sohn <matthias.sohn@sap.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
@@ -56,20 +57,44 @@ public class UserConfig {
}
};
- private final String authorName;
+ private String authorName;
- private final String authorEmail;
+ private String authorEmail;
- private final String committerName;
+ private String committerName;
- private final String committerEmail;
+ private String committerEmail;
+
+ private boolean isAuthorNameImplicit;
+
+ private boolean isAuthorEmailImplicit;
+
+ private boolean isCommitterNameImplicit;
+
+ private boolean isCommitterEmailImplicit;
private UserConfig(final Config rc) {
authorName = getNameInternal(rc, Constants.GIT_AUTHOR_NAME_KEY);
+ if (authorName == null) {
+ authorName = getDefaultUserName();
+ isAuthorNameImplicit = true;
+ }
authorEmail = getEmailInternal(rc, Constants.GIT_AUTHOR_EMAIL_KEY);
+ if (authorEmail == null) {
+ authorEmail = getDefaultEmail();
+ isAuthorEmailImplicit = true;
+ }
committerName = getNameInternal(rc, Constants.GIT_COMMITTER_NAME_KEY);
+ if (committerName == null) {
+ committerName = getDefaultUserName();
+ isCommitterNameImplicit = true;
+ }
committerEmail = getEmailInternal(rc, Constants.GIT_COMMITTER_EMAIL_KEY);
+ if (committerEmail == null) {
+ committerEmail = getDefaultEmail();
+ isCommitterEmailImplicit = true;
+ }
}
/**
@@ -110,6 +135,42 @@ public class UserConfig {
return committerEmail;
}
+ /**
+ * @return true if the author name was not explicitly configured but
+ * constructed from information the system has about the logged on
+ * user
+ */
+ public boolean isAuthorNameImplicit() {
+ return isAuthorNameImplicit;
+ }
+
+ /**
+ * @return true if the author email was not explicitly configured but
+ * constructed from information the system has about the logged on
+ * user
+ */
+ public boolean isAuthorEmailImplicit() {
+ return isAuthorEmailImplicit;
+ }
+
+ /**
+ * @return true if the committer name was not explicitly configured but
+ * constructed from information the system has about the logged on
+ * user
+ */
+ public boolean isCommitterNameImplicit() {
+ return isCommitterNameImplicit;
+ }
+
+ /**
+ * @return true if the author email was not explicitly configured but
+ * constructed from information the system has about the logged on
+ * user
+ */
+ public boolean isCommitterEmailImplicit() {
+ return isCommitterEmailImplicit;
+ }
+
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");
@@ -118,13 +179,19 @@ public class UserConfig {
// 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) {
+
+ return username;
+ }
+
+ /**
+ * @return try to get user name of the logged on user from the operating
+ * system
+ */
+ private static String getDefaultUserName() {
+ // get the system user name
+ String username = system().getProperty(Constants.OS_USER_NAME_KEY);
+ if (username == null)
username = Constants.UNKNOWN_USER_DEFAULT;
- }
return username;
}
@@ -137,18 +204,19 @@ public class UserConfig {
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;
}
+ /**
+ * @return try to construct email for logged on user using system
+ * information
+ */
+ private static String getDefaultEmail() {
+ // try to construct an email
+ String username = getDefaultUserName();
+ return username + "@" + system().getHostname();
+ }
+
private static SystemReader system() {
return SystemReader.getInstance();
}