Search in sources :

Example 16 with GitRevisionNumber

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

the class GitRebaseUtils method getCurrentRebaseCommit.

/**
   * Get rebase directory
   *
   *
   * @param project
   * @param root the vcs root
   * @return the commit information or null if no commit information could be detected
   */
@Nullable
public static CommitInfo getCurrentRebaseCommit(@NotNull Project project, @NotNull VirtualFile root) {
    File rebaseDir = getRebaseDir(project, root);
    if (rebaseDir == null) {
        LOG.warn("No rebase dir found for " + root.getPath());
        return null;
    }
    File nextFile = new File(rebaseDir, "next");
    int next;
    try {
        next = Integer.parseInt(FileUtil.loadFile(nextFile, CharsetToolkit.UTF8_CHARSET).trim());
    } catch (Exception e) {
        LOG.warn("Failed to load next commit number from file " + nextFile.getPath(), e);
        return null;
    }
    File commitFile = new File(rebaseDir, String.format("%04d", next));
    String hash = null;
    String subject = null;
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(commitFile), CharsetToolkit.UTF8_CHARSET));
        try {
            String line;
            while ((line = in.readLine()) != null) {
                if (line.startsWith("From ")) {
                    hash = line.substring(5, 5 + 40);
                }
                if (line.startsWith("Subject: ")) {
                    subject = line.substring("Subject: ".length());
                }
                if (hash != null && subject != null) {
                    break;
                }
            }
        } finally {
            in.close();
        }
    } catch (Exception e) {
        LOG.warn("Failed to load next commit number from file " + commitFile, e);
        return null;
    }
    if (subject == null || hash == null) {
        LOG.info("Unable to extract information from " + commitFile + " " + hash + ": " + subject);
        return null;
    }
    return new CommitInfo(new GitRevisionNumber(hash), subject);
}
Also used : GitRevisionNumber(git4idea.GitRevisionNumber) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with GitRevisionNumber

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

the class GitNewChangesCollector method getHead.

@NotNull
private VcsRevisionNumber getHead() throws VcsException {
    if (myRepository != null) {
        // we force update the GitRepository, because update is asynchronous, and thus the GitChangeProvider may be asked for changes
        // before the GitRepositoryUpdater has captures the current revision change and has updated the GitRepository.
        myRepository.update();
        final String rev = myRepository.getCurrentRevision();
        return rev != null ? new GitRevisionNumber(rev) : VcsRevisionNumber.NULL;
    } else {
        // this may happen on the project startup, when GitChangeProvider may be queried before GitRepository has been initialized.
        LOG.info("GitRepository is null for root " + myVcsRoot);
        return getHeadFromGit();
    }
}
Also used : GitRevisionNumber(git4idea.GitRevisionNumber) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with GitRevisionNumber

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

the class GitOldChangesCollector method parseFiles.

private void parseFiles(String list) throws VcsException {
    for (StringScanner sc = new StringScanner(list); sc.hasMoreData(); ) {
        if (sc.isEol()) {
            sc.nextLine();
            continue;
        }
        char status = sc.peek();
        sc.skipChars(2);
        if ('?' == status) {
            VirtualFile file = myVcsRoot.findFileByRelativePath(GitUtil.unescapePath(sc.line()));
            if (Comparing.equal(GitUtil.gitRootOrNull(file), myVcsRoot)) {
                myUnversioned.add(file);
            }
        } else {
            //noinspection HardCodedStringLiteral
            if ('M' == status) {
                sc.boundedToken('\t');
                String file = GitUtil.unescapePath(sc.line());
                VirtualFile vFile = myVcsRoot.findFileByRelativePath(file);
                if (!Comparing.equal(GitUtil.gitRootOrNull(vFile), myVcsRoot)) {
                    continue;
                }
                if (!myUnmergedNames.add(file)) {
                    continue;
                }
                // assume modify-modify conflict
                ContentRevision before = GitContentRevision.createRevision(myVcsRoot, file, new GitRevisionNumber("orig_head"), myProject, false, true, true);
                ContentRevision after = GitContentRevision.createRevision(myVcsRoot, file, null, myProject, false, false, true);
                myChanges.add(new Change(before, after, FileStatus.MERGED_WITH_CONFLICTS));
            } else {
                throw new VcsException("Unsupported type of the merge conflict detected: " + status);
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GitRevisionNumber(git4idea.GitRevisionNumber) ContentRevision(com.intellij.openapi.vcs.changes.ContentRevision) GitContentRevision(git4idea.GitContentRevision) Change(com.intellij.openapi.vcs.changes.Change) StringScanner(git4idea.util.StringScanner)

Example 19 with GitRevisionNumber

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

the class GitHistoryUtilsTest method testGetCurrentRevisionInOtherBranch.

@Test
public void testGetCurrentRevisionInOtherBranch() throws Exception {
    checkout("-b feature");
    overwrite(bfile, "new content");
    addCommit("new content");
    final String[] output = log("master --pretty=%H#%at", "-n1").trim().split("#");
    GitRevisionNumber revisionNumber = (GitRevisionNumber) GitHistoryUtils.getCurrentRevision(myProject, toFilePath(bfile), "master");
    assertEquals(revisionNumber.getRev(), output[0]);
    assertEquals(revisionNumber.getTimestamp(), GitTestRevision.gitTimeStampToDate(output[1]));
}
Also used : GitRevisionNumber(git4idea.GitRevisionNumber) Test(org.testng.annotations.Test) GitSingleRepoTest(git4idea.test.GitSingleRepoTest)

Example 20 with GitRevisionNumber

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

the class GitHistoryUtilsTest method testGetCurrentRevision.

@Test
public void testGetCurrentRevision() throws Exception {
    GitRevisionNumber revisionNumber = (GitRevisionNumber) GitHistoryUtils.getCurrentRevision(myProject, toFilePath(bfile), null);
    assertEquals(revisionNumber.getRev(), myRevisions.get(0).myHash);
    assertEquals(revisionNumber.getTimestamp(), myRevisions.get(0).myDate);
}
Also used : GitRevisionNumber(git4idea.GitRevisionNumber) Test(org.testng.annotations.Test) GitSingleRepoTest(git4idea.test.GitSingleRepoTest)

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