Example usage for org.eclipse.jgit.lib Repository getDirectory

List of usage examples for org.eclipse.jgit.lib Repository getDirectory

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository getDirectory.

Prototype


public File getDirectory() 

Source Link

Document

Get local metadata directory

Usage

From source file:svnserver.repository.git.push.GitPushNative.java

License:GNU General Public License

@Override
public boolean push(@NotNull Repository repository, @NotNull ObjectId commitId, @NotNull String branch,
        @NotNull User userInfo) throws SVNException, IOException {
    try {//from w  w w.jav a 2 s. c om
        repository.getDirectory();
        final ProcessBuilder processBuilder = new ProcessBuilder("git", "push", "--porcelain", "--quiet", ".",
                commitId.name() + ":" + branch).directory(repository.getDirectory()).redirectErrorStream(true);
        processBuilder.environment().put("LANG", "en_US.utf8");
        userInfo.updateEnvironment(processBuilder.environment());
        context.sure(UserDB.class).updateEnvironment(processBuilder.environment(), userInfo);
        final Process process = processBuilder.start();
        final StringBuilder resultBuilder = new StringBuilder();
        final StringBuilder hookBuilder = new StringBuilder();
        try (final BufferedReader stdout = new BufferedReader(
                new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
            while (true) {
                final String line = stdout.readLine();
                if (line == null) {
                    break;
                }
                if (line.startsWith(HOOK_MESSAGE_PREFIX)) {
                    if (hookBuilder.length() > 0)
                        hookBuilder.append('\n');
                    hookBuilder.append(line.substring(HOOK_MESSAGE_PREFIX.length() + 1));
                }
                if (line.startsWith(SYSTEM_MESSAGE_PREFIX)) {
                    // System message like:
                    // !   2d1ed4dcc45bef07f6dfffabe7d3ff53aa147705:refs/heads/local   [remote rejected] (pre-receive hook declined)
                    // !   75cad4dcb5f6982a1f2df073157f3aa2083ae272:refs/heads/local   [rejected] (non-fast-forward)
                    if (resultBuilder.length() > 0)
                        resultBuilder.append('\n');
                    resultBuilder.append(line.substring(SYSTEM_MESSAGE_PREFIX.length() + 1));
                }
            }
        }
        int exitCode = process.waitFor();
        if (exitCode == 0) {
            return true;
        }
        final String resultMessage = resultBuilder.toString();
        if (resultMessage.contains("non-fast-forward")) {
            return false;
        }
        if (resultMessage.contains("hook")) {
            final String hookMessage = hookBuilder.toString();
            log.warn("Push rejected by hook:\n{}", hookMessage);
            throw new SVNException(SVNErrorMessage.create(SVNErrorCode.REPOS_HOOK_FAILURE,
                    "Commit blocked by hook with output:\n" + hookMessage));
        }
        log.error("Unknown git push result:\n{}", resultMessage);
        throw new SVNException(SVNErrorMessage.create(SVNErrorCode.IO_WRITE_ERROR, resultMessage));
    } catch (InterruptedException e) {
        e.printStackTrace();
        throw new SVNException(SVNErrorMessage.create(SVNErrorCode.IO_WRITE_ERROR, e));
    }
}

From source file:svnserver.SvnTestServer.java

License:GNU General Public License

private SvnTestServer(@NotNull Repository repository, @Nullable String branch, @NotNull String prefix,
        boolean safeBranch, @Nullable UserDBConfig userDBConfig, boolean anonymousRead) throws Exception {
    SVNFileUtil.setSleepForTimestamp(false);
    this.repository = repository;
    this.safeBranch = safeBranch;
    this.prefix = prefix;
    tempDirectory = TestHelper.createTempDir("git-as-svn");
    final String srcBranch = branch == null ? repository.getBranch() : branch;
    if (safeBranch) {
        cleanupBranches(repository);//from  w w w .j a  va  2s. c  o  m
        testBranch = TEST_BRANCH_PREFIX + UUID.randomUUID().toString().replaceAll("-", "").substring(0, 8);
        new Git(repository).branchCreate().setName(testBranch).setStartPoint(srcBranch).call();
    } else {
        testBranch = srcBranch;
    }

    final Config config = new Config(BIND_HOST, 0);
    config.setCompressionEnabled(false);
    config.setCacheConfig(new MemoryCacheConfig());
    config.setRepositoryMapping(new TestRepositoryConfig(repository, testBranch, prefix, anonymousRead));
    if (userDBConfig != null) {
        config.setUserDB(userDBConfig);
    } else {
        config.setUserDB(new LocalUserDBConfig(new LocalUserDBConfig.UserEntry[] {
                new LocalUserDBConfig.UserEntry(USER_NAME, REAL_NAME, EMAIL, PASSWORD),
                new LocalUserDBConfig.UserEntry(USER_NAME_NO_MAIL, REAL_NAME, null, PASSWORD), }));
    }
    config.getShared().add(context -> context.add(LfsStorageFactory.class, new LfsMemoryStorage.Factory()));
    server = new SvnServer(tempDirectory, config);
    server.start();
    log.info("Temporary server started (url: {}, path: {}, branch: {} as {})", getUrl(),
            repository.getDirectory(), srcBranch, testBranch);
    log.info("Temporary directory: {}", tempDirectory);
}