Search in sources :

Example 56 with GitRepository

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

the class GitMergeUpdater method isSaveNeeded.

@Override
public boolean isSaveNeeded() {
    try {
        if (GitUtil.hasLocalChanges(true, myProject, myRoot)) {
            return true;
        }
    } catch (VcsException e) {
        LOG.info("isSaveNeeded failed to check staging area", e);
        return true;
    }
    // git log --name-status master..origin/master
    GitBranchPair gitBranchPair = myTrackedBranches.get(myRoot);
    String currentBranch = gitBranchPair.getBranch().getName();
    String remoteBranch = gitBranchPair.getDest().getName();
    try {
        GitRepository repository = GitUtil.getRepositoryManager(myProject).getRepositoryForRoot(myRoot);
        if (repository == null) {
            LOG.error("Repository is null for root " + myRoot);
            // fail safe
            return true;
        }
        final Collection<String> remotelyChanged = GitUtil.getPathsDiffBetweenRefs(Git.getInstance(), repository, currentBranch, remoteBranch);
        final List<File> locallyChanged = myChangeListManager.getAffectedPaths();
        for (final File localPath : locallyChanged) {
            if (ContainerUtil.exists(remotelyChanged, new Condition<String>() {

                @Override
                public boolean value(String remotelyChangedPath) {
                    return FileUtil.pathsEqual(localPath.getPath(), remotelyChangedPath);
                }
            })) {
                // found a file which was changed locally and remotely => need to save
                return true;
            }
        }
        return false;
    } catch (VcsException e) {
        LOG.info("failed to get remotely changed files for " + currentBranch + ".." + remoteBranch, e);
        // fail safe
        return true;
    }
}
Also used : GitRepository(git4idea.repo.GitRepository) GitBranchPair(git4idea.branch.GitBranchPair) VcsException(com.intellij.openapi.vcs.VcsException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 57 with GitRepository

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

the class GitUnstashDialog method refreshStashList.

private void refreshStashList() {
    final DefaultListModel listModel = (DefaultListModel) myStashList.getModel();
    listModel.clear();
    VirtualFile root = getGitRoot();
    GitStashUtils.loadStashStack(myProject, root, new Consumer<StashInfo>() {

        @Override
        public void consume(StashInfo stashInfo) {
            listModel.addElement(stashInfo);
        }
    });
    myBranches.clear();
    GitRepository repository = GitUtil.getRepositoryManager(myProject).getRepositoryForRoot(root);
    if (repository != null) {
        myBranches.addAll(GitBranchUtil.convertBranchesToNames(repository.getBranches().getLocalBranches()));
    } else {
        LOG.error("Repository is null for root " + root);
    }
    myStashList.setSelectedIndex(0);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GitRepository(git4idea.repo.GitRepository)

Example 58 with GitRepository

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

the class GitBranchPopup method getInstance.

/**
   * @param currentRepository Current repository, which means the repository of the currently open or selected file.
   *                          In the case of synchronized branch operations current repository matter much less, but sometimes is used,
   *                          for example, it is preselected in the repositories combobox in the compare branches dialog.
   */
static GitBranchPopup getInstance(@NotNull final Project project, @NotNull GitRepository currentRepository) {
    final GitVcsSettings vcsSettings = GitVcsSettings.getInstance(project);
    Condition<AnAction> preselectActionCondition = action -> {
        if (action instanceof GitBranchPopupActions.LocalBranchActions) {
            GitBranchPopupActions.LocalBranchActions branchAction = (GitBranchPopupActions.LocalBranchActions) action;
            String branchName = branchAction.getBranchName();
            String recentBranch;
            List<GitRepository> repositories = branchAction.getRepositories();
            if (repositories.size() == 1) {
                recentBranch = vcsSettings.getRecentBranchesByRepository().get(repositories.iterator().next().getRoot().getPath());
            } else {
                recentBranch = vcsSettings.getRecentCommonBranch();
            }
            if (recentBranch != null && recentBranch.equals(branchName)) {
                return true;
            }
        }
        return false;
    };
    return new GitBranchPopup(currentRepository, GitUtil.getRepositoryManager(project), vcsSettings, preselectActionCondition);
}
Also used : GitRepositoryManager(git4idea.repo.GitRepositoryManager) BranchActionGroupPopup.wrapWithMoreActionIfNeeded(com.intellij.dvcs.ui.BranchActionGroupPopup.wrapWithMoreActionIfNeeded) GitVcsSettings(git4idea.config.GitVcsSettings) ContainerUtil(com.intellij.util.containers.ContainerUtil) RootAction(com.intellij.dvcs.ui.RootAction) GitBranchUtil(git4idea.branch.GitBranchUtil) AbstractRepositoryManager(com.intellij.dvcs.repo.AbstractRepositoryManager) GitUtil(git4idea.GitUtil) DvcsUtil(com.intellij.dvcs.DvcsUtil) Project(com.intellij.openapi.project.Project) GitRepository(git4idea.repo.GitRepository) BranchActionGroup(com.intellij.dvcs.ui.BranchActionGroup) DvcsBranchPopup(com.intellij.dvcs.branch.DvcsBranchPopup) DEFAULT_NUM(com.intellij.dvcs.branch.DvcsBranchPopup.MyMoreIndex.DEFAULT_NUM) AnAction(com.intellij.openapi.actionSystem.AnAction) BranchActionUtil.getNumOfTopShownBranches(com.intellij.dvcs.ui.BranchActionUtil.getNumOfTopShownBranches) ActionGroup(com.intellij.openapi.actionSystem.ActionGroup) DefaultActionGroup(com.intellij.openapi.actionSystem.DefaultActionGroup) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) MAX_NUM(com.intellij.dvcs.branch.DvcsBranchPopup.MyMoreIndex.MAX_NUM) ContainerUtil.map(com.intellij.util.containers.ContainerUtil.map) NotNull(org.jetbrains.annotations.NotNull) FAVORITE_BRANCH_COMPARATOR(com.intellij.dvcs.ui.BranchActionUtil.FAVORITE_BRANCH_COMPARATOR) Condition(com.intellij.openapi.util.Condition) javax.swing(javax.swing) GitVcsSettings(git4idea.config.GitVcsSettings) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) AnAction(com.intellij.openapi.actionSystem.AnAction)

