Search in sources :

Example 1 with Git

use of git4idea.commands.Git in project intellij-community by JetBrains.

the class GithubCheckoutProvider method doCheckout.

@Override
public void doCheckout(@NotNull final Project project, @Nullable final Listener listener) {
    if (!GithubUtil.testGitExecutable(project)) {
        return;
    }
    BasicAction.saveAll();
    List<GithubRepo> availableRepos;
    try {
        availableRepos = GithubUtil.computeValueInModalIO(project, "Access to GitHub", indicator -> GithubUtil.runTask(project, GithubAuthDataHolder.createFromSettings(), indicator, connection -> GithubApiUtil.getAvailableRepos(connection)));
    } catch (IOException e) {
        GithubNotifications.showError(project, "Couldn't get the list of GitHub repositories", e);
        return;
    }
    Collections.sort(availableRepos, (r1, r2) -> {
        final int comparedOwners = r1.getUserName().compareTo(r2.getUserName());
        return comparedOwners != 0 ? comparedOwners : r1.getName().compareTo(r2.getName());
    });
    final GitCloneDialog dialog = new GitCloneDialog(project);
    // Add predefined repositories to history
    dialog.prependToHistory("-----------------------------------------------");
    for (int i = availableRepos.size() - 1; i >= 0; i--) {
        dialog.prependToHistory(GithubUrlUtil.getCloneUrl(availableRepos.get(i).getFullPath()));
    }
    if (!dialog.showAndGet()) {
        return;
    }
    dialog.rememberSettings();
    final VirtualFile destinationParent = LocalFileSystem.getInstance().findFileByIoFile(new File(dialog.getParentDirectory()));
    if (destinationParent == null) {
        return;
    }
    final String sourceRepositoryURL = dialog.getSourceRepositoryURL();
    final String directoryName = dialog.getDirectoryName();
    final String parentDirectory = dialog.getParentDirectory();
    Git git = ServiceManager.getService(Git.class);
    GitCheckoutProvider.clone(project, git, listener, destinationParent, sourceRepositoryURL, directoryName, parentDirectory);
}
Also used : Git(git4idea.commands.Git) GitCheckoutProvider(git4idea.checkout.GitCheckoutProvider) GithubUrlUtil(org.jetbrains.plugins.github.util.GithubUrlUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) IOException(java.io.IOException) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) BasicAction(git4idea.actions.BasicAction) File(java.io.File) GithubApiUtil(org.jetbrains.plugins.github.api.GithubApiUtil) GithubNotifications(org.jetbrains.plugins.github.util.GithubNotifications) Nullable(org.jetbrains.annotations.Nullable) GithubUtil(org.jetbrains.plugins.github.util.GithubUtil) List(java.util.List) ServiceManager(com.intellij.openapi.components.ServiceManager) GithubRepo(org.jetbrains.plugins.github.api.data.GithubRepo) Project(com.intellij.openapi.project.Project) GitCloneDialog(git4idea.checkout.GitCloneDialog) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) CheckoutProvider(com.intellij.openapi.vcs.CheckoutProvider) GithubAuthDataHolder(org.jetbrains.plugins.github.util.GithubAuthDataHolder) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Git(git4idea.commands.Git) GitCloneDialog(git4idea.checkout.GitCloneDialog) GithubRepo(org.jetbrains.plugins.github.api.data.GithubRepo) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 2 with Git

use of git4idea.commands.Git in project google-cloud-intellij by GoogleCloudPlatform.

the class SetupCloudRepositoryAction method pushCurrentBranch.

private static boolean pushCurrentBranch(@NotNull final Project project, @NotNull GitRepository repository, @NotNull String remoteName, @NotNull String remoteUrl) {
    Git git = ServiceManager.getService(Git.class);
    GitLocalBranch currentBranch = repository.getCurrentBranch();
    if (currentBranch == null) {
        SwingUtilities.invokeLater(() -> {
            Notification notification = new Notification(NOTIFICATION_GROUP_ID, GctBundle.message("uploadtogcp.initialpushfailedtitle"), GctBundle.message("uploadtogcp.initialpushfailed"), NotificationType.ERROR);
            notification.notify(project);
        });
        return false;
    }
    GitCommandResult result = git.push(repository, remoteName, remoteUrl, currentBranch.getName(), true);
    if (!result.success()) {
        LOG.warn(result.getErrorOutputAsJoinedString());
        SwingUtilities.invokeLater(() -> {
            Notification notification = new Notification(NOTIFICATION_GROUP_ID, GctBundle.message("uploadtogcp.initialpushfailedtitle"), result.getErrorOutputAsHtmlString() + "<br/>" + joinAsErrorHtmlString(result.getOutput()), NotificationType.ERROR);
            notification.notify(project);
        });
        return false;
    }
    return true;
}
Also used : GitLocalBranch(git4idea.GitLocalBranch) Git(git4idea.commands.Git) GitCommandResult(git4idea.commands.GitCommandResult) Notification(com.intellij.notification.Notification)

