Search in sources :

Example 11 with StringScanner

use of git4idea.util.StringScanner in project intellij-community by JetBrains.

the class GitUtil method getPathsDiffBetweenRefs.

/**
   * Returns absolute paths which have changed remotely comparing to the current branch, i.e. performs
   * <code>git diff --name-only master..origin/master</code>
   * <p/>
   * Paths are absolute, Git-formatted (i.e. with forward slashes).
   */
@NotNull
public static Collection<String> getPathsDiffBetweenRefs(@NotNull Git git, @NotNull GitRepository repository, @NotNull String beforeRef, @NotNull String afterRef) throws VcsException {
    List<String> parameters = Arrays.asList("--name-only", "--pretty=format:");
    String range = beforeRef + ".." + afterRef;
    GitCommandResult result = git.diff(repository, parameters, range);
    if (!result.success()) {
        LOG.info(String.format("Couldn't get diff in range [%s] for repository [%s]", range, repository.toLogString()));
        return Collections.emptyList();
    }
    final Collection<String> remoteChanges = new HashSet<>();
    for (StringScanner s = new StringScanner(result.getOutputAsJoinedString()); s.hasMoreData(); ) {
        final String relative = s.line();
        if (StringUtil.isEmptyOrSpaces(relative)) {
            continue;
        }
        final String path = repository.getRoot().getPath() + "/" + unescapePath(relative);
        remoteChanges.add(path);
    }
    return remoteChanges;
}
Also used : StringScanner(git4idea.util.StringScanner) OpenTHashSet(com.intellij.util.containers.OpenTHashSet) NotNull(org.jetbrains.annotations.NotNull) ObjectUtils.assertNotNull(com.intellij.util.ObjectUtils.assertNotNull)

Example 12 with StringScanner

use of git4idea.util.StringScanner in project intellij-community by JetBrains.

the class GitMergeProvider method doGetBlobPathInRevision.

@Nullable
private FilePath doGetBlobPathInRevision(@NotNull final VirtualFile root, @NotNull final String blob, @NotNull VcsRevisionNumber revision, @Nullable VirtualFile file) {
    final FilePath[] result = new FilePath[1];
    final boolean[] pathAmbiguous = new boolean[1];
    GitLineHandler h = new GitLineHandler(myProject, root, GitCommand.LS_TREE);
    h.addParameters(revision.asString());
    if (file != null) {
        h.endOptions();
        h.addRelativeFiles(Collections.singleton(file));
    } else {
        h.addParameters("-r");
        h.endOptions();
    }
    h.addLineListener(new GitLineHandlerAdapter() {

        @Override
        public void onLineAvailable(String line, Key outputType) {
            if (outputType != ProcessOutputTypes.STDOUT)
                return;
            if (!line.contains(blob))
                return;
            if (pathAmbiguous[0])
                return;
            try {
                StringScanner s = new StringScanner(line);
                // permissions
                s.spaceToken();
                // type
                String type = s.spaceToken();
                // blob
                String recordBlob = s.tabToken();
                FilePath file = VcsUtil.getFilePath(root, GitUtil.unescapePath(s.line()));
                if (!"blob".equals(type))
                    return;
                if (!blob.equals(recordBlob))
                    return;
                if (result[0] == null) {
                    result[0] = file;
                } else {
                    // there are multiple files with given content in this revision.
                    // we don't know which is right, so do not return any
                    pathAmbiguous[0] = true;
                }
            } catch (VcsException e) {
                LOG.warn(e);
            }
        }
    });
    h.runInCurrentThread(null);
    if (pathAmbiguous[0])
        return null;
    return result[0];
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) GitLineHandler(git4idea.commands.GitLineHandler) VcsException(com.intellij.openapi.vcs.VcsException) GitLineHandlerAdapter(git4idea.commands.GitLineHandlerAdapter) StringScanner(git4idea.util.StringScanner) Key(com.intellij.openapi.util.Key) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with StringScanner

use of git4idea.util.StringScanner in project intellij-community by JetBrains.

the class GitMergeProvider method getAffectedBlobs.

@NotNull
private Trinity<String, String, String> getAffectedBlobs(@NotNull VirtualFile root, @NotNull VirtualFile file) {
    try {
        GitSimpleHandler h = new GitSimpleHandler(myProject, root, GitCommand.LS_FILES);
        h.addParameters("--exclude-standard", "--unmerged", "-z");
        h.endOptions();
        h.addRelativeFiles(Collections.singleton(file));
        String output = h.run();
        StringScanner s = new StringScanner(output);
        String lastBlob = null;
        String currentBlob = null;
        String originalBlob = null;
        while (s.hasMoreData()) {
            // permissions
            s.spaceToken();
            String blob = s.spaceToken();
            // stage
            int source = Integer.parseInt(s.tabToken());
            // file name
            s.boundedToken('');
            if (source == theirsRevision(root)) {
                lastBlob = blob;
            } else if (source == yoursRevision(root)) {
                currentBlob = blob;
            } else if (source == ORIGINAL_REVISION_NUM) {
                originalBlob = blob;
            } else {
                throw new IllegalStateException("Unknown revision " + source + " for the file: " + file);
            }
        }
        return Trinity.create(currentBlob, originalBlob, lastBlob);
    } catch (VcsException e) {
        LOG.warn(e);
        return Trinity.create(null, null, null);
    }
}
Also used : GitSimpleHandler(git4idea.commands.GitSimpleHandler) VcsException(com.intellij.openapi.vcs.VcsException) StringScanner(git4idea.util.StringScanner) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with StringScanner

