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

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

Introduction

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

Prototype

public static Git wrap(Repository repo) 

Source Link

Document

Wrap repository

Usage

From source file:jbyoshi.gitupdate.processor.FastForward.java

License:Apache License

private static boolean tryFastForward(Repository repo, Ref ref, Ref target, Report report)
        throws GitAPIException, IOException {
    if (ref == null || target == null) {
        return false;
    }/*w  w w  . j  ava2  s. com*/
    target = repo.peel(target);
    if (!ref.equals(repo.getRef(Constants.HEAD).getTarget())) {
        try (RevWalk revWalk = new RevWalk(repo)) {
            ObjectId targetId = target.getPeeledObjectId();
            if (targetId == null) {
                targetId = target.getObjectId();
            }

            RevCommit targetCommit = revWalk.lookupCommit(targetId);
            ObjectId sourceId = ref.getObjectId();
            RevCommit sourceCommit = revWalk.lookupCommit(sourceId);
            if (revWalk.isMergedInto(sourceCommit, targetCommit)) {
                RefUpdate refUpdate = repo.updateRef(ref.getName());
                refUpdate.setNewObjectId(targetCommit);
                refUpdate.setRefLogMessage("Fast forward", false);
                refUpdate.setExpectedOldObjectId(sourceId);
                Result rc = refUpdate.update();
                switch (rc) {
                case NEW:
                case FAST_FORWARD:
                    report.newChild(ref.getName() + " -> " + target.getName()).modified();
                    return true;
                case REJECTED:
                case LOCK_FAILURE:
                    report.newErrorChild(new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD,
                            refUpdate.getRef(), rc));
                    break;
                case NO_CHANGE:
                    break;
                default:
                    report.newErrorChild(new JGitInternalException(MessageFormat
                            .format(JGitText.get().updatingRefFailed, ref.getName(), targetId.toString(), rc)));
                    break;
                }
            }
            return false;
        }
    }
    try {
        MergeResult result = Git.wrap(repo).merge().setFastForward(MergeCommand.FastForwardMode.FF_ONLY)
                .include(target.getTarget()).call();
        if (result.getMergeStatus() == MergeResult.MergeStatus.ALREADY_UP_TO_DATE) {
            // Ignore
        } else if (result.getMergeStatus() == MergeResult.MergeStatus.FAST_FORWARD) {
            report.newChild("Fast-forwarded " + ref.getName() + " to " + target.getName()).modified();
            return true;
        } else {
            report.newChild("Fast-forward failed: status " + result.getMergeStatus()).error();
        }
    } catch (NoHeadException e) {
        // Ignore
    }
    return false;
}

From source file:org.archicontribs.modelrepository.grafico.GraficoUtilsTests.java

License:Open Source License

@Test
public void getFileContents_IsCorrect() throws Exception {
    File localGitFolder = new File(getTempTestsFolder(), "testRepo");
    String contents = "Hello World!\nTesting.";

    try (Repository repo = GitHelper.createNewRepository(localGitFolder)) {
        File file = new File(localGitFolder, "test.txt");

        try (FileWriter fw = new FileWriter(file)) {
            fw.write(contents);/*w w w  .  j av  a 2 s.c  o  m*/
            fw.flush();
        }

        assertTrue(file.exists());

        // Add file to index
        AddCommand addCommand = new AddCommand(repo);
        addCommand.addFilepattern("."); //$NON-NLS-1$
        addCommand.setUpdate(false);
        addCommand.call();

        // Commit file
        CommitCommand commitCommand = Git.wrap(repo).commit();
        commitCommand.setAuthor("Test", "Test");
        commitCommand.setMessage("Message");
        commitCommand.call();

        assertEquals(contents, GraficoUtils.getFileContents(localGitFolder, "test.txt", "HEAD"));
    }
}

From source file:org.eclipse.egit.core.op.DeleteTagOperation.java

License:Open Source License

public void execute(IProgressMonitor monitor) throws CoreException {
    try {//from  w w  w. j  av a  2  s  . co m
        Git.wrap(repository).tagDelete().setTags(tag).call();
    } catch (GitAPIException e) {
        throw new CoreException(Activator.error(CoreText.DeleteTagOperation_exceptionMessage, e));
    }
}

From source file:org.eclipse.egit.core.op.StashApplyOperation.java

License:Open Source License

public void execute(IProgressMonitor monitor) throws CoreException {
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        public void run(IProgressMonitor pm) throws CoreException {
            pm.beginTask("", 3); //$NON-NLS-1$
            try {
                IProject[] validProjects = ProjectUtil.getValidOpenProjects(repository);
                pm.worked(1);/* w  w  w .  j a  v a  2 s .c  om*/
                Git.wrap(repository).stashApply().setStashRef(commit.name()).call();
                pm.worked(1);
                ProjectUtil.refreshValidProjects(validProjects, new SubProgressMonitor(pm, 1));
            } catch (JGitInternalException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } finally {
                pm.done();
            }
        }
    };
    ResourcesPlugin.getWorkspace().run(action, monitor != null ? monitor : new NullProgressMonitor());
}

From source file:org.eclipse.egit.core.op.StashCreateOperation.java

License:Open Source License

public void execute(IProgressMonitor monitor) throws CoreException {
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        public void run(IProgressMonitor pm) throws CoreException {
            try {
                StashCreateCommand command = Git.wrap(repository).stashCreate();
                if (message != null) {
                    command.setIndexMessage(message);
                    command.setWorkingDirectoryMessage(message);
                }//from  w  w  w  . jav  a 2 s  .c o  m
                commit = command.call();
            } catch (JGitInternalException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } finally {
                if (commit != null)
                    repository.notifyIndexChanged();
                pm.done();
            }
        }
    };
    ResourcesPlugin.getWorkspace().run(action, monitor != null ? monitor : new NullProgressMonitor());
}