Example 3 with Git

use of git4idea.commands.Git in project intellij-community by JetBrains.

the class GitFetcher method fetchNatively.

@NotNull
private static GitFetchResult fetchNatively(@NotNull GitRepository repository, @NotNull GitRemote remote, @Nullable String branch) {
    Git git = Git.getInstance();
    String[] additionalParams = branch != null ? new String[] { getFetchSpecForBranch(branch, remote.getName()) } : ArrayUtil.EMPTY_STRING_ARRAY;
    GitFetchPruneDetector pruneDetector = new GitFetchPruneDetector();
    GitCommandResult result = git.fetch(repository, remote, Collections.<GitLineHandlerListener>singletonList(pruneDetector), additionalParams);
    GitFetchResult fetchResult;
    if (result.success()) {
        fetchResult = GitFetchResult.success();
    } else if (result.cancelled()) {
        fetchResult = GitFetchResult.cancel();
    } else {
        fetchResult = GitFetchResult.error(result.getErrorOutputAsJoinedString());
    }
    fetchResult.addPruneInfo(pruneDetector.getPrunedRefs());
    return fetchResult;
}
Also used : Git(git4idea.commands.Git) GitCommandResult(git4idea.commands.GitCommandResult) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with Git

use of git4idea.commands.Git in project intellij-community by JetBrains.

the class GithubCreatePullRequestWorker method create.

@Nullable
public static GithubCreatePullRequestWorker create(@NotNull final Project project, @Nullable final VirtualFile file) {
    return GithubUtil.computeValueInModal(project, "Loading data...", indicator -> {
        Git git = ServiceManager.getService(Git.class);
        GitRepository gitRepository = GithubUtil.getGitRepository(project, file);
        if (gitRepository == null) {
            GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, "Can't find git repository");
            return null;
        }
        gitRepository.update();
        Pair<GitRemote, String> remote = GithubUtil.findGithubRemote(gitRepository);
        if (remote == null) {
            GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, "Can't find GitHub remote");
            return null;
        }
        String remoteName = remote.getFirst().getName();
        String remoteUrl = remote.getSecond();
        GithubFullPath path = GithubUrlUtil.getUserAndRepositoryFromRemoteUrl(remoteUrl);
        if (path == null) {
            GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, "Can't process remote: " + remoteUrl);
            return null;
        }
        GitLocalBranch currentBranch = gitRepository.getCurrentBranch();
        if (currentBranch == null) {
            GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, "No current branch");
            return null;
        }
        GithubAuthDataHolder authHolder;
        try {
            authHolder = GithubUtil.getValidAuthDataHolderFromConfig(project, AuthLevel.LOGGED, indicator);
        } catch (IOException e) {
            GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, e);
            return null;
        }
        GithubCreatePullRequestWorker worker = new GithubCreatePullRequestWorker(project, git, gitRepository, authHolder, path, remoteName, remoteUrl, currentBranch.getName());
        try {
            worker.initForks(indicator);
        } catch (IOException e) {
            GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, e);
            return null;
        }
        return worker;
    });
}
Also used : GitRemote(git4idea.repo.GitRemote) GitRepository(git4idea.repo.GitRepository) GitLocalBranch(git4idea.GitLocalBranch) Git(git4idea.commands.Git) IOException(java.io.IOException) GithubFullPath(org.jetbrains.plugins.github.api.GithubFullPath) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with Git

use of git4idea.commands.Git in project google-cloud-intellij by GoogleCloudPlatform.

the class ProjectRepositoryValidator method unstash.

