Search in sources :

Example 11 with GitRepositoryManager

use of git4idea.repo.GitRepositoryManager in project intellij-community by JetBrains.

the class GitMergeAction method perform.

protected void perform(@NotNull final Project project, @NotNull final List<VirtualFile> gitRoots, @NotNull final VirtualFile defaultRoot) {
    final DialogState dialogState = displayDialog(project, gitRoots, defaultRoot);
    if (dialogState == null) {
        return;
    }
    final VirtualFile selectedRoot = dialogState.selectedRoot;
    final Computable<GitLineHandler> handlerProvider = dialogState.handlerProvider;
    final Label beforeLabel = LocalHistory.getInstance().putSystemLabel(project, "Before update");
    new Task.Backgroundable(project, dialogState.progressTitle, true) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            final GitRepositoryManager repositoryManager = GitUtil.getRepositoryManager(project);
            final Git git = Git.getInstance();
            final GitLocalChangesWouldBeOverwrittenDetector localChangesDetector = new GitLocalChangesWouldBeOverwrittenDetector(selectedRoot, MERGE);
            final GitUntrackedFilesOverwrittenByOperationDetector untrackedFilesDetector = new GitUntrackedFilesOverwrittenByOperationDetector(selectedRoot);
            final GitSimpleEventDetector mergeConflict = new GitSimpleEventDetector(GitSimpleEventDetector.Event.MERGE_CONFLICT);
            AccessToken token = DvcsUtil.workingTreeChangeStarted(project);
            try {
                GitCommandResult result = git.runCommand(() -> {
                    GitLineHandler handler = handlerProvider.compute();
                    handler.addLineListener(localChangesDetector);
                    handler.addLineListener(untrackedFilesDetector);
                    handler.addLineListener(mergeConflict);
                    return handler;
                });
                GitRepository repository = repositoryManager.getRepositoryForRoot(selectedRoot);
                assert repository != null : "Repository can't be null for root " + selectedRoot;
                String revision = repository.getCurrentRevision();
                if (revision == null) {
                    return;
                }
                final GitRevisionNumber currentRev = new GitRevisionNumber(revision);
                handleResult(result, project, mergeConflict, localChangesDetector, untrackedFilesDetector, repository, currentRev, beforeLabel);
            } finally {
                token.finish();
            }
        }
    }.queue();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) Label(com.intellij.history.Label) GitRepositoryManager(git4idea.repo.GitRepositoryManager) GitRepository(git4idea.repo.GitRepository) GitRevisionNumber(git4idea.GitRevisionNumber) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AccessToken(com.intellij.openapi.application.AccessToken)

Example 12 with GitRepositoryManager

use of git4idea.repo.GitRepositoryManager in project intellij-community by JetBrains.

the class GitPull method displayDialog.

@Override
protected DialogState displayDialog(@NotNull Project project, @NotNull List<VirtualFile> gitRoots, @NotNull VirtualFile defaultRoot) {
    final GitPullDialog dialog = new GitPullDialog(project, gitRoots, defaultRoot);
    if (!dialog.showAndGet()) {
        return null;
    }
    GitRepositoryManager repositoryManager = GitUtil.getRepositoryManager(project);
    GitRepository repository = repositoryManager.getRepositoryForRoot(dialog.gitRoot());
    assert repository != null : "Repository can't be null for root " + dialog.gitRoot();
    String remoteOrUrl = dialog.getRemote();
    if (remoteOrUrl == null) {
        return null;
    }
    GitRemote remote = GitUtil.findRemoteByName(repository, remoteOrUrl);
    final List<String> urls = remote == null ? Collections.singletonList(remoteOrUrl) : remote.getUrls();
    Computable<GitLineHandler> handlerProvider = new Computable<GitLineHandler>() {

        @Override
        public GitLineHandler compute() {
            return dialog.makeHandler(urls);
        }
    };
    return new DialogState(dialog.gitRoot(), GitBundle.message("pulling.title", dialog.getRemote()), handlerProvider);
}
Also used : GitRemote(git4idea.repo.GitRemote) GitRepository(git4idea.repo.GitRepository) GitPullDialog(git4idea.merge.GitPullDialog) GitLineHandler(git4idea.commands.GitLineHandler) GitRepositoryManager(git4idea.repo.GitRepositoryManager) Computable(com.intellij.openapi.util.Computable)

