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

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

Introduction

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

Prototype

public CommitCommand commit() 

Source Link

Document

Return a command object to execute a Commit command

Usage

From source file:de.blizzy.documentr.page.PageStore.java

License:Open Source License

@Override
public void deletePage(String projectName, String branchName, final String path, User user) throws IOException {
    Assert.hasLength(projectName);/*w w w  .  ja v a2  s  .c o m*/
    Assert.hasLength(branchName);
    Assert.hasLength(path);
    Assert.notNull(user);

    ILockedRepository repo = null;
    List<String> oldPagePaths;
    boolean deleted = false;
    try {
        repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName);
        File workingDir = RepositoryUtil.getWorkingDir(repo.r());

        File pagesDir = new File(workingDir, DocumentrConstants.PAGES_DIR_NAME);

        File oldSubPagesDir = Util.toFile(pagesDir, path);
        oldPagePaths = listPagePaths(oldSubPagesDir, true);
        oldPagePaths = Lists.newArrayList(Lists.transform(oldPagePaths, new Function<String, String>() {
            @Override
            public String apply(String p) {
                return path + "/" + p; //$NON-NLS-1$
            }
        }));
        oldPagePaths.add(path);

        Git git = Git.wrap(repo.r());

        File file = Util.toFile(pagesDir, path + DocumentrConstants.PAGE_SUFFIX);
        if (file.isFile()) {
            git.rm().addFilepattern(
                    DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.PAGE_SUFFIX) //$NON-NLS-1$
                    .call();
            deleted = true;
        }
        file = Util.toFile(pagesDir, path + DocumentrConstants.META_SUFFIX);
        if (file.isFile()) {
            git.rm().addFilepattern(
                    DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.META_SUFFIX) //$NON-NLS-1$
                    .call();
            deleted = true;
        }
        file = Util.toFile(pagesDir, path);
        if (file.isDirectory()) {
            git.rm().addFilepattern(DocumentrConstants.PAGES_DIR_NAME + "/" + path) //$NON-NLS-1$
                    .call();
            deleted = true;
        }

        File attachmentsDir = new File(workingDir, DocumentrConstants.ATTACHMENTS_DIR_NAME);
        file = Util.toFile(attachmentsDir, path);
        if (file.isDirectory()) {
            git.rm().addFilepattern(DocumentrConstants.ATTACHMENTS_DIR_NAME + "/" + path) //$NON-NLS-1$
                    .call();
            deleted = true;
        }

        if (deleted) {
            PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail());
            git.commit().setAuthor(ident).setCommitter(ident).setMessage("delete " + path) //$NON-NLS-1$
                    .call();
            git.push().call();

            PageUtil.updateProjectEditTime(projectName);
        }
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }

    if (deleted) {
        eventBus.post(new PagesDeletedEvent(projectName, branchName, Sets.newHashSet(oldPagePaths)));
    }
}

From source file:de.blizzy.documentr.page.PageStore.java

License:Open Source License