private void unstash(@NotNull final Project project, @NotNull final Ref<StashInfo> targetStash, @NotNull final VirtualFile root) {
    if (repoState.getSourceRepository() == null || repoState.getOriginalBranchName() == null || (!repoState.getOriginalBranchName().equals(repoState.getSourceRepository().getCurrentBranchName()) && !repoState.getOriginalBranchName().equals(repoState.getSourceRepository().getCurrentRevision()))) {
        Messages.showErrorDialog(GctBundle.getString("clouddebug.erroroncheckout", repoState.getOriginalBranchName()), "Error");
        return;
    }
    final GitLineHandler handler = new GitLineHandler(project, root, GitCommand.STASH);
    handler.addParameters("apply");
    handler.addParameters("--index");
    addStashParameter(project, handler, targetStash.get().getStash());
    final AtomicBoolean conflict = new AtomicBoolean();
    handler.addLineListener(new GitLineHandlerAdapter() {

        @Override
        public void onLineAvailable(String line, Key outputType) {
            if (line.contains("Merge conflict")) {
                conflict.set(true);
            }
        }
    });
    GitUntrackedFilesOverwrittenByOperationDetector untrackedFilesDetector = new GitUntrackedFilesOverwrittenByOperationDetector(root);
    GitLocalChangesWouldBeOverwrittenDetector localChangesDetector = new GitLocalChangesWouldBeOverwrittenDetector(root, MERGE);
    handler.addLineListener(untrackedFilesDetector);
    handler.addLineListener(localChangesDetector);
    AccessToken token = DvcsUtil.workingTreeChangeStarted(project);
    try {
        final Ref<GitCommandResult> result = Ref.create();
        ProgressManager.getInstance().run(new Task.Modal(handler.project(), GitBundle.getString("unstash.unstashing"), false) {

            @Override
            public void run(@NotNull final ProgressIndicator indicator) {
                indicator.setIndeterminate(true);
                handler.addLineListener(new GitHandlerUtil.GitLineHandlerListenerProgress(indicator, handler, "stash", false));
                Git git = ServiceManager.getService(Git.class);
                result.set(git.runCommand(new Computable.PredefinedValueComputable<GitLineHandler>(handler)));
            }
        });
        ServiceManager.getService(project, GitPlatformFacade.class).hardRefresh(root);
        GitCommandResult res = result.get();
        if (conflict.get()) {
            Messages.showDialog(GctBundle.getString("clouddebug.unstashmergeconflicts"), "Merge Conflicts", new String[] { "Ok" }, 0, Messages.getErrorIcon());
        } else if (untrackedFilesDetector.wasMessageDetected()) {
            GitUntrackedFilesHelper.notifyUntrackedFilesOverwrittenBy(project, root, untrackedFilesDetector.getRelativeFilePaths(), "unstash", null);
        } else if (localChangesDetector.wasMessageDetected()) {
            LocalChangesWouldBeOverwrittenHelper.showErrorDialog(project, root, "unstash", localChangesDetector.getRelativeFilePaths());
        } else if (!res.success()) {
            GitUIUtil.showOperationErrors(project, handler.errors(), handler.printableCommandLine());
        } else if (res.success()) {
            ProgressManager.getInstance().run(new Task.Modal(project, GctBundle.getString("clouddebug.removestashx", targetStash.get().getStash()), false) {

                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                    if (project == null) {
                        return;
                    }
                    final GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.STASH);
                    h.addParameters("drop");
                    addStashParameter(project, h, targetStash.get().getStash());
                    try {
                        h.run();
                        h.unsilence();
                    } catch (final VcsException ex) {
                        ApplicationManager.getApplication().invokeLater(new Runnable() {

                            @Override
                            public void run() {
                                GitUIUtil.showOperationError(project, ex, h.printableCommandLine());
                            }
                        });
                    }
                }
            });
        }
    } finally {
        DvcsUtil.workingTreeChangeFinished(project, token);
    }
}
Also used : GitLineHandler(git4idea.commands.GitLineHandler) Task(com.intellij.openapi.progress.Task) GitSimpleHandler(git4idea.commands.GitSimpleHandler) GitLocalChangesWouldBeOverwrittenDetector(git4idea.commands.GitLocalChangesWouldBeOverwrittenDetector) GitPlatformFacade(git4idea.GitPlatformFacade) GitUntrackedFilesOverwrittenByOperationDetector(git4idea.commands.GitUntrackedFilesOverwrittenByOperationDetector) GitCommandResult(git4idea.commands.GitCommandResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Git(git4idea.commands.Git) AccessToken(com.intellij.openapi.application.AccessToken) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) VcsException(com.intellij.openapi.vcs.VcsException) GitLineHandlerAdapter(git4idea.commands.GitLineHandlerAdapter) Key(com.intellij.openapi.util.Key) Computable(com.intellij.openapi.util.Computable)

Aggregations

Git (git4idea.commands.Git)5 GitCommandResult (git4idea.commands.GitCommandResult)3 GitLocalBranch (git4idea.GitLocalBranch)2 IOException (java.io.IOException)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 Notification (com.intellij.notification.Notification)1 AccessToken (com.intellij.openapi.application.AccessToken)1 ServiceManager (com.intellij.openapi.components.ServiceManager)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 Project (com.intellij.openapi.project.Project)1 Computable (com.intellij.openapi.util.Computable)1 Key (com.intellij.openapi.util.Key)1 CheckoutProvider (com.intellij.openapi.vcs.CheckoutProvider)1 VcsException (com.intellij.openapi.vcs.VcsException)1 LocalFileSystem (com.intellij.openapi.vfs.LocalFileSystem)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 GitPlatformFacade (git4idea.GitPlatformFacade)1 BasicAction (git4idea.actions.BasicAction)1