Search in sources :

Example 21 with GitRevisionNumber

use of git4idea.GitRevisionNumber in project intellij-community by JetBrains.

the class GitHistoryUtilsTest method testGetLastRevisionForExistingFile.

@Test(enabled = false)
public void testGetLastRevisionForExistingFile() throws Exception {
    final ItemLatestState state = GitHistoryUtils.getLastRevision(myProject, toFilePath(bfile));
    assertTrue(state.isItemExists());
    final GitRevisionNumber revisionNumber = (GitRevisionNumber) state.getNumber();
    assertEquals(revisionNumber.getRev(), myRevisions.get(0).myHash);
    assertEquals(revisionNumber.getTimestamp(), myRevisions.get(0).myDate);
}
Also used : GitRevisionNumber(git4idea.GitRevisionNumber) ItemLatestState(com.intellij.openapi.vcs.diff.ItemLatestState) Test(org.testng.annotations.Test) GitSingleRepoTest(git4idea.test.GitSingleRepoTest)

Example 22 with GitRevisionNumber

use of git4idea.GitRevisionNumber in project intellij-community by JetBrains.

the class GithubOpenInBrowserAction method getCurrentFileRevisionHash.

@Nullable
private static String getCurrentFileRevisionHash(@NotNull final Project project, @NotNull final VirtualFile file) {
    final Ref<GitRevisionNumber> ref = new Ref<>();
    ProgressManager.getInstance().run(new Task.Modal(project, "Getting Last Revision", true) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            try {
                ref.set((GitRevisionNumber) GitHistoryUtils.getCurrentRevision(project, VcsUtil.getFilePath(file), "HEAD"));
            } catch (VcsException e) {
                LOG.warn(e);
            }
        }

        @Override
        public void onCancel() {
            throw new ProcessCanceledException();
        }
    });
    if (ref.isNull())
        return null;
    return ref.get().getRev();
}
Also used : Ref(com.intellij.openapi.util.Ref) Task(com.intellij.openapi.progress.Task) GitRevisionNumber(git4idea.GitRevisionNumber) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) VcsException(com.intellij.openapi.vcs.VcsException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with GitRevisionNumber

use of git4idea.GitRevisionNumber in project intellij-community by JetBrains.

the class GitCompareWithBranchAction method getDiffChanges.

@Override
@NotNull
protected Collection<Change> getDiffChanges(@NotNull Project project, @NotNull VirtualFile file, @NotNull String branchToCompare) throws VcsException {
    FilePath filePath = VcsUtil.getFilePath(file);
    final GitRepository gitRepository = GitUtil.getRepositoryManager(project).getRepositoryForFile(file);
    if (gitRepository == null) {
        throw new VcsException("Couldn't find Git Repository for " + file.getName());
    }
    final VirtualFile gitRepositoryRoot = gitRepository.getRoot();
    GitRevisionNumber compareRevisionNumber = new GitRevisionNumber(branchToCompare);
    Collection<Change> changes = GitChangeUtils.getDiffWithWorkingDir(project, gitRepositoryRoot, branchToCompare, Collections.singletonList(filePath), false);
    // a.e. when you perform compareWith for unversioned file
    if (changes.isEmpty() && GitHistoryUtils.getCurrentRevision(project, filePath, branchToCompare) == null) {
        throw new VcsException(fileDoesntExistInBranchError(file, branchToCompare));
    }
    return changes.isEmpty() && !filePath.isDirectory() ? createChangesWithCurrentContentForFile(filePath, GitContentRevision.createRevision(filePath, compareRevisionNumber, project, null)) : changes;
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) VirtualFile(com.intellij.openapi.vfs.VirtualFile) GitRepository(git4idea.repo.GitRepository) GitRevisionNumber(git4idea.GitRevisionNumber) VcsException(com.intellij.openapi.vcs.VcsException) Change(com.intellij.openapi.vcs.changes.Change) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with GitRevisionNumber

use of git4idea.GitRevisionNumber 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();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) Label(com.intellij.history.Label) GitRepositoryManager(git4idea.repo.GitRepositoryManager) GitRepository(git4idea.repo.GitRepository) GitRevisionNumber(git4idea.GitRevisionNumber) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AccessToken(com.intellij.openapi.application.AccessToken)

Example 25 with GitRevisionNumber

