use of git4idea.repo.GitRepository 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 git4idea.repo.GitRepository in project intellij-community by JetBrains.
the class GitCrlfProblemsDetector method findFilesWithoutAttrs.
@NotNull
private Collection<VirtualFile> findFilesWithoutAttrs(@NotNull VirtualFile root, @NotNull Collection<VirtualFile> files) {
GitRepository repository = myRepositoryManager.getRepositoryForRoot(root);
if (repository == null) {
LOG.warn("Repository is null for " + root);
return Collections.emptyList();
}
Collection<String> interestingAttributes = Arrays.asList(GitAttribute.TEXT.getName(), GitAttribute.CRLF.getName());
GitCommandResult result = myGit.checkAttr(repository, interestingAttributes, files);
if (!result.success()) {
LOG.warn(String.format("Couldn't git check-attr. Attributes: %s, files: %s", interestingAttributes, files));
return Collections.emptyList();
}
GitCheckAttrParser parser = GitCheckAttrParser.parse(result.getOutput());
Map<String, Collection<GitAttribute>> attributes = parser.getAttributes();
Collection<VirtualFile> filesWithoutAttrs = new ArrayList<>();
for (VirtualFile file : files) {
ProgressIndicatorProvider.checkCanceled();
String relativePath = FileUtil.getRelativePath(root.getPath(), file.getPath(), '/');
Collection<GitAttribute> attrs = attributes.get(relativePath);
if (attrs == null || !attrs.contains(GitAttribute.TEXT) && !attrs.contains(GitAttribute.CRLF)) {
filesWithoutAttrs.add(file);
}
}
return filesWithoutAttrs;
}
use of git4idea.repo.GitRepository in project intellij-community by JetBrains.
the class GitCrlfProblemsDetector method isAutoCrlfSetRight.
private boolean isAutoCrlfSetRight(@NotNull VirtualFile root) {
GitRepository repository = myRepositoryManager.getRepositoryForRoot(root);
if (repository == null) {
LOG.warn("Repository is null for " + root);
return true;
}
GitCommandResult result = myGit.config(repository, GitConfigUtil.CORE_AUTOCRLF);
String value = result.getOutputAsJoinedString();
return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("input");
}
use of git4idea.repo.GitRepository in project intellij-community by JetBrains.
the class GitDiffFromHistoryHandler method getChangesBetweenRevisions.
@NotNull
@Override
protected List<Change> getChangesBetweenRevisions(@NotNull FilePath path, @NotNull GitFileRevision rev1, @Nullable GitFileRevision rev2) throws VcsException {
GitRepository repository = getRepository(path);
String hash1 = rev1.getHash();
String hash2 = rev2 != null ? rev2.getHash() : null;
return ContainerUtil.newArrayList(GitChangeUtils.getDiff(repository.getProject(), repository.getRoot(), hash1, hash2, Collections.singletonList(path)));
}
use of git4idea.repo.GitRepository in project intellij-community by JetBrains.
the class GitAbstractRebaseAction method chooseRepository.
@Nullable
private GitRepository chooseRepository(@NotNull Project project, @NotNull Collection<GitRepository> repositories) {
GitRepository firstRepo = assertNotNull(ContainerUtil.getFirstItem(repositories));
if (repositories.size() == 1)
return firstRepo;
ArrayList<VirtualFile> roots = newArrayList(getRootsFromRepositories(repositories));
GitRebaseActionDialog dialog = new GitRebaseActionDialog(project, getTemplatePresentation().getText(), roots, firstRepo.getRoot());
dialog.show();
VirtualFile root = dialog.selectRoot();
if (root == null)
return null;
// TODO avoid root <-> GitRepository double conversion
return getRepositoryManager(project).getRepositoryForRootQuick(root);
}
Aggregations