Search in sources :

Example 31 with GitSimpleHandler

use of git4idea.commands.GitSimpleHandler in project intellij-community by JetBrains.

the class GitChangeUtils method parseChangeList.

/**
   * Parse changelist
   *
   *
   *
   * @param project the project
   * @param root    the git root
   * @param s       the scanner for log or show command output
   * @param skipDiffsForMerge
   * @param handler the handler that produced the output to parse. - for debugging purposes.
   * @param local   pass {@code true} to indicate that this revision should be an editable
   *                {@link com.intellij.openapi.vcs.changes.CurrentContentRevision}.
   *                Pass {@code false} for
   * @param revertable
   * @return the parsed changelist
   * @throws VcsException if there is a problem with running git
   */
public static GitCommittedChangeList parseChangeList(Project project, VirtualFile root, StringScanner s, boolean skipDiffsForMerge, GitHandler handler, boolean local, boolean revertable) throws VcsException {
    ArrayList<Change> changes = new ArrayList<>();
    // parse commit information
    final Date commitDate = GitUtil.parseTimestampWithNFEReport(s.line(), handler, s.getAllText());
    final String revisionNumber = s.line();
    final String parentsLine = s.line();
    final String[] parents = parentsLine.length() == 0 ? ArrayUtil.EMPTY_STRING_ARRAY : parentsLine.split(" ");
    String authorName = s.line();
    String committerName = s.line();
    committerName = GitUtil.adjustAuthorName(authorName, committerName);
    String commentSubject = s.boundedToken('', true);
    s.nextLine();
    String commentBody = s.boundedToken('', true);
    // construct full comment
    String fullComment;
    if (commentSubject.length() == 0) {
        fullComment = commentBody;
    } else if (commentBody.length() == 0) {
        fullComment = commentSubject;
    } else {
        fullComment = commentSubject + "\n" + commentBody;
    }
    GitRevisionNumber thisRevision = new GitRevisionNumber(revisionNumber, commitDate);
    if (skipDiffsForMerge || (parents.length <= 1)) {
        final GitRevisionNumber parentRevision = parents.length > 0 ? resolveReference(project, root, parents[0]) : null;
        // This is the first or normal commit with the single parent.
        // Just parse changes in this commit as returned by the show command.
        parseChanges(project, root, thisRevision, local ? null : parentRevision, s, changes, null);
    } else {
        for (String parent : parents) {
            final GitRevisionNumber parentRevision = resolveReference(project, root, parent);
            GitSimpleHandler diffHandler = new GitSimpleHandler(project, root, GitCommand.DIFF);
            diffHandler.setSilent(true);
            diffHandler.addParameters("--name-status", "-M", parentRevision.getRev(), thisRevision.getRev());
            String diff = diffHandler.run();
            parseChanges(project, root, thisRevision, parentRevision, diff, changes, null);
            if (changes.size() > 0) {
                break;
            }
        }
    }
    String changeListName = String.format("%s(%s)", commentSubject, revisionNumber);
    return new GitCommittedChangeList(changeListName, fullComment, committerName, thisRevision, commitDate, changes, assertNotNull(GitVcs.getInstance(project)), revertable);
}
Also used : GitRevisionNumber(git4idea.GitRevisionNumber) GitSimpleHandler(git4idea.commands.GitSimpleHandler) Change(com.intellij.openapi.vcs.changes.Change)

Example 32 with GitSimpleHandler

use of git4idea.commands.GitSimpleHandler in project intellij-community by JetBrains.

the class GitChangeUtils method getRevisionChanges.

/**
   * Get list of changes. Because native Git non-linear revision tree structure is not
   * supported by the current IDEA interfaces some simplifications are made in the case
   * of the merge, so changes are reported as difference with the first revision
   * listed on the the merge that has at least some changes.
   *
   *
   *
   * @param project      the project file
   * @param root         the git root
   * @param revisionName the name of revision (might be tag)
   * @param skipDiffsForMerge
   * @param local
   * @param revertable
   * @return change list for the respective revision
   * @throws VcsException in case of problem with running git
   */
public static GitCommittedChangeList getRevisionChanges(Project project, VirtualFile root, String revisionName, boolean skipDiffsForMerge, boolean local, boolean revertable) throws VcsException {
    GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.SHOW);
    h.setSilent(true);
    h.addParameters("--name-status", "--first-parent", "--no-abbrev", "-M", "--pretty=format:" + COMMITTED_CHANGELIST_FORMAT, "--encoding=UTF-8", revisionName, "--");
    String output = h.run();
    StringScanner s = new StringScanner(output);
    return parseChangeList(project, root, s, skipDiffsForMerge, h, local, revertable);
}
Also used : GitSimpleHandler(git4idea.commands.GitSimpleHandler) StringScanner(git4idea.util.StringScanner)