Example 59 with GitRepository

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

the class GitBranchPopupActions method createActions.

ActionGroup createActions(@Nullable DefaultActionGroup toInsert, @NotNull String repoInfo, boolean firstLevelGroup) {
    DefaultActionGroup popupGroup = new DefaultActionGroup(null, false);
    List<GitRepository> repositoryList = Collections.singletonList(myRepository);
    popupGroup.addAction(new GitNewBranchAction(myProject, repositoryList));
    popupGroup.addAction(new CheckoutRevisionActions(myProject, repositoryList));
    if (toInsert != null) {
        popupGroup.addAll(toInsert);
    }
    popupGroup.addSeparator("Local Branches" + repoInfo);
    List<BranchActionGroup> localBranchActions = myRepository.getBranches().getLocalBranches().stream().sorted().filter(branch -> !branch.equals(myRepository.getCurrentBranch())).map(branch -> new LocalBranchActions(myProject, repositoryList, branch.getName(), myRepository)).collect(toList());
    // if there are only a few local favorites -> show all;  for remotes it's better to show only favorites; 
    wrapWithMoreActionIfNeeded(myProject, popupGroup, ContainerUtil.sorted(localBranchActions, FAVORITE_BRANCH_COMPARATOR), getNumOfTopShownBranches(localBranchActions), firstLevelGroup ? GitBranchPopup.SHOW_ALL_LOCALS_KEY : null, firstLevelGroup);
    popupGroup.addSeparator("Remote Branches" + repoInfo);
    List<BranchActionGroup> remoteBranchActions = myRepository.getBranches().getRemoteBranches().stream().sorted().map(remoteBranch -> new RemoteBranchActions(myProject, repositoryList, remoteBranch.getName(), myRepository)).collect(toList());
    wrapWithMoreActionIfNeeded(myProject, popupGroup, ContainerUtil.sorted(remoteBranchActions, FAVORITE_BRANCH_COMPARATOR), getNumOfTopShownBranches(remoteBranchActions), firstLevelGroup ? GitBranchPopup.SHOW_ALL_REMOTES_KEY : null);
    return popupGroup;
}
Also used : BranchActionGroupPopup.wrapWithMoreActionIfNeeded(com.intellij.dvcs.ui.BranchActionGroupPopup.wrapWithMoreActionIfNeeded) ContainerUtil(com.intellij.util.containers.ContainerUtil) GitBranchUtil(git4idea.branch.GitBranchUtil) GitNewBranchOptions(git4idea.branch.GitNewBranchOptions) Project(com.intellij.openapi.project.Project) Messages(com.intellij.openapi.ui.Messages) GitStatisticsCollectorKt.reportUsage(git4idea.GitStatisticsCollectorKt.reportUsage) GitRepository(git4idea.repo.GitRepository) BranchActionGroup(com.intellij.dvcs.ui.BranchActionGroup) HEAD(git4idea.GitUtil.HEAD) PopupElementWithAdditionalInfo(com.intellij.dvcs.ui.PopupElementWithAdditionalInfo) GitBrancher(git4idea.branch.GitBrancher) AnAction(com.intellij.openapi.actionSystem.AnAction) BranchActionUtil.getNumOfTopShownBranches(com.intellij.dvcs.ui.BranchActionUtil.getNumOfTopShownBranches) ActionGroup(com.intellij.openapi.actionSystem.ActionGroup) DefaultActionGroup(com.intellij.openapi.actionSystem.DefaultActionGroup) GitNewBranchNameValidator(git4idea.validators.GitNewBranchNameValidator) LOCAL(git4idea.branch.GitBranchType.LOCAL) Nullable(org.jetbrains.annotations.Nullable) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ServiceManager(com.intellij.openapi.components.ServiceManager) NewBranchAction(com.intellij.dvcs.ui.NewBranchAction) StreamEx(one.util.streamex.StreamEx) REMOTE(git4idea.branch.GitBranchType.REMOTE) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) NotNull(org.jetbrains.annotations.NotNull) FAVORITE_BRANCH_COMPARATOR(com.intellij.dvcs.ui.BranchActionUtil.FAVORITE_BRANCH_COMPARATOR) Collections(java.util.Collections) GitRepository(git4idea.repo.GitRepository) BranchActionGroup(com.intellij.dvcs.ui.BranchActionGroup) DefaultActionGroup(com.intellij.openapi.actionSystem.DefaultActionGroup)