Example 13 with GitRepositoryManager

use of git4idea.repo.GitRepositoryManager in project intellij-community by JetBrains.

the class GithubUtil method getGitRepository.

@Nullable
public static GitRepository getGitRepository(@NotNull Project project, @Nullable VirtualFile file) {
    GitRepositoryManager manager = GitUtil.getRepositoryManager(project);
    List<GitRepository> repositories = manager.getRepositories();
    if (repositories.size() == 0) {
        return null;
    }
    if (repositories.size() == 1) {
        return repositories.get(0);
    }
    if (file != null) {
        GitRepository repository = manager.getRepositoryForFileQuick(file);
        if (repository != null) {
            return repository;
        }
    }
    return manager.getRepositoryForFileQuick(project.getBaseDir());
}
Also used : GitRepository(git4idea.repo.GitRepository) GitRepositoryManager(git4idea.repo.GitRepositoryManager) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with GitRepositoryManager

use of git4idea.repo.GitRepositoryManager in project intellij-community by JetBrains.

the class GitCheckinEnvironment method mergeCommit.

/**
   * Preform a merge commit
   *
   * @param project          a project
   * @param root             a vcs root
   * @param added            added files
   * @param removed          removed files
   * @param messageFile      a message file for commit
   * @param author           an author
   * @param exceptions       the list of exceptions to report
   * @param partialOperation
   * @return true if merge commit was successful
   */
private boolean mergeCommit(final Project project, final VirtualFile root, final Set<FilePath> added, final Set<FilePath> removed, final File messageFile, final String author, List<VcsException> exceptions, @NotNull final PartialOperation partialOperation) {
    HashSet<FilePath> realAdded = new HashSet<>();
    HashSet<FilePath> realRemoved = new HashSet<>();
    // perform diff
    GitSimpleHandler diff = new GitSimpleHandler(project, root, GitCommand.DIFF);
    diff.setSilent(true);
    diff.setStdoutSuppressed(true);
    diff.addParameters("--diff-filter=ADMRUX", "--name-status", "--no-renames", "HEAD");
    diff.endOptions();
    String output;
    try {
        output = diff.run();
    } catch (VcsException ex) {
        exceptions.add(ex);
        return false;
    }
    String rootPath = root.getPath();
    for (StringTokenizer lines = new StringTokenizer(output, "\n", false); lines.hasMoreTokens(); ) {
        String line = lines.nextToken().trim();
        if (line.length() == 0) {
            continue;
        }
        String[] tk = line.split("\t");
        switch(tk[0].charAt(0)) {
            case 'M':
            case 'A':
                realAdded.add(VcsUtil.getFilePath(rootPath + "/" + tk[1]));
                break;
            case 'D':
                realRemoved.add(VcsUtil.getFilePathForDeletedFile(rootPath + "/" + tk[1], false));
                break;
            default:
                throw new IllegalStateException("Unexpected status: " + line);
        }
    }
    realAdded.removeAll(added);
    realRemoved.removeAll(removed);
    if (realAdded.size() != 0 || realRemoved.size() != 0) {
        final List<FilePath> files = new ArrayList<>();
        files.addAll(realAdded);
        files.addAll(realRemoved);
        final Ref<Boolean> mergeAll = new Ref<>();
        try {
            GuiUtils.runOrInvokeAndWait(new Runnable() {

                public void run() {
                    String message = GitBundle.message("commit.partial.merge.message", partialOperation.getName());
                    SelectFilePathsDialog dialog = new SelectFilePathsDialog(project, files, message, null, "Commit All Files", CommonBundle.getCancelButtonText(), false);
                    dialog.setTitle(GitBundle.getString("commit.partial.merge.title"));
                    dialog.show();
                    mergeAll.set(dialog.isOK());
                }
            });
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new RuntimeException("Unable to invoke a message box on AWT thread", ex);
        }
        if (!mergeAll.get()) {
            return false;
        }
        // update non-indexed files
        if (!updateIndex(project, root, realAdded, realRemoved, exceptions)) {
            return false;
        }
        for (FilePath f : realAdded) {
            VcsDirtyScopeManager.getInstance(project).fileDirty(f);
        }
        for (FilePath f : realRemoved) {
            VcsDirtyScopeManager.getInstance(project).fileDirty(f);
        }
    }
    // perform merge commit
    try {
        commitWithoutPaths(project, root, messageFile, author);
        GitRepositoryManager manager = getRepositoryManager(project);
        manager.updateRepository(root);
    } catch (VcsException ex) {
        exceptions.add(ex);
        return false;
    }
    return true;
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) GitSimpleHandler(git4idea.commands.GitSimpleHandler) GitRepositoryManager(git4idea.repo.GitRepositoryManager) GitUtil.getLogString(git4idea.GitUtil.getLogString) VcsException(com.intellij.openapi.vcs.VcsException) SelectFilePathsDialog(com.intellij.openapi.vcs.changes.ui.SelectFilePathsDialog) Ref(com.intellij.openapi.util.Ref) VcsException(com.intellij.openapi.vcs.VcsException)