use of git4idea.GitRevisionNumber in project intellij-community by JetBrains.

the class GitChangeUtils method parseChangeList.

/**
   * Parse changelist
   *
   *
   *
   * @param project the project
   * @param root    the git root
   * @param s       the scanner for log or show command output
   * @param skipDiffsForMerge
   * @param handler the handler that produced the output to parse. - for debugging purposes.
   * @param local   pass {@code true} to indicate that this revision should be an editable
   *                {@link com.intellij.openapi.vcs.changes.CurrentContentRevision}.
   *                Pass {@code false} for
   * @param revertable
   * @return the parsed changelist
   * @throws VcsException if there is a problem with running git
   */
public static GitCommittedChangeList parseChangeList(Project project, VirtualFile root, StringScanner s, boolean skipDiffsForMerge, GitHandler handler, boolean local, boolean revertable) throws VcsException {
    ArrayList<Change> changes = new ArrayList<>();
    // parse commit information
    final Date commitDate = GitUtil.parseTimestampWithNFEReport(s.line(), handler, s.getAllText());
    final String revisionNumber = s.line();
    final String parentsLine = s.line();
    final String[] parents = parentsLine.length() == 0 ? ArrayUtil.EMPTY_STRING_ARRAY : parentsLine.split(" ");
    String authorName = s.line();
    String committerName = s.line();
    committerName = GitUtil.adjustAuthorName(authorName, committerName);
    String commentSubject = s.boundedToken('', true);
    s.nextLine();
    String commentBody = s.boundedToken('', true);
    // construct full comment
    String fullComment;
    if (commentSubject.length() == 0) {
        fullComment = commentBody;
    } else if (commentBody.length() == 0) {
        fullComment = commentSubject;
    } else {
        fullComment = commentSubject + "\n" + commentBody;
    }
    GitRevisionNumber thisRevision = new GitRevisionNumber(revisionNumber, commitDate);
    if (skipDiffsForMerge || (parents.length <= 1)) {
        final GitRevisionNumber parentRevision = parents.length > 0 ? resolveReference(project, root, parents[0]) : null;
        // This is the first or normal commit with the single parent.
        // Just parse changes in this commit as returned by the show command.
        parseChanges(project, root, thisRevision, local ? null : parentRevision, s, changes, null);
    } else {
        for (String parent : parents) {
            final GitRevisionNumber parentRevision = resolveReference(project, root, parent);
            GitSimpleHandler diffHandler = new GitSimpleHandler(project, root, GitCommand.DIFF);
            diffHandler.setSilent(true);
            diffHandler.addParameters("--name-status", "-M", parentRevision.getRev(), thisRevision.getRev());
            String diff = diffHandler.run();
            parseChanges(project, root, thisRevision, parentRevision, diff, changes, null);
            if (changes.size() > 0) {
                break;
            }
        }
    }
    String changeListName = String.format("%s(%s)", commentSubject, revisionNumber);
    return new GitCommittedChangeList(changeListName, fullComment, committerName, thisRevision, commitDate, changes, assertNotNull(GitVcs.getInstance(project)), revertable);
}
Also used : GitRevisionNumber(git4idea.GitRevisionNumber) GitSimpleHandler(git4idea.commands.GitSimpleHandler) Change(com.intellij.openapi.vcs.changes.Change)

Aggregations

GitRevisionNumber (git4idea.GitRevisionNumber)29 VcsException (com.intellij.openapi.vcs.VcsException)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)9 Change (com.intellij.openapi.vcs.changes.Change)8 NotNull (org.jetbrains.annotations.NotNull)8 Nullable (org.jetbrains.annotations.Nullable)8 FilePath (com.intellij.openapi.vcs.FilePath)4 GitSingleRepoTest (git4idea.test.GitSingleRepoTest)4 Test (org.testng.annotations.Test)4 ContentRevision (com.intellij.openapi.vcs.changes.ContentRevision)3 ObjectUtils.assertNotNull (com.intellij.util.ObjectUtils.assertNotNull)3 GitBranchesSearcher (git4idea.GitBranchesSearcher)3 GitSimpleHandler (git4idea.commands.GitSimpleHandler)3 GitRepository (git4idea.repo.GitRepository)3 StringScanner (git4idea.util.StringScanner)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Task (com.intellij.openapi.progress.Task)2 Pair (com.intellij.openapi.util.Pair)2