Skip to content

Commit

Permalink
Refactor GitflowConfigUtil into a mapped singletons prevent actions f…
Browse files Browse the repository at this point in the history
…rom running on EDT
  • Loading branch information
OpherV committed Aug 22, 2019
1 parent 513aa67 commit 8d35064
Show file tree
Hide file tree
Showing 21 changed files with 150 additions and 140 deletions.
14 changes: 8 additions & 6 deletions src/main/java/gitflow/GitflowBranchUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import git4idea.branch.GitBranchUtil;
import git4idea.repo.GitRemote;
import git4idea.repo.GitRepository;
import gitflow.GitflowConfigUtil;

import javax.swing.*;
import java.util.ArrayList;
Expand Down Expand Up @@ -42,12 +43,13 @@ public GitflowBranchUtil(Project project, GitRepository repo){
if (repo != null) {
currentBranchName = GitBranchUtil.getBranchNameOrRev(repo);

branchnameMaster= GitflowConfigUtil.getMasterBranch(project, repo);
branchnameDevelop = GitflowConfigUtil.getDevelopBranch(project, repo);
prefixFeature = GitflowConfigUtil.getFeaturePrefix(project, repo);
prefixRelease = GitflowConfigUtil.getReleasePrefix(project, repo);
prefixHotfix = GitflowConfigUtil.getHotfixPrefix(project, repo);
prefixBugfix = GitflowConfigUtil.getBugfixPrefix(project, repo);
GitflowConfigUtil gitflowConfigUtil = GitflowConfigUtil.getInstance(project, repo);
branchnameMaster = gitflowConfigUtil.masterBranch;
branchnameDevelop = gitflowConfigUtil.developBranch;
prefixFeature = gitflowConfigUtil.featurePrefix;
prefixRelease = gitflowConfigUtil.releasePrefix;
prefixHotfix = gitflowConfigUtil.hotfixPrefix;
prefixBugfix = gitflowConfigUtil.bugfixPrefix;

initRemoteBranches();
initLocalBranchNames();
Expand Down
34 changes: 24 additions & 10 deletions src/main/java/gitflow/GitflowBranchUtilManager.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package gitflow;

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import git4idea.GitUtil;
import git4idea.repo.GitRepository;
import gitflow.actions.GitflowActions;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Future;

/**
* This class maps repos to their corresponding branch utils
Expand Down Expand Up @@ -36,17 +43,24 @@ static public void setupBranchUtil(Project project, GitRepository repo){
* Repopulates the branchUtils for each repo
* @param project
*/
static public void update(Project project){
if (repoBranchUtilMap == null){
repoBranchUtilMap = new HashMap<GitRepository, GitflowBranchUtil>();
}
static public Future<Void> update(Project proj){
final Project project = proj;
return (Future<Void>) ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
@Override
public void run() {
if (repoBranchUtilMap == null){
repoBranchUtilMap = new HashMap<GitRepository, gitflow.GitflowBranchUtil>();
}

List<GitRepository> gitRepositories = GitUtil.getRepositoryManager(project).getRepositories();
List<GitRepository> gitRepositories = GitUtil.getRepositoryManager(project).getRepositories();

Iterator gitRepositoriesIterator = gitRepositories.iterator();
while(gitRepositoriesIterator.hasNext()){
GitRepository repo = (GitRepository) gitRepositoriesIterator.next();
GitflowBranchUtilManager.setupBranchUtil(project, repo);
}
}
});

Iterator gitRepositoriesIterator = gitRepositories.iterator();
while(gitRepositoriesIterator.hasNext()){
GitRepository repo = (GitRepository) gitRepositoriesIterator.next();
GitflowBranchUtilManager.setupBranchUtil(project, repo);
}
}
}
158 changes: 61 additions & 97 deletions src/main/java/gitflow/GitflowConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import git4idea.repo.GitRepository;
import gitflow.ui.NotifyUtil;

import java.util.HashMap;
import java.util.Map;

/**
*
*
* @author Opher Vishnia / opherv.com / opherv@gmail.com
*/

//TODO maybe have this as a singleton instead of static

