Skip to content

Commit

Permalink
Name & Email set from .gitconfig or local config
Browse files Browse the repository at this point in the history
* Global name/email taken from .gitconfig if it exists
* Allow per-repo name/email
  • Loading branch information
Phillipus committed Sep 19, 2017
1 parent 75a0c9c commit cbc666c
Show file tree
Hide file tree
Showing 14 changed files with 464 additions and 44 deletions.
5 changes: 3 additions & 2 deletions org.archicontribs.modelrepository/plugin.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

collaboration = Collaboration
model_repository = Collaboration Workspace
history_repository = Change History
repository = Collaboration
infoTab = Information
userTab = User Details
18 changes: 15 additions & 3 deletions org.archicontribs.modelrepository/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
<page
class="org.archicontribs.modelrepository.preferences.ModelRepositoryPreferencePage"
id="org.archicontribs.modelrepository.preferences.ModelRepositoryPreferencePage"
name="%repository">
name="%collaboration">
</page>
</extension>
<extension
Expand All @@ -295,7 +295,8 @@
<extension
point="org.eclipse.ui.views.properties.tabbed.propertyContributor">
<propertyContributor
contributorId="org.archicontribs.modelrepository">
contributorId="org.archicontribs.modelrepository"
labelProvider="org.archicontribs.modelrepository.propertysections.LabelProvider">
<propertyCategory
category="main.category"></propertyCategory>
</propertyContributor>
Expand All @@ -307,7 +308,12 @@
<propertyTab
category="main.category"
id="info.tab"
label="%repository">
label="%infoTab">
</propertyTab>
<propertyTab
category="main.category"
id="user.tab"
label="%userTab">
</propertyTab>
</propertyTabs>
</extension>
Expand All @@ -321,6 +327,12 @@
id="infoSection"
tab="info.tab">
</propertySection>
<propertySection
class="org.archicontribs.modelrepository.propertysections.UserDetailsSection"
filter="org.archicontribs.modelrepository.propertysections.UserDetailsSection$Filter"
id="userSection"
tab="user.tab">
</propertySection>
</propertySections>
</extension>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,9 @@ protected boolean offerToCommitChanges() {
int response = commitDialog.open();

if(response == Window.OK) {
String userName = commitDialog.getUserName();
String userEmail = commitDialog.getUserEmail();
String commitMessage = commitDialog.getCommitMessage();
boolean amend = commitDialog.getAmend();

// Store Prefs
ModelRepositoryPlugin.INSTANCE.getPreferenceStore().setValue(IPreferenceConstants.PREFS_COMMIT_USER_NAME, userName);
ModelRepositoryPlugin.INSTANCE.getPreferenceStore().setValue(IPreferenceConstants.PREFS_COMMIT_USER_EMAIL, userEmail);

try {
getRepository().commitChanges(commitMessage, amend);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import java.io.IOException;

import org.archicontribs.modelrepository.IModelRepositoryImages;
import org.archicontribs.modelrepository.ModelRepositoryPlugin;
import org.archicontribs.modelrepository.grafico.GraficoUtils;
import org.archicontribs.modelrepository.grafico.IArchiRepository;
import org.archicontribs.modelrepository.preferences.IPreferenceConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
Expand Down Expand Up @@ -39,7 +40,7 @@ public class CommitDialog extends ExtendedTitleAreaDialog {
private Text fTextUserName, fTextUserEmail, fTextCommitMessage;
private Button fAmendLastCommitCheckbox;

private String fUserName, fUserEmail, fCommitMessage;
private String fCommitMessage;
private boolean fAmend;

private IArchiRepository fRepository;
Expand Down Expand Up @@ -68,9 +69,18 @@ protected Control createDialogArea(Composite parent) {
container.setLayout(layout);

// User name & email
String userName = ModelRepositoryPlugin.INSTANCE.getPreferenceStore().getString(IPreferenceConstants.PREFS_COMMIT_USER_NAME);
String userEmail = ModelRepositoryPlugin.INSTANCE.getPreferenceStore().getString(IPreferenceConstants.PREFS_COMMIT_USER_EMAIL);
String userName = ""; //$NON-NLS-1$
String userEmail = ""; //$NON-NLS-1$

try {
PersonIdent result = fRepository.getUserDetails();
userName = result.getName();
userEmail = result.getEmailAddress();
}
catch(IOException ex) {
ex.printStackTrace();
}

Label label = new Label(container, SWT.NONE);
label.setText(Messages.CommitDialog_2);

Expand Down Expand Up @@ -137,14 +147,6 @@ protected Point getDefaultDialogSize() {
return new Point(600, 450);
}

public String getUserName() {
return fUserName;
}

public String getUserEmail() {
return fUserEmail;
}

public String getCommitMessage() {
return fCommitMessage;
}
Expand All @@ -155,12 +157,33 @@ public boolean getAmend() {

@Override
protected void okPressed() {
fUserEmail = fTextUserEmail.getText().trim();
fUserName = fTextUserName.getText().trim();
fCommitMessage = fTextCommitMessage.getText();
fAmend =fAmendLastCommitCheckbox.getSelection();

try {
storeUserDetails(fTextUserName.getText().trim(), fTextUserEmail.getText().trim());
}
catch(IOException | ConfigInvalidException ex) {
ex.printStackTrace();
}

super.okPressed();
}

/*
* Store user name and email
* If there are no global .gitconfig settings then store there, otherwise store in local repo config file
*/
private void storeUserDetails(String name, String email) throws IOException, ConfigInvalidException {
PersonIdent global = GraficoUtils.getGitConfigUserDetails();
String globalName = global.getName();
String globalEmail = global.getEmailAddress();

if(!StringUtils.isSet(globalName) && !StringUtils.isSet(globalEmail)) {
GraficoUtils.saveGitConfigUserDetails(name, email);
}
else {
fRepository.saveUserDetails(name, email);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import java.security.NoSuchAlgorithmException;
import java.util.stream.Stream;

import org.archicontribs.modelrepository.ModelRepositoryPlugin;
import org.archicontribs.modelrepository.preferences.IPreferenceConstants;
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CleanCommand;
import org.eclipse.jgit.api.CloneCommand;
Expand All @@ -35,11 +33,13 @@
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.BranchTrackingStatus;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
Expand All @@ -55,6 +55,7 @@
import org.eclipse.jgit.treewalk.filter.PathFilter;

import com.archimatetool.editor.model.IEditorModelManager;
import com.archimatetool.editor.utils.StringUtils;
import com.archimatetool.model.IArchimateModel;

/**
Expand Down Expand Up @@ -145,9 +146,6 @@ public boolean hasChangesToCommit() throws IOException, GitAPIException {

@Override
public RevCommit commitChanges(String commitMessage, boolean amend) throws GitAPIException, IOException {
String userName = ModelRepositoryPlugin.INSTANCE.getPreferenceStore().getString(IPreferenceConstants.PREFS_COMMIT_USER_NAME);
String userEmail = ModelRepositoryPlugin.INSTANCE.getPreferenceStore().getString(IPreferenceConstants.PREFS_COMMIT_USER_EMAIL);

try(Git git = Git.open(getLocalRepositoryFolder())) {
Status status = git.status().call();

Expand All @@ -169,7 +167,8 @@ public RevCommit commitChanges(String commitMessage, boolean amend) throws GitAP

// Commit
CommitCommand commitCommand = git.commit();
commitCommand.setAuthor(userName, userEmail);
PersonIdent userDetails = getUserDetails();
commitCommand.setAuthor(userDetails);
commitCommand.setMessage(commitMessage);
commitCommand.setAmend(amend);
return commitCommand.call();
Expand Down Expand Up @@ -366,6 +365,52 @@ public void exportModelToGraficoFiles() throws IOException {
exporter.exportModel();
}

@Override
public PersonIdent getUserDetails() throws IOException {
try(Git git = Git.open(getLocalRepositoryFolder())) {
StoredConfig config = git.getRepository().getConfig();
String name = StringUtils.safeString(config.getString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_NAME));
String email = StringUtils.safeString(config.getString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_EMAIL));
return new PersonIdent(name, email);
}
}

public void saveUserDetails(String name, String email) throws IOException {
// Get global user details from .gitconfig for comparison
PersonIdent global = new PersonIdent("", ""); //$NON-NLS-1$ //$NON-NLS-2$

try {
global = GraficoUtils.getGitConfigUserDetails();
}
catch(ConfigInvalidException ex) {
ex.printStackTrace();
}

// Save to local config
try(Git git = Git.open(getLocalRepositoryFolder())) {
StoredConfig config = git.getRepository().getConfig();

// If global name == local name or blank then unset
if(!StringUtils.isSet(name) || global.getName().equals(name)) {
config.unset(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_NAME);
}
// Set
else {
config.setString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_NAME, name);
}

// If global email == local email or blank then unset
if(!StringUtils.isSet(email) || global.getEmailAddress().equals(email)) {
config.unset(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_EMAIL);
}
else {
config.setString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_EMAIL, email);
}

config.save();
}
}

@Override
public boolean equals(Object obj) {
if((obj != null) && (obj instanceof ArchiRepository)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.SystemReader;

import com.archimatetool.editor.utils.StringUtils;
import com.archimatetool.model.IArchimateModel;

/**
Expand Down Expand Up @@ -90,6 +97,38 @@ public static File getLocalRepositoryFolderForModel(IArchimateModel model) {
return parent.getParentFile();
}

/**
* @return The global user name and email as set in .gitconfig file
* @throws IOException
* @throws ConfigInvalidException
*/
public static PersonIdent getGitConfigUserDetails() throws IOException, ConfigInvalidException {
StoredConfig config = SystemReader.getInstance().openUserConfig(null, FS.detect());
config.load();

String name = StringUtils.safeString(config.getString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_NAME));
String email = StringUtils.safeString(config.getString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_EMAIL));

return new PersonIdent(name, email);
}

/**
* Save the gloable user name and email as set in .gitconfig file
* @param name
* @param email
* @throws IOException
* @throws ConfigInvalidException
*/
public static void saveGitConfigUserDetails(String name, String email) throws IOException, ConfigInvalidException {
StoredConfig config = SystemReader.getInstance().openUserConfig(null, FS.detect());
config.load(); // It seems we have to load before save

config.setString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_NAME, name);
config.setString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_EMAIL, email);

config.save();
}

/**
* Write an object from an ObjectLoader to file using system file endings
* On Windows when a file is restored from the ObjectLoader it has CR line endings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.PullResult;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.FetchResult;
Expand Down Expand Up @@ -182,4 +183,18 @@ public interface IArchiRepository {
* @throws IOException
*/
boolean saveChecksum() throws IOException;

/**
* @return User name and email from config. This is either local or global
* @throws IOException
*/
PersonIdent getUserDetails() throws IOException;

/**
* Save user name and email
* @param name
* @param email
* @throws IOException
*/
public void saveUserDetails(String name, String email) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import java.util.List;
import java.util.Map;

import org.archicontribs.modelrepository.ModelRepositoryPlugin;
import org.archicontribs.modelrepository.dialogs.ConflictsDialog;
import org.archicontribs.modelrepository.preferences.IPreferenceConstants;
import org.eclipse.jface.window.Window;
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CheckoutCommand;
Expand All @@ -22,6 +20,7 @@
import org.eclipse.jgit.api.ResetCommand;
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.swt.widgets.Shell;

/**
Expand Down Expand Up @@ -108,9 +107,8 @@ public void mergeAndCommit(String commitMessage, boolean amend) throws IOExcepti

// Commit
CommitCommand commitCommand = git.commit();
String userName = ModelRepositoryPlugin.INSTANCE.getPreferenceStore().getString(IPreferenceConstants.PREFS_COMMIT_USER_NAME);
String userEmail = ModelRepositoryPlugin.INSTANCE.getPreferenceStore().getString(IPreferenceConstants.PREFS_COMMIT_USER_EMAIL);
commitCommand.setAuthor(userName, userEmail);
PersonIdent userDetails = fArchiRepo.getUserDetails();
commitCommand.setAuthor(userDetails);
commitCommand.setMessage(commitMessage);
commitCommand.setAmend(amend);
commitCommand.call();
Expand Down
Loading

0 comments on commit cbc666c

Please sign in to comment.