Search in sources :

Example 1 with WindowCacheConfig

use of org.eclipse.jgit.storage.file.WindowCacheConfig in project maven-scm by apache.

the class JGitCheckOutCommand method executeCheckOutCommand.

/**
 * For git, the given repository is a remote one. We have to clone it first if the working directory does not
 * contain a git repo yet, otherwise we have to git-pull it.
 * <p/>
 * {@inheritDoc}
 */
protected CheckOutScmResult executeCheckOutCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive, boolean shallow) throws ScmException {
    GitScmProviderRepository repository = (GitScmProviderRepository) repo;
    if (GitScmProviderRepository.PROTOCOL_FILE.equals(repository.getFetchInfo().getProtocol()) && repository.getFetchInfo().getPath().indexOf(fileSet.getBasedir().getPath()) >= 0) {
        throw new ScmException("remote repository must not be the working directory");
    }
    Git git = null;
    try {
        ProgressMonitor monitor = JGitUtils.getMonitor(getLogger());
        String branch = version != null ? version.getName() : null;
        if (StringUtils.isBlank(branch)) {
            branch = Constants.MASTER;
        }
        getLogger().debug("try checkout of branch: " + branch);
        if (!fileSet.getBasedir().exists() || !(new File(fileSet.getBasedir(), ".git").exists())) {
            if (fileSet.getBasedir().exists()) {
                // git refuses to clone otherwise
                fileSet.getBasedir().delete();
            }
            // FIXME only if windauze
            WindowCacheConfig cfg = new WindowCacheConfig();
            cfg.setPackedGitMMAP(false);
            cfg.install();
            // no git repo seems to exist, let's clone the original repo
            CredentialsProvider credentials = JGitUtils.getCredentials((GitScmProviderRepository) repo);
            getLogger().info("cloning [" + branch + "] to " + fileSet.getBasedir());
            CloneCommand command = Git.cloneRepository().setURI(repository.getFetchUrl());
            command.setCredentialsProvider(credentials).setBranch(branch).setDirectory(fileSet.getBasedir());
            command.setProgressMonitor(monitor);
            git = command.call();
        }
        JGitRemoteInfoCommand remoteInfoCommand = new JGitRemoteInfoCommand();
        remoteInfoCommand.setLogger(getLogger());
        RemoteInfoScmResult result = remoteInfoCommand.executeRemoteInfoCommand(repository, fileSet, null);
        if (git == null) {
            // deliberately not using JGitUtils.openRepo(), the caller told us exactly where to checkout
            git = Git.open(fileSet.getBasedir());
        }
        if (fileSet.getBasedir().exists() && new File(fileSet.getBasedir(), ".git").exists() && result.getBranches().size() > 0) {
            // git repo exists, so we must git-pull the changes
            CredentialsProvider credentials = JGitUtils.prepareSession(getLogger(), git, repository);
            if (version != null && StringUtils.isNotEmpty(version.getName()) && (version instanceof ScmTag)) {
                // A tag will not be pulled but we only fetch all the commits from the upstream repo
                // This is done because checking out a tag might not happen on the current branch
                // but create a 'detached HEAD'.
                // In fact, a tag in git may be in multiple branches. This occurs if
                // you create a branch after the tag has been created
                getLogger().debug("fetch...");
                git.fetch().setCredentialsProvider(credentials).setProgressMonitor(monitor).call();
            } else {
                getLogger().debug("pull...");
                git.pull().setCredentialsProvider(credentials).setProgressMonitor(monitor).call();
            }
        }
        Set<String> localBranchNames = JGitBranchCommand.getShortLocalBranchNames(git);
        if (version instanceof ScmTag) {
            getLogger().info("checkout tag [" + branch + "] at " + fileSet.getBasedir());
            git.checkout().setName(branch).call();
        } else if (localBranchNames.contains(branch)) {
            getLogger().info("checkout [" + branch + "] at " + fileSet.getBasedir());
            git.checkout().setName(branch).call();
        } else {
            getLogger().info("checkout remote branch [" + branch + "] at " + fileSet.getBasedir());
            git.checkout().setName(branch).setCreateBranch(true).setStartPoint(Constants.DEFAULT_REMOTE_NAME + "/" + branch).call();
        }
        RevWalk revWalk = new RevWalk(git.getRepository());
        RevCommit commit = revWalk.parseCommit(git.getRepository().resolve(Constants.HEAD));
        revWalk.release();
        final TreeWalk walk = new TreeWalk(git.getRepository());
        // drop the first empty tree, which we do not need here
        walk.reset();
        walk.setRecursive(true);
        walk.addTree(commit.getTree());
        List<ScmFile> listedFiles = new ArrayList<ScmFile>();
        while (walk.next()) {
            listedFiles.add(new ScmFile(walk.getPathString(), ScmFileStatus.CHECKED_OUT));
        }
        walk.release();
        getLogger().debug("current branch: " + git.getRepository().getBranch());
        return new CheckOutScmResult("checkout via JGit", listedFiles);
    } catch (Exception e) {
        throw new ScmException("JGit checkout failure!", e);
    } finally {
        JGitUtils.closeRepo(git);
    }
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) ScmException(org.apache.maven.scm.ScmException) WindowCacheConfig(org.eclipse.jgit.storage.file.WindowCacheConfig) GitScmProviderRepository(org.apache.maven.scm.provider.git.repository.GitScmProviderRepository) ArrayList(java.util.ArrayList) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) RevWalk(org.eclipse.jgit.revwalk.RevWalk) JGitRemoteInfoCommand(org.apache.maven.scm.provider.git.jgit.command.remoteinfo.JGitRemoteInfoCommand) RemoteInfoScmResult(org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult) ScmException(org.apache.maven.scm.ScmException) ScmFile(org.apache.maven.scm.ScmFile) ProgressMonitor(org.eclipse.jgit.lib.ProgressMonitor) Git(org.eclipse.jgit.api.Git) ScmTag(org.apache.maven.scm.ScmTag) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with WindowCacheConfig

