Merge branch 'SCM-765'

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
diff --git a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommand.java b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommand.java
index 98c0f98..b998638 100644
--- a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommand.java
+++ b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommand.java
@@ -33,15 +33,30 @@
 import org.eclipse.jgit.api.AddCommand;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.UserConfig;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.transport.RefSpec;
 
 import java.io.File;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 
 /**
+ * This provider uses the following strategy to discover the committer and author name/mail for a commit:
+ * <ol>
+ * <li>"user" section in .gitconfig</li>
+ * <li>"username" passed to maven execution</li>
+ * <li>default git config (system user and hostname for email)</li>
+ * </ol>
+ * the "maven-scm" config can be configured like this: <br>
+ * the default email domain to be used (will be used to create an email from the username passed to maven):<br>
+ * <code>git config --global maven-scm.maildomain mycomp.com</code> <br>
+ * you can also enforce the usage of the username for the author and committer:<br>
+ * <code>git config --global maven-scm.forceUsername true</code> <br>
+ * 
  * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
  * @author Dominik Bartholdi (imod)
  * @since 1.9
@@ -50,6 +65,13 @@
     extends AbstractCheckInCommand
     implements GitCommand
 {
+
+    protected static final String GIT_MAVEN_SECTION = "maven-scm";
+
+    protected static final String GIT_MAILDOMAIN = "maildomain";
+
+    protected static final String GIT_FORCE = "forceUsername";
+
     /**
      * {@inheritDoc}
      */
@@ -96,7 +118,12 @@
             List<ScmFile> checkedInFiles = Collections.emptyList();
             if ( doCommit )
             {
-                RevCommit commitRev = git.commit().setMessage( message ).call();
+                UserInfo author = getAuthor( repo, git );
+                UserInfo committer = getCommitter( repo, git );
+
+                RevCommit commitRev =
+                    git.commit().setMessage( message ).setAuthor( author.name, author.email ).setCommitter( committer.name,
+                                                                                                            committer.email ).call();
                 getLogger().info( "commit done: " + commitRev.getShortMessage() );
                 checkedInFiles = JGitUtils.getFilesInCommit( git.getRepository(), commitRev );
                 if ( getLogger().isDebugEnabled() )
@@ -132,4 +159,122 @@
         }
     }
 
+    private static final class UserInfo
+    {
+        final String name;
+
+        final String email;
+
+        public UserInfo( String name, String email )
+        {
+            this.name = name;
+            this.email = email;
+        }
+    }
+
+    private UserInfo getCommitter( ScmProviderRepository repo, Git git )
+    {
+        boolean forceMvnUser = git.getRepository().getConfig().getBoolean( GIT_MAVEN_SECTION, GIT_FORCE, false );
+
+        // git config
+        UserConfig user = git.getRepository().getConfig().get( UserConfig.KEY );
+        String committerName = null;
+        if ( !forceMvnUser && !user.isCommitterNameImplicit() )
+        {
+            committerName = user.getCommitterName();
+        }
+
+        // mvn parameter
+        if ( StringUtils.isBlank( committerName ) )
+        {
+            committerName = repo.getUser();
+        }
+
+        // git default
+        if ( StringUtils.isBlank( committerName ) )
+        {
+            committerName = user.getCommitterName();
+        }
+
+        // git config
+        String committerMail = null;
+        if ( !user.isCommitterEmailImplicit() )
+        {
+            committerMail = user.getCommitterEmail();
+        }
+
+        if ( StringUtils.isBlank( committerMail ) )
+        {
+            String defaultDomain = git.getRepository().getConfig().getString( GIT_MAVEN_SECTION, null, GIT_MAILDOMAIN );
+            defaultDomain = StringUtils.isNotBlank( defaultDomain ) ? defaultDomain : getHostname();
+
+            // mvn parameter (constructed with username) or git default
+            committerMail =
+                StringUtils.isNotBlank( repo.getUser() ) ? repo.getUser() + "@" + defaultDomain
+                                : user.getCommitterEmail();
+        }
+
+        return new UserInfo( committerName, committerMail );
+    }
+
+    private UserInfo getAuthor( ScmProviderRepository repo, Git git )
+    {
+        boolean forceMvnUser = git.getRepository().getConfig().getBoolean( GIT_MAVEN_SECTION, GIT_FORCE, false );
+
+        // git config
+        UserConfig user = git.getRepository().getConfig().get( UserConfig.KEY );
+        String authorName = null;
+        if ( !forceMvnUser && !user.isAuthorNameImplicit() )
+        {
+            authorName = user.getAuthorName();
+        }
+
+        // mvn parameter
+        if ( StringUtils.isBlank( authorName ) )
+        {
+            authorName = repo.getUser();
+        }
+
+        // git default
+        if ( StringUtils.isBlank( authorName ) )
+        {
+            authorName = user.getAuthorName();
+        }
+
+        // git config
+        String authorMail = null;
+        if ( !user.isAuthorEmailImplicit() )
+        {
+            authorMail = user.getAuthorEmail();
+        }
+
+        if ( StringUtils.isBlank( authorMail ) )
+        {
+            String defaultDomain = git.getRepository().getConfig().getString( GIT_MAVEN_SECTION, null, GIT_MAILDOMAIN );
+            defaultDomain = StringUtils.isNotBlank( defaultDomain ) ? defaultDomain : getHostname();
+
+            // mvn parameter (constructed with username) or git default
+            authorMail =
+                StringUtils.isNotBlank( repo.getUser() ) ? repo.getUser() + "@" + defaultDomain : user.getAuthorEmail();
+        }
+
+        return new UserInfo( authorName, authorMail );
+    }
+
+    private String getHostname()
+    {
+        String hostname;
+        try
+        {
+            InetAddress localhost = java.net.InetAddress.getLocalHost();
+            hostname = localhost.getHostName();
+        }
+        catch ( UnknownHostException e )
+        {
+            getLogger().warn( "failed to resolve hostname to create mail address, defaulting to 'maven-scm-provider-jgit'" );
+            hostname = "maven-scm-provider-jgit";
+        }
+        return hostname;
+    }
+
 }
