PageRenderTime 21ms CodeModel.GetById 1ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/bitbucket/connectors/jetbrains/BitbucketCheckoutProvider.java

https://bitbucket.org/atlassian/jetbrains-bitbucket-connector/
Java | 154 lines | 140 code | 14 blank | 0 comment | 28 complexity | 6ba2088f0f5364c26df4a633e8441ab5 MD5 | raw file
  1package org.bitbucket.connectors.jetbrains;
  2
  3import com.intellij.ide.GeneralSettings;
  4import com.intellij.openapi.application.ApplicationManager;
  5import com.intellij.openapi.application.ApplicationNamesInfo;
  6import com.intellij.openapi.fileEditor.FileDocumentManager;
  7import com.intellij.openapi.progress.ProgressIndicator;
  8import com.intellij.openapi.progress.Task;
  9import com.intellij.openapi.project.Project;
 10import com.intellij.openapi.ui.Messages;
 11import com.intellij.openapi.util.text.StringUtil;
 12import com.intellij.openapi.vcs.CheckoutProvider;
 13import com.intellij.openapi.vcs.VcsKey;
 14import com.intellij.util.SystemProperties;
 15import org.apache.commons.httpclient.URIException;
 16import org.bitbucket.connectors.jetbrains.ui.BitbucketBundle;
 17import org.bitbucket.connectors.jetbrains.ui.BitbucketCloneProjectDialog;
 18import org.bitbucket.connectors.jetbrains.vcs.GitHandler;
 19import org.bitbucket.connectors.jetbrains.vcs.HgHandler;
 20import org.bitbucket.connectors.jetbrains.vcs.VcsHandler;
 21import org.jetbrains.annotations.NotNull;
 22import org.jetbrains.annotations.Nullable;
 23
 24import java.io.File;
 25import java.lang.reflect.InvocationTargetException;
 26import java.lang.reflect.Method;
 27import java.util.Collections;
 28import java.util.Comparator;
 29import java.util.List;
 30
 31public class BitbucketCheckoutProvider implements CheckoutProvider {
 32    public void doCheckout(@NotNull Project project, @Nullable Listener listener) {
 33        List<RepositoryInfo> availableRepos = BitbucketUtil.getRepositories(project, false);
 34        if (availableRepos == null) {
 35            return;
 36        }
 37
 38        if (availableRepos.isEmpty()) {
 39            Messages.showErrorDialog(project, BitbucketBundle.message("clone-err"), BitbucketBundle.message("cannot-clone"));
 40            return;
 41        }
 42        Collections.sort(availableRepos, new Comparator<RepositoryInfo>() {
 43            public int compare(final RepositoryInfo r1, final RepositoryInfo r2) {
 44                int res = r1.getOwner().compareTo(r2.getOwner());
 45                if (res != 0) {
 46                    return res;
 47                }
 48                return r1.getName().compareTo(r2.getName());
 49            }
 50        });
 51
 52        String clonePath;
 53        String lastProjectLocation = GeneralSettings.getInstance().getLastProjectCreationLocation();
 54        String userHome = SystemProperties.getUserHome();
 55        if (lastProjectLocation != null) {
 56            clonePath = lastProjectLocation.replace('/', File.separatorChar);
 57        } else {
 58            clonePath = userHome.replace('/', File.separatorChar) + File.separator +
 59                    ApplicationNamesInfo.getInstance().getLowercaseProductName() + "Projects";
 60        }
 61        File file = new File(clonePath);
 62        if (!file.exists() || !file.isDirectory()) {
 63            clonePath = userHome;
 64        }
 65
 66        BitbucketCloneProjectDialog checkoutDialog = new BitbucketCloneProjectDialog(project, availableRepos);
 67        checkoutDialog.setSelectedPath(clonePath);
 68        checkoutDialog.show();
 69        if (!checkoutDialog.isOK()) {
 70            return;
 71        }
 72
 73        File folder = new File(checkoutDialog.getSelectedPath());
 74        if (!folder.exists() || !folder.isDirectory()) {
 75            return;
 76        }
 77
 78        String projectName = checkoutDialog.getProjectName();
 79        if (!StringUtil.isEmpty(projectName)) {
 80            folder = new File(folder, projectName);
 81            if (!folder.exists()) {
 82                if (!folder.mkdir()) {
 83                    return;
 84                }
 85            } else if (!folder.isDirectory()) {
 86                return;
 87            }
 88        }
 89
 90        RepositoryInfo repository = checkoutDialog.getSelectedRepository();
 91
 92        try {
 93            String repositoryUrl;
 94            boolean git;
 95            if (repository != null) {
 96                repositoryUrl = repository.getCheckoutUrl();
 97                git = repository.isGit();
 98            } else {
 99                repositoryUrl = checkoutDialog.getRepositoryUrl();
100                if (BitbucketUtil.isHttpUrl(repositoryUrl)) {
101                    repositoryUrl = RepositoryInfo.addPassword(repositoryUrl, false);
102                }
103                git = GitHandler.isGitRepository(repositoryUrl);
104            }
105            checkout(project, repositoryUrl, folder.getPath(), git, listener);
106        } catch (URIException e) {
107            Messages.showErrorDialog(project, e.getMessage(), BitbucketBundle.message("url-encode-err"));
108        }
109    }
110
111    private void checkout(final Project project, final String repositoryUrl, final String folder, final boolean git, final Listener listener) {
112        ApplicationManager.getApplication().runWriteAction(new Runnable() {
113            public void run() {
114                FileDocumentManager.getInstance().saveAllDocuments();
115            }
116        });
117
118        new Task.Backgroundable(project, BitbucketBundle.message("checkouting"), true) {
119            public void run(@NotNull ProgressIndicator progressIndicator) {
120                final VcsHandler vcsHandler = git ? new GitHandler() : new HgHandler();
121                if (vcsHandler.checkout(project, folder, repositoryUrl)) {
122                    ApplicationManager.getApplication().invokeLater(new Runnable() {
123                        public void run() {
124                            if (listener != null) {
125                                directoryCheckedOut(listener, new File(folder), vcsHandler.getVcs(project).getKeyInstanceMethod());
126                                listener.checkoutCompleted();
127                            }
128                        }
129                    });
130                }
131            }
132        }.queue();
133    }
134
135    private void directoryCheckedOut(Listener listener, File folder, VcsKey vcsKey) {
136        for (Method m: listener.getClass().getDeclaredMethods()) {
137            if ("directoryCheckedOut".equals(m.getName())) {
138                Object[] params = m.getParameterTypes().length == 2 ?
139                        new Object[] { folder, vcsKey } : // new API
140                        new Object[] { folder }; // old API
141                try {
142                    m.invoke(listener, params);
143                    return;
144                } catch (Exception e) {
145                    throw new RuntimeException(e);
146                }
147            }
148        }
149    }
150
151    public String getVcsName() {
152        return BitbucketUtil.BITBUCKET;
153    }
154}