Browse Source

Merge "Expose if name or email is based on a guess"

stable-0.12
Shawn Pearce 14 years ago committed by Code Review
parent
commit
5aab335f45
  1. 12
      org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
  2. 102
      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(); authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
assertEquals(Constants.UNKNOWN_USER_DEFAULT, authorName); assertEquals(Constants.UNKNOWN_USER_DEFAULT, authorName);
assertEquals(Constants.UNKNOWN_USER_DEFAULT + "@" + hostname, authorEmail); 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 // the system user name is defined
mockSystemReader.setProperty(Constants.OS_USER_NAME_KEY, "os user name"); mockSystemReader.setProperty(Constants.OS_USER_NAME_KEY, "os user name");
localConfig.uncache(UserConfig.KEY); localConfig.uncache(UserConfig.KEY);
authorName = localConfig.get(UserConfig.KEY).getAuthorName(); authorName = localConfig.get(UserConfig.KEY).getAuthorName();
assertEquals("os user name", authorName); assertEquals("os user name", authorName);
assertTrue(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
if (hostname != null && hostname.length() != 0) { if (hostname != null && hostname.length() != 0) {
authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail(); authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
assertEquals("os user name@" + hostname, authorEmail); assertEquals("os user name@" + hostname, authorEmail);
} }
assertTrue(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
// the git environment variables are defined // the git environment variables are defined
mockSystemReader.setProperty(Constants.GIT_AUTHOR_NAME_KEY, "git author name"); mockSystemReader.setProperty(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
@ -168,6 +172,8 @@ public class ConfigTest {
authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail(); authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
assertEquals("git author name", authorName); assertEquals("git author name", authorName);
assertEquals("author@email", authorEmail); assertEquals("author@email", authorEmail);
assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
// the values are defined in the global configuration // the values are defined in the global configuration
userGitConfig.setString("user", null, "name", "global username"); userGitConfig.setString("user", null, "name", "global username");
@ -176,6 +182,8 @@ public class ConfigTest {
authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail(); authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
assertEquals("global username", authorName); assertEquals("global username", authorName);
assertEquals("author@globalemail", authorEmail); assertEquals("author@globalemail", authorEmail);
assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
// the values are defined in the local configuration // the values are defined in the local configuration
localConfig.setString("user", null, "name", "local username"); localConfig.setString("user", null, "name", "local username");
@ -184,11 +192,15 @@ public class ConfigTest {
authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail(); authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
assertEquals("local username", authorName); assertEquals("local username", authorName);
assertEquals("author@localemail", authorEmail); assertEquals("author@localemail", authorEmail);
assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
authorName = localConfig.get(UserConfig.KEY).getCommitterName(); authorName = localConfig.get(UserConfig.KEY).getCommitterName();
authorEmail = localConfig.get(UserConfig.KEY).getCommitterEmail(); authorEmail = localConfig.get(UserConfig.KEY).getCommitterEmail();
assertEquals("local username", authorName); assertEquals("local username", authorName);
assertEquals("author@localemail", authorEmail); assertEquals("author@localemail", authorEmail);
assertFalse(localConfig.get(UserConfig.KEY).isCommitterNameImplicit());
assertFalse(localConfig.get(UserConfig.KEY).isCommitterEmailImplicit());
} }
@Test @Test

102
org.eclipse.jgit/src/org/eclipse/jgit/lib/UserConfig.java

@ -1,6 +1,7 @@
/* /*
* Copyright (C) 2009, Google Inc. * Copyright (C) 2009, Google Inc.
* Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.com> * 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. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * 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) { private UserConfig(final Config rc) {
authorName = getNameInternal(rc, Constants.GIT_AUTHOR_NAME_KEY); authorName = getNameInternal(rc, Constants.GIT_AUTHOR_NAME_KEY);
if (authorName == null) {
authorName = getDefaultUserName();
isAuthorNameImplicit = true;
}
authorEmail = getEmailInternal(rc, Constants.GIT_AUTHOR_EMAIL_KEY); authorEmail = getEmailInternal(rc, Constants.GIT_AUTHOR_EMAIL_KEY);
if (authorEmail == null) {
authorEmail = getDefaultEmail();
isAuthorEmailImplicit = true;
}
committerName = getNameInternal(rc, Constants.GIT_COMMITTER_NAME_KEY); committerName = getNameInternal(rc, Constants.GIT_COMMITTER_NAME_KEY);
if (committerName == null) {
committerName = getDefaultUserName();
isCommitterNameImplicit = true;
}
committerEmail = getEmailInternal(rc, Constants.GIT_COMMITTER_EMAIL_KEY); 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 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) { private static String getNameInternal(Config rc, String envKey) {
// try to get the user name from the local and global configurations. // try to get the user name from the local and global configurations.
String username = rc.getString("user", null, "name"); 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 // try to get the user name for the system property GIT_XXX_NAME
username = system().getenv(envKey); username = system().getenv(envKey);
} }
if (username == null) {
// get the system user name return username;
username = system().getProperty(Constants.OS_USER_NAME_KEY);
} }
if (username == null) {
/**
* @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; username = Constants.UNKNOWN_USER_DEFAULT;
}
return username; return username;
} }
@ -137,16 +204,17 @@ public class UserConfig {
email = system().getenv(envKey); email = system().getenv(envKey);
} }
if (email == null) { return email;
// 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() { private static SystemReader system() {

Loading…
Cancel
Save