Search in sources :

Example 26 with GitRepository

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

the class GitCheckinEnvironment method commit.

public List<VcsException> commit(@NotNull List<Change> changes, @NotNull String message, @NotNull NullableFunction<Object, Object> parametersHolder, Set<String> feedback) {
    List<VcsException> exceptions = new ArrayList<>();
    Map<VirtualFile, Collection<Change>> sortedChanges = sortChangesByGitRoot(changes, exceptions);
    LOG.assertTrue(!sortedChanges.isEmpty(), "Trying to commit an empty list of changes: " + changes);
    for (Map.Entry<VirtualFile, Collection<Change>> entry : sortedChanges.entrySet()) {
        VirtualFile root = entry.getKey();
        File messageFile;
        try {
            messageFile = createMessageFile(root, message);
        } catch (IOException ex) {
            //noinspection ThrowableInstanceNeverThrown
            exceptions.add(new VcsException("Creation of commit message file failed", ex));
            continue;
        }
        Set<FilePath> added = new HashSet<>();
        Set<FilePath> removed = new HashSet<>();
        final Set<Change> caseOnlyRenames = new HashSet<>();
        for (Change change : entry.getValue()) {
            switch(change.getType()) {
                case NEW:
                case MODIFICATION:
                    added.add(change.getAfterRevision().getFile());
                    break;
                case DELETED:
                    removed.add(change.getBeforeRevision().getFile());
                    break;
                case MOVED:
                    FilePath afterPath = change.getAfterRevision().getFile();
                    FilePath beforePath = change.getBeforeRevision().getFile();
                    if (!SystemInfo.isFileSystemCaseSensitive && GitUtil.isCaseOnlyChange(beforePath.getPath(), afterPath.getPath())) {
                        caseOnlyRenames.add(change);
                    } else {
                        added.add(afterPath);
                        removed.add(beforePath);
                    }
                    break;
                default:
                    throw new IllegalStateException("Unknown change type: " + change.getType());
            }
        }
        try {
            if (!caseOnlyRenames.isEmpty()) {
                List<VcsException> exs = commitWithCaseOnlyRename(myProject, root, caseOnlyRenames, added, removed, messageFile, myNextCommitAuthor);
                exceptions.addAll(map(exs, GitCheckinEnvironment::cleanupExceptionText));
            } else {
                try {
                    Set<FilePath> files = new HashSet<>();
                    files.addAll(added);
                    files.addAll(removed);
                    commit(myProject, root, files, messageFile);
                } catch (VcsException ex) {
                    PartialOperation partialOperation = isMergeCommit(ex);
                    if (partialOperation == PartialOperation.NONE) {
                        throw ex;
                    }
                    if (!mergeCommit(myProject, root, added, removed, messageFile, myNextCommitAuthor, exceptions, partialOperation)) {
                        throw ex;
                    }
                }
            }
        } catch (VcsException e) {
            exceptions.add(cleanupExceptionText(e));
        } finally {
            if (!messageFile.delete()) {
                LOG.warn("Failed to remove temporary file: " + messageFile);
            }
        }
    }
    if (myNextCommitIsPushed != null && myNextCommitIsPushed.booleanValue() && exceptions.isEmpty()) {
        GitRepositoryManager manager = getRepositoryManager(myProject);
        Collection<GitRepository> repositories = GitUtil.getRepositoriesFromRoots(manager, sortedChanges.keySet());
        final List<GitRepository> preselectedRepositories = newArrayList(repositories);
        GuiUtils.invokeLaterIfNeeded(() -> new VcsPushDialog(myProject, preselectedRepositories, GitBranchUtil.getCurrentRepository(myProject)).show(), ModalityState.defaultModalityState());
    }
    return exceptions;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FilePath(com.intellij.openapi.vcs.FilePath) GitRepositoryManager(git4idea.repo.GitRepositoryManager) GitRepository(git4idea.repo.GitRepository) VcsPushDialog(com.intellij.dvcs.push.ui.VcsPushDialog) VcsException(com.intellij.openapi.vcs.VcsException) VirtualFile(com.intellij.openapi.vfs.VirtualFile)

Example 27 with GitRepository

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

the class GitCrlfProblemsDetector method findFilesWithoutAttrs.

@NotNull
private Collection<VirtualFile> findFilesWithoutAttrs(@NotNull VirtualFile root, @NotNull Collection<VirtualFile> files) {
    GitRepository repository = myRepositoryManager.getRepositoryForRoot(root);
    if (repository == null) {
        LOG.warn("Repository is null for " + root);
        return Collections.emptyList();
    }
    Collection<String> interestingAttributes = Arrays.asList(GitAttribute.TEXT.getName(), GitAttribute.CRLF.getName());
    GitCommandResult result = myGit.checkAttr(repository, interestingAttributes, files);
    if (!result.success()) {
        LOG.warn(String.format("Couldn't git check-attr. Attributes: %s, files: %s", interestingAttributes, files));
        return Collections.emptyList();
    }
    GitCheckAttrParser parser = GitCheckAttrParser.parse(result.getOutput());
    Map<String, Collection<GitAttribute>> attributes = parser.getAttributes();
    Collection<VirtualFile> filesWithoutAttrs = new ArrayList<>();
    for (VirtualFile file : files) {
        ProgressIndicatorProvider.checkCanceled();
        String relativePath = FileUtil.getRelativePath(root.getPath(), file.getPath(), '/');
        Collection<GitAttribute> attrs = attributes.get(relativePath);
        if (attrs == null || !attrs.contains(GitAttribute.TEXT) && !attrs.contains(GitAttribute.CRLF)) {
            filesWithoutAttrs.add(file);
        }
    }
    return filesWithoutAttrs;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GitRepository(git4idea.repo.GitRepository) GitCheckAttrParser(git4idea.attributes.GitCheckAttrParser) GitAttribute(git4idea.attributes.GitAttribute) GitCommandResult(git4idea.commands.GitCommandResult) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with GitRepository

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

the class GitCrlfProblemsDetector method isAutoCrlfSetRight.

private boolean isAutoCrlfSetRight(@NotNull VirtualFile root) {
    GitRepository repository = myRepositoryManager.getRepositoryForRoot(root);
    if (repository == null) {
        LOG.warn("Repository is null for " + root);
        return true;
    }
    GitCommandResult result = myGit.config(repository, GitConfigUtil.CORE_AUTOCRLF);
    String value = result.getOutputAsJoinedString();
    return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("input");
}
Also used : GitRepository(git4idea.repo.GitRepository) GitCommandResult(git4idea.commands.GitCommandResult)

Example 29 with GitRepository

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

the class GitDiffFromHistoryHandler method getChangesBetweenRevisions.

@NotNull
@Override
protected List<Change> getChangesBetweenRevisions(@NotNull FilePath path, @NotNull GitFileRevision rev1, @Nullable GitFileRevision rev2) throws VcsException {
    GitRepository repository = getRepository(path);
    String hash1 = rev1.getHash();
    String hash2 = rev2 != null ? rev2.getHash() : null;
    return ContainerUtil.newArrayList(GitChangeUtils.getDiff(repository.getProject(), repository.getRoot(), hash1, hash2, Collections.singletonList(path)));
}
Also used : GitRepository(git4idea.repo.GitRepository) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with GitRepository

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

the class GitAbstractRebaseAction method chooseRepository.

@Nullable
private GitRepository chooseRepository(@NotNull Project project, @NotNull Collection<GitRepository> repositories) {
    GitRepository firstRepo = assertNotNull(ContainerUtil.getFirstItem(repositories));
    if (repositories.size() == 1)
        return firstRepo;
    ArrayList<VirtualFile> roots = newArrayList(getRootsFromRepositories(repositories));
    GitRebaseActionDialog dialog = new GitRebaseActionDialog(project, getTemplatePresentation().getText(), roots, firstRepo.getRoot());
    dialog.show();
    VirtualFile root = dialog.selectRoot();
    if (root == null)
        return null;
    // TODO avoid root <-> GitRepository double conversion
    return getRepositoryManager(project).getRepositoryForRootQuick(root);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GitRepository(git4idea.repo.GitRepository) GitRebaseActionDialog(git4idea.rebase.GitRebaseActionDialog) Nullable(org.jetbrains.annotations.Nullable)

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