Skip to contentSkip to conversation

Compute the author/commiter name and email from the git configuration

PATCH Applied Yann Simon 2 participants 2 messages Jun 15, 2016

lore
Share & tools
lore

Yann Simon

author
#0 lore raw

PATCH 1/1 Compute the author/commiter name and email from the git configuration

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)

---
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
org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/CommitAction.java +17 -0
index 17232d6..456745c 100644
@@ -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());
 
org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java +63 -0

73 lines not on this page · raw message

org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java +32 -0
index 20348f1..4e56b38 100644
@@ -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 {
org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java +18 -0
index 8f093d6..b0a5b22 100644
@@ -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.
 	 * 
org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java +120 -0

150 lines not on this page · raw message

org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
1.6.0.4
#1 lore raw
Quoted 8 lines · Yann Simon

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.

Quoted 6 lines

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)

Applied, thanks.

Signature · 1 line
Shawn.