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;
}
}
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;
}
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);
}
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;
}
}));
}
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();
}
Aggregations