use of git4idea.util.StringScanner in project intellij-community by JetBrains.

the class MergeChangeCollector method getChangedFilesExceptUnmerged.

/**
   * Populates the supplied collections of modified, created and removed files returned by 'git diff #revisions' command,
   * where revisions is the range of revisions to check.
   */
public void getChangedFilesExceptUnmerged(Collection<String> updated, Collection<String> created, Collection<String> removed, String revisions) throws VcsException {
    if (revisions == null) {
        return;
    }
    String root = myRoot.getPath();
    GitSimpleHandler h = new GitSimpleHandler(myProject, myRoot, GitCommand.DIFF);
    h.setSilent(true);
    // note that moves are not detected here
    h.addParameters("--name-status", "--diff-filter=ADMRUX", "--no-renames", revisions);
    for (StringScanner s = new StringScanner(h.run()); s.hasMoreData(); ) {
        if (s.isEol()) {
            s.nextLine();
            continue;
        }
        char status = s.peek();
        s.boundedToken('\t');
        final String relative = s.line();
        // eliminate conflicts
        if (myUnmergedPaths.contains(relative)) {
            continue;
        }
        String path = root + "/" + GitUtil.unescapePath(relative);
        switch(status) {
            case 'M':
                updated.add(path);
                break;
            case 'A':
                created.add(path);
                break;
            case 'D':
                removed.add(path);
                break;
            default:
                throw new IllegalStateException("Unexpected status: " + status);
        }
    }
}
Also used : GitSimpleHandler(git4idea.commands.GitSimpleHandler) StringScanner(git4idea.util.StringScanner)

Example 15 with StringScanner

use of git4idea.util.StringScanner in project intellij-community by JetBrains.

the class MergeChangeCollector method getRevisionsForDiff.

/**
   * @return The revision range which will be used to find merge diff (merge may be just finished, or in progress)
   * or null in case of error or inconsistency.
   */
@Nullable
public String getRevisionsForDiff() throws VcsException {
    String root = myRoot.getPath();
    GitRevisionNumber currentHead = GitRevisionNumber.resolve(myProject, myRoot, "HEAD");
    if (currentHead.equals(myStart)) {
        // The head has not advanced. This means that this is a merge that did not commit.
        // This could be caused by --no-commit option or by failed two-head merge. The MERGE_HEAD
        // should be available. In case of --no-commit option, the MERGE_HEAD might contain
        // multiple heads separated by newline. The changes are collected separately for each head
        // and they are merged using TreeSet class (that also sorts the changes).
        File mergeHeadsFile = myRepository.getRepositoryFiles().getMergeHeadFile();
        try {
            if (mergeHeadsFile.exists()) {
                String mergeHeads = new String(FileUtil.loadFileText(mergeHeadsFile, CharsetToolkit.UTF8));
                for (StringScanner s = new StringScanner(mergeHeads); s.hasMoreData(); ) {
                    String head = s.line();
                    if (head.length() == 0) {
                        continue;
                    }
                    // note that "..." cause the diff to start from common parent between head and merge head
                    return myStart.getRev() + "..." + head;
                }
            }
        } catch (IOException e) {
            //noinspection ThrowableInstanceNeverThrown
            throw new VcsException("Unable to read the file " + mergeHeadsFile + ": " + e.getMessage(), e);
        }
    } else {
        // and the expression below considers it as well.
        return myStart.getRev() + "..HEAD";
    }
    return null;
}
Also used : GitRevisionNumber(git4idea.GitRevisionNumber) VcsException(com.intellij.openapi.vcs.VcsException) IOException(java.io.IOException) StringScanner(git4idea.util.StringScanner) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

StringScanner (git4idea.util.StringScanner)15 VcsException (com.intellij.openapi.vcs.VcsException)6 GitSimpleHandler (git4idea.commands.GitSimpleHandler)5 NotNull (org.jetbrains.annotations.NotNull)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 GitRevisionNumber (git4idea.GitRevisionNumber)3 FilePath (com.intellij.openapi.vcs.FilePath)2 ObjectUtils.assertNotNull (com.intellij.util.ObjectUtils.assertNotNull)2 File (java.io.File)2 Nullable (org.jetbrains.annotations.Nullable)2 Attachment (com.intellij.openapi.diagnostic.Attachment)1 Key (com.intellij.openapi.util.Key)1 Change (com.intellij.openapi.vcs.changes.Change)1 ContentRevision (com.intellij.openapi.vcs.changes.ContentRevision)1 Interner (com.intellij.util.containers.Interner)1 OpenTHashSet (com.intellij.util.containers.OpenTHashSet)1 VcsUser (com.intellij.vcs.log.VcsUser)1 GitContentRevision (git4idea.GitContentRevision)1 LineInfo (git4idea.annotate.GitFileAnnotation.LineInfo)1 GitCommandResult (git4idea.commands.GitCommandResult)1