]> source.dussan.org Git - jgit.git/commitdiff
Read user.name and email from environment first 13/42413/3
authorMatthias Sohn <matthias.sohn@sap.com>
Mon, 23 Feb 2015 12:49:24 +0000 (13:49 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Tue, 24 Feb 2015 15:39:19 +0000 (16:39 +0100)
According to [1] user name and email are taken first from the
environment variables:
GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL
GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL

In case (some of) these environment variables are not set, the
information is taken from the git configuration.

JGit doesn not yet support the environment variables GIT_AUTHOR_DATE and
GIT_COMMITTER_DATE.

[1] https://www.kernel.org/pub/software/scm/git/docs/git-commit-tree.html#_commit_information

Bug: 460586
Change-Id: I3ba582b4ae13674cf319652b5b13ebcbb96dd8ec
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/UserConfig.java

index 69926f915d36226972a7ce9d4049719d5557bbc6..db31fd34cd63aba5bcf547db43735aa07316cc63 100644 (file)
@@ -186,6 +186,9 @@ public class ConfigTest {
                assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
 
                // the values are defined in the global configuration
+               // first clear environment variables since they would override
+               // configuration files
+               mockSystemReader.clearProperties();
                userGitConfig.setString("user", null, "name", "global username");
                userGitConfig.setString("user", null, "email", "author@globalemail");
                authorName = localConfig.get(UserConfig.KEY).getAuthorName();
@@ -211,6 +214,20 @@ public class ConfigTest {
                assertEquals("author@localemail", authorEmail);
                assertFalse(localConfig.get(UserConfig.KEY).isCommitterNameImplicit());
                assertFalse(localConfig.get(UserConfig.KEY).isCommitterEmailImplicit());
+
+               // also git environment variables are defined
+               mockSystemReader.setProperty(Constants.GIT_AUTHOR_NAME_KEY,
+                               "git author name");
+               mockSystemReader.setProperty(Constants.GIT_AUTHOR_EMAIL_KEY,
+                               "author@email");
+               localConfig.setString("user", null, "name", "local username");
+               localConfig.setString("user", null, "email", "author@localemail");
+               authorName = localConfig.get(UserConfig.KEY).getAuthorName();
+               authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
+               assertEquals("git author name", authorName);
+               assertEquals("author@email", authorEmail);
+               assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
+               assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
        }
 
        @Test
index 60ac6f176db138c17cc3bcb267ed21d1807a01b2..b8d236c1da6c8cc5639f3953c3eb2357588386e3 100644 (file)
@@ -172,12 +172,13 @@ public class UserConfig {
        }
 
        private static String getNameInternal(Config rc, String envKey) {
-               // try to get the user name from the local and global configurations.
-               String username = rc.getString("user", null, "name"); //$NON-NLS-1$ //$NON-NLS-2$
+               // try to get the user name for the system property GIT_XXX_NAME
+               String username = system().getenv(envKey);
 
                if (username == null) {
-                       // try to get the user name for the system property GIT_XXX_NAME
-                       username = system().getenv(envKey);
+                       // try to get the user name from the local and global
+                       // configurations.
+                       username = rc.getString("user", null, "name"); //$NON-NLS-1$ //$NON-NLS-2$
                }
 
                return stripInvalidCharacters(username);
@@ -196,12 +197,12 @@ public class UserConfig {
        }
 
        private static String getEmailInternal(Config rc, String envKey) {
-               // try to get the email from the local and global configurations.
-               String email = rc.getString("user", null, "email"); //$NON-NLS-1$ //$NON-NLS-2$
+               // try to get the email for the system property GIT_XXX_EMAIL
+               String email = system().getenv(envKey);
 
                if (email == null) {
-                       // try to get the email for the system property GIT_XXX_EMAIL
-                       email = system().getenv(envKey);
+                       // try to get the email from the local and global configurations.
+                       email = rc.getString("user", null, "email"); //$NON-NLS-1$ //$NON-NLS-2$
                }
 
                return stripInvalidCharacters(email);