Search in sources :

Example 31 with GitRepository

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

the class GitCherryPicker method getInfo.

@Override
public String getInfo(@NotNull VcsLog log, @NotNull Map<VirtualFile, List<Hash>> commits) {
    int commitsNum = commits.values().size();
    for (VirtualFile root : commits.keySet()) {
        // all these roots already related to this cherry-picker
        GitRepository repository = ObjectUtils.assertNotNull(myRepositoryManager.getRepositoryForRoot(root));
        for (Hash commit : commits.get(root)) {
            GitLocalBranch currentBranch = repository.getCurrentBranch();
            Collection<String> containingBranches = log.getContainingBranches(commit, root);
            if (currentBranch != null && containingBranches != null && containingBranches.contains(currentBranch.getName())) {
                // already in the current branch
                return String.format("The current branch already contains %s the selected %s", commitsNum > 1 ? "one of" : "", pluralize("commit", commitsNum));
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GitRepository(git4idea.repo.GitRepository) GitLocalBranch(git4idea.GitLocalBranch) Hash(com.intellij.vcs.log.Hash)

Example 32 with GitRepository

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

the class GitRefManager method groupForTable.

@NotNull
@Override
public List<RefGroup> groupForTable(@NotNull Collection<VcsRef> references, boolean compact, boolean showTagNames) {
    List<VcsRef> sortedReferences = ContainerUtil.sorted(references, myLabelsComparator);
    MultiMap<VcsRefType, VcsRef> groupedRefs = ContainerUtil.groupBy(sortedReferences, VcsRef::getType);
    List<RefGroup> result = ContainerUtil.newArrayList();
    if (groupedRefs.isEmpty())
        return result;
    VcsRef head = null;
    Map.Entry<VcsRefType, Collection<VcsRef>> firstGroup = ObjectUtils.notNull(ContainerUtil.getFirstItem(groupedRefs.entrySet()));
    if (firstGroup.getKey().equals(HEAD)) {
        head = ObjectUtils.assertNotNull(ContainerUtil.getFirstItem(firstGroup.getValue()));
        groupedRefs.remove(HEAD, head);
    }
    GitRepository repository = getRepository(references);
    if (repository != null) {
        result.addAll(getTrackedRefs(groupedRefs, repository));
    }
    result.forEach(refGroup -> {
        groupedRefs.remove(LOCAL_BRANCH, refGroup.getRefs().get(0));
        groupedRefs.remove(REMOTE_BRANCH, refGroup.getRefs().get(1));
    });
    SimpleRefGroup.buildGroups(groupedRefs, compact, showTagNames, result);
    if (head != null) {
        if (repository != null && !repository.isOnBranch()) {
            result.add(0, new SimpleRefGroup("!", Collections.singletonList(head)));
        } else {
            if (!result.isEmpty()) {
                RefGroup first = ObjectUtils.assertNotNull(ContainerUtil.getFirstItem(result));
                first.getRefs().add(0, head);
            } else {
                result.add(0, new SimpleRefGroup("", Collections.singletonList(head)));
            }
        }
    }
    return result;
}
Also used : GitRepository(git4idea.repo.GitRepository) SimpleRefGroup(com.intellij.vcs.log.impl.SimpleRefGroup) MultiMap(com.intellij.util.containers.MultiMap) SimpleRefGroup(com.intellij.vcs.log.impl.SimpleRefGroup) SingletonRefGroup(com.intellij.vcs.log.impl.SingletonRefGroup) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with GitRepository

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

the class GitConflictResolver method unmergedFiles.

/**
   * Parse changes from lines
   *
   *
   * @param root    the git root
   * @return a set of unmerged files
   * @throws com.intellij.openapi.vcs.VcsException if the input format does not matches expected format
   */
private List<VirtualFile> unmergedFiles(final VirtualFile root) throws VcsException {
    GitRepository repository = myRepositoryManager.getRepositoryForRoot(root);
    if (repository == null) {
        LOG.error("Repository not found for root " + root);
        return Collections.emptyList();
    }
    GitCommandResult result = myGit.getUnmergedFiles(repository);
    if (!result.success()) {
        throw new VcsException(result.getErrorOutputAsJoinedString());
    }
    String output = StringUtil.join(result.getOutput(), "\n");
    HashSet<String> unmergedPaths = ContainerUtil.newHashSet();
    for (StringScanner s = new StringScanner(output); s.hasMoreData(); ) {
        if (s.isEol()) {
            s.nextLine();
            continue;
        }
        s.boundedToken('\t');
        String relative = s.line();
        unmergedPaths.add(GitUtil.unescapePath(relative));
    }
    if (unmergedPaths.size() == 0) {
        return Collections.emptyList();
    } else {
        List<File> files = ContainerUtil.map(unmergedPaths, new Function<String, File>() {

            @Override
            public File fun(String path) {
                return new File(root.getPath(), path);
            }
        });
        return sortVirtualFilesByPresentation(findVirtualFilesWithRefresh(files));
    }
}
Also used : GitRepository(git4idea.repo.GitRepository) VcsException(com.intellij.openapi.vcs.VcsException) GitCommandResult(git4idea.commands.GitCommandResult) StringScanner(git4idea.util.StringScanner) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 34 with GitRepository

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

the class GitResetOperation method execute.

public void execute() {
    saveAllDocuments();
    AccessToken token = DvcsUtil.workingTreeChangeStarted(myProject);
    Map<GitRepository, GitCommandResult> results = ContainerUtil.newHashMap();
    try {
        for (Map.Entry<GitRepository, Hash> entry : myCommits.entrySet()) {
            GitRepository repository = entry.getKey();
            VirtualFile root = repository.getRoot();
            String target = entry.getValue().asString();
            GitLocalChangesWouldBeOverwrittenDetector detector = new GitLocalChangesWouldBeOverwrittenDetector(root, RESET);
            GitCommandResult result = myGit.reset(repository, myMode, target, detector);
            if (!result.success() && detector.wasMessageDetected()) {
                GitCommandResult smartResult = proposeSmartReset(detector, repository, target);
                if (smartResult != null) {
                    result = smartResult;
                }
            }
            results.put(repository, result);
            repository.update();
            VfsUtil.markDirtyAndRefresh(false, true, false, root);
            VcsDirtyScopeManager.getInstance(myProject).dirDirtyRecursively(root);
        }
    } finally {
        token.finish();
    }
    notifyResult(results);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GitRepository(git4idea.repo.GitRepository) GitLocalChangesWouldBeOverwrittenDetector(git4idea.commands.GitLocalChangesWouldBeOverwrittenDetector) AccessToken(com.intellij.openapi.application.AccessToken) GitCommandResult(git4idea.commands.GitCommandResult) Hash(com.intellij.vcs.log.Hash) Map(java.util.Map) MultiMap(com.intellij.util.containers.MultiMap)

Example 35 with GitRepository

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

the class GitResetOperation method notifyResult.

private void notifyResult(@NotNull Map<GitRepository, GitCommandResult> results) {
    Map<GitRepository, GitCommandResult> successes = ContainerUtil.newHashMap();
    Map<GitRepository, GitCommandResult> errors = ContainerUtil.newHashMap();
    for (Map.Entry<GitRepository, GitCommandResult> entry : results.entrySet()) {
        GitCommandResult result = entry.getValue();
        GitRepository repository = entry.getKey();
        if (result.success()) {
            successes.put(repository, result);
        } else {
            errors.put(repository, result);
        }
    }
    if (errors.isEmpty()) {
        myNotifier.notifySuccess("", "Reset successful");
    } else if (!successes.isEmpty()) {
        myNotifier.notifyImportantWarning("Reset partially failed", "Reset was successful for " + joinRepos(successes.keySet()) + "<br/>but failed for " + joinRepos(errors.keySet()) + ": <br/>" + formErrorReport(errors));
    } else {
        myNotifier.notifyError("Reset Failed", formErrorReport(errors));
    }
}
Also used : GitRepository(git4idea.repo.GitRepository) GitCommandResult(git4idea.commands.GitCommandResult) Map(java.util.Map) MultiMap(com.intellij.util.containers.MultiMap)

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