@Override
public void relocatePage(final String projectName, final String branchName, final String path,
        String newParentPagePath, User user) throws IOException {

    Assert.hasLength(projectName);//from  w ww  .  j av  a  2s. co  m
    Assert.hasLength(branchName);
    Assert.hasLength(path);
    Assert.hasLength(newParentPagePath);
    Assert.notNull(user);
    // check if pages exist by trying to load them
    getPage(projectName, branchName, path, false);
    getPage(projectName, branchName, newParentPagePath, false);

    ILockedRepository repo = null;
    List<String> oldPagePaths;
    List<String> deletedPagePaths;
    List<String> newPagePaths;
    List<PagePathChangedEvent> pagePathChangedEvents;
    try {
        repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName);
        String pageName = path.contains("/") ? StringUtils.substringAfterLast(path, "/") : path; //$NON-NLS-1$ //$NON-NLS-2$
        final String newPagePath = newParentPagePath + "/" + pageName; //$NON-NLS-1$

        File workingDir = RepositoryUtil.getWorkingDir(repo.r());

        File oldSubPagesDir = Util.toFile(new File(workingDir, DocumentrConstants.PAGES_DIR_NAME), path);
        oldPagePaths = listPagePaths(oldSubPagesDir, true);
        oldPagePaths = Lists.newArrayList(Lists.transform(oldPagePaths, new Function<String, String>() {
            @Override
            public String apply(String p) {
                return path + "/" + p; //$NON-NLS-1$
            }
        }));
        oldPagePaths.add(path);

        File deletedPagesSubDir = Util.toFile(new File(workingDir, DocumentrConstants.PAGES_DIR_NAME),
                newPagePath);
        deletedPagePaths = listPagePaths(deletedPagesSubDir, true);
        deletedPagePaths = Lists.newArrayList(Lists.transform(deletedPagePaths, new Function<String, String>() {
            @Override
            public String apply(String p) {
                return newPagePath + "/" + p; //$NON-NLS-1$
            }
        }));
        deletedPagePaths.add(newPagePath);

        Git git = Git.wrap(repo.r());
        AddCommand addCommand = git.add();

        for (String dirName : Sets.newHashSet(DocumentrConstants.PAGES_DIR_NAME,
                DocumentrConstants.ATTACHMENTS_DIR_NAME)) {
            File dir = new File(workingDir, dirName);

            File newSubPagesDir = Util.toFile(dir, newPagePath);
            if (newSubPagesDir.exists()) {
                git.rm().addFilepattern(dirName + "/" + newPagePath).call(); //$NON-NLS-1$
            }
            File newPageFile = Util.toFile(dir, newPagePath + DocumentrConstants.PAGE_SUFFIX);
            if (newPageFile.exists()) {
                git.rm().addFilepattern(dirName + "/" + newPagePath + DocumentrConstants.PAGE_SUFFIX).call(); //$NON-NLS-1$
            }
            File newMetaFile = Util.toFile(dir, newPagePath + DocumentrConstants.META_SUFFIX);
            if (newMetaFile.exists()) {
                git.rm().addFilepattern(dirName + "/" + newPagePath + DocumentrConstants.META_SUFFIX).call(); //$NON-NLS-1$
            }

            File newParentPageDir = Util.toFile(dir, newParentPagePath);
            File subPagesDir = Util.toFile(dir, path);
            if (subPagesDir.exists()) {
                FileUtils.copyDirectoryToDirectory(subPagesDir, newParentPageDir);
                git.rm().addFilepattern(dirName + "/" + path).call(); //$NON-NLS-1$
                addCommand.addFilepattern(dirName + "/" + newParentPagePath + "/" + pageName); //$NON-NLS-1$ //$NON-NLS-2$
            }
            File pageFile = Util.toFile(dir, path + DocumentrConstants.PAGE_SUFFIX);
            if (pageFile.exists()) {
                FileUtils.copyFileToDirectory(pageFile, newParentPageDir);
                git.rm().addFilepattern(dirName + "/" + path + DocumentrConstants.PAGE_SUFFIX).call(); //$NON-NLS-1$
                addCommand.addFilepattern(
                        dirName + "/" + newParentPagePath + "/" + pageName + DocumentrConstants.PAGE_SUFFIX); //$NON-NLS-1$ //$NON-NLS-2$
            }
            File metaFile = Util.toFile(dir, path + DocumentrConstants.META_SUFFIX);
            if (metaFile.exists()) {
                FileUtils.copyFileToDirectory(metaFile, newParentPageDir);
                git.rm().addFilepattern(dirName + "/" + path + DocumentrConstants.META_SUFFIX).call(); //$NON-NLS-1$
                addCommand.addFilepattern(
                        dirName + "/" + newParentPagePath + "/" + pageName + DocumentrConstants.META_SUFFIX); //$NON-NLS-1$ //$NON-NLS-2$
            }
        }

        addCommand.call();
        PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail());
        git.commit().setAuthor(ident).setCommitter(ident)
                .setMessage("move " + path + " to " + newParentPagePath + "/" + pageName) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                .call();
        git.push().call();

        newPagePaths = Lists.transform(oldPagePaths, new Function<String, String>() {
            @Override
            public String apply(String p) {
                return newPagePath + StringUtils.removeStart(p, path);
            }
        });

        pagePathChangedEvents = Lists.transform(oldPagePaths, new Function<String, PagePathChangedEvent>() {
            @Override
            public PagePathChangedEvent apply(String oldPath) {
                String newPath = newPagePath + StringUtils.removeStart(oldPath, path);
                return new PagePathChangedEvent(projectName, branchName, oldPath, newPath);
            }
        });

        PageUtil.updateProjectEditTime(projectName);
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }

    Set<String> allDeletedPagePaths = Sets.newHashSet(oldPagePaths);
    allDeletedPagePaths.addAll(deletedPagePaths);
    eventBus.post(new PagesDeletedEvent(projectName, branchName, allDeletedPagePaths));

    for (String newPath : newPagePaths) {
        eventBus.post(new PageChangedEvent(projectName, branchName, newPath));
    }

    for (PagePathChangedEvent event : pagePathChangedEvents) {
        eventBus.post(event);
    }
}

