use of com.intellij.openapi.vcs.VcsException in project intellij-community by JetBrains.
the class GitUtil method sortFilePathsByGitRoot.
/**
* Sort files by vcs root
*
* @param files files to sort.
* @param ignoreNonGit if true, non-git files are ignored
* @return the map from root to the files under the root
* @throws VcsException if non git files are passed when {@code ignoreNonGit} is false
*/
@NotNull
public static Map<VirtualFile, List<FilePath>> sortFilePathsByGitRoot(@NotNull Collection<FilePath> files, boolean ignoreNonGit) throws VcsException {
Map<VirtualFile, List<FilePath>> rc = new HashMap<>();
for (FilePath p : files) {
VirtualFile root = getGitRootOrNull(p);
if (root == null) {
if (ignoreNonGit) {
continue;
} else {
throw new VcsException("The file " + p.getPath() + " is not under Git");
}
}
List<FilePath> l = rc.get(root);
if (l == null) {
l = new ArrayList<>();
rc.put(root, l);
}
l.add(p);
}
return rc;
}
use of com.intellij.openapi.vcs.VcsException in project intellij-community by JetBrains.
the class GitLogProvider method getCommitsMatchingFilter.
@NotNull
@Override
public List<TimedVcsCommit> getCommitsMatchingFilter(@NotNull final VirtualFile root, @NotNull VcsLogFilterCollection filterCollection, int maxCount) throws VcsException {
if (!isRepositoryReady(root)) {
return Collections.emptyList();
}
List<String> filterParameters = ContainerUtil.newArrayList();
VcsLogBranchFilter branchFilter = filterCollection.getBranchFilter();
if (branchFilter != null) {
GitRepository repository = getRepository(root);
assert repository != null : "repository is null for root " + root + " but was previously reported as 'ready'";
Collection<GitBranch> branches = ContainerUtil.newArrayList(ContainerUtil.concat(repository.getBranches().getLocalBranches(), repository.getBranches().getRemoteBranches()));
Collection<String> branchNames = GitBranchUtil.convertBranchesToNames(branches);
Collection<String> predefinedNames = ContainerUtil.list("HEAD");
boolean atLeastOneBranchExists = false;
for (String branchName : ContainerUtil.concat(branchNames, predefinedNames)) {
if (branchFilter.matches(branchName)) {
filterParameters.add(branchName);
atLeastOneBranchExists = true;
}
}
if (!atLeastOneBranchExists) {
// no such branches in this repository => filter matches nothing
return Collections.emptyList();
}
} else {
filterParameters.addAll(GitHistoryUtils.LOG_ALL);
}
if (filterCollection.getDateFilter() != null) {
// assuming there is only one date filter, until filter expressions are defined
VcsLogDateFilter filter = filterCollection.getDateFilter();
if (filter.getAfter() != null) {
filterParameters.add(prepareParameter("after", filter.getAfter().toString()));
}
if (filter.getBefore() != null) {
filterParameters.add(prepareParameter("before", filter.getBefore().toString()));
}
}
boolean regexp = true;
boolean caseSensitive = false;
if (filterCollection.getTextFilter() != null) {
regexp = filterCollection.getTextFilter().isRegex();
caseSensitive = filterCollection.getTextFilter().matchesCase();
String textFilter = filterCollection.getTextFilter().getText();
filterParameters.add(prepareParameter("grep", textFilter));
}
filterParameters.add(regexp ? "--extended-regexp" : "--fixed-strings");
if (!caseSensitive) {
// affects case sensitivity of any filter (except file filter)
filterParameters.add("--regexp-ignore-case");
}
if (filterCollection.getUserFilter() != null) {
Collection<String> names = ContainerUtil.map(filterCollection.getUserFilter().getUsers(root), VcsUserUtil::toExactString);
if (regexp) {
List<String> authors = ContainerUtil.map(names, UserNameRegex.EXTENDED_INSTANCE);
if (GitVersionSpecialty.LOG_AUTHOR_FILTER_SUPPORTS_VERTICAL_BAR.existsIn(myVcs.getVersion())) {
filterParameters.add(prepareParameter("author", StringUtil.join(authors, "|")));
} else {
filterParameters.addAll(authors.stream().map(a -> prepareParameter("author", a)).collect(Collectors.toList()));
}
} else {
filterParameters.addAll(ContainerUtil.map(names, a -> prepareParameter("author", StringUtil.escapeBackSlashes(a))));
}
}
if (maxCount > 0) {
filterParameters.add(prepareParameter("max-count", String.valueOf(maxCount)));
}
// note: structure filter must be the last parameter, because it uses "--" which separates parameters from paths
if (filterCollection.getStructureFilter() != null) {
Collection<FilePath> files = filterCollection.getStructureFilter().getFiles();
if (!files.isEmpty()) {
filterParameters.add("--full-history");
filterParameters.add("--simplify-merges");
filterParameters.add("--");
for (FilePath file : files) {
filterParameters.add(file.getPath());
}
}
}
List<TimedVcsCommit> commits = ContainerUtil.newArrayList();
GitHistoryUtils.readCommits(myProject, root, filterParameters, EmptyConsumer.getInstance(), EmptyConsumer.getInstance(), new CollectConsumer<>(commits));
return commits;
}
use of com.intellij.openapi.vcs.VcsException in project intellij-community by JetBrains.
the class GitCheckinEnvironment method commit.
public List<VcsException> commit(@NotNull List<Change> changes, @NotNull String message, @NotNull NullableFunction<Object, Object> parametersHolder, Set<String> feedback) {
List<VcsException> exceptions = new ArrayList<>();
Map<VirtualFile, Collection<Change>> sortedChanges = sortChangesByGitRoot(changes, exceptions);
LOG.assertTrue(!sortedChanges.isEmpty(), "Trying to commit an empty list of changes: " + changes);
for (Map.Entry<VirtualFile, Collection<Change>> entry : sortedChanges.entrySet()) {
VirtualFile root = entry.getKey();
File messageFile;
try {
messageFile = createMessageFile(root, message);
} catch (IOException ex) {
//noinspection ThrowableInstanceNeverThrown
exceptions.add(new VcsException("Creation of commit message file failed", ex));
continue;
}
Set<FilePath> added = new HashSet<>();
Set<FilePath> removed = new HashSet<>();
final Set<Change> caseOnlyRenames = new HashSet<>();
for (Change change : entry.getValue()) {
switch(change.getType()) {
case NEW:
case MODIFICATION:
added.add(change.getAfterRevision().getFile());
break;
case DELETED:
removed.add(change.getBeforeRevision().getFile());
break;
case MOVED:
FilePath afterPath = change.getAfterRevision().getFile();
FilePath beforePath = change.getBeforeRevision().getFile();
if (!SystemInfo.isFileSystemCaseSensitive && GitUtil.isCaseOnlyChange(beforePath.getPath(), afterPath.getPath())) {
caseOnlyRenames.add(change);
} else {
added.add(afterPath);
removed.add(beforePath);
}
break;
default:
throw new IllegalStateException("Unknown change type: " + change.getType());
}
}
try {
if (!caseOnlyRenames.isEmpty()) {
List<VcsException> exs = commitWithCaseOnlyRename(myProject, root, caseOnlyRenames, added, removed, messageFile, myNextCommitAuthor);
exceptions.addAll(map(exs, GitCheckinEnvironment::cleanupExceptionText));
} else {
try {
Set<FilePath> files = new HashSet<>();
files.addAll(added);
files.addAll(removed);
commit(myProject, root, files, messageFile);
} catch (VcsException ex) {
PartialOperation partialOperation = isMergeCommit(ex);
if (partialOperation == PartialOperation.NONE) {
throw ex;
}
if (!mergeCommit(myProject, root, added, removed, messageFile, myNextCommitAuthor, exceptions, partialOperation)) {
throw ex;
}
}
}
} catch (VcsException e) {
exceptions.add(cleanupExceptionText(e));
} finally {
if (!messageFile.delete()) {
LOG.warn("Failed to remove temporary file: " + messageFile);
}
}
}
if (myNextCommitIsPushed != null && myNextCommitIsPushed.booleanValue() && exceptions.isEmpty()) {
GitRepositoryManager manager = getRepositoryManager(myProject);
Collection<GitRepository> repositories = GitUtil.getRepositoriesFromRoots(manager, sortedChanges.keySet());
final List<GitRepository> preselectedRepositories = newArrayList(repositories);
GuiUtils.invokeLaterIfNeeded(() -> new VcsPushDialog(myProject, preselectedRepositories, GitBranchUtil.getCurrentRepository(myProject)).show(), ModalityState.defaultModalityState());
}
return exceptions;
}
use of com.intellij.openapi.vcs.VcsException in project intellij-community by JetBrains.
the class GitCheckinEnvironment method cleanupExceptionText.
@NotNull
private static VcsException cleanupExceptionText(VcsException original) {
String msg = original.getMessage();
msg = GitUtil.cleanupErrorPrefixes(msg);
final String DURING_EXECUTING_SUFFIX = GitSimpleHandler.DURING_EXECUTING_ERROR_MESSAGE;
int suffix = msg.indexOf(DURING_EXECUTING_SUFFIX);
if (suffix > 0) {
msg = msg.substring(0, suffix);
}
return new VcsException(msg.trim(), original.getCause());
}
use of com.intellij.openapi.vcs.VcsException in project intellij-community by JetBrains.
the class GitCheckinEnvironment method scheduleMissingFileForDeletion.
public List<VcsException> scheduleMissingFileForDeletion(List<FilePath> files) {
ArrayList<VcsException> rc = new ArrayList<>();
Map<VirtualFile, List<FilePath>> sortedFiles;
try {
sortedFiles = GitUtil.sortFilePathsByGitRoot(files);
} catch (VcsException e) {
rc.add(e);
return rc;
}
for (Map.Entry<VirtualFile, List<FilePath>> e : sortedFiles.entrySet()) {
try {
final VirtualFile root = e.getKey();
GitFileUtils.delete(myProject, root, e.getValue());
markRootDirty(root);
} catch (VcsException ex) {
rc.add(ex);
}
}
return rc;
}
Aggregations