PageRenderTime 94ms CodeModel.GetById 40ms app.highlight 18ms RepoModel.GetById 32ms app.codeStats 0ms

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

https://bitbucket.org/atlassian/jetbrains-bitbucket-connector/
Java | 194 lines | 164 code | 25 blank | 5 comment | 31 complexity | a51e324d7ee6f719b81426ec360bd924 MD5 | raw file
  1package org.bitbucket.connectors.jetbrains.vcs;
  2
  3import com.intellij.openapi.progress.ProgressManager;
  4import com.intellij.openapi.project.Project;
  5import com.intellij.openapi.ui.Messages;
  6import com.intellij.openapi.vcs.AbstractVcs;
  7import com.intellij.openapi.vcs.VcsException;
  8import com.intellij.openapi.vfs.VirtualFile;
  9import com.intellij.util.Consumer;
 10import com.intellij.util.ui.UIUtil;
 11import org.bitbucket.connectors.jetbrains.ProjectUtil;
 12import org.bitbucket.connectors.jetbrains.ui.BitbucketBundle;
 13import org.jetbrains.annotations.Nullable;
 14import org.zmlx.hg4idea.HgVcs;
 15import org.zmlx.hg4idea.command.*;
 16import org.zmlx.hg4idea.execution.HgCommandException;
 17import org.zmlx.hg4idea.execution.HgCommandResult;
 18import org.zmlx.hg4idea.execution.HgCommandResultHandler;
 19import org.zmlx.hg4idea.util.HgUtil;
 20
 21import java.io.*;
 22import java.util.ArrayList;
 23import java.util.List;
 24
 25/**
 26 * User: leha2000
 27 * Date: Oct 14, 2011
 28 * Time: 11:34:36 AM
 29 */
 30public class HgHandler implements VcsHandler {
 31
 32    public boolean checkout(Project project, String folder, String repositoryUrl) {
 33        HgCloneCommand cmd = new HgCloneCommand(project);
 34        cmd.setRepositoryURL(repositoryUrl);
 35        cmd.setDirectory(folder);
 36
 37        HgCommandResult result = cmd.execute();
 38
 39        if (result == null) {
 40            deleteEmptyFolder(folder);
 41            error(project, BitbucketBundle.message("clone-failed"), BitbucketBundle.message("clone-failed-unknown-err"));
 42            return false;
 43        } else if (result.getExitValue() != 0) {
 44            deleteEmptyFolder(folder);
 45            error(project, BitbucketBundle.message("clone-failed"), BitbucketBundle.message("clone-failed-msg", repositoryUrl, result.getRawError() + "\n" + result.getRawOutput()));
 46            return false;
 47        }
 48        return true;
 49    }
 50
 51    private void deleteEmptyFolder(String folder) {
 52        File f = new File(folder);
 53        if (f.exists() && f.isDirectory() && f.list().length == 0) {
 54            f.delete();
 55        }
 56    }
 57
 58    private static void error(final Project project, final String title, final String msg) {
 59        UIUtil.invokeLaterIfNeeded(new Runnable() {
 60            public void run() {
 61                Messages.showErrorDialog(project, msg, title);
 62            }
 63        });
 64    }
 65
 66    public VirtualFile getRepositoryRoot(VirtualFile folder) {
 67        return HgUtil.getNearestHgRoot(folder);
 68    }
 69
 70    public boolean initRepository(final Project project, final VirtualFile root) {
 71        final Boolean[] res = new Boolean[1];
 72
 73        new HgInitCommand(project).execute(root, new HgCommandResultHandler() {
 74
 75            @Override
 76            public void process(@Nullable HgCommandResult hgCommandResult) {
 77                synchronized (res) {
 78                    res[0] = hgCommandResult != null && hgCommandResult.getExitValue() == 0;
 79                    res.notify();
 80                }
 81            }
 82        });
 83
 84        try {
 85            synchronized (res) {
 86                if (res[0] == null) {
 87                    res.wait();
 88                }
 89            }
 90        } catch (InterruptedException e) {
 91            return false;
 92        }
 93
 94        final Exception[] exc = new Exception[1];
 95
 96        ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
 97            public void run() {
 98                ProgressManager.getInstance().getProgressIndicator().setText(BitbucketBundle.message("share-project-on-bitbucket"));
 99                new HgAddCommand(project).execute(ProjectUtil.getSourceFolders(project, root));
100                try {
101                    new HgCommitCommand(project, root, BitbucketBundle.message("initial-rev-msg")).execute();
102                } catch (HgCommandException e) {
103                    exc[0] = e;
104                } catch (VcsException e) {
105                    exc[0] = e;
106                }
107            }
108        }, BitbucketBundle.message("create-local-repository"), true, project);
109
110        return exc[0] == null;
111    }
112
113    public boolean push(Project project, VirtualFile root, String repositoryUrl) {
114        final Boolean[] res = new Boolean[1];
115        new HgPushCommand(project, root, repositoryUrl).execute(new HgCommandResultHandler() {
116            public void process(@Nullable HgCommandResult result) {
117                synchronized (res) {
118                    res[0] = result != null && result.getExitValue() == 0;
119                    res.notify();
120                }
121            }
122        });
123        try {
124            synchronized (res) {
125                if (res[0] == null) {
126                    res.wait();
127                }
128            }
129            return res[0] == Boolean.TRUE;
130        } catch (InterruptedException e) {
131            return false;
132        }
133    }
134
135    public void setRepositoryDefaultUrl(Project project, VirtualFile root, String repositoryUrl) throws IOException {
136        File hgrc = new File(new File(root.getPath(), ".hg"), "hgrc");
137        if (!hgrc.exists()) {
138            hgrc.createNewFile();
139        }
140
141        List<String> lines = new ArrayList<String>();
142        List<String> pathLines = new ArrayList<String>();
143        BufferedReader reader = new BufferedReader(new FileReader(hgrc));
144        try {
145            String line;
146            boolean paths = false;
147            while ((line = reader.readLine()) != null) {
148                line = line.trim();
149                if (line.startsWith("[")) {
150                    paths = "[paths]".equals(line);
151                    if (!paths) {
152                        lines.add(line);
153                    }
154                    continue;
155                }
156
157                if (paths) {
158                    if (!"default".equals(getKey(line))) {
159                        pathLines.add(line);
160                    }
161                } else {
162                    lines.add(line);
163                }
164            }
165        } finally {
166            reader.close();
167        }
168
169        PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(hgrc, false)));
170        try {
171            writer.println("[paths]");
172            writer.println("default=" + repositoryUrl);
173
174            for (String line: pathLines) {
175                writer.println(line);
176            }
177
178            for (String line: lines) {
179                writer.println(line);
180            }
181        } finally {
182            writer.close();
183        }
184    }
185
186    private static String getKey(String s) {
187        String[] parts = s.split("=");
188        return parts.length > 0 ? parts[0].trim() : null;
189    }
190
191    public AbstractVcs getVcs(Project project) {
192        return HgVcs.getInstance(project);
193    }
194}