Example usage for org.eclipse.jgit.api Git Git

List of usage examples for org.eclipse.jgit.api Git Git

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git Git.

Prototype

public Git(Repository repo) 

Source Link

Document

Construct a new org.eclipse.jgit.api.Git object which can interact with the specified git repository.

Usage

From source file:org.eclipse.egit.core.test.op.CloneOperationTest.java

License:Open Source License

protected void createNoteInOrigin() throws GitAPIException {
    Git git = new Git(repository1.getRepository());
    git.add().addFilepattern("file.txt").call();
    RevCommit commit = git.commit().setMessage("Initial commit").call();
    git.notesAdd().setNotesRef("refs/notes/review").setObjectId(commit).setMessage("text").call();
}

From source file:org.eclipse.egit.core.test.op.CommitOperationTest.java

License:Open Source License

@Test
public void testCommitAll() throws Exception {
    IFile file1 = testUtils.addFileToProject(project.getProject(), "sub/a.txt", "some text");
    testUtils.addFileToProject(project.getProject(), "sub/b.txt", "some text");

    resources.add(project.getProject().getFolder("sub"));
    new AddToIndexOperation(resources).execute(null);
    CommitOperation commitOperation = new CommitOperation(null, null, null, TestUtils.AUTHOR,
            TestUtils.COMMITTER, "first commit");
    commitOperation.setCommitAll(true);/*from  ww  w.  j a  va2s .c o  m*/
    commitOperation.setRepos(new Repository[] { repository });
    commitOperation.execute(null);

    Git git = new Git(repository);
    Iterator<RevCommit> commits = git.log().call().iterator();
    RevCommit firstCommit = commits.next();
    assertTrue(firstCommit.getCommitTime() > 0);

    assertEquals("first commit", firstCommit.getFullMessage());

    testUtils.changeContentOfFile(project.getProject(), file1, "changed text");

    commitOperation = new CommitOperation(null, null, null, TestUtils.AUTHOR, TestUtils.COMMITTER,
            "second commit");
    commitOperation.setCommitAll(true);
    commitOperation.setRepos(new Repository[] { repository });
    commitOperation.execute(null);

    git = new Git(repository);
    commits = git.log().call().iterator();
    RevCommit secondCommit = commits.next();
    assertTrue(secondCommit.getCommitTime() > 0);

    assertEquals("second commit", secondCommit.getFullMessage());
    secondCommit.getParent(0).equals(firstCommit);
    assertEquals("The Author", secondCommit.getAuthorIdent().getName());
    assertEquals("The.author@some.com", secondCommit.getAuthorIdent().getEmailAddress());
    assertEquals("The Commiter", secondCommit.getCommitterIdent().getName());
    assertEquals("The.committer@some.com", secondCommit.getCommitterIdent().getEmailAddress());
}

From source file:org.eclipse.egit.core.test.op.MergeOperationTest.java

License:Open Source License

private int countCommitsInHead() throws GitAPIException {
    LogCommand log = new Git(testRepository.getRepository()).log();
    Iterable<RevCommit> commits = log.call();
    int result = 0;
    for (Iterator i = commits.iterator(); i.hasNext();) {
        i.next();/*from www .j  a v a 2 s  . com*/
        result++;
    }
    return result;
}

From source file:org.eclipse.egit.core.test.op.RebaseOperationTest.java

License:Open Source License

@Before
public void setUp() throws Exception {
    super.setUp();
    testRepository = new TestRepository(gitDir);
    repository = testRepository.getRepository();
    // create first commit containing a dummy file
    testRepository.createInitialCommit("testRebaseOperation\n\nfirst commit\n");
    git = new Git(repository);
}

From source file:org.eclipse.egit.core.test.op.RewordCommitsOperationTest.java

License:Open Source License

@Test
public void reword() throws Exception {
    RewordCommitOperation op = new RewordCommitOperation(testRepository.getRepository(), commit, "new message");
    op.execute(new NullProgressMonitor());

    LogCommand log = new Git(testRepository.getRepository()).log();
    RevCommit newCommit = log.call().iterator().next();
    assertEquals("new message", newCommit.getFullMessage());
}

