PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 0ms

/src/org/bitbucket/connectors/jetbrains/vcs/GitHandler.java

https://bitbucket.org/atlassian/jetbrains-bitbucket-connector/
Java | 103 lines | 81 code | 14 blank | 8 comment | 9 complexity | a0d0ae71b63fdbf12ff4be7144ae8706 MD5 | raw file
  1package org.bitbucket.connectors.jetbrains.vcs;
  2
  3import com.intellij.openapi.project.Project;
  4import com.intellij.openapi.vcs.AbstractVcs;
  5import com.intellij.openapi.vcs.VcsException;
  6import com.intellij.openapi.vfs.VirtualFile;
  7import git4idea.GitUtil;
  8import git4idea.GitVcs;
  9import git4idea.commands.GitCommand;
 10import git4idea.commands.GitHandlerUtil;
 11import git4idea.commands.GitLineHandler;
 12import git4idea.commands.GitSimpleHandler;
 13import org.bitbucket.connectors.jetbrains.BitbucketUtil;
 14import org.bitbucket.connectors.jetbrains.ui.BitbucketBundle;
 15
 16import java.io.File;
 17import java.io.IOException;
 18import java.util.Collection;
 19
 20/**
 21 * User: leha2000
 22 * Date: Oct 14, 2011
 23 * Time: 11:35:12 AM
 24 */
 25public class GitHandler implements VcsHandler {
 26
 27    public boolean checkout(Project project, String folderPath, String repositoryUrl) {
 28        File folder = new File(folderPath);
 29        GitSimpleHandler handler = new GitSimpleHandler(project, folder.getParentFile(), GitCommand.CLONE);
 30        handler.addParameters(repositoryUrl, folder.getName());
 31        handler.runInCurrentThread(null);
 32        return true;
 33    }
 34
 35    public static boolean isGitRepository(String url) {
 36        return url != null && url.endsWith(".git");
 37    }
 38
 39    public boolean push(Project project, VirtualFile root, String repositoryUrl) {
 40        if (BitbucketUtil.isHttpUrl(repositoryUrl)) {
 41            if (!disableHttpSslCheck(project, root)) {
 42                return false;
 43            }
 44        }
 45
 46        GitLineHandler handler = new GitLineHandler(project, root, GitCommand.PUSH);
 47        handler.addParameters("origin", "master");
 48        Collection<VcsException> err = GitHandlerUtil.doSynchronouslyWithExceptions(handler);
 49        return err.isEmpty();
 50    }
 51
 52    private boolean disableHttpSslCheck(Project project, VirtualFile root) {
 53        return execute(project, root, GitCommand.CONFIG, "--global", "http.sslverify", "false");
 54    }
 55
 56    public void setRepositoryDefaultUrl(Project project, VirtualFile root, String repositoryUrl) throws IOException {
 57        execute(project, root, GitCommand.REMOTE, "rm", "origin");
 58        execute(project, root, GitCommand.REMOTE, "add", "origin", repositoryUrl);
 59    }
 60
 61    public VirtualFile getRepositoryRoot(VirtualFile folder) {
 62        return folder != null ? GitUtil.getGitRootOrNull(new File(folder.getPath())) : null;
 63    }
 64
 65    public boolean initRepository(Project project, VirtualFile root) {
 66        if (!execute(project, root, GitCommand.INIT)) {
 67            return false;
 68        }
 69        GitLineHandler h = new GitLineHandler(project, root, GitCommand.ADD);
 70        h.addParameters(".");
 71        //h.setNoSSH(true);
 72        GitHandlerUtil.doSynchronously(h, BitbucketBundle.message("create-local-repository"), BitbucketBundle.message("share-project-on-bitbucket"));
 73        if (!h.errors().isEmpty()) {
 74            return false;
 75        }
 76
 77        h = new GitLineHandler(project, root, GitCommand.COMMIT);
 78        h.addParameters("-m", BitbucketBundle.message("initial-rev-msg"));
 79        //h.setNoSSH(true);
 80        GitHandlerUtil.doSynchronously(h, BitbucketBundle.message("create-local-repository"), BitbucketBundle.message("share-project-on-bitbucket"));
 81        return h.errors().isEmpty();
 82    }
 83
 84    public AbstractVcs getVcs(Project project) {
 85        return GitVcs.getInstance(project);
 86    }
 87
 88    private static boolean execute(Project project, VirtualFile root, GitCommand cmd, String... params) {
 89        GitSimpleHandler handler = new GitSimpleHandler(project, root, cmd);
 90        handler.addParameters(params);
 91        handler.setSilent(true);
 92        //handler.setNoSSH(true);
 93        try {
 94            handler.run();
 95            if (handler.getExitCode() != 0) {
 96                return false;
 97            }
 98        } catch (VcsException e) {
 99            return false;
100        }
101        return true;
102    }
103}