From source file:org.eclipse.egit.core.op.StashDropOperation.java

License:Open Source License

public void execute(IProgressMonitor monitor) throws CoreException {
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        public void run(IProgressMonitor pm) throws CoreException {
            pm.beginTask("", 1); //$NON-NLS-1$
            StashDropCommand command = Git.wrap(repo).stashDrop();
            command.setStashRef(index);/*w  w  w. ja v  a  2s.co m*/
            try {
                command.call();
                repo.fireEvent(new RefsChangedEvent());
            } catch (JGitInternalException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } finally {
                pm.done();
            }
        }
    };
    ResourcesPlugin.getWorkspace().run(action, monitor != null ? monitor : new NullProgressMonitor());
}

From source file:org.eclipse.egit.core.op.SubmoduleAddOperation.java

License:Open Source License

public void execute(IProgressMonitor monitor) throws CoreException {
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        public void run(IProgressMonitor pm) throws CoreException {
            final SubmoduleAddCommand add = Git.wrap(repo).submoduleAdd();
            add.setProgressMonitor(new EclipseGitProgressTransformer(pm));
            add.setPath(path);//from ww  w.ja  v a  2s  .  c om
            add.setURI(uri);
            try {
                if (add.call() != null)
                    repo.notifyIndexChanged();
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            }
        }
    };
    ResourcesPlugin.getWorkspace().run(action, monitor != null ? monitor : new NullProgressMonitor());
}

From source file:org.eclipse.egit.core.op.SubmoduleSyncOperation.java

License:Open Source License

public void execute(final IProgressMonitor monitor) throws CoreException {
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        public void run(IProgressMonitor pm) throws CoreException {
            pm.beginTask("", 1); //$NON-NLS-1$
            Map<String, String> updates = null;
            try {
                SubmoduleSyncCommand sync = Git.wrap(repository).submoduleSync();
                for (String path : paths)
                    sync.addPath(path);//from   www  .ja  v  a  2s  . c  o m
                updates = sync.call();
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } finally {
                if (updates != null && !updates.isEmpty())
                    repository.notifyIndexChanged();
                pm.done();
            }
        }
    };
    ResourcesPlugin.getWorkspace().run(action, monitor != null ? monitor : new NullProgressMonitor());
}

From source file:org.eclipse.egit.core.op.SubmoduleUpdateOperation.java

License:Open Source License

public void execute(final IProgressMonitor monitor) throws CoreException {
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        public void run(IProgressMonitor pm) throws CoreException {
            pm.beginTask("", 3); //$NON-NLS-1$
            Git git = Git.wrap(repository);

            Collection<String> updated = null;
            try {
                SubmoduleInitCommand init = git.submoduleInit();
                for (String path : paths)
                    init.addPath(path);/*from   w w  w  .  ja v  a  2s  .  co  m*/
                init.call();
                pm.worked(1);

                SubmoduleUpdateCommand update = git.submoduleUpdate();
                for (String path : paths)
                    update.addPath(path);
                update.setProgressMonitor(new EclipseGitProgressTransformer(new SubProgressMonitor(pm, 2)));
                updated = update.call();
                pm.worked(1);
                SubProgressMonitor refreshMonitor = new SubProgressMonitor(pm, 1);
                refreshMonitor.beginTask("", updated.size()); //$NON-NLS-1$
                for (String path : updated) {
                    Repository subRepo = SubmoduleWalk.getSubmoduleRepository(repository, path);
                    if (subRepo != null)
                        ProjectUtil.refreshValidProjects(ProjectUtil.getValidOpenProjects(subRepo),
                                new SubProgressMonitor(refreshMonitor, 1));
                    else
                        refreshMonitor.worked(1);
                }
                refreshMonitor.done();
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } catch (IOException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } finally {
                if (updated != null && !updated.isEmpty())
                    repository.notifyIndexChanged();
                pm.done();
            }
        }
    };
    ResourcesPlugin.getWorkspace().run(action, monitor != null ? monitor : new NullProgressMonitor());
}

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

License:Open Source License

@Before
public void setUp() throws Exception {
    testUtils = new TestUtils();

    // Create child repo and project
    childProject = testUtils.createProjectInLocalFileSystem("child");
    childProjectDir = childProject.getRawLocation().toFile();
    childRepository = new TestRepository(new File(childProjectDir, Constants.DOT_GIT));
    testUtils.addFileToProject(childProject, "child.txt", "Hello world");
    childRepository.connect(childProject);
    // We have to wait after until the filesystem timer has advanced to
    // avoid smudged states
    RepositoryTestCase.fsTick(null);//from w  ww.jav  a  2 s .  c o  m
    childRepository.trackAllFiles(childProject);
    childRepository.commit("Initial commit");
    // Create parent repo and project
    parentProject = testUtils.createProjectInLocalFileSystem("parent");
    parentProjectDir = parentProject.getRawLocation().toFile();
    parentRepository = new TestRepository(new File(parentProjectDir, Constants.DOT_GIT));
    parentFile = testUtils.addFileToProject(parentProject, "parent.txt", "Hello world");
    Git.wrap(parentRepository.getRepository()).submoduleAdd().setPath("children/child")
            .setURI(childProjectDir.toURI().toString()).call();
    parentRepository.connect(parentProject);
    RepositoryTestCase.fsTick(null);
    parentRepository.trackAllFiles(parentProject);
    parentRepository.commit("Initial commit");

    treeIt = new FileTreeIterator(parentRepository.getRepository());
}