Search in sources :

Example 1 with GitSimpleHandler

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

the class GitBranchUtil method getCurrentBranchFromGit.

@Nullable
private static GitLocalBranch getCurrentBranchFromGit(@NotNull Project project, @NotNull VirtualFile root) {
    GitSimpleHandler handler = new GitSimpleHandler(project, root, GitCommand.REV_PARSE);
    handler.addParameters("--abbrev-ref", "HEAD");
    handler.setSilent(true);
    try {
        String name = handler.run();
        if (!name.equals("HEAD")) {
            return new GitLocalBranch(name);
        } else {
            return null;
        }
    } catch (VcsException e) {
        LOG.info("git rev-parse --abbrev-ref HEAD", e);
        return null;
    }
}
Also used : GitSimpleHandler(git4idea.commands.GitSimpleHandler) VcsException(com.intellij.openapi.vcs.VcsException) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with GitSimpleHandler

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

the class GitBranchUtil method getBranches.

/**
   * List branches containing a commit. Specify null if no commit filtering is needed.
   */
@NotNull
public static Collection<String> getBranches(@NotNull Project project, @NotNull VirtualFile root, boolean localWanted, boolean remoteWanted, @Nullable String containingCommit) throws VcsException {
    // preparing native command executor
    final GitSimpleHandler handler = new GitSimpleHandler(project, root, GitCommand.BRANCH);
    handler.setSilent(true);
    handler.addParameters("--no-color");
    boolean remoteOnly = false;
    if (remoteWanted && localWanted) {
        handler.addParameters("-a");
        remoteOnly = false;
    } else if (remoteWanted) {
        handler.addParameters("-r");
        remoteOnly = true;
    }
    if (containingCommit != null) {
        handler.addParameters("--contains", containingCommit);
    }
    final String output = handler.run();
    if (output.trim().length() == 0) {
        // the case after git init and before first commit - there is no branch and no output, and we'll take refs/heads/master
        String head;
        try {
            File headFile = assertNotNull(GitUtil.getRepositoryManager(project).getRepositoryForRoot(root)).getRepositoryFiles().getHeadFile();
            head = FileUtil.loadFile(headFile, CharsetToolkit.UTF8_CHARSET).trim();
            final String prefix = "ref: refs/heads/";
            return head.startsWith(prefix) ? Collections.singletonList(head.substring(prefix.length())) : Collections.<String>emptyList();
        } catch (IOException e) {
            LOG.info(e);
            return Collections.emptyList();
        }
    }
    Collection<String> branches = ContainerUtil.newArrayList();
    // standard situation. output example:
    //  master
    //* my_feature
    //  remotes/origin/HEAD -> origin/master
    //  remotes/origin/eap
    //  remotes/origin/feature
    //  remotes/origin/master
    // also possible:
    //* (no branch)
    // and if we call with -r instead of -a, remotes/ prefix is omitted:
    // origin/HEAD -> origin/master
    final String[] split = output.split("\n");
    for (String b : split) {
        b = b.substring(2).trim();
        if (b.equals(NO_BRANCH_NAME)) {
            continue;
        }
        String remotePrefix = null;
        if (b.startsWith("remotes/")) {
            remotePrefix = "remotes/";
        } else if (b.startsWith(GitBranch.REFS_REMOTES_PREFIX)) {
            remotePrefix = GitBranch.REFS_REMOTES_PREFIX;
        }
        boolean isRemote = remotePrefix != null || remoteOnly;
        if (isRemote) {
            if (!remoteOnly) {
                b = b.substring(remotePrefix.length());
            }
            final int idx = b.indexOf("HEAD ->");
            if (idx > 0) {
                continue;
            }
        }
        branches.add(b);
    }
    return branches;
}
Also used : GitSimpleHandler(git4idea.commands.GitSimpleHandler) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) ObjectUtils.assertNotNull(com.intellij.util.ObjectUtils.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GitSimpleHandler

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

the class GitRevisionNumber method resolve.

/**
   * Resolve revision number for the specified revision
   *
   * @param project a project
   * @param vcsRoot a vcs root
   * @param rev     a revision expression
   * @return a resolved revision number with correct time
   * @throws VcsException if there is a problem with running git
   */
@NotNull
public static GitRevisionNumber resolve(Project project, VirtualFile vcsRoot, @NonNls String rev) throws VcsException {
    GitSimpleHandler h = new GitSimpleHandler(project, vcsRoot, GitCommand.REV_LIST);
    h.setSilent(true);
    h.addParameters("--timestamp", "--max-count=1", rev);
    h.endOptions();
    final String output = h.run();
    return parseRevlistOutputAsRevisionNumber(h, output);
}
Also used : GitSimpleHandler(git4idea.commands.GitSimpleHandler) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GitSimpleHandler

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

the class GitOutgoingChangesProvider method getOutgoingChanges.

public Pair<VcsRevisionNumber, List<CommittedChangeList>> getOutgoingChanges(final VirtualFile vcsRoot, final boolean findRemote) throws VcsException {
    LOG.debug("getOutgoingChanges root: " + vcsRoot.getPath());
    final GitBranchesSearcher searcher = new GitBranchesSearcher(myProject, vcsRoot, findRemote);
    if (searcher.getLocal() == null || searcher.getRemote() == null) {
        return new Pair<>(null, Collections.<CommittedChangeList>emptyList());
    }
    final GitRevisionNumber base = getMergeBase(myProject, vcsRoot, searcher.getLocal(), searcher.getRemote());
    if (base == null) {
        return new Pair<>(null, Collections.<CommittedChangeList>emptyList());
    }
    final List<GitCommittedChangeList> lists = GitUtil.getLocalCommittedChanges(myProject, vcsRoot, new Consumer<GitSimpleHandler>() {

        public void consume(final GitSimpleHandler handler) {
            handler.addParameters(base.asString() + "..HEAD");
        }
    });
    return new Pair<>(base, ObjectsConvertor.convert(lists, new Convertor<GitCommittedChangeList, CommittedChangeList>() {

        @Override
        public CommittedChangeList convert(GitCommittedChangeList o) {
            return o;
        }
    }));
}
Also used : GitRevisionNumber(git4idea.GitRevisionNumber) GitSimpleHandler(git4idea.commands.GitSimpleHandler) GitBranchesSearcher(git4idea.GitBranchesSearcher) Pair(com.intellij.openapi.util.Pair) Convertor(com.intellij.util.containers.Convertor)

Example 5 with GitSimpleHandler

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

the class GitCheckinEnvironment method reset.

private static void reset(@NotNull Project project, @NotNull VirtualFile root, @NotNull Collection<Change> changes) throws VcsException {
    Set<FilePath> paths = new HashSet<>();
    paths.addAll(mapNotNull(changes, ChangesUtil::getAfterPath));
    paths.addAll(mapNotNull(changes, ChangesUtil::getBeforePath));
    GitSimpleHandler handler = new GitSimpleHandler(project, root, GitCommand.RESET);
    handler.endOptions();
    handler.addRelativePaths(paths);
    handler.run();
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) GitSimpleHandler(git4idea.commands.GitSimpleHandler)

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