From source file:de.blizzy.documentr.page.PageStore.java

License:Open Source License

@Override
public void deleteAttachment(String projectName, String branchName, String pagePath, String name, User user)
        throws IOException {

    Assert.hasLength(projectName);//from w ww  . ja  v a2 s.c  o  m
    Assert.hasLength(branchName);
    Assert.hasLength(pagePath);
    Assert.hasLength(name);
    Assert.notNull(user);

    ILockedRepository repo = null;
    try {
        repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName);
        File workingDir = RepositoryUtil.getWorkingDir(repo.r());

        Git git = Git.wrap(repo.r());

        File attachmentsDir = new File(workingDir, DocumentrConstants.ATTACHMENTS_DIR_NAME);
        boolean deleted = false;
        File file = Util.toFile(attachmentsDir, pagePath + "/" + name + DocumentrConstants.PAGE_SUFFIX); //$NON-NLS-1$
        if (file.isFile()) {
            FileUtils.forceDelete(file);
            git.rm().addFilepattern(DocumentrConstants.ATTACHMENTS_DIR_NAME + "/" + //$NON-NLS-1$
                    pagePath + "/" + name + DocumentrConstants.PAGE_SUFFIX) //$NON-NLS-1$
                    .call();
            deleted = true;
        }
        file = Util.toFile(attachmentsDir, pagePath + "/" + name + DocumentrConstants.META_SUFFIX); //$NON-NLS-1$
        if (file.isFile()) {
            FileUtils.forceDelete(file);
            git.rm().addFilepattern(DocumentrConstants.ATTACHMENTS_DIR_NAME + "/" + //$NON-NLS-1$
                    pagePath + "/" + name + DocumentrConstants.META_SUFFIX) //$NON-NLS-1$
                    .call();
            deleted = true;
        }

        if (deleted) {
            PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail());
            git.commit().setAuthor(ident).setCommitter(ident).setMessage("delete " + pagePath + "/" + name) //$NON-NLS-1$ //$NON-NLS-2$
                    .call();
            git.push().call();

            PageUtil.updateProjectEditTime(projectName);
        }
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }
}

From source file:de.blizzy.documentr.page.PageStore.java

License:Open Source License

