Example usage for org.eclipse.jgit.lib Constants DOT_GIT_EXT

List of usage examples for org.eclipse.jgit.lib Constants DOT_GIT_EXT

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Constants DOT_GIT_EXT.

Prototype

String DOT_GIT_EXT

To view the source code for org.eclipse.jgit.lib Constants DOT_GIT_EXT.

Click Source Link

Document

A bare repository typically ends with this string

Usage

From source file:com.gitblit.utils.JGitUtils.java

License:Apache License

/**
 * Clone or Fetch a repository. If the local repository does not exist,
 * clone is called. If the repository does exist, fetch is called. By
 * default the clone/fetch retrieves the remote heads, tags, and notes.
 *
 * @param repositoriesFolder/*from  w w  w .j  a  v  a 2  s.  co  m*/
 * @param name
 * @param fromUrl
 * @param bare
 * @param credentialsProvider
 * @return CloneResult
 * @throws Exception
 */
public static CloneResult cloneRepository(File repositoriesFolder, String name, String fromUrl, boolean bare,
        CredentialsProvider credentialsProvider) throws Exception {
    CloneResult result = new CloneResult();
    if (bare) {
        // bare repository, ensure .git suffix
        if (!name.toLowerCase().endsWith(Constants.DOT_GIT_EXT)) {
            name += Constants.DOT_GIT_EXT;
        }
    } else {
        // normal repository, strip .git suffix
        if (name.toLowerCase().endsWith(Constants.DOT_GIT_EXT)) {
            name = name.substring(0, name.indexOf(Constants.DOT_GIT_EXT));
        }
    }
    result.name = name;

    File folder = new File(repositoriesFolder, name);
    if (folder.exists()) {
        File gitDir = FileKey.resolve(new File(repositoriesFolder, name), FS.DETECTED);
        Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
        result.fetchResult = fetchRepository(credentialsProvider, repository);
        repository.close();
    } else {
        CloneCommand clone = new CloneCommand();
        clone.setBare(bare);
        clone.setCloneAllBranches(true);
        clone.setURI(fromUrl);
        clone.setDirectory(folder);
        if (credentialsProvider != null) {
            clone.setCredentialsProvider(credentialsProvider);
        }
        Repository repository = clone.call().getRepository();

        // Now we have to fetch because CloneCommand doesn't fetch
        // refs/notes nor does it allow manual RefSpec.
        result.createdRepository = true;
        result.fetchResult = fetchRepository(credentialsProvider, repository);
        repository.close();
    }
    return result;
}

From source file:com.google.gerrit.httpd.UrlModule.java

License:Apache License

private Key<HttpServlet> queryProjectNew() {
    return key(new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override//from w ww .ja v a  2 s  .c  om
        protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
            String name = req.getPathInfo();
            if (Strings.isNullOrEmpty(name)) {
                toGerrit(PageLinks.ADMIN_PROJECTS, req, rsp);
                return;
            }

            while (name.endsWith("/")) {
                name = name.substring(0, name.length() - 1);
            }
            if (name.endsWith(Constants.DOT_GIT_EXT)) {
                name = name.substring(0, //
                        name.length() - Constants.DOT_GIT_EXT.length());
            }
            while (name.endsWith("/")) {
                name = name.substring(0, name.length() - 1);
            }
            Project.NameKey project = new Project.NameKey(name);
            toGerrit(PageLinks.toChangeQuery(PageLinks.projectQuery(project, Change.Status.NEW)), req, rsp);
        }
    });
}

From source file:com.google.gerrit.server.git.LocalDiskRepositoryManager.java

License:Apache License

private Repository openRepository(Path path, Project.NameKey name) throws RepositoryNotFoundException {
    if (isUnreasonableName(name)) {
        throw new RepositoryNotFoundException("Invalid name: " + name);
    }//from w  w  w .  j av  a  2 s.  c  o m
    File gitDir = path.resolve(name.get()).toFile();
    if (!names.contains(name)) {
        // The this.names list does not hold the project-name but it can still exist
        // on disk; for instance when the project has been created directly on the
        // file-system through replication.
        //
        if (!name.get().endsWith(Constants.DOT_GIT_EXT)) {
            if (FileKey.resolve(gitDir, FS.DETECTED) != null) {
                onCreateProject(name);
            } else {
                throw new RepositoryNotFoundException(gitDir);
            }
        } else {
            final File directory = gitDir;
            if (FileKey.isGitRepository(new File(directory, Constants.DOT_GIT), FS.DETECTED)) {
                onCreateProject(name);
            } else if (FileKey.isGitRepository(
                    new File(directory.getParentFile(), directory.getName() + Constants.DOT_GIT_EXT),
                    FS.DETECTED)) {
                onCreateProject(name);
            } else {
                throw new RepositoryNotFoundException(gitDir);
            }
        }
    }
    final FileKey loc = FileKey.lenient(gitDir, FS.DETECTED);
    try {
        return RepositoryCache.open(loc);
    } catch (IOException e1) {
        final RepositoryNotFoundException e2;
        e2 = new RepositoryNotFoundException("Cannot open repository " + name);
        e2.initCause(e1);
        throw e2;
    }
}