Example 33 with GitSimpleHandler

use of git4idea.commands.GitSimpleHandler in project intellij-community by JetBrains.

the class GitChangeUtils method getStagedChanges.

@NotNull
public static Collection<Change> getStagedChanges(@NotNull Project project, @NotNull VirtualFile root) throws VcsException {
    GitSimpleHandler diff = new GitSimpleHandler(project, root, GitCommand.DIFF);
    diff.addParameters("--name-status", "--cached", "-M");
    String output = diff.run();
    Collection<Change> changes = new ArrayList<>();
    parseChanges(project, root, null, GitRevisionNumber.HEAD, output, changes, Collections.emptySet());
    return changes;
}
Also used : GitSimpleHandler(git4idea.commands.GitSimpleHandler) Change(com.intellij.openapi.vcs.changes.Change) ObjectUtils.assertNotNull(com.intellij.util.ObjectUtils.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 34 with GitSimpleHandler

use of git4idea.commands.GitSimpleHandler in project intellij-community by JetBrains.

the class GitCommittedChangeListProvider method getCommittedChangesImpl.

private void getCommittedChangesImpl(ChangeBrowserSettings settings, RepositoryLocation location, final int maxCount, final Consumer<GitCommittedChangeList> consumer) throws VcsException {
    GitRepositoryLocation l = (GitRepositoryLocation) location;
    final Long beforeRev = settings.getChangeBeforeFilter();
    final Long afterRev = settings.getChangeAfterFilter();
    final Date beforeDate = settings.getDateBeforeFilter();
    final Date afterDate = settings.getDateAfterFilter();
    final String author = settings.getUserFilter();
    VirtualFile root = LocalFileSystem.getInstance().findFileByIoFile(l.getRoot());
    if (root == null) {
        throw new VcsException("The repository does not exists anymore: " + l.getRoot());
    }
    GitUtil.getLocalCommittedChanges(myProject, root, new Consumer<GitSimpleHandler>() {

        public void consume(GitSimpleHandler h) {
            if (!StringUtil.isEmpty(author)) {
                h.addParameters("--author=" + author);
            }
            if (beforeDate != null) {
                h.addParameters("--before=" + GitUtil.gitTime(beforeDate));
            }
            if (afterDate != null) {
                h.addParameters("--after=" + GitUtil.gitTime(afterDate));
            }
            if (maxCount != getUnlimitedCountValue()) {
                h.addParameters("-n" + maxCount);
            }
            if (beforeRev != null && afterRev != null) {
                h.addParameters(GitUtil.formatLongRev(afterRev) + ".." + GitUtil.formatLongRev(beforeRev));
            } else if (beforeRev != null) {
                h.addParameters(GitUtil.formatLongRev(beforeRev));
            } else if (afterRev != null) {
                h.addParameters(GitUtil.formatLongRev(afterRev) + "..");
            }
        }
    }, consumer, false);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GitSimpleHandler(git4idea.commands.GitSimpleHandler)

Example 35 with GitSimpleHandler

use of git4idea.commands.GitSimpleHandler 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)

Aggregations

GitSimpleHandler (git4idea.commands.GitSimpleHandler)42 NotNull (org.jetbrains.annotations.NotNull)10 VcsException (com.intellij.openapi.vcs.VcsException)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 ObjectUtils.assertNotNull (com.intellij.util.ObjectUtils.assertNotNull)6 StringScanner (git4idea.util.StringScanner)5 FilePath (com.intellij.openapi.vcs.FilePath)3 Change (com.intellij.openapi.vcs.changes.Change)3 GitRevisionNumber (git4idea.GitRevisionNumber)3 File (java.io.File)3 Nullable (org.jetbrains.annotations.Nullable)3 GitUtil.getLogString (git4idea.GitUtil.getLogString)2 GitRepositoryManager (git4idea.repo.GitRepositoryManager)2 Couple (com.intellij.openapi.util.Couple)1 Pair (com.intellij.openapi.util.Pair)1 Ref (com.intellij.openapi.util.Ref)1 SelectFilePathsDialog (com.intellij.openapi.vcs.changes.ui.SelectFilePathsDialog)1 VcsVirtualFile (com.intellij.openapi.vcs.vfs.VcsVirtualFile)1 Convertor (com.intellij.util.containers.Convertor)1 GitBranch (git4idea.GitBranch)1