Browse Source

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>
stable-0.12
Matthias Sohn 14 years ago
parent
commit
157a996e1d
  1. 12
      org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
  2. 106
      org.eclipse.jgit/src/org/eclipse/jgit/lib/UserConfig.java

12
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

106
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();
}

Loading…
Cancel
Save