From source file:com.google.gerrit.server.git.LocalDiskRepositoryManager.java

License:Apache License

private Repository createRepository(Path path, Project.NameKey name)
        throws RepositoryNotFoundException, RepositoryCaseMismatchException {
    if (isUnreasonableName(name)) {
        throw new RepositoryNotFoundException("Invalid name: " + name);
    }/*from  www.  j a va2 s  . c  o m*/

    File dir = FileKey.resolve(path.resolve(name.get()).toFile(), FS.DETECTED);
    FileKey loc;
    if (dir != null) {
        // Already exists on disk, use the repository we found.
        //
        loc = FileKey.exact(dir, FS.DETECTED);

        if (!names.contains(name)) {
            throw new RepositoryCaseMismatchException(name);
        }
    } else {
        // It doesn't exist under any of the standard permutations
        // of the repository name, so prefer the standard bare name.
        //
        String n = name.get() + Constants.DOT_GIT_EXT;
        loc = FileKey.exact(path.resolve(n).toFile(), FS.DETECTED);
    }

    try {
        Repository db = RepositoryCache.open(loc, false);
        db.create(true /* bare */);

        StoredConfig config = db.getConfig();
        config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
                ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES, true);
        config.save();

        // JGit only writes to the reflog for refs/meta/config if the log file
        // already exists.
        //
        File metaConfigLog = new File(db.getDirectory(), "logs/" + RefNames.REFS_CONFIG);
        if (!metaConfigLog.getParentFile().mkdirs() || !metaConfigLog.createNewFile()) {
            log.error(String.format("Failed to create ref log for %s in repository %s", RefNames.REFS_CONFIG,
                    name));
        }

        onCreateProject(name);

        return db;
    } catch (IOException e1) {
        final RepositoryNotFoundException e2;
        e2 = new RepositoryNotFoundException("Cannot create repository " + name);
        e2.initCause(e1);
        throw e2;
    }
}

From source file:com.google.gerrit.server.git.LocalDiskRepositoryManagerTest.java

License:Apache License

private void createRepository(Path directory, String projectName) throws IOException {
    String n = projectName + Constants.DOT_GIT_EXT;
    FileKey loc = FileKey.exact(directory.resolve(n).toFile(), FS.DETECTED);
    try (Repository db = RepositoryCache.open(loc, false)) {
        db.create(true /* bare */);
    }//from  w  ww .  j  a  v  a  2 s. com
}

From source file:com.google.gerrit.server.git.MultiBaseLocalDiskRepositoryManagerTest.java

License:Apache License

private void createRepository(Path directory, Project.NameKey projectName) throws IOException {
    String n = projectName.get() + Constants.DOT_GIT_EXT;
    FileKey loc = FileKey.exact(directory.resolve(n).toFile(), FS.DETECTED);
    try (Repository db = RepositoryCache.open(loc, false)) {
        db.create(true /* bare */);
    }/*from   www  .ja  va2  s  . co  m*/
}

From source file:com.google.gerrit.server.project.ProjectsCollection.java

License:Apache License

private ProjectResource _parse(String id) throws IOException {
    if (id.endsWith(Constants.DOT_GIT_EXT)) {
        id = id.substring(0, id.length() - Constants.DOT_GIT_EXT.length());
    }/*  w  w  w  .  ja  v a  2  s .c o m*/
    ProjectControl ctl;
    try {
        ctl = controlFactory.controlFor(new Project.NameKey(id), user.get());
    } catch (NoSuchProjectException e) {
        return null;
    }
    if (!ctl.isVisible() && !ctl.isOwner()) {
        return null;
    }
    return new ProjectResource(ctl);
}

From source file:com.google.gerrit.server.schema.Schema_55.java

License:Apache License

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException {
    SystemConfig sc = db.systemConfig().get(new SystemConfig.Key());
    String oldName = sc.wildProjectName.get();
    String newName = "All-Projects";
    if ("-- All Projects --".equals(oldName)) {
        ui.message("Renaming \"" + oldName + "\" to \"" + newName + "\"");

        File base = mgr.getBasePath();
        File oldDir = FileKey.resolve(new File(base, oldName), FS.DETECTED);
        File newDir = new File(base, newName + Constants.DOT_GIT_EXT);
        if (!oldDir.renameTo(newDir)) {
            throw new OrmException(
                    "Cannot rename " + oldDir.getAbsolutePath() + " to " + newDir.getAbsolutePath());
        }/*from   ww  w.  j av a2 s  . c  o m*/

        sc.wildProjectName = new Project.NameKey(newName);
        db.systemConfig().update(Collections.singleton(sc));
    }
}