Example 15 with GitRepositoryManager

use of git4idea.repo.GitRepositoryManager in project intellij-community by JetBrains.

the class GitCheckinEnvironment method getDefaultMessageFor.

@Nullable
public String getDefaultMessageFor(FilePath[] filesToCheckin) {
    LinkedHashSet<String> messages = newLinkedHashSet();
    GitRepositoryManager manager = getRepositoryManager(myProject);
    for (VirtualFile root : GitUtil.gitRoots(asList(filesToCheckin))) {
        GitRepository repository = manager.getRepositoryForRoot(root);
        if (repository == null) {
            // unregistered nested submodule found by GitUtil.getGitRoot
            LOG.warn("Unregistered repository: " + root);
            continue;
        }
        File mergeMsg = repository.getRepositoryFiles().getMergeMessageFile();
        File squashMsg = repository.getRepositoryFiles().getSquashMessageFile();
        try {
            if (!mergeMsg.exists() && !squashMsg.exists()) {
                continue;
            }
            String encoding = GitConfigUtil.getCommitEncoding(myProject, root);
            if (mergeMsg.exists()) {
                messages.add(loadMessage(mergeMsg, encoding));
            } else {
                messages.add(loadMessage(squashMsg, encoding));
            }
        } catch (IOException e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Unable to load merge message", e);
            }
        }
    }
    return DvcsUtil.joinMessagesOrNull(messages);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GitRepository(git4idea.repo.GitRepository) GitRepositoryManager(git4idea.repo.GitRepositoryManager) GitUtil.getLogString(git4idea.GitUtil.getLogString) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GitRepositoryManager (git4idea.repo.GitRepositoryManager)18 GitRepository (git4idea.repo.GitRepository)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)5 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 Task (com.intellij.openapi.progress.Task)3 VcsException (com.intellij.openapi.vcs.VcsException)3 ArrayList (java.util.ArrayList)3 AccessToken (com.intellij.openapi.application.AccessToken)2 Project (com.intellij.openapi.project.Project)2 FilePath (com.intellij.openapi.vcs.FilePath)2 GitUtil.getLogString (git4idea.GitUtil.getLogString)2 VcsPushDialog (com.intellij.dvcs.push.ui.VcsPushDialog)1 Label (com.intellij.history.Label)1 BooleanOptionDescription (com.intellij.ide.ui.search.BooleanOptionDescription)1 Disposable (com.intellij.openapi.Disposable)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 CommonDataKeys (com.intellij.openapi.actionSystem.CommonDataKeys)1 ServiceManager (com.intellij.openapi.components.ServiceManager)1