Search in sources :

Example 21 with GitRepository

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

the class GitDeleteRemoteBranchOperation method execute.

@Override
protected void execute() {
    Collection<GitRepository> repositories = getRepositories();
    Collection<String> commonTrackingBranches = getCommonTrackingBranches(myBranchName, repositories);
    // don't propose to remove current branch even if it tracks the remote branch
    for (GitRepository repository : repositories) {
        String currentBranch = repository.getCurrentBranchName();
        if (currentBranch != null) {
            commonTrackingBranches.remove(currentBranch);
        }
    }
    Ref<DeleteRemoteBranchDecision> decision = Ref.create();
    ApplicationManager.getApplication().invokeAndWait(() -> decision.set(myUiHandler.confirmRemoteBranchDeletion(myBranchName, commonTrackingBranches, repositories)));
    if (decision.get() != CANCEL) {
        boolean deletedSuccessfully = doDeleteRemote(myBranchName, repositories);
        if (deletedSuccessfully) {
            Collection<String> successfullyDeletedLocalBranches = new ArrayList<>(1);
            if (decision.get() == DELETE_WITH_TRACKING) {
                for (String branch : commonTrackingBranches) {
                    getIndicator().setText("Deleting " + branch);
                    new GitDeleteBranchOperation(myProject, myGit, myUiHandler, repositories, branch) {

                        @Override
                        protected void notifySuccess(@NotNull String message) {
                            // do nothing - will display a combo notification for all deleted branches below
                            successfullyDeletedLocalBranches.add(branch);
                        }
                    }.execute();
                }
            }
            notifySuccessfulDeletion(myBranchName, successfullyDeletedLocalBranches);
        }
    }
}
Also used : GitRepository(git4idea.repo.GitRepository) ArrayList(java.util.ArrayList) DeleteRemoteBranchDecision(git4idea.branch.GitBranchUiHandler.DeleteRemoteBranchDecision)

Example 22 with GitRepository

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

the class GitMergeOperation method execute.