Example 60 with GitRepository

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

the class GitUpdateProcess method update.

/**
   * Checks if update is possible, saves local changes and updates all roots.
   * In case of error shows notification and returns false. If update completes without errors, returns true.
   *
   * Perform update on all roots.
   * 0. Blocks reloading project on external change, saving/syncing on frame deactivation.
   * 1. Checks if update is possible (rebase/merge in progress, no tracked branches...) and provides merge dialog to solve problems.
   * 2. Finds updaters to use (merge or rebase).
   * 3. Preserves local changes if needed (not needed for merge sometimes).
   * 4. Updates via 'git pull' or equivalent.
   * 5. Restores local changes if update completed or failed with error. If update is incomplete, i.e. some unmerged files remain,
   * local changes are not restored.
   *
   */
@NotNull
public GitUpdateResult update(final UpdateMethod updateMethod) {
    LOG.info("update started|" + updateMethod);
    String oldText = myProgressIndicator.getText();
    myProgressIndicator.setText("Updating...");
    for (GitRepository repository : myRepositories) {
        repository.update();
    }
    // check if update is possible
    if (checkRebaseInProgress() || isMergeInProgress() || areUnmergedFiles() || !checkTrackedBranchesConfigured()) {
        return GitUpdateResult.NOT_READY;
    }
    if (!fetchAndNotify()) {
        return GitUpdateResult.NOT_READY;
    }
    AccessToken token = DvcsUtil.workingTreeChangeStarted(myProject);
    GitUpdateResult result;
    try {
        result = updateImpl(updateMethod);
    } finally {
        token.finish();
    }
    myProgressIndicator.setText(oldText);
    return result;
}
Also used : GitRepository(git4idea.repo.GitRepository) AccessToken(com.intellij.openapi.application.AccessToken) 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