public class GitflowConfigUtil {

public static final String BRANCH_MASTER = "gitflow.branch.master";
Expand All @@ -26,138 +27,100 @@ public class GitflowConfigUtil {
public static final String PREFIX_SUPPORT = "gitflow.prefix.support";
public static final String PREFIX_VERSIONTAG = "gitflow.prefix.versiontag";

public static String getMasterBranch(Project project, GitRepository repo){
VirtualFile root = repo.getRoot();

String masterBranch=null;
try{
masterBranch = GitConfigUtil.getValue(project, root, BRANCH_MASTER);
}
catch (VcsException e) {
NotifyUtil.notifyError(project, "Config error", e);
}
private static Map<Project, Map<GitRepository, GitflowConfigUtil>> gitflowConfigUtilMap = new HashMap<Project, Map<GitRepository, GitflowConfigUtil>>();

return masterBranch;
}
Project project;
GitRepository repo;
public String masterBranch;
public String developBranch;
public String featurePrefix;
public String releasePrefix;
public String hotfixPrefix;
public String bugfixPrefix;
public String supportPrefix;
public String versiontagPrefix;

public static String getDevelopBranch(Project project, GitRepository repo){
VirtualFile root = repo.getRoot();
public static GitflowConfigUtil getInstance(Project project_, GitRepository repo_)
{
GitflowConfigUtil instance;
if (gitflowConfigUtilMap.containsKey(project_) && gitflowConfigUtilMap.get(project_).containsKey(repo_)) {
instance = gitflowConfigUtilMap.get(project_).get(repo_);
} else {
Map<GitRepository, GitflowConfigUtil> innerMap = new HashMap<GitRepository, GitflowConfigUtil>();
instance = new GitflowConfigUtil(project_, repo_);

String developBranch=null;
try{
developBranch = GitConfigUtil.getValue(project, root, BRANCH_DEVELOP);
}
catch (VcsException e) {
NotifyUtil.notifyError(project, "Config error", e);
gitflowConfigUtilMap.put(project_, innerMap);
innerMap.put(repo_, instance);
}

return developBranch;
return instance;
}

public static String getFeaturePrefix(Project project, GitRepository repo){
VirtualFile root = repo.getRoot();

String featurePrefix=null;

try{
featurePrefix = GitConfigUtil.getValue(project,root,PREFIX_FEATURE);
}
catch (VcsException e) {
NotifyUtil.notifyError(project, "Config error", e);
}
return featurePrefix;
}
GitflowConfigUtil(Project project_, GitRepository repo_){
project = project_;
repo = repo_;

public static String getReleasePrefix(Project project, GitRepository repo){
VirtualFile root = repo.getRoot();

String releasePrefix=null;

try{
masterBranch = GitConfigUtil.getValue(project, root, BRANCH_MASTER);
developBranch = GitConfigUtil.getValue(project, root, BRANCH_DEVELOP);
featurePrefix = GitConfigUtil.getValue(project,root,PREFIX_FEATURE);
releasePrefix = GitConfigUtil.getValue(project,root,PREFIX_RELEASE);
}
catch (VcsException e) {
NotifyUtil.notifyError(project, "Config error", e);
}
return releasePrefix;
}

public static String getHotfixPrefix(Project project, GitRepository repo){
VirtualFile root = repo.getRoot();

String hotfixPrefix=null;

try{
hotfixPrefix = GitConfigUtil.getValue(project,root,PREFIX_HOTFIX);
}
catch (VcsException e) {
NotifyUtil.notifyError(project, "Config error", e);
}
return hotfixPrefix;
}

public static String getBugfixPrefix(Project project, GitRepository repo){
VirtualFile root = repo.getRoot();

String bugfixPrefix=null;

try{
bugfixPrefix = GitConfigUtil.getValue(project,root,PREFIX_BUGFIX);
}
catch (VcsException e) {
supportPrefix = GitConfigUtil.getValue(project,root,PREFIX_SUPPORT);
versiontagPrefix = GitConfigUtil.getValue(project,root,PREFIX_VERSIONTAG);

} catch (VcsException e) {
NotifyUtil.notifyError(project, "Config error", e);
}
return bugfixPrefix;
}

public static String getFeatureNameFromBranch(Project project, GitRepository repo, String branchName){
String featurePrefix= GitflowConfigUtil.getFeaturePrefix(project, repo);

public String getFeatureNameFromBranch(String branchName){
return branchName.substring(branchName.indexOf(featurePrefix)+featurePrefix.length(),branchName.length());
}

public static String getReleaseNameFromBranch(Project project, GitRepository repo, String branchName){
String releasePrefix= GitflowConfigUtil.getReleasePrefix(project, repo);
public String getReleaseNameFromBranch(String branchName){
return branchName.substring(branchName.indexOf(releasePrefix) + releasePrefix.length(), branchName.length());
}

public static String getHotfixNameFromBranch(Project project, GitRepository repo, String branchName){
String hotfixPrefix= GitflowConfigUtil.getHotfixPrefix(project, repo);
public String getHotfixNameFromBranch(String branchName){
return branchName.substring(branchName.indexOf(hotfixPrefix) + hotfixPrefix.length(), branchName.length());
}

public static String getBugfixNameFromBranch(Project project, GitRepository repo, String branchName){
String bugfixPrefix= GitflowConfigUtil.getBugfixPrefix(project, repo);
public String getBugfixNameFromBranch(String branchName){
return branchName.substring(branchName.indexOf(bugfixPrefix)+bugfixPrefix.length(),branchName.length());
}

public static String getRemoteNameFromBranch(Project project, String branchName){
public String getRemoteNameFromBranch(String branchName){
return branchName.substring(0,branchName.indexOf("/"));
}

public static void setMasterBranch(Project project, GitRepository repo, String branchName)
public void setMasterBranch(String branchName)
{
masterBranch = branchName;
VirtualFile root = repo.getRoot();

try {
GitConfigUtil.setValue(project, root, BRANCH_MASTER, branchName);
} catch (VcsException e) {
NotifyUtil.notifyError(project, "Config error", e);
}
}

public static void setDevelopBranch(Project project, GitRepository repo, String branchName) {

public void setDevelopBranch(String branchName) {
developBranch = branchName;
VirtualFile root = repo.getRoot();

try {
GitConfigUtil.setValue(project, root, BRANCH_DEVELOP, branchName);
} catch (VcsException e) {
NotifyUtil.notifyError(project, "Config error", e);
}
}

public static void setReleasePrefix(Project project, GitRepository repo, String prefix) {

public void setReleasePrefix(String prefix) {
releasePrefix = prefix;
VirtualFile root = repo.getRoot();

try {
Expand All @@ -167,8 +130,8 @@ public static void setReleasePrefix(Project project, GitRepository repo, String
}
}

public static void setFeaturePrefix(Project project, GitRepository repo, String prefix) {

public void setFeaturePrefix(String prefix) {
featurePrefix = prefix;
VirtualFile root = repo.getRoot();

try {
Expand All @@ -178,8 +141,8 @@ public static void setFeaturePrefix(Project project, GitRepository repo, String
}
}

public static void setHotfixPrefix(Project project, GitRepository repo, String prefix) {

public void setHotfixPrefix(String prefix) {
hotfixPrefix = prefix;
VirtualFile root = repo.getRoot();

try {
Expand All @@ -189,8 +152,8 @@ public static void setHotfixPrefix(Project project, GitRepository repo, String p
}
}

public static void setBugfixPrefix(Project project, GitRepository repo, String prefix) {

public void setBugfixPrefix(String prefix) {
bugfixPrefix = prefix;
VirtualFile root = repo.getRoot();

try {
Expand All @@ -200,8 +163,8 @@ public static void setBugfixPrefix(Project project, GitRepository repo, String p
}
}

public static void setSupportPrefix(Project project, GitRepository repo, String prefix) {

public void setSupportPrefix(String prefix) {
supportPrefix = prefix;
VirtualFile root = repo.getRoot();

try {
Expand All @@ -211,8 +174,8 @@ public static void setSupportPrefix(Project project, GitRepository repo, String
}
}

public static void setVersionPrefix(Project project, GitRepository repo, String prefix) {

public void setVersionPrefix(String prefix) {
versiontagPrefix = prefix;
VirtualFile root = repo.getRoot();

try {
Expand All @@ -221,9 +184,10 @@ public static void setVersionPrefix(Project project, GitRepository repo, String
NotifyUtil.notifyError(project, "Config error", e);
}
}

public static String getBaseBranch(Project project, GitRepository repo, String branchName){


// this still reads from config because it always runs from an action and never from the EDT
public String getBaseBranch(String branchName){

VirtualFile root = repo.getRoot();

String baseBranch=null;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/gitflow/actions/FinishBugfixAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public void actionPerformed(AnActionEvent e) {
bugfixName = customBugfixName;
}
else{
bugfixName = GitflowConfigUtil.getBugfixNameFromBranch(myProject, myRepo, currentBranchName);
GitflowConfigUtil gitflowConfigUtil = GitflowConfigUtil.getInstance(myProject, myRepo);
bugfixName = gitflowConfigUtil.getBugfixNameFromBranch(currentBranchName);
}

this.runAction(myProject, bugfixName);
Expand All @@ -56,8 +57,10 @@ public void runAction(final Project project, final String bugfixName){
final GitflowErrorsListener errorLineHandler = new GitflowErrorsListener(myProject);
final FinishBugfixAction that = this;

GitflowConfigUtil gitflowConfigUtil = GitflowConfigUtil.getInstance(project, myRepo);

//get the base branch for this bugfix
final String baseBranch = GitflowConfigUtil.getBaseBranch(project, myRepo, bugfixPrefix+bugfixName);
final String baseBranch = gitflowConfigUtil.getBaseBranch(bugfixPrefix+bugfixName);

new Task.Backgroundable(myProject,"Finishing bugfix "+bugfixName,false){
@Override
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/gitflow/actions/FinishFeatureAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public void actionPerformed(AnActionEvent e) {
featureName = customFeatureName;
}
else{
featureName = GitflowConfigUtil.getFeatureNameFromBranch(myProject, myRepo, currentBranchName);
GitflowConfigUtil gitflowConfigUtil = GitflowConfigUtil.getInstance(myProject, myRepo);
featureName = gitflowConfigUtil.getFeatureNameFromBranch(currentBranchName);
}

this.runAction(myProject, featureName);
Expand All @@ -57,7 +58,8 @@ public void runAction(final Project project, final String featureName){
final FinishFeatureAction that = this;

//get the base branch for this feature
final String baseBranch = GitflowConfigUtil.getBaseBranch(project, myRepo, featurePrefix+featureName);
GitflowConfigUtil gitflowConfigUtil = GitflowConfigUtil.getInstance(project, myRepo);
final String baseBranch = gitflowConfigUtil.getBaseBranch(featurePrefix+featureName);

new Task.Backgroundable(myProject,"Finishing feature "+featureName,false){
@Override
Expand Down
Loading

0 comments on commit 8d35064

Please sign in to comment.