use of git4idea.repo.GitRepository in project intellij-community by JetBrains.
the class GitDeleteRemoteBranchOperation method execute.
@Override
protected void execute() {
Collection<GitRepository> repositories = getRepositories();
Collection<String> commonTrackingBranches = getCommonTrackingBranches(myBranchName, repositories);
// don't propose to remove current branch even if it tracks the remote branch
for (GitRepository repository : repositories) {
String currentBranch = repository.getCurrentBranchName();
if (currentBranch != null) {
commonTrackingBranches.remove(currentBranch);
}
}
Ref<DeleteRemoteBranchDecision> decision = Ref.create();
ApplicationManager.getApplication().invokeAndWait(() -> decision.set(myUiHandler.confirmRemoteBranchDeletion(myBranchName, commonTrackingBranches, repositories)));
if (decision.get() != CANCEL) {
boolean deletedSuccessfully = doDeleteRemote(myBranchName, repositories);
if (deletedSuccessfully) {
Collection<String> successfullyDeletedLocalBranches = new ArrayList<>(1);
if (decision.get() == DELETE_WITH_TRACKING) {
for (String branch : commonTrackingBranches) {
getIndicator().setText("Deleting " + branch);
new GitDeleteBranchOperation(myProject, myGit, myUiHandler, repositories, branch) {
@Override
protected void notifySuccess(@NotNull String message) {
// do nothing - will display a combo notification for all deleted branches below
successfullyDeletedLocalBranches.add(branch);
}
}.execute();
}
}
notifySuccessfulDeletion(myBranchName, successfullyDeletedLocalBranches);
}
}
}
use of git4idea.repo.GitRepository in project intellij-community by JetBrains.
the class GitMergeOperation method execute.
@Override
protected void execute() {
LOG.info("starting");
saveAllDocuments();
boolean fatalErrorHappened = false;
int alreadyUpToDateRepositories = 0;
AccessToken token = DvcsUtil.workingTreeChangeStarted(myProject);
try {
while (hasMoreRepositories() && !fatalErrorHappened) {
final GitRepository repository = next();
LOG.info("next repository: " + repository);
VirtualFile root = repository.getRoot();
GitLocalChangesWouldBeOverwrittenDetector localChangesDetector = new GitLocalChangesWouldBeOverwrittenDetector(root, GitLocalChangesWouldBeOverwrittenDetector.Operation.MERGE);
GitSimpleEventDetector unmergedFiles = new GitSimpleEventDetector(GitSimpleEventDetector.Event.UNMERGED_PREVENTING_MERGE);
GitUntrackedFilesOverwrittenByOperationDetector untrackedOverwrittenByMerge = new GitUntrackedFilesOverwrittenByOperationDetector(root);
GitSimpleEventDetector mergeConflict = new GitSimpleEventDetector(GitSimpleEventDetector.Event.MERGE_CONFLICT);
GitSimpleEventDetector alreadyUpToDateDetector = new GitSimpleEventDetector(GitSimpleEventDetector.Event.ALREADY_UP_TO_DATE);
GitCommandResult result = myGit.merge(repository, myBranchToMerge, Collections.<String>emptyList(), localChangesDetector, unmergedFiles, untrackedOverwrittenByMerge, mergeConflict, alreadyUpToDateDetector);
if (result.success()) {
LOG.info("Merged successfully");
refresh(repository);
markSuccessful(repository);
if (alreadyUpToDateDetector.hasHappened()) {
alreadyUpToDateRepositories += 1;
}
} else if (unmergedFiles.hasHappened()) {
LOG.info("Unmerged files error!");
fatalUnmergedFilesError();
fatalErrorHappened = true;
} else if (localChangesDetector.wasMessageDetected()) {
LOG.info("Local changes would be overwritten by merge!");
boolean smartMergeSucceeded = proposeSmartMergePerformAndNotify(repository, localChangesDetector);
if (!smartMergeSucceeded) {
fatalErrorHappened = true;
}
} else if (mergeConflict.hasHappened()) {
LOG.info("Merge conflict");
myConflictedRepositories.put(repository, Boolean.FALSE);
refresh(repository);
markSuccessful(repository);
} else if (untrackedOverwrittenByMerge.wasMessageDetected()) {
LOG.info("Untracked files would be overwritten by merge!");
fatalUntrackedFilesError(repository.getRoot(), untrackedOverwrittenByMerge.getRelativeFilePaths());
fatalErrorHappened = true;
} else {
LOG.info("Unknown error. " + result);
fatalError(getCommonErrorTitle(), result.getErrorOutputAsJoinedString());
fatalErrorHappened = true;
}
}
if (fatalErrorHappened) {
notifyAboutRemainingConflicts();
} else {
boolean allConflictsResolved = resolveConflicts();
if (allConflictsResolved) {
if (alreadyUpToDateRepositories < getRepositories().size()) {
notifySuccess();
} else {
notifySuccess("Already up-to-date");
}
}
}
restoreLocalChanges();
} finally {
token.finish();
}
}
use of git4idea.repo.GitRepository in project intellij-community by JetBrains.
the class GitMergeOperation method refresh.
private void refresh(GitRepository... repositories) {
for (GitRepository repository : repositories) {
refreshRoot(repository);
repository.update();
}
}
use of git4idea.repo.GitRepository in project intellij-community by JetBrains.
the class GitLogProvider method getCurrentBranch.
@Nullable
@Override
public String getCurrentBranch(@NotNull VirtualFile root) {
GitRepository repository = myRepositoryManager.getRepositoryForRoot(root);
if (repository == null)
return null;
String currentBranchName = repository.getCurrentBranchName();
if (currentBranchName == null && repository.getCurrentRevision() != null) {
return "HEAD";
}
return currentBranchName;
}
use of git4idea.repo.GitRepository 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;
}
Aggregations