From source file:com.google.gerrit.server.util.SubmoduleSectionParser.java

License:Apache License

private SubmoduleSubscription parse(final String id) {
    final String url = bbc.getString("submodule", id, "url");
    final String path = bbc.getString("submodule", id, "path");
    String branch = bbc.getString("submodule", id, "branch");
    SubmoduleSubscription ss = null;/* w  ww  .j  a  va  2s .  c o m*/

    try {
        if (url != null && url.length() > 0 && path != null && path.length() > 0 && branch != null
                && branch.length() > 0) {
            // All required fields filled.

            boolean urlIsRelative = url.startsWith("../");
            String server = null;
            if (!urlIsRelative) {
                // It is actually an URI. It could be ssh://localhost/project-a.
                server = new URI(url).getHost();
            }
            if ((urlIsRelative) || (server != null && server.equalsIgnoreCase(thisServer))) {
                // Subscription really related to this running server.
                if (branch.equals(".")) {
                    branch = superProjectBranch.get();
                }

                final String urlExtractedPath = new URI(url).getPath();
                String projectName;
                int fromIndex = urlExtractedPath.length() - 1;
                while (fromIndex > 0) {
                    fromIndex = urlExtractedPath.lastIndexOf('/', fromIndex - 1);
                    projectName = urlExtractedPath.substring(fromIndex + 1);

                    if (projectName.endsWith(Constants.DOT_GIT_EXT)) {
                        projectName = projectName.substring(0, //
                                projectName.length() - Constants.DOT_GIT_EXT.length());
                    }
                    Project.NameKey projectKey = new Project.NameKey(projectName);
                    if (projectCache.get(projectKey) != null) {
                        ss = new SubmoduleSubscription(superProjectBranch,
                                new Branch.NameKey(new Project.NameKey(projectName), branch), path);
                    }
                }
            }
        }
    } catch (URISyntaxException e) {
        // Error in url syntax (in fact it is uri syntax)
    }

    return ss;
}

From source file:com.google.gerrit.server.util.SubmoduleSectionParserTest.java

License:Apache License

private void execute(final Branch.NameKey superProjectBranch,
        final Map<String, SubmoduleSection> sectionsToReturn, final Map<String, String> reposToBeFound,
        final Set<SubmoduleSubscription> expectedSubscriptions) throws Exception {
    expect(bbc.getSubsections("submodule")).andReturn(sectionsToReturn.keySet());

    for (Map.Entry<String, SubmoduleSection> entry : sectionsToReturn.entrySet()) {
        String id = entry.getKey();
        final SubmoduleSection section = entry.getValue();
        expect(bbc.getString("submodule", id, "url")).andReturn(section.getUrl());
        expect(bbc.getString("submodule", id, "path")).andReturn(section.getPath());
        expect(bbc.getString("submodule", id, "branch")).andReturn(section.getBranch());

        if (THIS_SERVER.equals(new URI(section.getUrl()).getHost())) {
            String projectNameCandidate;
            final String urlExtractedPath = new URI(section.getUrl()).getPath();
            int fromIndex = urlExtractedPath.length() - 1;
            while (fromIndex > 0) {
                fromIndex = urlExtractedPath.lastIndexOf('/', fromIndex - 1);
                projectNameCandidate = urlExtractedPath.substring(fromIndex + 1);
                if (projectNameCandidate.endsWith(Constants.DOT_GIT_EXT)) {
                    projectNameCandidate = projectNameCandidate.substring(0, //
                            projectNameCandidate.length() - Constants.DOT_GIT_EXT.length());
                }//  w  w w  .  j a v a  2s. co  m
                if (reposToBeFound.containsValue(projectNameCandidate)) {
                    expect(projectCache.get(new Project.NameKey(projectNameCandidate)))
                            .andReturn(createNiceMock(ProjectState.class));
                } else {
                    expect(projectCache.get(new Project.NameKey(projectNameCandidate))).andReturn(null);
                }
            }
        }
    }

    doReplay();

    final SubmoduleSectionParser ssp = new SubmoduleSectionParser(projectCache, bbc, THIS_SERVER,
            superProjectBranch);

    Set<SubmoduleSubscription> returnedSubscriptions = ssp.parseAllSections();

    doVerify();

    assertEquals(expectedSubscriptions, returnedSubscriptions);
}