From source file:org.eclipse.egit.core.test.op.SquashCommitsOperationTest.java

License:Open Source License

@Test
public void squash() throws Exception {
    InteractiveHandler messageHandler = new InteractiveHandler() {
        @Override//from  ww  w  . java  2s.  co m
        public void prepareSteps(List<RebaseTodoLine> steps) {
            // not used
        }

        @Override
        public String modifyCommitMessage(String commit) {
            return "squashed";
        }
    };

    List<RevCommit> commits = Arrays.asList(commit1, commit2, commit3);
    SquashCommitsOperation op = new SquashCommitsOperation(testRepository.getRepository(), commits,
            messageHandler);
    op.execute(new NullProgressMonitor());

    assertEquals(2, countCommitsInHead());

    LogCommand log = new Git(testRepository.getRepository()).log();
    Iterable<RevCommit> logCommits = log.call();
    RevCommit latestCommit = logCommits.iterator().next();
    assertEquals("squashed", latestCommit.getFullMessage());
}

From source file:org.eclipse.egit.core.test.TestRepository.java

License:Open Source License

/**
 * Commits the current index//  w  w w  .  ja v a  2  s.  co m
 *
 * @param message
 *            commit message
 * @return commit object
 *
 * @throws NoHeadException
 * @throws NoMessageException
 * @throws UnmergedPathException
 * @throws ConcurrentRefUpdateException
 * @throws JGitInternalException
 * @throws WrongRepositoryStateException
 */
public RevCommit commit(String message) throws NoHeadException, NoMessageException, UnmergedPathException,
        ConcurrentRefUpdateException, JGitInternalException, WrongRepositoryStateException {
    Git git = new Git(repository);
    CommitCommand commitCommand = git.commit();
    commitCommand.setAuthor("J. Git", "j.git@egit.org");
    commitCommand.setCommitter(commitCommand.getAuthor());
    commitCommand.setMessage(message);
    return commitCommand.call();
}

From source file:org.eclipse.egit.core.test.TestRepository.java

License:Open Source License

/**
 * Adds file to version control/*from  w w w .j  a v a2s  .  c  o m*/
 *
 * @param file
 * @throws IOException
 */
public void track(File file) throws IOException {
    String repoPath = getRepoRelativePath(new Path(file.getPath()).toString());
    try {
        new Git(repository).add().addFilepattern(repoPath).call();
    } catch (NoFilepatternException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:org.eclipse.egit.core.test.TestRepository.java

License:Open Source License

/**
 * Adds the given file to the index/*from w  ww. j  a v  a  2  s  . co m*/
 *
 * @param file
 * @throws CoreException
 * @throws IOException
 */
public void addToIndex(IFile file) throws CoreException, IOException {
    String repoPath = getRepoRelativePath(file.getLocation().toOSString());
    try {
        new Git(repository).add().addFilepattern(repoPath).call();
    } catch (NoFilepatternException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:org.eclipse.egit.gitflow.ui.internal.actions.FeatureCheckoutHandler.java

License:Open Source License

private boolean handleUncommittedFiles(Repository repo, Shell shell, String repoName) throws GitAPIException {
    try (Git git = new Git(repo)) {
        org.eclipse.jgit.api.Status status = git.status().call();

        if (status.hasUncommittedChanges()) {
            List<String> files = new ArrayList<String>(status.getUncommittedChanges());
            Collections.sort(files);
            CleanupUncomittedChangesDialog cleanupUncomittedChangesDialog = new CleanupUncomittedChangesDialog(
                    shell, MessageFormat.format(UIText.FeatureCheckoutHandler_cleanupDialog_title, repoName),
                    UIText.FeatureCheckoutHandler_cleanupDialog_text, repo, files);
            cleanupUncomittedChangesDialog.open();
            return cleanupUncomittedChangesDialog.shouldContinue();
        } else {/*w  ww .  j  a  v  a  2  s . co  m*/
            return true;
        }
    }
}