@Override
public void restorePageVersion(String projectName, String branchName, String path, String version, User user)
        throws IOException {

    Assert.hasLength(projectName);//from w  ww. j  a v a2 s  .co m
    Assert.hasLength(branchName);
    Assert.hasLength(path);
    Assert.hasLength(version);

    ILockedRepository repo = null;
    try {
        repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName);
        String text = BlobUtils.getContent(repo.r(), version,
                DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.PAGE_SUFFIX); //$NON-NLS-1$
        File workingDir = RepositoryUtil.getWorkingDir(repo.r());
        File pagesDir = new File(workingDir, DocumentrConstants.PAGES_DIR_NAME);
        File file = Util.toFile(pagesDir, path + DocumentrConstants.PAGE_SUFFIX);
        FileUtils.writeStringToFile(file, text, Charsets.UTF_8.name());

        Git git = Git.wrap(repo.r());
        git.add()
                .addFilepattern(DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.PAGE_SUFFIX) //$NON-NLS-1$
                .call();
        PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail());
        git.commit().setAuthor(ident).setCommitter(ident)
                .setMessage(DocumentrConstants.PAGES_DIR_NAME + "/" + path) //$NON-NLS-1$
                .call();
        git.push().call();
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }

    eventBus.post(new PageChangedEvent(projectName, branchName, path));
}

From source file:de.blizzy.documentr.page.PageUtilTest.java

License:Open Source License

@Test
public void toPageVersion() throws GitAPIException, IOException {
    File repoDir = tempDir.getRoot();
    Git git = Git.init().setDirectory(repoDir).call();
    PersonIdent ident = new PersonIdent("user", "user@example.com"); //$NON-NLS-1$ //$NON-NLS-2$
    git.commit().setAuthor(ident).setCommitter(ident).setMessage("test").call(); //$NON-NLS-1$
    RevCommit commit = CommitUtils.getHead(git.getRepository());

    PageVersion pageVersion = PageUtil.toPageVersion(commit);
    assertEquals("user", pageVersion.getLastEditedBy()); //$NON-NLS-1$
    assertSecondsAgo(pageVersion.getLastEdited(), 5);
    assertEquals(commit.getName(), pageVersion.getCommitName());
}

From source file:de.blizzy.documentr.repository.ProjectRepositoryManager.java

License:Open Source License

ILockedRepository createCentralRepository(boolean bare, User user) throws IOException, GitAPIException {
    if (centralRepoDir.isDirectory()) {
        throw new IllegalStateException("repository already exists: " + centralRepoDir.getAbsolutePath()); //$NON-NLS-1$
    }/*from  www. j  av  a2s  .  c  om*/

    ILock lock = lockManager.lockAll();
    try {
        Repository repo = null;
        File gitDir = new File(centralRepoDir, ".git"); //$NON-NLS-1$
        try {
            RepositoryBuilder builder = new RepositoryBuilder().setGitDir(gitDir);
            if (bare) {
                builder.setBare();
            }
            repo = builder.build();
            repo.create();
        } finally {
            RepositoryUtil.closeQuietly(repo);
        }

        File tempGitDir = new File(new File(reposDir, CENTRAL_REPO_NAME + "_temp"), ".git"); //$NON-NLS-1$ //$NON-NLS-2$
        Repository tempRepo = null;
        try {
            tempRepo = Git.cloneRepository().setURI(gitDir.toURI().toString()).setDirectory(tempGitDir).call()
                    .getRepository();
            Git git = Git.wrap(tempRepo);
            PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail());
            git.commit().setAuthor(ident).setCommitter(ident).setMessage("init").call(); //$NON-NLS-1$
            git.push().call();
        } finally {
            RepositoryUtil.closeQuietly(tempRepo);
        }
        FileUtils.forceDelete(tempGitDir.getParentFile());
    } finally {
        lockManager.unlock(lock);
    }

    return getCentralRepository(bare);
}

From source file:de.blizzy.documentr.subscription.SubscriptionStore.java

License:Open Source License