use of org.eclipse.jgit.storage.file.WindowCacheConfig in project fabric8 by jboss-fuse.

the class GitHttpServerRegistrationHandler method unregisterServlet.

/**
 * The complexity of this code is due to problems with jgit and Windows file system.
 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=300084
 */
private void unregisterServlet() {
    unregisterGitHttpEndpoint();
    synchronized (gitRemoteUrl) {
        if (basePath != null) {
            try {
                httpService.get().unregister("/git");
            } catch (IllegalArgumentException e) {
                LOGGER.warn("/git Servlet wasn't registered. Unregistration not required.");
            }
            // this should foul jgit to use flush its cache and release locks for .pack files on Windows
            WindowCacheConfig cfg = new WindowCacheConfig();
            cfg.install();
            git.getRepository().close();
            git = null;
            boolean basePathExists = basePath.toFile().exists();
            for (int i = 0; i < 10; i++) {
                try {
                    if (basePathExists) {
                        recursiveDelete(basePath.toFile());
                        basePathExists = false;
                    }
                    break;
                } catch (IOException e) {
                    LOGGER.debug("Failed to recursively delete this filesystem path: {}", basePath, e);
                    System.gc();
                    try {
                        Thread.sleep(3 * 1000);
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
            if (basePathExists) {
                LOGGER.error("Failed to recursively delete this filesystem path: {}", basePath);
            } else {
                LOGGER.info("Correctly removed old git repo: {}", basePath);
            }
        }
    }
}
Also used : WindowCacheConfig(org.eclipse.jgit.storage.file.WindowCacheConfig) IOException(java.io.IOException) GitHttpEndpoint(io.fabric8.git.GitHttpEndpoint)

Example 3 with WindowCacheConfig

use of org.eclipse.jgit.storage.file.WindowCacheConfig in project egit by eclipse.

the class GitProjectData method reconfigureWindowCache.

/**
 * Update the settings for the global window cache of the workspace.
 */
public static void reconfigureWindowCache() {
    final WindowCacheConfig c = new WindowCacheConfig();
    IEclipsePreferences d = DefaultScope.INSTANCE.getNode(Activator.getPluginId());
    IEclipsePreferences p = InstanceScope.INSTANCE.getNode(Activator.getPluginId());
    c.setPackedGitLimit(p.getInt(GitCorePreferences.core_packedGitLimit, d.getInt(GitCorePreferences.core_packedGitLimit, 0)));
    c.setPackedGitWindowSize(p.getInt(GitCorePreferences.core_packedGitWindowSize, d.getInt(GitCorePreferences.core_packedGitWindowSize, 0)));
    c.setPackedGitMMAP(p.getBoolean(GitCorePreferences.core_packedGitMMAP, d.getBoolean(GitCorePreferences.core_packedGitMMAP, false)));
    c.setDeltaBaseCacheLimit(p.getInt(GitCorePreferences.core_deltaBaseCacheLimit, d.getInt(GitCorePreferences.core_deltaBaseCacheLimit, 0)));
    c.setStreamFileThreshold(p.getInt(GitCorePreferences.core_streamFileThreshold, d.getInt(GitCorePreferences.core_streamFileThreshold, 0)));
    c.install();
}
Also used : WindowCacheConfig(org.eclipse.jgit.storage.file.WindowCacheConfig) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences)

Example 4 with WindowCacheConfig

use of org.eclipse.jgit.storage.file.WindowCacheConfig in project gitblit by gitblit.

the class RepositoryManager method configureJGit.

protected void configureJGit() {
    // Configure JGit
    WindowCacheConfig cfg = new WindowCacheConfig();
    cfg.setPackedGitWindowSize(settings.getFilesize(Keys.git.packedGitWindowSize, cfg.getPackedGitWindowSize()));
    cfg.setPackedGitLimit(settings.getFilesize(Keys.git.packedGitLimit, cfg.getPackedGitLimit()));
    cfg.setDeltaBaseCacheLimit(settings.getFilesize(Keys.git.deltaBaseCacheLimit, cfg.getDeltaBaseCacheLimit()));
    cfg.setPackedGitOpenFiles(settings.getInteger(Keys.git.packedGitOpenFiles, cfg.getPackedGitOpenFiles()));
    cfg.setPackedGitMMAP(settings.getBoolean(Keys.git.packedGitMmap, cfg.isPackedGitMMAP()));
    try {
        cfg.install();
        logger.debug(MessageFormat.format("{0} = {1,number,0}", Keys.git.packedGitWindowSize, cfg.getPackedGitWindowSize()));
        logger.debug(MessageFormat.format("{0} = {1,number,0}", Keys.git.packedGitLimit, cfg.getPackedGitLimit()));
        logger.debug(MessageFormat.format("{0} = {1,number,0}", Keys.git.deltaBaseCacheLimit, cfg.getDeltaBaseCacheLimit()));
        logger.debug(MessageFormat.format("{0} = {1,number,0}", Keys.git.packedGitOpenFiles, cfg.getPackedGitOpenFiles()));
        logger.debug(MessageFormat.format("{0} = {1}", Keys.git.packedGitMmap, cfg.isPackedGitMMAP()));
    } catch (IllegalArgumentException e) {
        logger.error("Failed to configure JGit parameters!", e);
    }
    try {
        // issue-486/ticket-151: UTF-9 & UTF-18
        // issue-560/ticket-237: 'UTF8'
        Field field = RawParseUtils.class.getDeclaredField("encodingAliases");
        field.setAccessible(true);
        Map<String, Charset> encodingAliases = (Map<String, Charset>) field.get(null);
        encodingAliases.put("'utf8'", RawParseUtils.UTF8_CHARSET);
        encodingAliases.put("utf-9", RawParseUtils.UTF8_CHARSET);
        encodingAliases.put("utf-18", RawParseUtils.UTF8_CHARSET);
        logger.info("Alias 'UTF8', UTF-9 & UTF-18 encodings as UTF-8 in JGit");
    } catch (Throwable t) {
        logger.error("Failed to inject UTF-9 & UTF-18 encoding aliases into JGit", t);
    }
}
Also used : Field(java.lang.reflect.Field) WindowCacheConfig(org.eclipse.jgit.storage.file.WindowCacheConfig) Charset(java.nio.charset.Charset) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

WindowCacheConfig (org.eclipse.jgit.storage.file.WindowCacheConfig)4 GitHttpEndpoint (io.fabric8.git.GitHttpEndpoint)1 File (java.io.File)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Charset (java.nio.charset.Charset)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ScmException (org.apache.maven.scm.ScmException)1 ScmFile (org.apache.maven.scm.ScmFile)1 ScmTag (org.apache.maven.scm.ScmTag)1 CheckOutScmResult (org.apache.maven.scm.command.checkout.CheckOutScmResult)1 RemoteInfoScmResult (org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult)1 JGitRemoteInfoCommand (org.apache.maven.scm.provider.git.jgit.command.remoteinfo.JGitRemoteInfoCommand)1 GitScmProviderRepository (org.apache.maven.scm.provider.git.repository.GitScmProviderRepository)1 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)1 CloneCommand (org.eclipse.jgit.api.CloneCommand)1 Git (org.eclipse.jgit.api.Git)1