@Override
protected void execute() {
    LOG.info("starting");
    saveAllDocuments();
    boolean fatalErrorHappened = false;
    int alreadyUpToDateRepositories = 0;
    AccessToken token = DvcsUtil.workingTreeChangeStarted(myProject);
    try {
        while (hasMoreRepositories() && !fatalErrorHappened) {
            final GitRepository repository = next();
            LOG.info("next repository: " + repository);
            VirtualFile root = repository.getRoot();
            GitLocalChangesWouldBeOverwrittenDetector localChangesDetector = new GitLocalChangesWouldBeOverwrittenDetector(root, GitLocalChangesWouldBeOverwrittenDetector.Operation.MERGE);
            GitSimpleEventDetector unmergedFiles = new GitSimpleEventDetector(GitSimpleEventDetector.Event.UNMERGED_PREVENTING_MERGE);
            GitUntrackedFilesOverwrittenByOperationDetector untrackedOverwrittenByMerge = new GitUntrackedFilesOverwrittenByOperationDetector(root);
            GitSimpleEventDetector mergeConflict = new GitSimpleEventDetector(GitSimpleEventDetector.Event.MERGE_CONFLICT);
            GitSimpleEventDetector alreadyUpToDateDetector = new GitSimpleEventDetector(GitSimpleEventDetector.Event.ALREADY_UP_TO_DATE);
            GitCommandResult result = myGit.merge(repository, myBranchToMerge, Collections.<String>emptyList(), localChangesDetector, unmergedFiles, untrackedOverwrittenByMerge, mergeConflict, alreadyUpToDateDetector);
            if (result.success()) {
                LOG.info("Merged successfully");
                refresh(repository);
                markSuccessful(repository);
                if (alreadyUpToDateDetector.hasHappened()) {
                    alreadyUpToDateRepositories += 1;
                }
            } else if (unmergedFiles.hasHappened()) {
                LOG.info("Unmerged files error!");
                fatalUnmergedFilesError();
                fatalErrorHappened = true;
            } else if (localChangesDetector.wasMessageDetected()) {
                LOG.info("Local changes would be overwritten by merge!");
                boolean smartMergeSucceeded = proposeSmartMergePerformAndNotify(repository, localChangesDetector);
                if (!smartMergeSucceeded) {
                    fatalErrorHappened = true;
                }
            } else if (mergeConflict.hasHappened()) {
                LOG.info("Merge conflict");
                myConflictedRepositories.put(repository, Boolean.FALSE);
                refresh(repository);
                markSuccessful(repository);
            } else if (untrackedOverwrittenByMerge.wasMessageDetected()) {
                LOG.info("Untracked files would be overwritten by merge!");
                fatalUntrackedFilesError(repository.getRoot(), untrackedOverwrittenByMerge.getRelativeFilePaths());
                fatalErrorHappened = true;
            } else {
                LOG.info("Unknown error. " + result);
                fatalError(getCommonErrorTitle(), result.getErrorOutputAsJoinedString());
                fatalErrorHappened = true;
            }
        }
        if (fatalErrorHappened) {
            notifyAboutRemainingConflicts();
        } else {
            boolean allConflictsResolved = resolveConflicts();
            if (allConflictsResolved) {
                if (alreadyUpToDateRepositories < getRepositories().size()) {
                    notifySuccess();
                } else {
                    notifySuccess("Already up-to-date");
                }
            }
        }
        restoreLocalChanges();
    } finally {
        token.finish();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GitRepository(git4idea.repo.GitRepository) AccessToken(com.intellij.openapi.application.AccessToken)

Example 23 with GitRepository

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

the class GitMergeOperation method refresh.

private void refresh(GitRepository... repositories) {
    for (GitRepository repository : repositories) {
        refreshRoot(repository);
        repository.update();
    }
}
Also used : GitRepository(git4idea.repo.GitRepository)

Example 24 with GitRepository

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

the class GitLogProvider method getCurrentBranch.

@Nullable
@Override
public String getCurrentBranch(@NotNull VirtualFile root) {
    GitRepository repository = myRepositoryManager.getRepositoryForRoot(root);
    if (repository == null)
        return null;
    String currentBranchName = repository.getCurrentBranchName();
    if (currentBranchName == null && repository.getCurrentRevision() != null) {
        return "HEAD";
    }
    return currentBranchName;
}
Also used : GitRepository(git4idea.repo.GitRepository) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with GitRepository

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

the class GitLogProvider method getCommitsMatchingFilter.

@NotNull
@Override
public List<TimedVcsCommit> getCommitsMatchingFilter(@NotNull final VirtualFile root, @NotNull VcsLogFilterCollection filterCollection, int maxCount) throws VcsException {
    if (!isRepositoryReady(root)) {
        return Collections.emptyList();
    }
    List<String> filterParameters = ContainerUtil.newArrayList();
    VcsLogBranchFilter branchFilter = filterCollection.getBranchFilter();
    if (branchFilter != null) {
        GitRepository repository = getRepository(root);
        assert repository != null : "repository is null for root " + root + " but was previously reported as 'ready'";
        Collection<GitBranch> branches = ContainerUtil.newArrayList(ContainerUtil.concat(repository.getBranches().getLocalBranches(), repository.getBranches().getRemoteBranches()));
        Collection<String> branchNames = GitBranchUtil.convertBranchesToNames(branches);
        Collection<String> predefinedNames = ContainerUtil.list("HEAD");
        boolean atLeastOneBranchExists = false;
        for (String branchName : ContainerUtil.concat(branchNames, predefinedNames)) {
            if (branchFilter.matches(branchName)) {
                filterParameters.add(branchName);
                atLeastOneBranchExists = true;
            }
        }
        if (!atLeastOneBranchExists) {
            // no such branches in this repository => filter matches nothing
            return Collections.emptyList();
        }
    } else {
        filterParameters.addAll(GitHistoryUtils.LOG_ALL);
    }
    if (filterCollection.getDateFilter() != null) {
        // assuming there is only one date filter, until filter expressions are defined
        VcsLogDateFilter filter = filterCollection.getDateFilter();
        if (filter.getAfter() != null) {
            filterParameters.add(prepareParameter("after", filter.getAfter().toString()));
        }
        if (filter.getBefore() != null) {
            filterParameters.add(prepareParameter("before", filter.getBefore().toString()));
        }
    }
    boolean regexp = true;
    boolean caseSensitive = false;
    if (filterCollection.getTextFilter() != null) {
        regexp = filterCollection.getTextFilter().isRegex();
        caseSensitive = filterCollection.getTextFilter().matchesCase();
        String textFilter = filterCollection.getTextFilter().getText();
        filterParameters.add(prepareParameter("grep", textFilter));
    }
    filterParameters.add(regexp ? "--extended-regexp" : "--fixed-strings");
    if (!caseSensitive) {
        // affects case sensitivity of any filter (except file filter)
        filterParameters.add("--regexp-ignore-case");
    }
    if (filterCollection.getUserFilter() != null) {
        Collection<String> names = ContainerUtil.map(filterCollection.getUserFilter().getUsers(root), VcsUserUtil::toExactString);
        if (regexp) {
            List<String> authors = ContainerUtil.map(names, UserNameRegex.EXTENDED_INSTANCE);
            if (GitVersionSpecialty.LOG_AUTHOR_FILTER_SUPPORTS_VERTICAL_BAR.existsIn(myVcs.getVersion())) {
                filterParameters.add(prepareParameter("author", StringUtil.join(authors, "|")));
            } else {
                filterParameters.addAll(authors.stream().map(a -> prepareParameter("author", a)).collect(Collectors.toList()));
            }
        } else {
            filterParameters.addAll(ContainerUtil.map(names, a -> prepareParameter("author", StringUtil.escapeBackSlashes(a))));
        }
    }
    if (maxCount > 0) {
        filterParameters.add(prepareParameter("max-count", String.valueOf(maxCount)));
    }
    // note: structure filter must be the last parameter, because it uses "--" which separates parameters from paths
    if (filterCollection.getStructureFilter() != null) {
        Collection<FilePath> files = filterCollection.getStructureFilter().getFiles();
        if (!files.isEmpty()) {
            filterParameters.add("--full-history");
            filterParameters.add("--simplify-merges");
            filterParameters.add("--");
            for (FilePath file : files) {
                filterParameters.add(file.getPath());
            }
        }
    }
    List<TimedVcsCommit> commits = ContainerUtil.newArrayList();
    GitHistoryUtils.readCommits(myProject, root, filterParameters, EmptyConsumer.getInstance(), EmptyConsumer.getInstance(), new CollectConsumer<>(commits));
    return commits;
}
Also used : java.util(java.util) ThrowableNotNullFunction(com.intellij.openapi.util.ThrowableNotNullFunction) VirtualFile(com.intellij.openapi.vfs.VirtualFile) LogDataImpl(com.intellij.vcs.log.impl.LogDataImpl) GitVersionSpecialty(git4idea.config.GitVersionSpecialty) GitRepositoryManager(git4idea.repo.GitRepositoryManager) THashSet(gnu.trove.THashSet) VcsKey(com.intellij.openapi.vcs.VcsKey) ContainerUtil(com.intellij.util.containers.ContainerUtil) GitBranchUtil(git4idea.branch.GitBranchUtil) GraphColorManager(com.intellij.vcs.log.graph.GraphColorManager) PermanentGraphImpl(com.intellij.vcs.log.graph.impl.facade.PermanentGraphImpl) TObjectHashingStrategy(gnu.trove.TObjectHashingStrategy) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) GitHistoryUtils(git4idea.history.GitHistoryUtils) UserNameRegex(com.intellij.vcs.log.util.UserNameRegex) Project(com.intellij.openapi.project.Project) VcsFileUtil(com.intellij.vcsUtil.VcsFileUtil) Logger(com.intellij.openapi.diagnostic.Logger) VcsException(com.intellij.openapi.vcs.VcsException) GitRepository(git4idea.repo.GitRepository) FilePath(com.intellij.openapi.vcs.FilePath) git4idea(git4idea) VcsLogSorter(com.intellij.vcs.log.data.VcsLogSorter) GraphCommit(com.intellij.vcs.log.graph.GraphCommit) StringUtil(com.intellij.openapi.util.text.StringUtil) com.intellij.vcs.log(com.intellij.vcs.log) OpenTHashSet(com.intellij.util.containers.OpenTHashSet) Collectors(java.util.stream.Collectors) Disposable(com.intellij.openapi.Disposable) Nullable(org.jetbrains.annotations.Nullable) GitBranchesCollection(git4idea.branch.GitBranchesCollection) HashImpl(com.intellij.vcs.log.impl.HashImpl) StopWatch(com.intellij.vcs.log.util.StopWatch) VcsUserUtil(com.intellij.vcs.log.util.VcsUserUtil) Attachment(com.intellij.openapi.diagnostic.Attachment) Registry(com.intellij.openapi.util.registry.Registry) com.intellij.util(com.intellij.util) NotNull(org.jetbrains.annotations.NotNull) FilePath(com.intellij.openapi.vcs.FilePath) VcsUserUtil(com.intellij.vcs.log.util.VcsUserUtil) GitRepository(git4idea.repo.GitRepository) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GitRepository (git4idea.repo.GitRepository)123 VirtualFile (com.intellij.openapi.vfs.VirtualFile)46 NotNull (org.jetbrains.annotations.NotNull)33 Nullable (org.jetbrains.annotations.Nullable)19 Project (com.intellij.openapi.project.Project)18 GitCommandResult (git4idea.commands.GitCommandResult)14 GitRepositoryManager (git4idea.repo.GitRepositoryManager)12 VcsException (com.intellij.openapi.vcs.VcsException)11 AccessToken (com.intellij.openapi.application.AccessToken)9 File (java.io.File)8 Map (java.util.Map)8 GitRemote (git4idea.repo.GitRemote)7 FilePath (com.intellij.openapi.vcs.FilePath)6 Change (com.intellij.openapi.vcs.changes.Change)6 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)5 Task (com.intellij.openapi.progress.Task)5 ArrayList (java.util.ArrayList)5 ObjectUtils.assertNotNull (com.intellij.util.ObjectUtils.assertNotNull)4 MultiMap (com.intellij.util.containers.MultiMap)4 GitBranch (git4idea.GitBranch)4