diff --git a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/site/markdown/index.md.vm b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/site/markdown/index.md.vm
index 8734528..d95a1a6 100644
--- a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/site/markdown/index.md.vm
+++ b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/site/markdown/index.md.vm
@@ -68,6 +68,7 @@
 			
 Examples
 ____
+
 changelog
 
 	mvn org.apache.maven.plugins:maven-scm-plugin:${project.version}:changelog
@@ -76,7 +77,20 @@
 
 	mvn release:prepare release:perform -Dresume=false -Dusername=XXX -Dpassword=XXX
 
+
+Committer / Author
+____
+
+The jgit provider per default uses the information of the "user" section in the .gitconfig. If you explicitly configure a user there, this user will be used as author and committer. 
+If you don't define a user in the .gitconfig, then the user passed as "username" with the maven execution is used. The email per default will be 'username'@hostname. If you don't like 
+the hostname to be used as the domain. you can configure a default domain in the .gitconfig as follows:   
+
+	git config --global maven-scm.maildomain mycomp.com	
 	
+You can also enforce the usage of the "username" for the author and committer (omit the default in the .gitconfig):
+	
+	git config --global maven-scm.forceUsername true
+
 
 			
 			
diff --git a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/test/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommandCommitterAuthorTckTest.java b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/test/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommandCommitterAuthorTckTest.java
new file mode 100644
index 0000000..10fdb85
--- /dev/null
+++ b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/test/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommandCommitterAuthorTckTest.java
@@ -0,0 +1,279 @@
+package org.apache.maven.scm.provider.git.jgit.command.checkin;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import org.apache.maven.scm.ScmException;
+import org.apache.maven.scm.ScmFileSet;
+import org.apache.maven.scm.command.add.AddScmResult;
+import org.apache.maven.scm.command.checkin.CheckInScmResult;
+import org.apache.maven.scm.provider.git.GitScmTestUtils;
+import org.apache.maven.scm.provider.git.command.checkin.GitCheckInCommandTckTest;
+import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
+import org.apache.maven.scm.repository.ScmRepository;
+import org.codehaus.plexus.util.IOUtil;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.lib.AnyObjectId;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.StoredConfig;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
+import org.eclipse.jgit.util.FileUtils;
+
+/**
+ * @author Dominik Bartholdi (imod)
+ */
+public class JGitCheckInCommandCommitterAuthorTckTest
+    extends GitCheckInCommandTckTest
+{
+    /**
+     * {@inheritDoc}
+     */
+    public String getScmUrl()
+        throws Exception
+    {
+        return GitScmTestUtils.getScmUrl( getRepositoryRoot(), "jgit" );
+    }
+
+    @Override
+    protected void deleteDirectory( File directory )
+        throws IOException
+    {
+        if ( directory.exists() )
+        {
+            FileUtils.delete( directory, FileUtils.RECURSIVE | FileUtils.RETRY );
+        }
+    }
+
+    @Override
+    public void testCheckInCommandTest()
+        throws Exception
+    {
+        File fooJava = new File( getWorkingCopy(), "src/main/java/Foo.java" );
+        assertFalse( "check Foo.java doesn't yet exist", fooJava.canRead() );
+
+        Git git = Git.open( getWorkingCopy() );
+
+        RevCommit head = getHeadCommit( git.getRepository() );
+        // Mark created the test repo...
+        assertEquals( "Mark Struberg", head.getCommitterIdent().getName() );
+        JGitUtils.closeRepo( git );
+
+        createAndCommitFile( fooJava, null );
+
+        // change user in config
+        git = Git.open( getWorkingCopy() );
+        StoredConfig config = git.getRepository().getConfig();
+        unsetConfig( config );
+        config.setString( "user", null, "name", "Dominik" );
+        config.setString( "user", null, "email", "domi@mycomp.com" );
+        config.save();
+
+        // make a commit
+        createAndCommitFile( fooJava, null );
+
+        // check new commit is done with new user in config
+        head = getHeadCommit( git.getRepository() );
+        assertEquals( "Dominik", head.getCommitterIdent().getName() );
+        assertEquals( "Dominik", head.getAuthorIdent().getName() );
+        assertEquals( "domi@mycomp.com", head.getAuthorIdent().getEmailAddress() );
+        assertEquals( "domi@mycomp.com", head.getCommitterIdent().getEmailAddress() );
+        JGitUtils.closeRepo( git );
+
+        // change user in config
+        git = Git.open( getWorkingCopy() );
+        config = git.getRepository().getConfig();
+        unsetConfig( config );
+        config.setString( "user", null, "name", "dbartholdi" );
+        config.save();
+
+        // make a change
+        createAndCommitFile( fooJava, null );
+
+        // check new commit is done with new user in config
+        head = getHeadCommit( git.getRepository() );
+        assertEquals( "dbartholdi", head.getCommitterIdent().getName() );
+        assertFalse( "no mail domain is configured, git system default should be used",
+                     head.getCommitterIdent().getEmailAddress().contains( "dbartholdi" ) );
+        JGitUtils.closeRepo( git );
+
+        // unset a user and maven user but set default mail domain
+        git = Git.open( getWorkingCopy() );
+        config = git.getRepository().getConfig();
+        unsetConfig( config );
+        config.setString( JGitCheckInCommand.GIT_MAVEN_SECTION, null, JGitCheckInCommand.GIT_MAILDOMAIN, "comp.com" );
+        config.save();
+
+        // make a change with an user on the commandline
+        createAndCommitFile( fooJava, "dude" );
+
+        // check new commit is done with new maven user in config
+        head = getHeadCommit( git.getRepository() );
+        assertEquals( "dude", head.getCommitterIdent().getName() );
+        assertEquals( "dude@comp.com", head.getCommitterIdent().getEmailAddress() );
+        assertEquals( "dude", head.getAuthorIdent().getName() );
+        assertEquals( "dude@comp.com", head.getAuthorIdent().getEmailAddress() );
+        JGitUtils.closeRepo( git );
+
+        // unset a user and maven user but set default mail domain
+        git = Git.open( getWorkingCopy() );
+        config = git.getRepository().getConfig();
+        unsetConfig( config );
+        config.setString( "user", null, "name", "dbartholdi" );
+        config.setBoolean( JGitCheckInCommand.GIT_MAVEN_SECTION, null, JGitCheckInCommand.GIT_FORCE, true );
+        config.setString( JGitCheckInCommand.GIT_MAVEN_SECTION, null, JGitCheckInCommand.GIT_MAILDOMAIN, "anycomp.com" );
+        config.save();
+
+        // make a change with an user on the commandline
+        createAndCommitFile( fooJava, "dude" );
+
+        // check new commit is done with new maven user in config
+        head = getHeadCommit( git.getRepository() );
+        assertEquals( "dude", head.getCommitterIdent().getName() );
+        assertEquals( "dude@anycomp.com", head.getCommitterIdent().getEmailAddress() );
+        assertEquals( "dude", head.getAuthorIdent().getName() );
+        assertEquals( "dude@anycomp.com", head.getAuthorIdent().getEmailAddress() );
+        JGitUtils.closeRepo( git );
+
+        // unset a user and maven user but set default mail domain
+        git = Git.open( getWorkingCopy() );
+        config = git.getRepository().getConfig();
+        unsetConfig( config );
+        config.setString( JGitCheckInCommand.GIT_MAVEN_SECTION, null, JGitCheckInCommand.GIT_MAILDOMAIN, "anycomp.com" );
+        config.save();
+
+        // make a change with no username given
+        createAndCommitFile( fooJava, null );
+
+        // check new commit does not contain the configured email domain
+        head = getHeadCommit( git.getRepository() );
+        assertFalse( head.getCommitterIdent().getEmailAddress().contains( "anycomp.com" ) );
+        assertFalse( head.getAuthorIdent().getEmailAddress().contains( "anycomp.com" ) );
+        JGitUtils.closeRepo( git );
+
+        // unset a user and full maven section
+        git = Git.open( getWorkingCopy() );
+        config = git.getRepository().getConfig();
+        unsetConfig( config );
+        config.save();
+
+        // make a change with an user on the commandline
+        createAndCommitFile( fooJava, "dundy" );
+
+        // check new commit is done with new maven user in config
+        head = getHeadCommit( git.getRepository() );
+        assertEquals( "dundy", head.getCommitterIdent().getName() );
+        assertEquals( "dundy", head.getAuthorIdent().getName() );
+        assertTrue( "the maven user (from parameter) name must be in the committer mail when nothing else is configured",
+                    head.getCommitterIdent().getEmailAddress().contains( "dundy" ) );
+        assertTrue( "the user name (from parameter) must be in the author mail when nothing else is configured",
+                    head.getAuthorIdent().getEmailAddress().contains( "dundy" ) );
+        JGitUtils.closeRepo( git );
+
+        // unset all configs
+        git = Git.open( getWorkingCopy() );
+        config = git.getRepository().getConfig();
+        unsetConfig( config );
+        config.save();
+
+        // make a change with no user on the commandline
+        createAndCommitFile( fooJava, null );
+
+        // check new commit is has a committer/author with email set
+        head = getHeadCommit( git.getRepository() );
+        assertNotNull( head.getCommitterIdent().getName() );
+        assertNotNull( head.getAuthorIdent().getName() );
+        assertNotNull( head.getCommitterIdent().getEmailAddress() );
+        assertNotNull( head.getAuthorIdent().getEmailAddress() );
+        JGitUtils.closeRepo( git );
+    }
+
+    /**
+     * make sure the local .gitconfig is in a clean state
+     */
+    private void unsetConfig( StoredConfig config )
+    {
+        config.unsetSection( "user", null );
+        config.unset( "user", null, "name" );
+        // somehow unset does not always work on "user"
+        config.setString( "user", null, "name", null );
+        config.setString( "user", null, "email", null );
+        config.unsetSection( JGitCheckInCommand.GIT_MAVEN_SECTION, null );
+    }
+
+    private void createAndCommitFile( File file, String username )
+        throws Exception, ScmException, IOException
+    {
+        createFooJava( file );
+
+        ScmRepository scmRepository = getScmRepository();
+        scmRepository.getProviderRepository().setUser( username );
+        AddScmResult addResult = getScmManager().add( scmRepository, new ScmFileSet( getWorkingCopy(), "**/*.java" ) );
+
+        assertResultIsSuccess( addResult );
+
+        CheckInScmResult result =
+            getScmManager().checkIn( scmRepository, new ScmFileSet( getWorkingCopy(), "**/Foo.java" ), "Commit message" );
+
+        assertResultIsSuccess( result );
+    }
+
+    private RevCommit getHeadCommit( Repository repository )
+        throws Exception
+    {
+        RevWalk rw = new RevWalk( repository );
+        AnyObjectId headId = repository.resolve( Constants.HEAD );
+        RevCommit head = rw.parseCommit( headId );
+        rw.release();
+        return head;
+    }
+
+    private void createFooJava( File fooJava )
+        throws Exception
+    {
+        FileWriter output = new FileWriter( fooJava );
+
+        PrintWriter printer = new PrintWriter( output );
+        try
+        {
+            printer.println( "public class Foo" );
+            printer.println( "{" );
+
+            printer.println( "    public void foo()" );
+            printer.println( "    {" );
+            printer.println( "        //" + System.currentTimeMillis() );
+            printer.println( "        int i = 10;" );
+            printer.println( "    }" );
+
+            printer.println( "}" );
+        }
+        finally
+        {
+            IOUtil.close( output );
+            IOUtil.close( printer );
+        }
+    }
+
+}