* [PATCH JGIT] Compute the author/commiter name and email from the git configuration
@ 2009-02-03 21:13 Yann Simon
2009-02-03 23:13 ` Shawn O. Pearce
2009-02-04 8:08 ` Yann Simon
0 siblings, 2 replies; 15+ messages in thread
From: Yann Simon @ 2009-02-03 21:13 UTC (permalink / raw)
To: Robin Rosenberg, Shawn O. Pearce; +Cc: git
The author/commiter name and email are retrieved from the system configuration
and the local and global git configurations.
When the name is not available, propose one default by using the system user name.
When the email is not available, propose one default by concatenate the
user name and the host name.
The author name and email are used as default value in the author field
while committing with the GUI.
Fix issue 47 (field Author don't fill)
Signed-off-by: Yann Simon <yann.simon.fr@gmail.com>
---
.../egit/ui/internal/actions/CommitAction.java | 17 ++++
.../org/spearce/jgit/lib/RepositoryConfigTest.java | 56 +++++++++++
.../src/org/spearce/jgit/lib/Constants.java | 16 +++
.../src/org/spearce/jgit/lib/RepositoryConfig.java | 97 ++++++++++++++++++++
4 files changed, 186 insertions(+), 0 deletions(-)
diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java
index 17232d6..456745c 100644
--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java
+++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java
@@ -41,6 +41,7 @@
import org.spearce.jgit.lib.PersonIdent;
import org.spearce.jgit.lib.RefUpdate;
import org.spearce.jgit.lib.Repository;
+import org.spearce.jgit.lib.RepositoryConfig;
import org.spearce.jgit.lib.Tree;
import org.spearce.jgit.lib.TreeEntry;
import org.spearce.jgit.lib.GitIndex.Entry;
@@ -70,8 +71,10 @@ public void run(IAction act) {
}
Repository[] repos = getRepositoriesFor(getProjectsForSelectedResources());
+ Repository repository = null;
amendAllowed = repos.length == 1;
for (Repository repo : repos) {
+ repository = repo;
if (!repo.getRepositoryState().canCommit()) {
MessageDialog.openError(getTargetPart().getSite().getShell(),
"Cannot commit now", "Repository state:"
@@ -95,12 +98,26 @@ public void run(IAction act) {
}
}
+ assert repository != null;
+ RepositoryConfig repositoryConfig = repository.getConfig();
+ String author = null;
+ String username = repositoryConfig.getAuthorName();
+ if (username != null && username.length() != 0) {
+ author = username;
+ String email = repositoryConfig.getAuthorEmail();
+ if (email != null && email.length() != 0) {
+ author = author + " <" + email + ">";
+ }
+ }
+
loadPreviousCommit();
CommitDialog commitDialog = new CommitDialog(getTargetPart().getSite().getShell());
commitDialog.setAmending(amending);
commitDialog.setAmendAllowed(amendAllowed);
commitDialog.setFileList(files);
+ commitDialog.setAuthor(author);
+
if (previousCommit != null)
commitDialog.setPreviousCommitMessage(previousCommit.getMessage());
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 34ce04a..4f53ca0 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
@@ -42,6 +42,8 @@
import java.io.File;
import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.LinkedList;
@@ -116,4 +118,58 @@ public void test006_readCaseInsensitive() throws IOException {
assertEquals(true, repositoryConfig.getBoolean("foo", null, "bar", false));
assertEquals("", repositoryConfig.getString("foo", null, "bar"));
}
+
+ public void test007_readUserInfos() throws IOException {
+ final File globalConfig = writeTrashFile("global.config", "");
+ final File localConfig = writeTrashFile("global.config", "");
+ System.clearProperty(Constants.OS_USER_NAME_KEY);
+ System.clearProperty(Constants.GIT_AUTHOR_NAME_KEY);
+ System.clearProperty(Constants.GIT_AUTHOR_EMAIL_KEY);
+
+ RepositoryConfig globalRepositoryConfig = new RepositoryConfig(null, globalConfig);
+ RepositoryConfig localRepositoryConfig = new RepositoryConfig(globalRepositoryConfig, localConfig);
+
+ String authorName;
+ String authorEmail;
+
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertNull(authorName);
+ assertNull(authorEmail);
+
+ System.setProperty(Constants.OS_USER_NAME_KEY, "os user name");
+ authorName = localRepositoryConfig.getAuthorName();
+ assertEquals("os user name", authorName);
+ InetAddress localMachine;
+ try {
+ localMachine = InetAddress.getLocalHost();
+ String hostname = localMachine.getHostName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("os user name@" + hostname, authorEmail);
+ } catch (UnknownHostException e) {
+ // we do nothing
+ }
+
+
+ System.setProperty(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
+ System.setProperty(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("git author name", authorName);
+ assertEquals("author@email", authorEmail);
+
+ globalRepositoryConfig.setString("user", null, "name", "global username");
+ globalRepositoryConfig.setString("user", null, "email", "author@globalemail");
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("global username", authorName);
+ assertEquals("author@globalemail", authorEmail);
+
+ localRepositoryConfig.setString("user", null, "name", "local username");
+ localRepositoryConfig.setString("user", null, "email", "author@localemail");
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("local username", authorName);
+ assertEquals("author@localemail", authorEmail);
+ }
}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
index 8f093d6..372fba5 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
@@ -228,6 +228,22 @@
/** Packed refs file */
public static final String PACKED_REFS = "packed-refs";
+ /** The environment variable that contains the system user name */
+ public static final String OS_USER_NAME_KEY = "user.name";
+
+ /** The environment variable that contains the author's name */
+ public static final String GIT_AUTHOR_NAME_KEY = "GIT_AUTHOR_NAME";
+
+ /** The environment variable that contains the author's email */
+ public static final String GIT_AUTHOR_EMAIL_KEY = "GIT_AUTHOR_EMAIL";
+
+ /** The environment variable that contains the commiter's name */
+ public static final String GIT_COMMITER_NAME_KEY = "GIT_COMMITER_NAME";
+
+ /** The environment variable that contains the commiter's email */
+ public static final String GIT_COMMITER_EMAIL_KEY = "GIT_COMMITER_EMAIL";
+
+
/**
* Create a new digest function for objects.
*
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 7df90cd..5821f83 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
@@ -50,6 +50,8 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -98,6 +100,8 @@ public static RepositoryConfig openUserConfig() {
private Map<String, Object> byName;
+ private String hostname;
+
private static final String MAGIC_EMPTY_VALUE = "%%magic%%empty%%";
RepositoryConfig(final Repository repo) {
@@ -308,6 +312,83 @@ public String getString(final String section, String subsection, final String na
return result;
}
+ /**
+ * @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 the commiter 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 getCommiterName() {
+ return getUsernameInternal(Constants.GIT_COMMITER_NAME_KEY);
+ }
+
+ private String getUsernameInternal(String gitVariableKey) {
+ // 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 = System.getProperty(gitVariableKey);
+ }
+ if (username == null) {
+ // get the system user name
+ username = System.getProperty(Constants.OS_USER_NAME_KEY);
+ }
+
+ return username;
+ }
+
+ /**
+ * @return the author email as defined in git variables and
+ * configurations. If no email could be found, try to
+ * propose one default with the author name and the
+ * host name.
+ */
+ public String getAuthorEmail() {
+ return getUserEmailInternal(Constants.GIT_AUTHOR_EMAIL_KEY, true);
+ }
+
+ /**
+ * @return the commiter email as defined in git variables and
+ * configurations. If no email could be found, try to
+ * propose one default with the author name and the
+ * host name.
+ */
+ public String getCommiterEmail() {
+ return getUserEmailInternal(Constants.GIT_COMMITER_EMAIL_KEY, false);
+ }
+
+ private String getUserEmailInternal(String gitVariableKey, boolean author) {
+ // try to get the email from the local and global configs.
+ String email = getString("user", null, "email");
+
+ if (email == null) {
+ // try to get the email for the system property GIT_XXX_EMAIL
+ email = System.getProperty(gitVariableKey);
+ }
+
+ if (email == null) {
+ // try to construct an email
+ String userName = author ? getAuthorName() : getCommiterName();
+ if (userName != null && userName.length() != 0) {
+ String hostnameTmp = getHostname();
+ if (hostnameTmp != null && hostnameTmp.length() != 0) {
+ email = userName + "@" + hostnameTmp;
+ }
+ }
+ }
+
+ return email;
+ }
+
private String getRawString(final String section, final String subsection,
final String name) {
final Object o = getRawEntry(section, subsection, name);
@@ -957,4 +1038,20 @@ private static boolean eq(final String a, final String b) {
return a.equalsIgnoreCase(b);
}
}
+
+ /**
+ * @return the hostname
+ */
+ public String getHostname() {
+ if (hostname == null) {
+ InetAddress localMachine;
+ try {
+ localMachine = InetAddress.getLocalHost();
+ hostname = localMachine.getHostName();
+ } catch (UnknownHostException e) {
+ // we do nothing
+ }
+ }
+ return hostname;
+ }
}
--
1.6.0.6
^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration
2009-02-03 21:13 Yann Simon
@ 2009-02-03 23:13 ` Shawn O. Pearce
2009-02-03 23:20 ` Johannes Schindelin
` (2 more replies)
2009-02-04 8:08 ` Yann Simon
1 sibling, 3 replies; 15+ messages in thread
From: Shawn O. Pearce @ 2009-02-03 23:13 UTC (permalink / raw)
To: Yann Simon; +Cc: Robin Rosenberg, git
Yann Simon <yann.simon.fr@gmail.com> wrote:
> index 7df90cd..5821f83 100644
> --- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
> +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
> @@ -50,6 +50,8 @@
> import java.io.InputStreamReader;
> import java.io.OutputStreamWriter;
> import java.io.PrintWriter;
> +import java.net.InetAddress;
> +import java.net.UnknownHostException;
> import java.util.ArrayList;
> import java.util.Collections;
> import java.util.HashMap;
> @@ -98,6 +100,8 @@ public static RepositoryConfig openUserConfig() {
>
> private Map<String, Object> byName;
>
> + private String hostname;
> +
> private static final String MAGIC_EMPTY_VALUE = "%%magic%%empty%%";
>
> RepositoryConfig(final Repository repo) {
> @@ -308,6 +312,83 @@ public String getString(final String section, String subsection, final String na
> return result;
> }
>
> + /**
> + * @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 the commiter 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 getCommiterName() {
> + return getUsernameInternal(Constants.GIT_COMMITER_NAME_KEY);
> + }
> +
> + private String getUsernameInternal(String gitVariableKey) {
> + // 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 = System.getProperty(gitVariableKey);
Shouldn't that be System.getenv()?
> + private String getUserEmailInternal(String gitVariableKey, boolean author) {
> + // try to get the email from the local and global configs.
> + String email = getString("user", null, "email");
> +
> + if (email == null) {
> + // try to get the email for the system property GIT_XXX_EMAIL
> + email = System.getProperty(gitVariableKey);
Again, System.getenv()?
> + public String getHostname() {
> + if (hostname == null) {
> + InetAddress localMachine;
> + try {
> + localMachine = InetAddress.getLocalHost();
> + hostname = localMachine.getHostName();
> + } catch (UnknownHostException e) {
> + // we do nothing
> + }
> + }
> + return hostname;
Do we want getHostName() or getCanonicalHostName() here?
I think we'd want getCanonicalHostName().
Should we be caching this at the RepositoryConfig level, or at the
whole JVM level (in a static). If the application is long-running
its likely to keep the same RepositoryConfig instance around for
the life of that JVM, so we'd only make this request once. Thus any
change in hostname while the application is running would probably
not take effect until after restart. But any long running app is
also likely to access more than one Repository, and thus more than
one RepositoryConfig, so they should at least use consistent names,
even if the underlying hostname has changed.
IMHO, just cache it in a static on first demand.
--
Shawn.
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration
2009-02-03 23:13 ` Shawn O. Pearce
@ 2009-02-03 23:20 ` Johannes Schindelin
2009-02-04 0:55 ` Robin Rosenberg
2009-02-04 0:59 ` Shawn O. Pearce
2009-02-04 8:14 ` Yann Simon
2009-02-04 8:42 ` Yann Simon
2 siblings, 2 replies; 15+ messages in thread
From: Johannes Schindelin @ 2009-02-03 23:20 UTC (permalink / raw)
To: Shawn O. Pearce; +Cc: Yann Simon, Robin Rosenberg, git
Hi,
On Tue, 3 Feb 2009, Shawn O. Pearce wrote:
> Yann Simon <yann.simon.fr@gmail.com> wrote:
>
> > @@ -308,6 +312,83 @@ public String getString(final String section, String subsection, final String na
> > return result;
> > }
> >
> > + /**
> > + * @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 the commiter 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 getCommiterName() {
> > + return getUsernameInternal(Constants.GIT_COMMITER_NAME_KEY);
> > + }
> > +
> > + private String getUsernameInternal(String gitVariableKey) {
> > + // 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 = System.getProperty(gitVariableKey);
>
> Shouldn't that be System.getenv()?
According to
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv(java.lang.String)
getenv() is deprecated. However, in later editions (Java5 and later, to
be precise), that deprecation seems to be lifted...
Ciao,
Dscho
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration
2009-02-03 23:20 ` Johannes Schindelin
@ 2009-02-04 0:55 ` Robin Rosenberg
2009-02-04 1:01 ` Shawn O. Pearce
2009-02-04 0:59 ` Shawn O. Pearce
1 sibling, 1 reply; 15+ messages in thread
From: Robin Rosenberg @ 2009-02-04 0:55 UTC (permalink / raw)
To: Johannes Schindelin; +Cc: Shawn O. Pearce, Yann Simon, git
onsdag 04 februari 2009 00:20:03 skrev Johannes Schindelin:
> According to
>
> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv(java.lang.String)
>
> getenv() is deprecated. However, in later editions (Java5 and later, to
> be precise), that deprecation seems to be lifted...
It was worse, it wasn't even implemented. You got a runtime exception back then (1.3 or 1.4).
So now it's fine.
-- robin
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration
2009-02-03 23:20 ` Johannes Schindelin
2009-02-04 0:55 ` Robin Rosenberg
@ 2009-02-04 0:59 ` Shawn O. Pearce
2009-02-04 1:04 ` Robin Rosenberg
1 sibling, 1 reply; 15+ messages in thread
From: Shawn O. Pearce @ 2009-02-04 0:59 UTC (permalink / raw)
To: Johannes Schindelin; +Cc: Yann Simon, Robin Rosenberg, git
Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> On Tue, 3 Feb 2009, Shawn O. Pearce wrote:
> > Yann Simon <yann.simon.fr@gmail.com> wrote:
> > > + username = System.getProperty(gitVariableKey);
> >
> > Shouldn't that be System.getenv()?
>
> According to
>
> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv(java.lang.String)
>
> getenv() is deprecated. However, in later editions (Java5 and later, to
> be precise), that deprecation seems to be lifted...
Yea, funny thing about that. In 1.3 getenv() worked. In 1.4 they
not only marked it deprecated, they changed the method to always
throw a RuntimeException. Java developers were outraged, so in 1.5
(aka Java 5) they added it back in as a supported method.
JGit only runs on Java 5 and later. So we're safe, we don't have
to worry about this #@(!@(! method and the 1.4 mistake.
Hell, Java 6 added an executable flag to java.io.File. This is
a rather funny concept on Windows where executablity is based on
file name and not a "mode bit", but its in the Java 6 S2SE API.
At least even on Windows getenv makes sense and provides data.
--
Shawn.
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration
2009-02-04 0:55 ` Robin Rosenberg
@ 2009-02-04 1:01 ` Shawn O. Pearce
0 siblings, 0 replies; 15+ messages in thread
From: Shawn O. Pearce @ 2009-02-04 1:01 UTC (permalink / raw)
To: Robin Rosenberg; +Cc: Johannes Schindelin, Yann Simon, git
Robin Rosenberg <robin.rosenberg.lists@dewire.com> wrote:
> onsdag 04 februari 2009 00:20:03 skrev Johannes Schindelin:
> > According to
> >
> > http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv(java.lang.String)
> >
> > getenv() is deprecated. However, in later editions (Java5 and later, to
> > be precise), that deprecation seems to be lifted...
>
> It was worse, it wasn't even implemented. You got a runtime exception back then (1.3 or 1.4).
> So now it's fine.
FWIW, it worked in 1.3 and returned correct data on platforms that
had a notion of "environment" (POSIX, Windows, not Mac OS 9). It
was only 1.4 that threw a RuntimeException.
That was a HUGE mistake on the Sun developer's part. IMHO,
1.4 should have always returned NULL instead of throwing
RuntimeException. Most applications had ways to work around
not getting a particular value from the environment, e.g. if the
end-user didn't set something. But they weren't prepared to handle
a new class of RuntimeException coming out of a method in System.
--
Shawn.
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration
2009-02-04 0:59 ` Shawn O. Pearce
@ 2009-02-04 1:04 ` Robin Rosenberg
0 siblings, 0 replies; 15+ messages in thread
From: Robin Rosenberg @ 2009-02-04 1:04 UTC (permalink / raw)
To: Shawn O. Pearce; +Cc: Johannes Schindelin, Yann Simon, git
onsdag 04 februari 2009 01:59:07 skrev Shawn O. Pearce:
> Hell, Java 6 added an executable flag to java.io.File. This is
> a rather funny concept on Windows where executablity is based on
> file name and not a "mode bit", but its in the Java 6 S2SE API.
> At least even on Windows getenv makes sense and provides data.
There is an execute bit in windows ACL's. Disable it for an executables (exe, dll etc and
see for yourself). I've hit it when trying to track an eclipse installation using git and
some DLL wasn't executable after a checkout.
-- robin
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration
2009-02-03 21:13 Yann Simon
2009-02-03 23:13 ` Shawn O. Pearce
@ 2009-02-04 8:08 ` Yann Simon
1 sibling, 0 replies; 15+ messages in thread
From: Yann Simon @ 2009-02-04 8:08 UTC (permalink / raw)
To: Robin Rosenberg, Shawn O. Pearce; +Cc: git
Sorry to all, there is still 2 trailing whitespaces in this patch.
I send a wrong version..
I will anyway send the patch again after dealing with the comments.
Yann, bad man for patches
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration
2009-02-03 23:13 ` Shawn O. Pearce
2009-02-03 23:20 ` Johannes Schindelin
@ 2009-02-04 8:14 ` Yann Simon
2009-02-04 8:42 ` Yann Simon
2 siblings, 0 replies; 15+ messages in thread
From: Yann Simon @ 2009-02-04 8:14 UTC (permalink / raw)
To: Shawn O. Pearce; +Cc: Robin Rosenberg, git
2009/2/4 Shawn O. Pearce <spearce@spearce.org>:
> Yann Simon <yann.simon.fr@gmail.com> wrote:
>> index 7df90cd..5821f83 100644
>> --- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
>> +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
>> @@ -50,6 +50,8 @@
>> import java.io.InputStreamReader;
>> import java.io.OutputStreamWriter;
>> import java.io.PrintWriter;
>> +import java.net.InetAddress;
>> +import java.net.UnknownHostException;
>> import java.util.ArrayList;
>> import java.util.Collections;
>> import java.util.HashMap;
>> @@ -98,6 +100,8 @@ public static RepositoryConfig openUserConfig() {
>>
>> private Map<String, Object> byName;
>>
>> + private String hostname;
>> +
>> private static final String MAGIC_EMPTY_VALUE = "%%magic%%empty%%";
>>
>> RepositoryConfig(final Repository repo) {
>> @@ -308,6 +312,83 @@ public String getString(final String section, String subsection, final String na
>> return result;
>> }
>>
>> + /**
>> + * @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 the commiter 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 getCommiterName() {
>> + return getUsernameInternal(Constants.GIT_COMMITER_NAME_KEY);
>> + }
>> +
>> + private String getUsernameInternal(String gitVariableKey) {
>> + // 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 = System.getProperty(gitVariableKey);
>
> Shouldn't that be System.getenv()?
>
>> + private String getUserEmailInternal(String gitVariableKey, boolean author) {
>> + // try to get the email from the local and global configs.
>> + String email = getString("user", null, "email");
>> +
>> + if (email == null) {
>> + // try to get the email for the system property GIT_XXX_EMAIL
>> + email = System.getProperty(gitVariableKey);
>
> Again, System.getenv()?
>
>> + public String getHostname() {
>> + if (hostname == null) {
>> + InetAddress localMachine;
>> + try {
>> + localMachine = InetAddress.getLocalHost();
>> + hostname = localMachine.getHostName();
>> + } catch (UnknownHostException e) {
>> + // we do nothing
>> + }
>> + }
>> + return hostname;
>
> Do we want getHostName() or getCanonicalHostName() here?
>
> I think we'd want getCanonicalHostName().
Yes, indeed. I change this.
>
> Should we be caching this at the RepositoryConfig level, or at the
> whole JVM level (in a static). If the application is long-running
> its likely to keep the same RepositoryConfig instance around for
> the life of that JVM, so we'd only make this request once. Thus any
> change in hostname while the application is running would probably
> not take effect until after restart. But any long running app is
> also likely to access more than one Repository, and thus more than
> one RepositoryConfig, so they should at least use consistent names,
> even if the underlying hostname has changed.
>
> IMHO, just cache it in a static on first demand.
OK, that make sense.
Should the call be thread-safe if it can be called from different instances?
Yann
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration
2009-02-03 23:13 ` Shawn O. Pearce
2009-02-03 23:20 ` Johannes Schindelin
2009-02-04 8:14 ` Yann Simon
@ 2009-02-04 8:42 ` Yann Simon
2 siblings, 0 replies; 15+ messages in thread
From: Yann Simon @ 2009-02-04 8:42 UTC (permalink / raw)
To: Shawn O. Pearce; +Cc: Robin Rosenberg, git
2009/2/4 Shawn O. Pearce <spearce@spearce.org>:
> Yann Simon <yann.simon.fr@gmail.com> wrote:
>> index 7df90cd..5821f83 100644
>> --- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
>> +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
>> @@ -50,6 +50,8 @@
>> import java.io.InputStreamReader;
>> import java.io.OutputStreamWriter;
>> import java.io.PrintWriter;
>> +import java.net.InetAddress;
>> +import java.net.UnknownHostException;
>> import java.util.ArrayList;
>> import java.util.Collections;
>> import java.util.HashMap;
>> @@ -98,6 +100,8 @@ public static RepositoryConfig openUserConfig() {
>>
>> private Map<String, Object> byName;
>>
>> + private String hostname;
>> +
>> private static final String MAGIC_EMPTY_VALUE = "%%magic%%empty%%";
>>
>> RepositoryConfig(final Repository repo) {
>> @@ -308,6 +312,83 @@ public String getString(final String section, String subsection, final String na
>> return result;
>> }
>>
>> + /**
>> + * @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 the commiter 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 getCommiterName() {
>> + return getUsernameInternal(Constants.GIT_COMMITER_NAME_KEY);
>> + }
>> +
>> + private String getUsernameInternal(String gitVariableKey) {
>> + // 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 = System.getProperty(gitVariableKey);
>
> Shouldn't that be System.getenv()?
>
>> + private String getUserEmailInternal(String gitVariableKey, boolean author) {
>> + // try to get the email from the local and global configs.
>> + String email = getString("user", null, "email");
>> +
>> + if (email == null) {
>> + // try to get the email for the system property GIT_XXX_EMAIL
>> + email = System.getProperty(gitVariableKey);
>
> Again, System.getenv()?
Just a precision:
One of the consequences of using System.getenv() is that it does not
let java programs to define the value of the variable.
System.getProperty(), on the other hand, offers a System.setProperty()
If we use System.getenv(), we cannot overwrite the value before.
But we maybe do not need to overwrite system variables (except for
unit test... :) )
In that case, we can live with System.getenv().
A workaround would be to use a method like:
private static String readEnvironmentVariable(String key) {
String result = System.getProperty(key);
if (result == null || result.length() == 0) {
result = System.getenv(key);
}
return result;
}
but I really dislike it. This is much more complex and make the
results less predictable.
I change my code to use System.getenv() unless you got other ideas.
Yann
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH JGIT] Compute the author/commiter name and email from the git configuration
@ 2009-02-04 12:28 Yann Simon
2009-02-04 17:36 ` Shawn O. Pearce
0 siblings, 1 reply; 15+ messages in thread
From: Yann Simon @ 2009-02-04 12:28 UTC (permalink / raw)
To: Robin Rosenberg, Shawn O. Pearce; +Cc: git
The author/commiter name and email are retrieved from the system configuration
and the local and global git configurations.
When the name is not available, propose one default by using the system user name.
When the email is not available, propose one default by concatenate the
user name and the host name.
The author name and email are used as default value in the author field
while committing with the GUI.
Fix issue 47 (field Author don't fill)
Signed-off-by: Yann Simon <yann.simon.fr@gmail.com>
---
This is the second version of the patch.
The differences are the following ones:
- use System.getenv for environment variables
- use canonical host name.
- static cache the host name
.../egit/ui/internal/actions/CommitAction.java | 17 +++
.../org/spearce/jgit/lib/RepositoryConfigTest.java | 70 ++++++++++++
.../src/org/spearce/jgit/lib/Constants.java | 16 +++
.../src/org/spearce/jgit/lib/RepositoryConfig.java | 116 ++++++++++++++++++++
.../src/org/spearce/jgit/util/SystemReader.java | 24 ++++
5 files changed, 243 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java
index 17232d6..456745c 100644
--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java
+++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java
@@ -41,6 +41,7 @@
import org.spearce.jgit.lib.PersonIdent;
import org.spearce.jgit.lib.RefUpdate;
import org.spearce.jgit.lib.Repository;
+import org.spearce.jgit.lib.RepositoryConfig;
import org.spearce.jgit.lib.Tree;
import org.spearce.jgit.lib.TreeEntry;
import org.spearce.jgit.lib.GitIndex.Entry;
@@ -70,8 +71,10 @@ public void run(IAction act) {
}
Repository[] repos = getRepositoriesFor(getProjectsForSelectedResources());
+ Repository repository = null;
amendAllowed = repos.length == 1;
for (Repository repo : repos) {
+ repository = repo;
if (!repo.getRepositoryState().canCommit()) {
MessageDialog.openError(getTargetPart().getSite().getShell(),
"Cannot commit now", "Repository state:"
@@ -95,12 +98,26 @@ public void run(IAction act) {
}
}
+ assert repository != null;
+ RepositoryConfig repositoryConfig = repository.getConfig();
+ String author = null;
+ String username = repositoryConfig.getAuthorName();
+ if (username != null && username.length() != 0) {
+ author = username;
+ String email = repositoryConfig.getAuthorEmail();
+ if (email != null && email.length() != 0) {
+ author = author + " <" + email + ">";
+ }
+ }
+
loadPreviousCommit();
CommitDialog commitDialog = new CommitDialog(getTargetPart().getSite().getShell());
commitDialog.setAmending(amending);
commitDialog.setAmendAllowed(amendAllowed);
commitDialog.setFileList(files);
+ commitDialog.setAuthor(author);
+
if (previousCommit != null)
commitDialog.setPreviousCommitMessage(previousCommit.getMessage());
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 34ce04a..113eb1c 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
@@ -43,7 +43,11 @@
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.LinkedList;
+import java.util.Map;
+
+import org.spearce.jgit.util.SystemReader;
/**
* Test reading of git config
@@ -116,4 +120,70 @@ public void test006_readCaseInsensitive() throws IOException {
assertEquals(true, repositoryConfig.getBoolean("foo", null, "bar", false));
assertEquals("", repositoryConfig.getString("foo", null, "bar"));
}
+
+ private class FakeSystemReader implements SystemReader {
+ Map<String, String> values = new HashMap<String, String>();
+ public String getEnvironmentVariable(String variable) {
+ return values.get(variable);
+ }
+ public String getProperty(String key) {
+ return values.get(key);
+ }
+ }
+
+ public void test007_readUserInfos() throws IOException {
+ final File globalConfig = writeTrashFile("global.config", "");
+ final File localConfig = writeTrashFile("local.config", "");
+ System.clearProperty(Constants.OS_USER_NAME_KEY);
+
+ FakeSystemReader fakeSystemReader = new FakeSystemReader();
+
+ RepositoryConfig globalRepositoryConfig = new RepositoryConfig(null, globalConfig);
+ RepositoryConfig localRepositoryConfig = new RepositoryConfig(globalRepositoryConfig, localConfig);
+ localRepositoryConfig.setSystemReader(fakeSystemReader);
+
+ String authorName;
+ String authorEmail;
+
+ // no values defined nowhere
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertNull(authorName);
+ assertNull(authorEmail);
+
+ // the system user name is defined
+ fakeSystemReader.values.put(Constants.OS_USER_NAME_KEY, "os user name");
+ authorName = localRepositoryConfig.getAuthorName();
+ assertEquals("os user name", authorName);
+
+ String hostname = RepositoryConfig.getHostname();
+ if (hostname != null && hostname.length() != 0) {
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("os user name@" + hostname, authorEmail);
+ }
+
+ // the git environment variables are defined
+ fakeSystemReader.values.put(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
+ fakeSystemReader.values.put(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("git author name", authorName);
+ assertEquals("author@email", authorEmail);
+
+ // the values are defined in the global configuration
+ globalRepositoryConfig.setString("user", null, "name", "global username");
+ globalRepositoryConfig.setString("user", null, "email", "author@globalemail");
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("global username", authorName);
+ assertEquals("author@globalemail", authorEmail);
+
+ // the values are defined in the local configuration
+ localRepositoryConfig.setString("user", null, "name", "local username");
+ localRepositoryConfig.setString("user", null, "email", "author@localemail");
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("local username", authorName);
+ assertEquals("author@localemail", authorEmail);
+ }
}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
index 8f093d6..372fba5 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
@@ -228,6 +228,22 @@
/** Packed refs file */
public static final String PACKED_REFS = "packed-refs";
+ /** The environment variable that contains the system user name */
+ public static final String OS_USER_NAME_KEY = "user.name";
+
+ /** The environment variable that contains the author's name */
+ public static final String GIT_AUTHOR_NAME_KEY = "GIT_AUTHOR_NAME";
+
+ /** The environment variable that contains the author's email */
+ public static final String GIT_AUTHOR_EMAIL_KEY = "GIT_AUTHOR_EMAIL";
+
+ /** The environment variable that contains the commiter's name */
+ public static final String GIT_COMMITER_NAME_KEY = "GIT_COMMITER_NAME";
+
+ /** The environment variable that contains the commiter's email */
+ public static final String GIT_COMMITER_EMAIL_KEY = "GIT_COMMITER_EMAIL";
+
+
/**
* Create a new digest function for objects.
*
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 7df90cd..0fa4b1f 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
@@ -50,6 +50,8 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -60,6 +62,7 @@
import java.util.Set;
import org.spearce.jgit.util.FS;
+import org.spearce.jgit.util.SystemReader;
/**
* An object representing the Git config file.
@@ -98,8 +101,20 @@ public static RepositoryConfig openUserConfig() {
private Map<String, Object> byName;
+ private static String hostname;
+
private static final String MAGIC_EMPTY_VALUE = "%%magic%%empty%%";
+ // default system reader gets the value from the system
+ private static SystemReader systemReader = new SystemReader() {
+ public String getEnvironmentVariable(String variable) {
+ return System.getenv(variable);
+ }
+ public String getProperty(String key) {
+ return System.getProperty(key);
+ }
+ };
+
RepositoryConfig(final Repository repo) {
this(openUserConfig(), FS.resolve(repo.getDirectory(), "config"));
}
@@ -308,6 +323,83 @@ public String getString(final String section, String subsection, final String na
return result;
}
+ /**
+ * @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 the commiter 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 getCommiterName() {
+ return getUsernameInternal(Constants.GIT_COMMITER_NAME_KEY);
+ }
+
+ private String getUsernameInternal(String gitVariableKey) {
+ // 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.getEnvironmentVariable(gitVariableKey);
+ }
+ if (username == null) {
+ // get the system user name
+ username = systemReader.getProperty(Constants.OS_USER_NAME_KEY);
+ }
+
+ return username;
+ }
+
+ /**
+ * @return the author email as defined in git variables and
+ * configurations. If no email could be found, try to
+ * propose one default with the author name and the
+ * host name.
+ */
+ public String getAuthorEmail() {
+ return getUserEmailInternal(Constants.GIT_AUTHOR_EMAIL_KEY, true);
+ }
+
+ /**
+ * @return the commiter email as defined in git variables and
+ * configurations. If no email could be found, try to
+ * propose one default with the author name and the
+ * host name.
+ */
+ public String getCommiterEmail() {
+ return getUserEmailInternal(Constants.GIT_COMMITER_EMAIL_KEY, false);
+ }
+
+ private String getUserEmailInternal(String gitVariableKey, boolean author) {
+ // 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.getEnvironmentVariable(gitVariableKey);
+ }
+
+ if (email == null) {
+ // try to construct an email
+ String userName = author ? getAuthorName() : getCommiterName();
+ if (userName != null && userName.length() != 0) {
+ String hostnameTmp = getHostname();
+ if (hostnameTmp != null && hostnameTmp.length() != 0) {
+ email = userName + "@" + hostnameTmp;
+ }
+ }
+ }
+
+ return email;
+ }
+
private String getRawString(final String section, final String subsection,
final String name) {
final Object o = getRawEntry(section, subsection, name);
@@ -957,4 +1049,28 @@ private static boolean eq(final String a, final String b) {
return a.equalsIgnoreCase(b);
}
}
+
+ /**
+ * @return the canonical hostname
+ */
+ public static String getHostname() {
+ if (hostname == null) {
+ try {
+ InetAddress localMachine = InetAddress.getLocalHost();
+ hostname = localMachine.getCanonicalHostName();
+ } catch (UnknownHostException e) {
+ // we do nothing
+ }
+ }
+ return hostname;
+ }
+
+ /**
+ * Overrides the default system reader by a custom one.
+ * @param systemReader new system reader
+ */
+ public void setSystemReader(SystemReader systemReader) {
+ RepositoryConfig.systemReader = systemReader;
+ }
+
}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
new file mode 100644
index 0000000..89b4021
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
@@ -0,0 +1,24 @@
+package org.spearce.jgit.util;
+
+/**
+ * Interface to read values from the system.
+ * <p>
+ * When writing unit tests, extending this interface with a custom class
+ * permits to simulate an access to a system variable or property.
+ * </p>
+ */
+public interface SystemReader {
+
+ /**
+ * @param variable system variable to read
+ * @return value of the system variable
+ */
+ String getEnvironmentVariable(String variable);
+
+ /**
+ * @param key of the system property to read
+ * @return value of the system property
+ */
+ String getProperty(String key);
+
+}
--
1.6.0.4
^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration
2009-02-04 12:28 [PATCH JGIT] Compute the author/commiter name and email from the git configuration Yann Simon
@ 2009-02-04 17:36 ` Shawn O. Pearce
0 siblings, 0 replies; 15+ messages in thread
From: Shawn O. Pearce @ 2009-02-04 17:36 UTC (permalink / raw)
To: Yann Simon; +Cc: Robin Rosenberg, git
Yann Simon <yann.simon.fr@gmail.com> wrote:
> 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 34ce04a..113eb1c 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
> @@ -116,4 +120,70 @@ public void test006_readCaseInsensitive() throws IOException {
> assertEquals(true, repositoryConfig.getBoolean("foo", null, "bar", false));
> assertEquals("", repositoryConfig.getString("foo", null, "bar"));
> }
> +
> + private class FakeSystemReader implements SystemReader {
> + Map<String, String> values = new HashMap<String, String>();
> + public String getEnvironmentVariable(String variable) {
> + return values.get(variable);
> + }
> + public String getProperty(String key) {
> + return values.get(key);
> + }
> + }
I like this approach. Perhaps this should simply be done in
RepositoryTestCase so all of our unit tests have a stable default
identity, no matter what. They could change it on a test-by-test
basis if needed, especially if the reader is created and populated
with default values during the setUp() method.
> diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
> index 8f093d6..372fba5 100644
> --- a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
> +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
> +
> + /** The environment variable that contains the commiter's name */
> + public static final String GIT_COMMITER_NAME_KEY = "GIT_COMMITER_NAME";
There are 2 't's in GIT_COMMITTER_NAME. Both the variable name
and the value are wrong.
> + /** The environment variable that contains the commiter's email */
> + public static final String GIT_COMMITER_EMAIL_KEY = "GIT_COMMITER_EMAIL";
Again, 2 't's.
> 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 7df90cd..0fa4b1f 100644
> --- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
> +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
> @@ -308,6 +323,83 @@ public String getString(final String section, String subsection, final String na
...
> + public String getCommiterName() {
2 't's in comitter.
> + public String getCommiterEmail() {
There are 2 't's in Committer.
> + private String getUserEmailInternal(String gitVariableKey, boolean author) {
...
> + // try to construct an email
> + String userName = author ? getAuthorName() : getCommiterName();
> + if (userName != null && userName.length() != 0) {
> + String hostnameTmp = getHostname();
> + if (hostnameTmp != null && hostnameTmp.length() != 0) {
> + email = userName + "@" + hostnameTmp;
> + }
If you do what I suggest below, getHostname() will never return
null, so this logic will simplify out and we'll always be able to
set email if we have a userName.
Also, I think this should be defaulting to the Java "user.name"
property and not to the GIT_AUTHOR_NAME environment variable. So if
the user.email isn't set, and the *_EMAIL env var isn't set, use the
"user.name" property and the hostname to form the email address.
I don't think this should ever produce null. If we don't have a
username from user.name, use some generic string like "unknown-user".
Then we at least still have some identity data. Its very unlikely
the JVM won't have a "user.name" property for us, so its very
likely we won't get good tests in application level code for null
return values.
> @@ -957,4 +1049,28 @@ private static boolean eq(final String a, final String b) {
> +
> + /**
> + * @return the canonical hostname
> + */
> + public static String getHostname() {
I don't see a reason for this to be public. Please make it private.
> + if (hostname == null) {
> + try {
> + InetAddress localMachine = InetAddress.getLocalHost();
> + hostname = localMachine.getCanonicalHostName();
> + } catch (UnknownHostException e) {
> + // we do nothing
Set hostname = "localhost" so we don't incur an UHE on every attempt
to get the hostname. If it failed once, its very likely to fail
again, and again, and again.
> + /**
> + * Overrides the default system reader by a custom one.
> + * @param systemReader new system reader
> + */
> + public void setSystemReader(SystemReader systemReader) {
Method should be static.
> + RepositoryConfig.systemReader = systemReader;
There is no need for RepositoryConfig here. Rename the parameter
so it doesn't hide the field.
> diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
> new file mode 100644
> index 0000000..89b4021
> --- /dev/null
> +++ b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
> @@ -0,0 +1,24 @@
> +package org.spearce.jgit.util;
Missing copyright header.
> + /**
> + * @param variable system variable to read
> + * @return value of the system variable
> + */
> + String getEnvironmentVariable(String variable);
Maybe just emulate the System class and call this getenv() ?
--
Shawn.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH JGIT] Compute the author/commiter name and email from the git configuration
@ 2009-02-05 9:27 Yann Simon
0 siblings, 0 replies; 15+ messages in thread
From: Yann Simon @ 2009-02-05 9:27 UTC (permalink / raw)
To: Robin Rosenberg, Shawn O. Pearce; +Cc: git
The author/commiter name and email are retrieved from the system configuration
and the local and global git configurations.
When the name is not available, propose one default by using the system user name.
When the email is not available, propose one default by concatenating the
user name and the host name.
The author name and email are used as default value in the author field
while committing with the GUI.
Fix issue 47 (field Author don't fill)
Signed-off-by: Yann Simon <yann.simon.fr@gmail.com>
---
This is the 3rd version of the patch.
I applied the received comments.
Changes between v2 and v3 of the patch:
- move the fakeSystemReader into RepositoryTestCase and fill it with default values.
- hange commiter -> committer.
- getHostName() never returns null. I used "localhost" when the host name is not available.
- the user name is set to a default one is no other information is available.
- change the way to form a default email using only the system property "user.name".
- add a copyright header to SystemReader. I did not know if I had to change the copyright to include my name.
I personally do not care about that.
- change SystemReader.getEnvironmentVariable to SystemReader.getenv to emulate System class.
.../egit/ui/internal/actions/CommitAction.java | 17 +++
.../org/spearce/jgit/lib/RepositoryConfigTest.java | 63 ++++++++++
.../org/spearce/jgit/lib/RepositoryTestCase.java | 30 +++++
.../src/org/spearce/jgit/lib/Constants.java | 18 +++
.../src/org/spearce/jgit/lib/RepositoryConfig.java | 120 ++++++++++++++++++++
.../src/org/spearce/jgit/util/SystemReader.java | 62 ++++++++++
6 files changed, 310 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java
index 17232d6..456745c 100644
--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java
+++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java
@@ -41,6 +41,7 @@
import org.spearce.jgit.lib.PersonIdent;
import org.spearce.jgit.lib.RefUpdate;
import org.spearce.jgit.lib.Repository;
+import org.spearce.jgit.lib.RepositoryConfig;
import org.spearce.jgit.lib.Tree;
import org.spearce.jgit.lib.TreeEntry;
import org.spearce.jgit.lib.GitIndex.Entry;
@@ -70,8 +71,10 @@ public void run(IAction act) {
}
Repository[] repos = getRepositoriesFor(getProjectsForSelectedResources());
+ Repository repository = null;
amendAllowed = repos.length == 1;
for (Repository repo : repos) {
+ repository = repo;
if (!repo.getRepositoryState().canCommit()) {
MessageDialog.openError(getTargetPart().getSite().getShell(),
"Cannot commit now", "Repository state:"
@@ -95,12 +98,26 @@ public void run(IAction act) {
}
}
+ assert repository != null;
+ RepositoryConfig repositoryConfig = repository.getConfig();
+ String author = null;
+ String username = repositoryConfig.getAuthorName();
+ if (username != null && username.length() != 0) {
+ author = username;
+ String email = repositoryConfig.getAuthorEmail();
+ if (email != null && email.length() != 0) {
+ author = author + " <" + email + ">";
+ }
+ }
+
loadPreviousCommit();
CommitDialog commitDialog = new CommitDialog(getTargetPart().getSite().getShell());
commitDialog.setAmending(amending);
commitDialog.setAmendAllowed(amendAllowed);
commitDialog.setFileList(files);
+ commitDialog.setAuthor(author);
+
if (previousCommit != null)
commitDialog.setPreviousCommitMessage(previousCommit.getMessage());
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 34ce04a..5c14e89 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
@@ -42,6 +42,8 @@
import java.io.File;
import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.LinkedList;
@@ -116,4 +118,65 @@ public void test006_readCaseInsensitive() throws IOException {
assertEquals(true, repositoryConfig.getBoolean("foo", null, "bar", false));
assertEquals("", repositoryConfig.getString("foo", null, "bar"));
}
+
+ public void test007_readUserInfos() throws IOException {
+ String hostname;
+ try {
+ InetAddress localMachine = InetAddress.getLocalHost();
+ hostname = localMachine.getCanonicalHostName();
+ } catch (UnknownHostException e) {
+ hostname = "localhost";
+ }
+
+ final File globalConfig = writeTrashFile("global.config", "");
+ final File localConfig = writeTrashFile("local.config", "");
+ System.clearProperty(Constants.OS_USER_NAME_KEY);
+
+ RepositoryConfig globalRepositoryConfig = new RepositoryConfig(null, globalConfig);
+ RepositoryConfig localRepositoryConfig = new RepositoryConfig(globalRepositoryConfig, localConfig);
+ fakeSystemReader.values.clear();
+
+ String authorName;
+ String authorEmail;
+
+ // no values defined nowhere
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals(Constants.UNKNOWN_USER_DEFAULT, authorName);
+ assertEquals(Constants.UNKNOWN_USER_DEFAULT + "@" + hostname, authorEmail);
+
+ // the system user name is defined
+ fakeSystemReader.values.put(Constants.OS_USER_NAME_KEY, "os user name");
+ authorName = localRepositoryConfig.getAuthorName();
+ assertEquals("os user name", authorName);
+
+ if (hostname != null && hostname.length() != 0) {
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("os user name@" + hostname, authorEmail);
+ }
+
+ // the git environment variables are defined
+ fakeSystemReader.values.put(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
+ fakeSystemReader.values.put(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("git author name", authorName);
+ assertEquals("author@email", authorEmail);
+
+ // the values are defined in the global configuration
+ globalRepositoryConfig.setString("user", null, "name", "global username");
+ globalRepositoryConfig.setString("user", null, "email", "author@globalemail");
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("global username", authorName);
+ assertEquals("author@globalemail", authorEmail);
+
+ // the values are defined in the local configuration
+ localRepositoryConfig.setString("user", null, "name", "local username");
+ localRepositoryConfig.setString("user", null, "email", "author@localemail");
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("local username", authorName);
+ assertEquals("author@localemail", authorEmail);
+ }
}
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
index 20348f1..6391cb4 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
@@ -46,11 +46,14 @@
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import junit.framework.TestCase;
import org.spearce.jgit.util.JGitTestUtil;
+import org.spearce.jgit.util.SystemReader;
/**
* Base class for most JGit unit tests.
@@ -84,6 +87,33 @@
protected boolean packedGitMMAP;
+ protected static class FakeSystemReader implements SystemReader {
+ Map<String, String> values = new HashMap<String, String>();
+ public String getenv(String variable) {
+ return values.get(variable);
+ }
+ public String getProperty(String key) {
+ return values.get(key);
+ }
+ }
+
+ /**
+ * Simulates the reading of system variables and properties.
+ * Unit test can control the returned values by manipulating
+ * {@link FakeSystemReader#values}.
+ */
+ protected static FakeSystemReader fakeSystemReader;
+
+ static {
+ fakeSystemReader = new FakeSystemReader();
+ RepositoryConfig.setSystemReader(fakeSystemReader);
+ fakeSystemReader.values.put(Constants.OS_USER_NAME_KEY, Constants.OS_USER_NAME_KEY);
+ fakeSystemReader.values.put(Constants.GIT_AUTHOR_NAME_KEY, Constants.GIT_AUTHOR_NAME_KEY);
+ fakeSystemReader.values.put(Constants.GIT_AUTHOR_EMAIL_KEY, Constants.GIT_AUTHOR_EMAIL_KEY);
+ fakeSystemReader.values.put(Constants.GIT_COMMITTER_NAME_KEY, Constants.GIT_COMMITTER_NAME_KEY);
+ fakeSystemReader.values.put(Constants.GIT_COMMITTER_EMAIL_KEY, Constants.GIT_COMMITTER_EMAIL_KEY);
+ }
+
/**
* Configure JGit before setting up test repositories.
*/
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
index 8f093d6..b0a5b22 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
@@ -228,6 +228,24 @@
/** Packed refs file */
public static final String PACKED_REFS = "packed-refs";
+ /** The environment variable that contains the system user name */
+ public static final String OS_USER_NAME_KEY = "user.name";
+
+ /** The environment variable that contains the author's name */
+ public static final String GIT_AUTHOR_NAME_KEY = "GIT_AUTHOR_NAME";
+
+ /** The environment variable that contains the author's email */
+ public static final String GIT_AUTHOR_EMAIL_KEY = "GIT_AUTHOR_EMAIL";
+
+ /** The environment variable that contains the commiter's name */
+ public static final String GIT_COMMITTER_NAME_KEY = "GIT_COMMITTER_NAME";
+
+ /** The environment variable that contains the commiter's email */
+ public static final String GIT_COMMITTER_EMAIL_KEY = "GIT_COMMITTER_EMAIL";
+
+ /** Default value for the user name if no other information is available */
+ public static final String UNKNOWN_USER_DEFAULT = "unknown-user";
+
/**
* Create a new digest function for objects.
*
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 7df90cd..821d8be 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
@@ -50,6 +50,8 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -60,6 +62,7 @@
import java.util.Set;
import org.spearce.jgit.util.FS;
+import org.spearce.jgit.util.SystemReader;
/**
* An object representing the Git config file.
@@ -98,8 +101,20 @@ public static RepositoryConfig openUserConfig() {
private Map<String, Object> byName;
+ private static String hostname;
+
private static final String MAGIC_EMPTY_VALUE = "%%magic%%empty%%";
+ // default system reader gets the value from the system
+ private static SystemReader systemReader = new SystemReader() {
+ public String getenv(String variable) {
+ return System.getenv(variable);
+ }
+ public String getProperty(String key) {
+ return System.getProperty(key);
+ }
+ };
+
RepositoryConfig(final Repository repo) {
this(openUserConfig(), FS.resolve(repo.getDirectory(), "config"));
}
@@ -308,6 +323,83 @@ public String getString(final String section, String subsection, final String na
return result;
}
+ /**
+ * @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 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 getUsernameInternal(Constants.GIT_COMMITTER_NAME_KEY);
+ }
+
+ private String getUsernameInternal(String gitVariableKey) {
+ // 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 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 getUserEmailInternal(Constants.GIT_AUTHOR_EMAIL_KEY);
+ }
+
+ /**
+ * @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 getUserEmailInternal(Constants.GIT_COMMITTER_EMAIL_KEY);
+ }
+
+ private String getUserEmailInternal(String gitVariableKey) {
+ // 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 + "@" + getHostname();
+ }
+
+ return email;
+ }
+
private String getRawString(final String section, final String subsection,
final String name) {
final Object o = getRawEntry(section, subsection, name);
@@ -957,4 +1049,32 @@ private static boolean eq(final String a, final String b) {
return a.equalsIgnoreCase(b);
}
}
+
+ /**
+ * Gets the hostname of the local host.
+ * If no hostname can be found, the hostname is set to the default value "localhost".
+ * @return the canonical hostname
+ */
+ private static String getHostname() {
+ if (hostname == null) {
+ try {
+ InetAddress localMachine = InetAddress.getLocalHost();
+ hostname = localMachine.getCanonicalHostName();
+ } catch (UnknownHostException e) {
+ // we do nothing
+ hostname = "localhost";
+ }
+ assert hostname != null;
+ }
+ return hostname;
+ }
+
+ /**
+ * Overrides the default system reader by a custom one.
+ * @param newSystemReader new system reader
+ */
+ public static void setSystemReader(SystemReader newSystemReader) {
+ systemReader = newSystemReader;
+ }
+
}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
new file mode 100644
index 0000000..4a9715f
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com>
+ * Copyright (C) 2009, Shawn O. Pearce <spearce@spearce.org>
+ *
+ * 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.util;
+
+/**
+ * Interface to read values from the system.
+ * <p>
+ * When writing unit tests, extending this interface with a custom class
+ * permits to simulate an access to a system variable or property.
+ * </p>
+ */
+public interface SystemReader {
+
+ /**
+ * @param variable system variable to read
+ * @return value of the system variable
+ */
+ String getenv(String variable);
+
+ /**
+ * @param key of the system property to read
+ * @return value of the system property
+ */
+ String getProperty(String key);
+
+}
--
1.6.0.4
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH JGIT] Compute the author/commiter name and email from the git configuration
@ 2009-02-05 10:44 Yann Simon
2009-02-05 15:38 ` Shawn O. Pearce
0 siblings, 1 reply; 15+ messages in thread
From: Yann Simon @ 2009-02-05 10:44 UTC (permalink / raw)
To: Robin Rosenberg, Shawn O. Pearce; +Cc: git
The author/commiter name and email are retrieved from the system configuration
and the local and global git configurations.
When the name is not available, propose one default by using the system user name.
When the email is not available, propose one default by concatenating the
user name and the host name.
The author name and email are used as default value in the author field
while committing with the GUI.
Fix issue 47 (field Author don't fill)
Signed-off-by: Yann Simon <yann.simon.fr@gmail.com>
---
Replace the last email.
This is the 3rd version of the patch.
I applied the received comments.
Changes between v2 and v3 of the patch:
- move the fakeSystemReader into RepositoryTestCase and fill it with default values.
- hange commiter -> committer.
- getHostName() never returns null. I used "localhost" when the host name is not available.
- the user name is set to a default one is no other information is available.
- change the way to form a default email using only the system property "user.name".
- add a copyright header to SystemReader. I did not know if I had to change the copyright to include my name.
I personally do not care about that.
- change SystemReader.getEnvironmentVariable to SystemReader.getenv to emulate System class.
.../egit/ui/internal/actions/CommitAction.java | 17 +++
.../org/spearce/jgit/lib/RepositoryConfigTest.java | 63 ++++++++++
.../org/spearce/jgit/lib/RepositoryTestCase.java | 32 +++++
.../src/org/spearce/jgit/lib/Constants.java | 18 +++
.../src/org/spearce/jgit/lib/RepositoryConfig.java | 120 ++++++++++++++++++++
.../src/org/spearce/jgit/util/SystemReader.java | 62 ++++++++++
6 files changed, 312 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java
index 17232d6..456745c 100644
--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java
+++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java
@@ -41,6 +41,7 @@
import org.spearce.jgit.lib.PersonIdent;
import org.spearce.jgit.lib.RefUpdate;
import org.spearce.jgit.lib.Repository;
+import org.spearce.jgit.lib.RepositoryConfig;
import org.spearce.jgit.lib.Tree;
import org.spearce.jgit.lib.TreeEntry;
import org.spearce.jgit.lib.GitIndex.Entry;
@@ -70,8 +71,10 @@ public void run(IAction act) {
}
Repository[] repos = getRepositoriesFor(getProjectsForSelectedResources());
+ Repository repository = null;
amendAllowed = repos.length == 1;
for (Repository repo : repos) {
+ repository = repo;
if (!repo.getRepositoryState().canCommit()) {
MessageDialog.openError(getTargetPart().getSite().getShell(),
"Cannot commit now", "Repository state:"
@@ -95,12 +98,26 @@ public void run(IAction act) {
}
}
+ assert repository != null;
+ RepositoryConfig repositoryConfig = repository.getConfig();
+ String author = null;
+ String username = repositoryConfig.getAuthorName();
+ if (username != null && username.length() != 0) {
+ author = username;
+ String email = repositoryConfig.getAuthorEmail();
+ if (email != null && email.length() != 0) {
+ author = author + " <" + email + ">";
+ }
+ }
+
loadPreviousCommit();
CommitDialog commitDialog = new CommitDialog(getTargetPart().getSite().getShell());
commitDialog.setAmending(amending);
commitDialog.setAmendAllowed(amendAllowed);
commitDialog.setFileList(files);
+ commitDialog.setAuthor(author);
+
if (previousCommit != null)
commitDialog.setPreviousCommitMessage(previousCommit.getMessage());
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 34ce04a..5c14e89 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
@@ -42,6 +42,8 @@
import java.io.File;
import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.LinkedList;
@@ -116,4 +118,65 @@ public void test006_readCaseInsensitive() throws IOException {
assertEquals(true, repositoryConfig.getBoolean("foo", null, "bar", false));
assertEquals("", repositoryConfig.getString("foo", null, "bar"));
}
+
+ public void test007_readUserInfos() throws IOException {
+ String hostname;
+ try {
+ InetAddress localMachine = InetAddress.getLocalHost();
+ hostname = localMachine.getCanonicalHostName();
+ } catch (UnknownHostException e) {
+ hostname = "localhost";
+ }
+
+ final File globalConfig = writeTrashFile("global.config", "");
+ final File localConfig = writeTrashFile("local.config", "");
+ System.clearProperty(Constants.OS_USER_NAME_KEY);
+
+ RepositoryConfig globalRepositoryConfig = new RepositoryConfig(null, globalConfig);
+ RepositoryConfig localRepositoryConfig = new RepositoryConfig(globalRepositoryConfig, localConfig);
+ fakeSystemReader.values.clear();
+
+ String authorName;
+ String authorEmail;
+
+ // no values defined nowhere
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals(Constants.UNKNOWN_USER_DEFAULT, authorName);
+ assertEquals(Constants.UNKNOWN_USER_DEFAULT + "@" + hostname, authorEmail);
+
+ // the system user name is defined
+ fakeSystemReader.values.put(Constants.OS_USER_NAME_KEY, "os user name");
+ authorName = localRepositoryConfig.getAuthorName();
+ assertEquals("os user name", authorName);
+
+ if (hostname != null && hostname.length() != 0) {
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("os user name@" + hostname, authorEmail);
+ }
+
+ // the git environment variables are defined
+ fakeSystemReader.values.put(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
+ fakeSystemReader.values.put(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("git author name", authorName);
+ assertEquals("author@email", authorEmail);
+
+ // the values are defined in the global configuration
+ globalRepositoryConfig.setString("user", null, "name", "global username");
+ globalRepositoryConfig.setString("user", null, "email", "author@globalemail");
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("global username", authorName);
+ assertEquals("author@globalemail", authorEmail);
+
+ // the values are defined in the local configuration
+ localRepositoryConfig.setString("user", null, "name", "local username");
+ localRepositoryConfig.setString("user", null, "email", "author@localemail");
+ authorName = localRepositoryConfig.getAuthorName();
+ authorEmail = localRepositoryConfig.getAuthorEmail();
+ assertEquals("local username", authorName);
+ assertEquals("author@localemail", authorEmail);
+ }
}
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
index 20348f1..4e56b38 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
@@ -46,11 +46,14 @@
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import junit.framework.TestCase;
import org.spearce.jgit.util.JGitTestUtil;
+import org.spearce.jgit.util.SystemReader;
/**
* Base class for most JGit unit tests.
@@ -84,6 +87,28 @@
protected boolean packedGitMMAP;
+ protected static class FakeSystemReader implements SystemReader {
+ Map<String, String> values = new HashMap<String, String>();
+ public String getenv(String variable) {
+ return values.get(variable);
+ }
+ public String getProperty(String key) {
+ return values.get(key);
+ }
+ }
+
+ /**
+ * Simulates the reading of system variables and properties.
+ * Unit test can control the returned values by manipulating
+ * {@link FakeSystemReader#values}.
+ */
+ protected static FakeSystemReader fakeSystemReader;
+
+ static {
+ fakeSystemReader = new FakeSystemReader();
+ RepositoryConfig.setSystemReader(fakeSystemReader);
+ }
+
/**
* Configure JGit before setting up test repositories.
*/
@@ -250,6 +275,13 @@ copyFile(JGitTestUtil.getTestResourceFile(packs[k] + ".idx"), new File(packDir,
copyFile(JGitTestUtil.getTestResourceFile("packed-refs"), new File(trash_git,"packed-refs"));
db.scanForPacks();
+
+ fakeSystemReader.values.clear();
+ fakeSystemReader.values.put(Constants.OS_USER_NAME_KEY, Constants.OS_USER_NAME_KEY);
+ fakeSystemReader.values.put(Constants.GIT_AUTHOR_NAME_KEY, Constants.GIT_AUTHOR_NAME_KEY);
+ fakeSystemReader.values.put(Constants.GIT_AUTHOR_EMAIL_KEY, Constants.GIT_AUTHOR_EMAIL_KEY);
+ fakeSystemReader.values.put(Constants.GIT_COMMITTER_NAME_KEY, Constants.GIT_COMMITTER_NAME_KEY);
+ fakeSystemReader.values.put(Constants.GIT_COMMITTER_EMAIL_KEY, Constants.GIT_COMMITTER_EMAIL_KEY);
}
protected void tearDown() throws Exception {
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
index 8f093d6..b0a5b22 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
@@ -228,6 +228,24 @@
/** Packed refs file */
public static final String PACKED_REFS = "packed-refs";
+ /** The environment variable that contains the system user name */
+ public static final String OS_USER_NAME_KEY = "user.name";
+
+ /** The environment variable that contains the author's name */
+ public static final String GIT_AUTHOR_NAME_KEY = "GIT_AUTHOR_NAME";
+
+ /** The environment variable that contains the author's email */
+ public static final String GIT_AUTHOR_EMAIL_KEY = "GIT_AUTHOR_EMAIL";
+
+ /** The environment variable that contains the commiter's name */
+ public static final String GIT_COMMITTER_NAME_KEY = "GIT_COMMITTER_NAME";
+
+ /** The environment variable that contains the commiter's email */
+ public static final String GIT_COMMITTER_EMAIL_KEY = "GIT_COMMITTER_EMAIL";
+
+ /** Default value for the user name if no other information is available */
+ public static final String UNKNOWN_USER_DEFAULT = "unknown-user";
+
/**
* Create a new digest function for objects.
*
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 7df90cd..821d8be 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java
@@ -50,6 +50,8 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -60,6 +62,7 @@
import java.util.Set;
import org.spearce.jgit.util.FS;
+import org.spearce.jgit.util.SystemReader;
/**
* An object representing the Git config file.
@@ -98,8 +101,20 @@ public static RepositoryConfig openUserConfig() {
private Map<String, Object> byName;
+ private static String hostname;
+
private static final String MAGIC_EMPTY_VALUE = "%%magic%%empty%%";
+ // default system reader gets the value from the system
+ private static SystemReader systemReader = new SystemReader() {
+ public String getenv(String variable) {
+ return System.getenv(variable);
+ }
+ public String getProperty(String key) {
+ return System.getProperty(key);
+ }
+ };
+
RepositoryConfig(final Repository repo) {
this(openUserConfig(), FS.resolve(repo.getDirectory(), "config"));
}
@@ -308,6 +323,83 @@ public String getString(final String section, String subsection, final String na
return result;
}
+ /**
+ * @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 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 getUsernameInternal(Constants.GIT_COMMITTER_NAME_KEY);
+ }
+
+ private String getUsernameInternal(String gitVariableKey) {
+ // 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 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 getUserEmailInternal(Constants.GIT_AUTHOR_EMAIL_KEY);
+ }
+
+ /**
+ * @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 getUserEmailInternal(Constants.GIT_COMMITTER_EMAIL_KEY);
+ }
+
+ private String getUserEmailInternal(String gitVariableKey) {
+ // 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 + "@" + getHostname();
+ }
+
+ return email;
+ }
+
private String getRawString(final String section, final String subsection,
final String name) {
final Object o = getRawEntry(section, subsection, name);
@@ -957,4 +1049,32 @@ private static boolean eq(final String a, final String b) {
return a.equalsIgnoreCase(b);
}
}
+
+ /**
+ * Gets the hostname of the local host.
+ * If no hostname can be found, the hostname is set to the default value "localhost".
+ * @return the canonical hostname
+ */
+ private static String getHostname() {
+ if (hostname == null) {
+ try {
+ InetAddress localMachine = InetAddress.getLocalHost();
+ hostname = localMachine.getCanonicalHostName();
+ } catch (UnknownHostException e) {
+ // we do nothing
+ hostname = "localhost";
+ }
+ assert hostname != null;
+ }
+ return hostname;
+ }
+
+ /**
+ * Overrides the default system reader by a custom one.
+ * @param newSystemReader new system reader
+ */
+ public static void setSystemReader(SystemReader newSystemReader) {
+ systemReader = newSystemReader;
+ }
+
}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
new file mode 100644
index 0000000..4a9715f
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com>
+ * Copyright (C) 2009, Shawn O. Pearce <spearce@spearce.org>
+ *
+ * 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.util;
+
+/**
+ * Interface to read values from the system.
+ * <p>
+ * When writing unit tests, extending this interface with a custom class
+ * permits to simulate an access to a system variable or property.
+ * </p>
+ */
+public interface SystemReader {
+
+ /**
+ * @param variable system variable to read
+ * @return value of the system variable
+ */
+ String getenv(String variable);
+
+ /**
+ * @param key of the system property to read
+ * @return value of the system property
+ */
+ String getProperty(String key);
+
+}
--
1.6.0.4
^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration
2009-02-05 10:44 Yann Simon
@ 2009-02-05 15:38 ` Shawn O. Pearce
0 siblings, 0 replies; 15+ messages in thread
From: Shawn O. Pearce @ 2009-02-05 15:38 UTC (permalink / raw)
To: Yann Simon; +Cc: Robin Rosenberg, git
Yann Simon <yann.simon.fr@gmail.com> wrote:
> The author/commiter name and email are retrieved from the system configuration
> and the local and global git configurations.
>
> When the name is not available, propose one default by using the system user name.
>
> When the email is not available, propose one default by concatenating the
> user name and the host name.
>
> The author name and email are used as default value in the author field
> while committing with the GUI.
>
> Fix issue 47 (field Author don't fill)
>
> Signed-off-by: Yann Simon <yann.simon.fr@gmail.com>
Applied, thanks.
--
Shawn.
^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2009-02-05 15:40 UTC | newest]
Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-02-04 12:28 [PATCH JGIT] Compute the author/commiter name and email from the git configuration Yann Simon
2009-02-04 17:36 ` Shawn O. Pearce
-- strict thread matches above, loose matches on Subject: below --
2009-02-05 10:44 Yann Simon
2009-02-05 15:38 ` Shawn O. Pearce
2009-02-05 9:27 Yann Simon
2009-02-03 21:13 Yann Simon
2009-02-03 23:13 ` Shawn O. Pearce
2009-02-03 23:20 ` Johannes Schindelin
2009-02-04 0:55 ` Robin Rosenberg
2009-02-04 1:01 ` Shawn O. Pearce
2009-02-04 0:59 ` Shawn O. Pearce
2009-02-04 1:04 ` Robin Rosenberg
2009-02-04 8:14 ` Yann Simon
2009-02-04 8:42 ` Yann Simon
2009-02-04 8:08 ` Yann Simon
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).