use of git4idea.repo.GitRepositoryManager in project intellij-community by JetBrains.
the class GitMergeAction method perform.
protected void perform(@NotNull final Project project, @NotNull final List<VirtualFile> gitRoots, @NotNull final VirtualFile defaultRoot) {
final DialogState dialogState = displayDialog(project, gitRoots, defaultRoot);
if (dialogState == null) {
return;
}
final VirtualFile selectedRoot = dialogState.selectedRoot;
final Computable<GitLineHandler> handlerProvider = dialogState.handlerProvider;
final Label beforeLabel = LocalHistory.getInstance().putSystemLabel(project, "Before update");
new Task.Backgroundable(project, dialogState.progressTitle, true) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
final GitRepositoryManager repositoryManager = GitUtil.getRepositoryManager(project);
final Git git = Git.getInstance();
final GitLocalChangesWouldBeOverwrittenDetector localChangesDetector = new GitLocalChangesWouldBeOverwrittenDetector(selectedRoot, MERGE);
final GitUntrackedFilesOverwrittenByOperationDetector untrackedFilesDetector = new GitUntrackedFilesOverwrittenByOperationDetector(selectedRoot);
final GitSimpleEventDetector mergeConflict = new GitSimpleEventDetector(GitSimpleEventDetector.Event.MERGE_CONFLICT);
AccessToken token = DvcsUtil.workingTreeChangeStarted(project);
try {
GitCommandResult result = git.runCommand(() -> {
GitLineHandler handler = handlerProvider.compute();
handler.addLineListener(localChangesDetector);
handler.addLineListener(untrackedFilesDetector);
handler.addLineListener(mergeConflict);
return handler;
});
GitRepository repository = repositoryManager.getRepositoryForRoot(selectedRoot);
assert repository != null : "Repository can't be null for root " + selectedRoot;
String revision = repository.getCurrentRevision();
if (revision == null) {
return;
}
final GitRevisionNumber currentRev = new GitRevisionNumber(revision);
handleResult(result, project, mergeConflict, localChangesDetector, untrackedFilesDetector, repository, currentRev, beforeLabel);
} finally {
token.finish();
}
}
}.queue();
}
use of git4idea.repo.GitRepositoryManager in project intellij-community by JetBrains.
the class GitPull method displayDialog.
@Override
protected DialogState displayDialog(@NotNull Project project, @NotNull List<VirtualFile> gitRoots, @NotNull VirtualFile defaultRoot) {
final GitPullDialog dialog = new GitPullDialog(project, gitRoots, defaultRoot);
if (!dialog.showAndGet()) {
return null;
}
GitRepositoryManager repositoryManager = GitUtil.getRepositoryManager(project);
GitRepository repository = repositoryManager.getRepositoryForRoot(dialog.gitRoot());
assert repository != null : "Repository can't be null for root " + dialog.gitRoot();
String remoteOrUrl = dialog.getRemote();
if (remoteOrUrl == null) {
return null;
}
GitRemote remote = GitUtil.findRemoteByName(repository, remoteOrUrl);
final List<String> urls = remote == null ? Collections.singletonList(remoteOrUrl) : remote.getUrls();
Computable<GitLineHandler> handlerProvider = new Computable<GitLineHandler>() {
@Override
public GitLineHandler compute() {
return dialog.makeHandler(urls);
}
};
return new DialogState(dialog.gitRoot(), GitBundle.message("pulling.title", dialog.getRemote()), handlerProvider);
}
use of git4idea.repo.GitRepositoryManager in project intellij-community by JetBrains.
the class GithubUtil method getGitRepository.
@Nullable
public static GitRepository getGitRepository(@NotNull Project project, @Nullable VirtualFile file) {
GitRepositoryManager manager = GitUtil.getRepositoryManager(project);
List<GitRepository> repositories = manager.getRepositories();
if (repositories.size() == 0) {
return null;
}
if (repositories.size() == 1) {
return repositories.get(0);
}
if (file != null) {
GitRepository repository = manager.getRepositoryForFileQuick(file);
if (repository != null) {
return repository;
}
}
return manager.getRepositoryForFileQuick(project.getBaseDir());
}
use of git4idea.repo.GitRepositoryManager in project intellij-community by JetBrains.
the class GitCheckinEnvironment method mergeCommit.
/**
* Preform a merge commit
*
* @param project a project
* @param root a vcs root
* @param added added files
* @param removed removed files
* @param messageFile a message file for commit
* @param author an author
* @param exceptions the list of exceptions to report
* @param partialOperation
* @return true if merge commit was successful
*/
private boolean mergeCommit(final Project project, final VirtualFile root, final Set<FilePath> added, final Set<FilePath> removed, final File messageFile, final String author, List<VcsException> exceptions, @NotNull final PartialOperation partialOperation) {
HashSet<FilePath> realAdded = new HashSet<>();
HashSet<FilePath> realRemoved = new HashSet<>();
// perform diff
GitSimpleHandler diff = new GitSimpleHandler(project, root, GitCommand.DIFF);
diff.setSilent(true);
diff.setStdoutSuppressed(true);
diff.addParameters("--diff-filter=ADMRUX", "--name-status", "--no-renames", "HEAD");
diff.endOptions();
String output;
try {
output = diff.run();
} catch (VcsException ex) {
exceptions.add(ex);
return false;
}
String rootPath = root.getPath();
for (StringTokenizer lines = new StringTokenizer(output, "\n", false); lines.hasMoreTokens(); ) {
String line = lines.nextToken().trim();
if (line.length() == 0) {
continue;
}
String[] tk = line.split("\t");
switch(tk[0].charAt(0)) {
case 'M':
case 'A':
realAdded.add(VcsUtil.getFilePath(rootPath + "/" + tk[1]));
break;
case 'D':
realRemoved.add(VcsUtil.getFilePathForDeletedFile(rootPath + "/" + tk[1], false));
break;
default:
throw new IllegalStateException("Unexpected status: " + line);
}
}
realAdded.removeAll(added);
realRemoved.removeAll(removed);
if (realAdded.size() != 0 || realRemoved.size() != 0) {
final List<FilePath> files = new ArrayList<>();
files.addAll(realAdded);
files.addAll(realRemoved);
final Ref<Boolean> mergeAll = new Ref<>();
try {
GuiUtils.runOrInvokeAndWait(new Runnable() {
public void run() {
String message = GitBundle.message("commit.partial.merge.message", partialOperation.getName());
SelectFilePathsDialog dialog = new SelectFilePathsDialog(project, files, message, null, "Commit All Files", CommonBundle.getCancelButtonText(), false);
dialog.setTitle(GitBundle.getString("commit.partial.merge.title"));
dialog.show();
mergeAll.set(dialog.isOK());
}
});
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException("Unable to invoke a message box on AWT thread", ex);
}
if (!mergeAll.get()) {
return false;
}
// update non-indexed files
if (!updateIndex(project, root, realAdded, realRemoved, exceptions)) {
return false;
}
for (FilePath f : realAdded) {
VcsDirtyScopeManager.getInstance(project).fileDirty(f);
}
for (FilePath f : realRemoved) {
VcsDirtyScopeManager.getInstance(project).fileDirty(f);
}
}
// perform merge commit
try {
commitWithoutPaths(project, root, messageFile, author);
GitRepositoryManager manager = getRepositoryManager(project);
manager.updateRepository(root);
} catch (VcsException ex) {
exceptions.add(ex);
return false;
}
return true;
}
use of git4idea.repo.GitRepositoryManager in project intellij-community by JetBrains.
the class GitCheckinEnvironment method getDefaultMessageFor.
@Nullable
public String getDefaultMessageFor(FilePath[] filesToCheckin) {
LinkedHashSet<String> messages = newLinkedHashSet();
GitRepositoryManager manager = getRepositoryManager(myProject);
for (VirtualFile root : GitUtil.gitRoots(asList(filesToCheckin))) {
GitRepository repository = manager.getRepositoryForRoot(root);
if (repository == null) {
// unregistered nested submodule found by GitUtil.getGitRoot
LOG.warn("Unregistered repository: " + root);
continue;
}
File mergeMsg = repository.getRepositoryFiles().getMergeMessageFile();
File squashMsg = repository.getRepositoryFiles().getSquashMessageFile();
try {
if (!mergeMsg.exists() && !squashMsg.exists()) {
continue;
}
String encoding = GitConfigUtil.getCommitEncoding(myProject, root);
if (mergeMsg.exists()) {
messages.add(loadMessage(mergeMsg, encoding));
} else {
messages.add(loadMessage(squashMsg, encoding));
}
} catch (IOException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Unable to load merge message", e);
}
}
}
return DvcsUtil.joinMessagesOrNull(messages);
}
Aggregations