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