public void subscribe(String projectName, String branchName, String path, User user) throws IOException {
    ILockedRepository repo = null;//from w w  w .  ja  v a  2 s .co  m
    try {
        repo = getOrCreateRepository(user);
        String loginName = user.getLoginName();
        String json = BlobUtils.getHeadContent(repo.r(), loginName + SUBSCRIPTIONS_SUFFIX);
        Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
        Set<Page> pages = Sets.newHashSet();
        if (StringUtils.isNotBlank(json)) {
            List<Page> pagesList = gson.fromJson(json, new TypeToken<List<Page>>() {
            }.getType());
            pages = Sets.newHashSet(pagesList);
        }

        Page page = new Page(projectName, branchName, path);
        if (pages.add(page)) {
            json = gson.toJson(pages);
            File workingDir = RepositoryUtil.getWorkingDir(repo.r());
            File file = new File(workingDir, loginName + SUBSCRIPTIONS_SUFFIX);
            FileUtils.writeStringToFile(file, json, Charsets.UTF_8);

            Git git = Git.wrap(repo.r());
            git.add().addFilepattern(loginName + SUBSCRIPTIONS_SUFFIX).call();
            PersonIdent ident = new PersonIdent(loginName, user.getEmail());
            git.commit().setAuthor(ident).setCommitter(ident).setMessage(loginName + SUBSCRIPTIONS_SUFFIX)
                    .call();
        }
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }
}

From source file:de.blizzy.documentr.subscription.SubscriptionStore.java

License:Open Source License

public void unsubscribe(String projectName, String branchName, String path, User user) throws IOException {
    ILockedRepository repo = null;//  w  ww  .j  a va 2 s  . c om
    try {
        repo = getOrCreateRepository(user);
        String json = BlobUtils.getHeadContent(repo.r(), user.getLoginName() + SUBSCRIPTIONS_SUFFIX);
        if (StringUtils.isNotBlank(json)) {
            Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
            List<Page> pagesList = gson.fromJson(json, new TypeToken<List<Page>>() {
            }.getType());
            Set<Page> pages = Sets.newHashSet(pagesList);
            Page page = new Page(projectName, branchName, path);
            if (pages.remove(page)) {
                Git git = Git.wrap(repo.r());
                if (!pages.isEmpty()) {
                    json = gson.toJson(pages);
                    File workingDir = RepositoryUtil.getWorkingDir(repo.r());
                    File file = new File(workingDir, user.getLoginName() + SUBSCRIPTIONS_SUFFIX);
                    FileUtils.writeStringToFile(file, json, Charsets.UTF_8);
                    git.add().addFilepattern(user.getLoginName() + SUBSCRIPTIONS_SUFFIX).call();
                } else {
                    git.rm().addFilepattern(user.getLoginName() + SUBSCRIPTIONS_SUFFIX).call();
                }

                PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail());
                git.commit().setAuthor(ident).setCommitter(ident)
                        .setMessage(user.getLoginName() + SUBSCRIPTIONS_SUFFIX).call();
            }
        }
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }
}

From source file:de.blizzy.documentr.system.SystemSettingsStore.java

License:Open Source License

private void storeSettings(Map<String, String> settings, User currentUser) throws IOException {
    log.info("storing system settings"); //$NON-NLS-1$

    ILockedRepository repo = null;//from  ww  w . jav a2s .  com
    try {
        repo = getOrCreateRepository(currentUser);
        File workingDir = RepositoryUtil.getWorkingDir(repo.r());
        File file = new File(workingDir, SETTINGS_FILE_NAME);
        Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
        String json = gson.toJson(settings);
        FileUtils.writeStringToFile(file, json, Charsets.UTF_8);
        Git git = Git.wrap(repo.r());
        git.add().addFilepattern(SETTINGS_FILE_NAME).call();
        PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail());
        git.commit().setAuthor(ident).setCommitter(ident).setMessage(SETTINGS_FILE_NAME).call();
    } catch (GitAPIException e) {
        throw new IOException(e);
    } finally {
        Closeables.closeQuietly(repo);
    }
}

From source file:de.fkoeberle.autocommit.git.GitRepositoryAdapter.java

License:Open Source License

private void commitStagedFiles(Git git, String message) throws UnmergedPathException, IOException {
    CommitCommand commitCommand = git.commit();
    commitCommand.setMessage(message);/*from  www .j a va  2 s  .c  o m*/

    try {
        commitCommand.call();
    } catch (GitAPIException e) {
        throw new IOException(e);
    }
}