Search in sources :

Example 1 with StringScanner

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

the class GitAnnotationProvider method parseAnnotations.

@NotNull
private GitFileAnnotation parseAnnotations(@Nullable VcsRevisionNumber revision, @NotNull VirtualFile file, @NotNull VirtualFile root, @NotNull String output) throws VcsException {
    Interner<FilePath> pathInterner = new Interner<>();
    try {
        List<LineInfo> lines = new ArrayList<>();
        HashMap<String, LineInfo> commits = new HashMap<>();
        for (StringScanner s = new StringScanner(output); s.hasMoreData(); ) {
            // parse header line
            String commitHash = s.spaceToken();
            if (commitHash.equals(GitRevisionNumber.NOT_COMMITTED_HASH)) {
                commitHash = null;
            }
            // skip revision line number
            s.spaceToken();
            String s1 = s.spaceToken();
            int lineNum = Integer.parseInt(s1);
            s.nextLine();
            // parse commit information
            LineInfo commit = commits.get(commitHash);
            if (commit != null || commitHash == null) {
                while (s.hasMoreData() && !s.startsWith('\t')) {
                    s.nextLine();
                }
            } else {
                Date committerDate = null;
                FilePath filePath = null;
                String subject = null;
                String authorName = null;
                String authorEmail = null;
                String previousRevision = null;
                FilePath previousFilePath = null;
                while (s.hasMoreData() && !s.startsWith('\t')) {
                    String key = s.spaceToken();
                    String value = s.line();
                    if (SUBJECT_KEY.equals(key)) {
                        subject = value;
                    } else if (AUTHOR_KEY.equals(key)) {
                        authorName = value;
                    } else if (COMMITTER_TIME_KEY.equals(key)) {
                        committerDate = GitUtil.parseTimestamp(value);
                    } else if (FILENAME_KEY.equals(key)) {
                        filePath = VcsUtil.getFilePath(root, value);
                    } else if (AUTHOR_EMAIL_KEY.equals(key)) {
                        authorEmail = value;
                        if (authorEmail.startsWith("<") && authorEmail.endsWith(">")) {
                            authorEmail = authorEmail.substring(1, authorEmail.length() - 1);
                        }
                    } else if (PREVIOUS_KEY.equals(key)) {
                        int index = value.indexOf(' ');
                        if (index != -1) {
                            previousRevision = value.substring(0, index);
                            previousFilePath = VcsUtil.getFilePath(root, value.substring(index + 1, value.length()));
                        }
                    }
                }
                if (committerDate == null || filePath == null || authorName == null || authorEmail == null || subject == null) {
                    throw new VcsException("Output for line " + lineNum + " lacks necessary data");
                }
                GitRevisionNumber revisionNumber = new GitRevisionNumber(commitHash, committerDate);
                VcsUser author = myUserRegistry.createUser(authorName, authorEmail);
                GitRevisionNumber previousRevisionNumber = previousRevision != null ? new GitRevisionNumber(previousRevision) : null;
                filePath = pathInterner.intern(filePath);
                if (previousFilePath != null)
                    previousFilePath = pathInterner.intern(previousFilePath);
                commit = new LineInfo(myProject, revisionNumber, filePath, committerDate, author, subject, previousRevisionNumber, previousFilePath);
                commits.put(commitHash, commit);
            }
            s.nextLine();
            int expectedLineNum = lines.size() + 1;
            if (lineNum != expectedLineNum) {
                throw new VcsException("Adding for info for line " + lineNum + " but we are expecting it to be for " + expectedLineNum);
            }
            lines.add(commit);
        }
        return new GitFileAnnotation(myProject, file, revision, lines);
    } catch (Exception e) {
        LOG.error("Couldn't parse annotation: " + e, new Attachment("output.txt", output));
        throw new VcsException(e);
    }
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) Interner(com.intellij.util.containers.Interner) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Attachment(com.intellij.openapi.diagnostic.Attachment) LineInfo(git4idea.annotate.GitFileAnnotation.LineInfo) Date(java.util.Date) VcsException(com.intellij.openapi.vcs.VcsException) GitRevisionNumber(git4idea.GitRevisionNumber) VcsUser(com.intellij.vcs.log.VcsUser) VcsException(com.intellij.openapi.vcs.VcsException) StringScanner(git4idea.util.StringScanner) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with StringScanner

use of git4idea.util.StringScanner 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 3 with StringScanner

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

the class MergeChangeCollector method getUnmergedPaths.

/**
   * Returns absolute paths to files which are currently unmerged, and also populates myUnmergedPaths with relative paths.
   */
@NotNull
public Set<String> getUnmergedPaths() throws VcsException {
    String root = myRoot.getPath();
    final GitSimpleHandler h = new GitSimpleHandler(myProject, myRoot, GitCommand.LS_FILES);
    h.setSilent(true);
    h.addParameters("--unmerged");
    final String result = h.run();
    final Set<String> paths = new HashSet<>();
    for (StringScanner s = new StringScanner(result); s.hasMoreData(); ) {
        if (s.isEol()) {
            s.nextLine();
            continue;
        }
        s.boundedToken('\t');
        final String relative = s.line();
        if (!myUnmergedPaths.add(relative)) {
            continue;
        }
        String path = root + "/" + GitUtil.unescapePath(relative);
        paths.add(path);
    }
    return paths;
}
Also used : GitSimpleHandler(git4idea.commands.GitSimpleHandler) StringScanner(git4idea.util.StringScanner) ObjectUtils.assertNotNull(com.intellij.util.ObjectUtils.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with StringScanner

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

the class GitInteractiveRebaseFile method load.

@NotNull
public List<GitRebaseEntry> load() throws IOException, NoopException {
    String encoding = GitConfigUtil.getLogEncoding(myProject, myRoot);
    List<GitRebaseEntry> entries = ContainerUtil.newArrayList();
    final StringScanner s = new StringScanner(FileUtil.loadFile(new File(myFile), encoding));
    boolean noop = false;
    while (s.hasMoreData()) {
        if (s.isEol() || s.startsWith('#')) {
            s.nextLine();
            continue;
        }
        if (s.startsWith("noop")) {
            noop = true;
            s.nextLine();
            continue;
        }
        String action = s.spaceToken();
        String hash = s.spaceToken();
        String comment = s.line();
        entries.add(new GitRebaseEntry(action, hash, comment));
    }
    if (noop && entries.isEmpty()) {
        throw new NoopException();
    }
    return entries;
}
Also used : StringScanner(git4idea.util.StringScanner) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with StringScanner

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

the class GitStashUtils method loadStashStack.

public static void loadStashStack(@NotNull Project project, @NotNull VirtualFile root, @NotNull Charset charset, final Consumer<StashInfo> consumer) {
    GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.STASH.readLockingCommand());
    h.setSilent(true);
    h.addParameters("list");
    String out;
    try {
        h.setCharset(charset);
        out = h.run();
    } catch (VcsException e) {
        GitUIUtil.showOperationError(project, e, h.printableCommandLine());
        return;
    }
    for (StringScanner s = new StringScanner(out); s.hasMoreData(); ) {
        consumer.consume(new StashInfo(s.boundedToken(':'), s.boundedToken(':'), s.line().trim()));
    }
}
Also used : GitSimpleHandler(git4idea.commands.GitSimpleHandler) VcsException(com.intellij.openapi.vcs.VcsException) StringScanner(git4idea.util.StringScanner) StashInfo(git4idea.ui.StashInfo)

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