{"thread":{"id":"17538","subject":"[PATCH JGIT] Compute the author/commiter name and email from the git configuration","startedAt":"2009-02-03T21:13:14Z","lastAt":"2009-02-04T08:42:09Z","messageCount":10,"participants":["Yann Simon","Shawn O. Pearce","Johannes Schindelin","Robin Rosenberg"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"103025","messageId":"1233695594.8042.6.camel@localhost","threadId":"17538","inReplyTo":null,"subject":"[PATCH JGIT] Compute the author/commiter name and email from the git configuration","fromName":"Yann Simon","fromEmail":"yann.simon.fr@gmail.com","sentAt":"2009-02-03T21:13:14Z","receivedAt":"2009-02-03T21:13:14Z","isPatch":true,"sender":{"key":"yann.simon.fr@gmail.com","avatar":"https://gravatar.com/avatar/2d926895d27ac988c5c8e591887e5a6a4c7036390403c74fce92519119b887a0?d=mp&s=160"},"body":"The author/commiter name and email are retrieved from the system configuration\nand the local and global git configurations.\n\nWhen the name is not available, propose one default by using the system user name.\n\nWhen the email is not available, propose one default by concatenate the\nuser name and the host name.\n\nThe author name and email are used as default value in the author field\nwhile committing with the GUI.\n\nFix issue 47 (field Author don't fill)\n\nSigned-off-by: Yann Simon <yann.simon.fr@gmail.com>\n---\n .../egit/ui/internal/actions/CommitAction.java     |   17 ++++\n .../org/spearce/jgit/lib/RepositoryConfigTest.java |   56 +++++++++++\n .../src/org/spearce/jgit/lib/Constants.java        |   16 +++\n .../src/org/spearce/jgit/lib/RepositoryConfig.java |   97 ++++++++++++++++++++\n 4 files changed, 186 insertions(+), 0 deletions(-)\n\ndiff --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\nindex 17232d6..456745c 100644\n--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java\n+++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java\n@@ -41,6 +41,7 @@\n import org.spearce.jgit.lib.PersonIdent;\n import org.spearce.jgit.lib.RefUpdate;\n import org.spearce.jgit.lib.Repository;\n+import org.spearce.jgit.lib.RepositoryConfig;\n import org.spearce.jgit.lib.Tree;\n import org.spearce.jgit.lib.TreeEntry;\n import org.spearce.jgit.lib.GitIndex.Entry;\n@@ -70,8 +71,10 @@ public void run(IAction act) {\n \t\t}\n \n \t\tRepository[] repos = getRepositoriesFor(getProjectsForSelectedResources());\n+\t\tRepository repository = null;\n \t\tamendAllowed = repos.length == 1;\n \t\tfor (Repository repo : repos) {\n+\t\t\trepository = repo;\n \t\t\tif (!repo.getRepositoryState().canCommit()) {\n \t\t\t\tMessageDialog.openError(getTargetPart().getSite().getShell(),\n \t\t\t\t\t\"Cannot commit now\", \"Repository state:\"\n@@ -95,12 +98,26 @@ public void run(IAction act) {\n \t\t\t}\n \t\t}\n \n+\t\tassert repository != null;\n+\t\tRepositoryConfig repositoryConfig = repository.getConfig();\n+\t\tString author = null;\n+\t\tString username = repositoryConfig.getAuthorName();\n+\t\tif (username != null && username.length() != 0) {\n+\t\t\tauthor = username;\n+\t\t\tString email = repositoryConfig.getAuthorEmail();\n+\t\t\tif (email != null && email.length() != 0) {\n+\t\t\t\tauthor = author + \" <\" + email + \">\";\n+\t\t\t}\n+\t\t}\n+\n \t\tloadPreviousCommit();\n \n \t\tCommitDialog commitDialog = new CommitDialog(getTargetPart().getSite().getShell());\n \t\tcommitDialog.setAmending(amending);\n \t\tcommitDialog.setAmendAllowed(amendAllowed);\n \t\tcommitDialog.setFileList(files);\n+\t\tcommitDialog.setAuthor(author);\n+\n \t\tif (previousCommit != null)\n \t\t\tcommitDialog.setPreviousCommitMessage(previousCommit.getMessage());\n \ndiff --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\nindex 34ce04a..4f53ca0 100644\n--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java\n+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java\n@@ -42,6 +42,8 @@\n \n import java.io.File;\n import java.io.IOException;\n+import java.net.InetAddress;\n+import java.net.UnknownHostException;\n import java.util.Arrays;\n import java.util.LinkedList;\n \n@@ -116,4 +118,58 @@ public void test006_readCaseInsensitive() throws IOException {\n \t\tassertEquals(true, repositoryConfig.getBoolean(\"foo\", null, \"bar\", false));\n \t\tassertEquals(\"\", repositoryConfig.getString(\"foo\", null, \"bar\"));\n \t}\n+\n+\tpublic void test007_readUserInfos() throws IOException {\n+\t\tfinal File globalConfig = writeTrashFile(\"global.config\", \"\");\n+\t\tfinal File localConfig = writeTrashFile(\"global.config\", \"\");\n+\t\tSystem.clearProperty(Constants.OS_USER_NAME_KEY);\n+\t\tSystem.clearProperty(Constants.GIT_AUTHOR_NAME_KEY);\n+\t\tSystem.clearProperty(Constants.GIT_AUTHOR_EMAIL_KEY);\n+\n+\t\tRepositoryConfig globalRepositoryConfig = new RepositoryConfig(null, globalConfig);\n+\t\tRepositoryConfig localRepositoryConfig = new RepositoryConfig(globalRepositoryConfig, localConfig);\n+\n+\t\tString authorName;\n+\t\tString authorEmail;\n+\n+\t\tauthorName = localRepositoryConfig.getAuthorName();\n+\t\tauthorEmail = localRepositoryConfig.getAuthorEmail();\n+\t\tassertNull(authorName);\n+\t\tassertNull(authorEmail);\n+\n+\t\tSystem.setProperty(Constants.OS_USER_NAME_KEY, \"os user name\");\n+\t\tauthorName = localRepositoryConfig.getAuthorName();\n+\t\tassertEquals(\"os user name\", authorName);\n+\t\tInetAddress localMachine;\n+\t\ttry {\n+\t\t\tlocalMachine = InetAddress.getLocalHost();\n+\t\t\tString hostname = localMachine.getHostName();\n+\t\t\tauthorEmail = localRepositoryConfig.getAuthorEmail();\n+\t\t\tassertEquals(\"os user name@\" + hostname, authorEmail);\n+\t\t} catch (UnknownHostException e) {\n+\t\t\t// we do nothing\n+\t\t}\n+\n+\n+\t\tSystem.setProperty(Constants.GIT_AUTHOR_NAME_KEY, \"git author name\");\n+\t\tSystem.setProperty(Constants.GIT_AUTHOR_EMAIL_KEY, \"author@email\");\n+\t\tauthorName = localRepositoryConfig.getAuthorName();\n+\t\tauthorEmail = localRepositoryConfig.getAuthorEmail();\n+\t\tassertEquals(\"git author name\", authorName);\n+\t\tassertEquals(\"author@email\", authorEmail);\n+\n+\t\tglobalRepositoryConfig.setString(\"user\", null, \"name\", \"global username\");\n+\t\tglobalRepositoryConfig.setString(\"user\", null, \"email\", \"author@globalemail\");\n+\t\tauthorName = localRepositoryConfig.getAuthorName();\n+\t\tauthorEmail = localRepositoryConfig.getAuthorEmail();\n+\t\tassertEquals(\"global username\", authorName);\n+\t\tassertEquals(\"author@globalemail\", authorEmail);\n+\n+\t\tlocalRepositoryConfig.setString(\"user\", null, \"name\", \"local username\");\n+\t\tlocalRepositoryConfig.setString(\"user\", null, \"email\", \"author@localemail\");\n+\t\tauthorName = localRepositoryConfig.getAuthorName();\n+\t\tauthorEmail = localRepositoryConfig.getAuthorEmail();\n+\t\tassertEquals(\"local username\", authorName);\n+\t\tassertEquals(\"author@localemail\", authorEmail);\n+\t}\n }\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java\nindex 8f093d6..372fba5 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java\n@@ -228,6 +228,22 @@\n \t/** Packed refs file */\n \tpublic static final String PACKED_REFS = \"packed-refs\";\n \n+\t/** The environment variable that contains the system user name */\n+\tpublic static final String OS_USER_NAME_KEY = \"user.name\";\n+\n+\t/** The environment variable that contains the author's name */\n+\tpublic static final String GIT_AUTHOR_NAME_KEY = \"GIT_AUTHOR_NAME\";\n+\n+\t/** The environment variable that contains the author's email */\n+\tpublic static final String GIT_AUTHOR_EMAIL_KEY = \"GIT_AUTHOR_EMAIL\";\n+\n+\t/** The environment variable that contains the commiter's name */\n+\tpublic static final String GIT_COMMITER_NAME_KEY = \"GIT_COMMITER_NAME\";\n+\n+\t/** The environment variable that contains the commiter's email */\n+\tpublic static final String GIT_COMMITER_EMAIL_KEY = \"GIT_COMMITER_EMAIL\";\n+\n+\n \t/**\n \t * Create a new digest function for objects.\n \t * \ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java\nindex 7df90cd..5821f83 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java\n@@ -50,6 +50,8 @@\n import java.io.InputStreamReader;\n import java.io.OutputStreamWriter;\n import java.io.PrintWriter;\n+import java.net.InetAddress;\n+import java.net.UnknownHostException;\n import java.util.ArrayList;\n import java.util.Collections;\n import java.util.HashMap;\n@@ -98,6 +100,8 @@ public static RepositoryConfig openUserConfig() {\n \n \tprivate Map<String, Object> byName;\n \n+\tprivate String hostname;\n+\n \tprivate static final String MAGIC_EMPTY_VALUE = \"%%magic%%empty%%\";\n \n \tRepositoryConfig(final Repository repo) {\n@@ -308,6 +312,83 @@ public String getString(final String section, String subsection, final String na\n \t\treturn result;\n \t}\n \n+\t/**\n+\t * @return the author name as defined in the git variables\n+\t *         and configurations. If no name could be found, try\n+\t *         to use the system user name instead.\n+\t */\n+\tpublic String getAuthorName() {\n+\t\treturn getUsernameInternal(Constants.GIT_AUTHOR_NAME_KEY);\n+\t}\n+\n+\t/**\n+\t * @return the commiter name as defined in the git variables\n+\t *         and configurations. If no name could be found, try\n+\t *         to use the system user name instead.\n+\t */\n+\tpublic String getCommiterName() {\n+\t\treturn getUsernameInternal(Constants.GIT_COMMITER_NAME_KEY);\n+\t}\n+\n+\tprivate String getUsernameInternal(String gitVariableKey) {\n+\t\t// try to get the user name from the local and global configurations.\n+\t\tString username = getString(\"user\", null, \"name\");\n+\n+\t\tif (username == null) {\n+\t\t\t// try to get the user name for the system property GIT_XXX_NAME \n+\t\t\tusername = System.getProperty(gitVariableKey);\n+\t\t}\n+\t\tif (username == null) {\n+\t\t\t// get the system user name\n+\t\t\tusername = System.getProperty(Constants.OS_USER_NAME_KEY);\n+\t\t}\n+\n+\t\treturn username;\n+\t}\n+\n+\t/**\n+\t * @return the author email as defined in git variables and\n+\t *         configurations. If no email could be found, try to\n+\t *         propose one default with the author name and the\n+\t *         host name.\n+\t */\n+\tpublic String getAuthorEmail() {\n+\t\treturn getUserEmailInternal(Constants.GIT_AUTHOR_EMAIL_KEY, true);\n+\t}\n+\n+\t/**\n+\t * @return the commiter email as defined in git variables and\n+\t *         configurations. If no email could be found, try to\n+\t *         propose one default with the author name and the\n+\t *         host name.\n+\t */\n+\tpublic String getCommiterEmail() {\n+\t\treturn getUserEmailInternal(Constants.GIT_COMMITER_EMAIL_KEY, false);\n+\t}\n+\n+\tprivate String getUserEmailInternal(String gitVariableKey, boolean author) {\n+\t\t// try to get the email from the local and global configs.\n+\t\tString email = getString(\"user\", null, \"email\");\n+\n+\t\tif (email == null) {\n+\t\t\t// try to get the email for the system property GIT_XXX_EMAIL \n+\t\t\temail = System.getProperty(gitVariableKey);\n+\t\t}\n+\n+\t\tif (email == null) {\n+\t\t\t// try to construct an email\n+\t\t\tString userName = author ? getAuthorName() : getCommiterName();\n+\t\t\tif (userName != null && userName.length() != 0) {\n+\t\t\t\tString hostnameTmp = getHostname();\n+\t\t\t\tif (hostnameTmp != null && hostnameTmp.length() != 0) {\n+\t\t\t\t\temail = userName + \"@\" + hostnameTmp;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t}\n+\n+\t\treturn email;\n+\t}\n+\n \tprivate String getRawString(final String section, final String subsection,\n \t\t\tfinal String name) {\n \t\tfinal Object o = getRawEntry(section, subsection, name);\n@@ -957,4 +1038,20 @@ private static boolean eq(final String a, final String b) {\n \t\t\treturn a.equalsIgnoreCase(b);\n \t\t}\n \t}\n+\n+\t/**\n+\t * @return the hostname\n+\t */\n+\tpublic String getHostname() {\n+\t\tif (hostname == null) {\n+\t\t\tInetAddress localMachine;\n+\t\t\ttry {\n+\t\t\t\tlocalMachine = InetAddress.getLocalHost();\n+\t\t\t\thostname = localMachine.getHostName();\n+\t\t\t} catch (UnknownHostException e) {\n+\t\t\t\t// we do nothing\n+\t\t\t}\n+\t\t}\n+\t\treturn hostname;\n+\t}\n }\n-- \n1.6.0.6\n"},{"id":"103037","messageId":"20090203231357.GZ26880@spearce.org","threadId":"17538","inReplyTo":"1233695594.8042.6.camel@localhost","subject":"Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-02-03T23:13:57Z","receivedAt":"2009-02-03T23:13:57Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Yann Simon <yann.simon.fr@gmail.com> wrote:\n> index 7df90cd..5821f83 100644\n> --- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java\n> +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java\n> @@ -50,6 +50,8 @@\n>  import java.io.InputStreamReader;\n>  import java.io.OutputStreamWriter;\n>  import java.io.PrintWriter;\n> +import java.net.InetAddress;\n> +import java.net.UnknownHostException;\n>  import java.util.ArrayList;\n>  import java.util.Collections;\n>  import java.util.HashMap;\n> @@ -98,6 +100,8 @@ public static RepositoryConfig openUserConfig() {\n>  \n>  \tprivate Map<String, Object> byName;\n>  \n> +\tprivate String hostname;\n> +\n>  \tprivate static final String MAGIC_EMPTY_VALUE = \"%%magic%%empty%%\";\n>  \n>  \tRepositoryConfig(final Repository repo) {\n> @@ -308,6 +312,83 @@ public String getString(final String section, String subsection, final String na\n>  \t\treturn result;\n>  \t}\n>  \n> +\t/**\n> +\t * @return the author name as defined in the git variables\n> +\t *         and configurations. If no name could be found, try\n> +\t *         to use the system user name instead.\n> +\t */\n> +\tpublic String getAuthorName() {\n> +\t\treturn getUsernameInternal(Constants.GIT_AUTHOR_NAME_KEY);\n> +\t}\n> +\n> +\t/**\n> +\t * @return the commiter name as defined in the git variables\n> +\t *         and configurations. If no name could be found, try\n> +\t *         to use the system user name instead.\n> +\t */\n> +\tpublic String getCommiterName() {\n> +\t\treturn getUsernameInternal(Constants.GIT_COMMITER_NAME_KEY);\n> +\t}\n> +\n> +\tprivate String getUsernameInternal(String gitVariableKey) {\n> +\t\t// try to get the user name from the local and global configurations.\n> +\t\tString username = getString(\"user\", null, \"name\");\n> +\n> +\t\tif (username == null) {\n> +\t\t\t// try to get the user name for the system property GIT_XXX_NAME \n> +\t\t\tusername = System.getProperty(gitVariableKey);\n\nShouldn't that be System.getenv()?\n\n> +\tprivate String getUserEmailInternal(String gitVariableKey, boolean author) {\n> +\t\t// try to get the email from the local and global configs.\n> +\t\tString email = getString(\"user\", null, \"email\");\n> +\n> +\t\tif (email == null) {\n> +\t\t\t// try to get the email for the system property GIT_XXX_EMAIL \n> +\t\t\temail = System.getProperty(gitVariableKey);\n\nAgain, System.getenv()?\n\n> +\tpublic String getHostname() {\n> +\t\tif (hostname == null) {\n> +\t\t\tInetAddress localMachine;\n> +\t\t\ttry {\n> +\t\t\t\tlocalMachine = InetAddress.getLocalHost();\n> +\t\t\t\thostname = localMachine.getHostName();\n> +\t\t\t} catch (UnknownHostException e) {\n> +\t\t\t\t// we do nothing\n> +\t\t\t}\n> +\t\t}\n> +\t\treturn hostname;\n\nDo we want getHostName() or getCanonicalHostName() here?\n\nI think we'd want getCanonicalHostName().\n\nShould we be caching this at the RepositoryConfig level, or at the\nwhole JVM level (in a static).  If the application is long-running\nits likely to keep the same RepositoryConfig instance around for\nthe life of that JVM, so we'd only make this request once.  Thus any\nchange in hostname while the application is running would probably\nnot take effect until after restart.  But any long running app is\nalso likely to access more than one Repository, and thus more than\none RepositoryConfig, so they should at least use consistent names,\neven if the underlying hostname has changed.\n\nIMHO, just cache it in a static on first demand.\n\n-- \nShawn.\n"},{"id":"103038","messageId":"alpine.DEB.1.00.0902040019030.9822@pacific.mpi-cbg.de","threadId":"17538","inReplyTo":"20090203231357.GZ26880@spearce.org","subject":"Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2009-02-03T23:20:03Z","receivedAt":"2009-02-03T23:20:03Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Tue, 3 Feb 2009, Shawn O. Pearce wrote:\n\n> Yann Simon <yann.simon.fr@gmail.com> wrote:\n>\n> > @@ -308,6 +312,83 @@ public String getString(final String section, String subsection, final String na\n> >  \t\treturn result;\n> >  \t}\n> >  \n> > +\t/**\n> > +\t * @return the author name as defined in the git variables\n> > +\t *         and configurations. If no name could be found, try\n> > +\t *         to use the system user name instead.\n> > +\t */\n> > +\tpublic String getAuthorName() {\n> > +\t\treturn getUsernameInternal(Constants.GIT_AUTHOR_NAME_KEY);\n> > +\t}\n> > +\n> > +\t/**\n> > +\t * @return the commiter name as defined in the git variables\n> > +\t *         and configurations. If no name could be found, try\n> > +\t *         to use the system user name instead.\n> > +\t */\n> > +\tpublic String getCommiterName() {\n> > +\t\treturn getUsernameInternal(Constants.GIT_COMMITER_NAME_KEY);\n> > +\t}\n> > +\n> > +\tprivate String getUsernameInternal(String gitVariableKey) {\n> > +\t\t// try to get the user name from the local and global configurations.\n> > +\t\tString username = getString(\"user\", null, \"name\");\n> > +\n> > +\t\tif (username == null) {\n> > +\t\t\t// try to get the user name for the system property GIT_XXX_NAME \n> > +\t\t\tusername = System.getProperty(gitVariableKey);\n> \n> Shouldn't that be System.getenv()?\n\nAccording to\n\nhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv(java.lang.String)\n\ngetenv() is deprecated.  However, in later editions (Java5 and later, to \nbe precise), that deprecation seems to be lifted...\n\nCiao,\nDscho\n"},{"id":"103054","messageId":"200902040155.15206.robin.rosenberg.lists@dewire.com","threadId":"17538","inReplyTo":"alpine.DEB.1.00.0902040019030.9822@pacific.mpi-cbg.de","subject":"Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration","fromName":"Robin Rosenberg","fromEmail":"robin.rosenberg.lists@dewire.com","sentAt":"2009-02-04T00:55:14Z","receivedAt":"2009-02-04T00:55:14Z","isPatch":true,"sender":{"key":"robin.rosenberg@dewire.com","avatar":"https://avatars.githubusercontent.com/u/46357?v=4"},"body":"onsdag 04 februari 2009 00:20:03 skrev Johannes Schindelin:\n> According to\n> \n> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv(java.lang.String)\n> \n> getenv() is deprecated.  However, in later editions (Java5 and later, to \n> be precise), that deprecation seems to be lifted...\n\nIt was worse, it wasn't even implemented. You got a runtime exception back then (1.3 or 1.4).\nSo now it's fine.\n\n-- robin\n"},{"id":"103055","messageId":"20090204005907.GA23383@spearce.org","threadId":"17538","inReplyTo":"alpine.DEB.1.00.0902040019030.9822@pacific.mpi-cbg.de","subject":"Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-02-04T00:59:07Z","receivedAt":"2009-02-04T00:59:07Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:\n> On Tue, 3 Feb 2009, Shawn O. Pearce wrote:\n> > Yann Simon <yann.simon.fr@gmail.com> wrote:\n> > > +\t\t\tusername = System.getProperty(gitVariableKey);\n> > \n> > Shouldn't that be System.getenv()?\n> \n> According to\n> \n> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv(java.lang.String)\n> \n> getenv() is deprecated.  However, in later editions (Java5 and later, to \n> be precise), that deprecation seems to be lifted...\n\nYea, funny thing about that.  In 1.3 getenv() worked.  In 1.4 they\nnot only marked it deprecated, they changed the method to always\nthrow a RuntimeException.  Java developers were outraged, so in 1.5\n(aka Java 5) they added it back in as a supported method.\n\nJGit only runs on Java 5 and later.  So we're safe, we don't have\nto worry about this #@(!@(! method and the 1.4 mistake.\n\nHell, Java 6 added an executable flag to java.io.File.  This is\na rather funny concept on Windows where executablity is based on\nfile name and not a \"mode bit\", but its in the Java 6 S2SE API.\nAt least even on Windows getenv makes sense and provides data.\n\n-- \nShawn.\n"},{"id":"103056","messageId":"20090204010116.GB23383@spearce.org","threadId":"17538","inReplyTo":"200902040155.15206.robin.rosenberg.lists@dewire.com","subject":"Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-02-04T01:01:16Z","receivedAt":"2009-02-04T01:01:16Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Robin Rosenberg <robin.rosenberg.lists@dewire.com> wrote:\n> onsdag 04 februari 2009 00:20:03 skrev Johannes Schindelin:\n> > According to\n> > \n> > http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv(java.lang.String)\n> > \n> > getenv() is deprecated.  However, in later editions (Java5 and later, to \n> > be precise), that deprecation seems to be lifted...\n> \n> It was worse, it wasn't even implemented. You got a runtime exception back then (1.3 or 1.4).\n> So now it's fine.\n\nFWIW, it worked in 1.3 and returned correct data on platforms that\nhad a notion of \"environment\" (POSIX, Windows, not Mac OS 9).  It\nwas only 1.4 that threw a RuntimeException.\n\nThat was a HUGE mistake on the Sun developer's part.  IMHO,\n1.4 should have always returned NULL instead of throwing\nRuntimeException.  Most applications had ways to work around\nnot getting a particular value from the environment, e.g. if the\nend-user didn't set something.  But they weren't prepared to handle\na new class of RuntimeException coming out of a method in System.\n\n-- \nShawn.\n"},{"id":"103057","messageId":"200902040204.16102.robin.rosenberg.lists@dewire.com","threadId":"17538","inReplyTo":"20090204005907.GA23383@spearce.org","subject":"Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration","fromName":"Robin Rosenberg","fromEmail":"robin.rosenberg.lists@dewire.com","sentAt":"2009-02-04T01:04:15Z","receivedAt":"2009-02-04T01:04:15Z","isPatch":true,"sender":{"key":"robin.rosenberg@dewire.com","avatar":"https://avatars.githubusercontent.com/u/46357?v=4"},"body":"onsdag 04 februari 2009 01:59:07 skrev Shawn O. Pearce:\n> Hell, Java 6 added an executable flag to java.io.File.  This is\n> a rather funny concept on Windows where executablity is based on\n> file name and not a \"mode bit\", but its in the Java 6 S2SE API.\n> At least even on Windows getenv makes sense and provides data.\n\nThere is an execute bit in windows ACL's. Disable it for an executables (exe, dll etc and\nsee for yourself). I've hit it when trying to track an eclipse installation using git and\nsome DLL wasn't executable after a checkout.\n\n-- robin\n"},{"id":"103080","messageId":"551f769b0902040008o7e833b93ud94eeb87dc072084@mail.gmail.com","threadId":"17538","inReplyTo":"1233695594.8042.6.camel@localhost","subject":"Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration","fromName":"Yann Simon","fromEmail":"yann.simon.fr@gmail.com","sentAt":"2009-02-04T08:08:56Z","receivedAt":"2009-02-04T08:08:56Z","isPatch":true,"sender":{"key":"yann.simon.fr@gmail.com","avatar":"https://gravatar.com/avatar/2d926895d27ac988c5c8e591887e5a6a4c7036390403c74fce92519119b887a0?d=mp&s=160"},"body":"Sorry to all, there is still 2 trailing whitespaces in this patch.\nI send a wrong version..\n\nI will anyway send the patch again after dealing with the comments.\n\nYann, bad man for patches\n"},{"id":"103081","messageId":"551f769b0902040014g380d4f00nd3dbbd6322fca434@mail.gmail.com","threadId":"17538","inReplyTo":"20090203231357.GZ26880@spearce.org","subject":"Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration","fromName":"Yann Simon","fromEmail":"yann.simon.fr@gmail.com","sentAt":"2009-02-04T08:14:55Z","receivedAt":"2009-02-04T08:14:55Z","isPatch":true,"sender":{"key":"yann.simon.fr@gmail.com","avatar":"https://gravatar.com/avatar/2d926895d27ac988c5c8e591887e5a6a4c7036390403c74fce92519119b887a0?d=mp&s=160"},"body":"2009/2/4 Shawn O. Pearce <spearce@spearce.org>:\n> Yann Simon <yann.simon.fr@gmail.com> wrote:\n>> index 7df90cd..5821f83 100644\n>> --- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java\n>> +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java\n>> @@ -50,6 +50,8 @@\n>>  import java.io.InputStreamReader;\n>>  import java.io.OutputStreamWriter;\n>>  import java.io.PrintWriter;\n>> +import java.net.InetAddress;\n>> +import java.net.UnknownHostException;\n>>  import java.util.ArrayList;\n>>  import java.util.Collections;\n>>  import java.util.HashMap;\n>> @@ -98,6 +100,8 @@ public static RepositoryConfig openUserConfig() {\n>>\n>>       private Map<String, Object> byName;\n>>\n>> +     private String hostname;\n>> +\n>>       private static final String MAGIC_EMPTY_VALUE = \"%%magic%%empty%%\";\n>>\n>>       RepositoryConfig(final Repository repo) {\n>> @@ -308,6 +312,83 @@ public String getString(final String section, String subsection, final String na\n>>               return result;\n>>       }\n>>\n>> +     /**\n>> +      * @return the author name as defined in the git variables\n>> +      *         and configurations. If no name could be found, try\n>> +      *         to use the system user name instead.\n>> +      */\n>> +     public String getAuthorName() {\n>> +             return getUsernameInternal(Constants.GIT_AUTHOR_NAME_KEY);\n>> +     }\n>> +\n>> +     /**\n>> +      * @return the commiter name as defined in the git variables\n>> +      *         and configurations. If no name could be found, try\n>> +      *         to use the system user name instead.\n>> +      */\n>> +     public String getCommiterName() {\n>> +             return getUsernameInternal(Constants.GIT_COMMITER_NAME_KEY);\n>> +     }\n>> +\n>> +     private String getUsernameInternal(String gitVariableKey) {\n>> +             // try to get the user name from the local and global configurations.\n>> +             String username = getString(\"user\", null, \"name\");\n>> +\n>> +             if (username == null) {\n>> +                     // try to get the user name for the system property GIT_XXX_NAME\n>> +                     username = System.getProperty(gitVariableKey);\n>\n> Shouldn't that be System.getenv()?\n>\n>> +     private String getUserEmailInternal(String gitVariableKey, boolean author) {\n>> +             // try to get the email from the local and global configs.\n>> +             String email = getString(\"user\", null, \"email\");\n>> +\n>> +             if (email == null) {\n>> +                     // try to get the email for the system property GIT_XXX_EMAIL\n>> +                     email = System.getProperty(gitVariableKey);\n>\n> Again, System.getenv()?\n>\n>> +     public String getHostname() {\n>> +             if (hostname == null) {\n>> +                     InetAddress localMachine;\n>> +                     try {\n>> +                             localMachine = InetAddress.getLocalHost();\n>> +                             hostname = localMachine.getHostName();\n>> +                     } catch (UnknownHostException e) {\n>> +                             // we do nothing\n>> +                     }\n>> +             }\n>> +             return hostname;\n>\n> Do we want getHostName() or getCanonicalHostName() here?\n>\n> I think we'd want getCanonicalHostName().\n\nYes, indeed. I change this.\n\n>\n> Should we be caching this at the RepositoryConfig level, or at the\n> whole JVM level (in a static).  If the application is long-running\n> its likely to keep the same RepositoryConfig instance around for\n> the life of that JVM, so we'd only make this request once.  Thus any\n> change in hostname while the application is running would probably\n> not take effect until after restart.  But any long running app is\n> also likely to access more than one Repository, and thus more than\n> one RepositoryConfig, so they should at least use consistent names,\n> even if the underlying hostname has changed.\n>\n> IMHO, just cache it in a static on first demand.\n\nOK, that make sense.\nShould the call be thread-safe if it can be called from different instances?\n\nYann\n"},{"id":"103085","messageId":"551f769b0902040042j23238581ia248c949ee395ff7@mail.gmail.com","threadId":"17538","inReplyTo":"20090203231357.GZ26880@spearce.org","subject":"Re: [PATCH JGIT] Compute the author/commiter name and email from the git configuration","fromName":"Yann Simon","fromEmail":"yann.simon.fr@gmail.com","sentAt":"2009-02-04T08:42:09Z","receivedAt":"2009-02-04T08:42:09Z","isPatch":true,"sender":{"key":"yann.simon.fr@gmail.com","avatar":"https://gravatar.com/avatar/2d926895d27ac988c5c8e591887e5a6a4c7036390403c74fce92519119b887a0?d=mp&s=160"},"body":"2009/2/4 Shawn O. Pearce <spearce@spearce.org>:\n> Yann Simon <yann.simon.fr@gmail.com> wrote:\n>> index 7df90cd..5821f83 100644\n>> --- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java\n>> +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java\n>> @@ -50,6 +50,8 @@\n>>  import java.io.InputStreamReader;\n>>  import java.io.OutputStreamWriter;\n>>  import java.io.PrintWriter;\n>> +import java.net.InetAddress;\n>> +import java.net.UnknownHostException;\n>>  import java.util.ArrayList;\n>>  import java.util.Collections;\n>>  import java.util.HashMap;\n>> @@ -98,6 +100,8 @@ public static RepositoryConfig openUserConfig() {\n>>\n>>       private Map<String, Object> byName;\n>>\n>> +     private String hostname;\n>> +\n>>       private static final String MAGIC_EMPTY_VALUE = \"%%magic%%empty%%\";\n>>\n>>       RepositoryConfig(final Repository repo) {\n>> @@ -308,6 +312,83 @@ public String getString(final String section, String subsection, final String na\n>>               return result;\n>>       }\n>>\n>> +     /**\n>> +      * @return the author name as defined in the git variables\n>> +      *         and configurations. If no name could be found, try\n>> +      *         to use the system user name instead.\n>> +      */\n>> +     public String getAuthorName() {\n>> +             return getUsernameInternal(Constants.GIT_AUTHOR_NAME_KEY);\n>> +     }\n>> +\n>> +     /**\n>> +      * @return the commiter name as defined in the git variables\n>> +      *         and configurations. If no name could be found, try\n>> +      *         to use the system user name instead.\n>> +      */\n>> +     public String getCommiterName() {\n>> +             return getUsernameInternal(Constants.GIT_COMMITER_NAME_KEY);\n>> +     }\n>> +\n>> +     private String getUsernameInternal(String gitVariableKey) {\n>> +             // try to get the user name from the local and global configurations.\n>> +             String username = getString(\"user\", null, \"name\");\n>> +\n>> +             if (username == null) {\n>> +                     // try to get the user name for the system property GIT_XXX_NAME\n>> +                     username = System.getProperty(gitVariableKey);\n>\n> Shouldn't that be System.getenv()?\n>\n>> +     private String getUserEmailInternal(String gitVariableKey, boolean author) {\n>> +             // try to get the email from the local and global configs.\n>> +             String email = getString(\"user\", null, \"email\");\n>> +\n>> +             if (email == null) {\n>> +                     // try to get the email for the system property GIT_XXX_EMAIL\n>> +                     email = System.getProperty(gitVariableKey);\n>\n> Again, System.getenv()?\n\nJust a precision:\nOne of the consequences of using System.getenv() is that it does not\nlet java programs to define the value of the variable.\nSystem.getProperty(), on the other hand, offers a System.setProperty()\n\nIf we use System.getenv(), we cannot overwrite the value before.\n\nBut we maybe do not need to overwrite system variables (except for\nunit test... :) )\n\nIn that case, we can live with System.getenv().\n\nA workaround would be to use a method like:\n\tprivate static String readEnvironmentVariable(String key) {\n\t\tString result = System.getProperty(key);\n\t\tif (result == null || result.length() == 0) {\n\t\t\tresult = System.getenv(key);\n\t\t}\n\t\treturn result;\n\t}\n\nbut I really dislike it. This is much more complex and make the\nresults less predictable.\n\nI change my code to use System.getenv() unless you got other ideas.\n\nYann\n"}]}