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

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

Introduction

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

Prototype

@NonNull
public File getWorkTree() throws NoWorkTreeException 

Source Link

Document

Get the root directory of the working tree, where files are checked out for viewing and editing.

Usage

From source file:org.eclipse.orion.server.git.servlets.GitCloneHandlerV1.java

License:Open Source License

private boolean handleDelete(HttpServletRequest request, HttpServletResponse response, String pathString)
        throws IOException, JSONException, ServletException, URISyntaxException, CoreException,
        JGitInternalException, GitAPIException {
    IPath path = pathString == null ? Path.EMPTY : new Path(pathString);
    if (path.segment(0).equals("file") && path.segmentCount() > 1) { //$NON-NLS-1$

        // make sure a clone is addressed
        WebProject webProject = WebProject.fromId(path.segment(1));
        if (isAccessAllowed(request.getRemoteUser(), webProject)) {
            URI contentLocation = URI.create(webProject.getId());
            IPath projectPath = new Path(contentLocation.getPath()).append(path.removeFirstSegments(2));
            File gitDir = GitUtils.getGitDirs(new Path("file").append(projectPath), Traverse.CURRENT).values()
                    .iterator().next();//from w  ww .j  av  a2  s  .  c  o m

            Repository repo = new FileRepository(gitDir);
            repo.close();
            FileUtils.delete(repo.getWorkTree(), FileUtils.RECURSIVE | FileUtils.RETRY);
            if (path.segmentCount() == 2)
                return statusHandler.handleRequest(request, response,
                        removeProject(request.getRemoteUser(), webProject));
            return true;
        } else {
            String msg = NLS.bind("Nothing found for the given ID: {0}", path);
            return statusHandler.handleRequest(request, response,
                    new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null));
        }
    }
    String msg = NLS.bind("Invalid delete request {0}", pathString);
    return statusHandler.handleRequest(request, response,
            new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
}

From source file:org.eclipse.orion.server.git.servlets.GitSubmoduleHandlerV1.java

License:Open Source License

@Override
protected boolean handleDelete(RequestInfo requestInfo) throws ServletException {
    HttpServletRequest request = requestInfo.request;
    HttpServletResponse response = requestInfo.response;
    Repository db = requestInfo.db;
    Repository parentRepo = null;/* w w w. j  av  a  2s.c  om*/
    try {
        Map<IPath, File> parents = GitUtils.getGitDirs(requestInfo.filePath.removeLastSegments(1),
                Traverse.GO_UP);
        if (parents.size() < 1)
            return false;
        parentRepo = FileRepositoryBuilder.create(parents.entrySet().iterator().next().getValue());
        String pathToSubmodule = db.getWorkTree().toString()
                .substring(parentRepo.getWorkTree().toString().length() + 1);
        removeSubmodule(db, parentRepo, pathToSubmodule);
        return true;
    } catch (Exception ex) {
        String msg = "An error occured for delete submodule command.";
        return statusHandler.handleRequest(request, response,
                new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, ex));
    } finally {
        if (parentRepo != null) {
            parentRepo.close();
        }
    }

}

From source file:org.eclipse.orion.server.git.servlets.GitSubmoduleHandlerV1.java

License:Open Source License

public static void removeSubmodule(Repository db, Repository parentRepo, String pathToSubmodule)
        throws Exception {
    pathToSubmodule = pathToSubmodule.replace("\\", "/");
    StoredConfig gitSubmodulesConfig = getGitSubmodulesConfig(parentRepo);
    gitSubmodulesConfig.unsetSection(CONFIG_SUBMODULE_SECTION, pathToSubmodule);
    gitSubmodulesConfig.save();/*www  .  j av  a  2  s.c  om*/
    StoredConfig repositoryConfig = parentRepo.getConfig();
    repositoryConfig.unsetSection(CONFIG_SUBMODULE_SECTION, pathToSubmodule);
    repositoryConfig.save();
    Git git = Git.wrap(parentRepo);
    git.add().addFilepattern(DOT_GIT_MODULES).call();
    RmCommand rm = git.rm().addFilepattern(pathToSubmodule);
    if (gitSubmodulesConfig.getSections().size() == 0) {
        rm.addFilepattern(DOT_GIT_MODULES);
    }
    rm.call();
    FileUtils.delete(db.getWorkTree(), FileUtils.RECURSIVE);
    FileUtils.delete(db.getDirectory(), FileUtils.RECURSIVE);
}

From source file:org.eclipse.orion.server.git.servlets.GitSubmoduleHandlerV1.java

License:Open Source License

private static StoredConfig getGitSubmodulesConfig(Repository repository)
        throws IOException, ConfigInvalidException {
    File gitSubmodulesFile = new File(repository.getWorkTree(), DOT_GIT_MODULES);
    FileBasedConfig gitSubmodulesConfig = new FileBasedConfig(null, gitSubmodulesFile, FS.DETECTED);
    gitSubmodulesConfig.load();//from  w  w  w . ja  v a 2 s .c om
    return gitSubmodulesConfig;
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitSubmoduleTest.java

License:Open Source License

@Test
public void testAddSubmodule() throws Exception {
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    String workspaceId = getWorkspaceId(workspaceLocation);

    JSONObject project = createProjectOrLink(workspaceLocation, getMethodName().concat("Project1"), null);
    JSONObject clone = clone(workspaceId, project);
    String contentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
    String submoduleLocation = clone.getString(GitConstants.KEY_SUBMODULE);
    String location = clone.getString(ProtocolConstants.KEY_LOCATION);

    Repository repository = getRepositoryForContentLocation(contentLocation);
    File file = new File(repository.getWorkTree(), DOT_GIT_MODULES);
    assertFalse(file.exists());/*from   w ww  .j  a  va  2s. com*/

    URIish uri = new URIish(gitDir.toURI().toURL());
    WebRequest request = postSubmoduleRequest(submoduleLocation, "test", uri.toString(), location);
    WebResponse response = webConversation.getResponse(request);

    file = new File(repository.getWorkTree(), DOT_GIT_MODULES);
    assertTrue(file.exists());

    assertNotNull(repository);
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitSubmoduleTest.java

License:Open Source License

@Test
public void testSyncSubmodule()
        throws IOException, SAXException, JSONException, CoreException, ConfigInvalidException {
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    String workspaceId = getWorkspaceId(workspaceLocation);

    JSONObject project = createProjectOrLink(workspaceLocation, getMethodName().concat("Project1"), null);
    JSONObject clone = clone(workspaceId, project);
    String contentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
    String submoduleLocation = clone.getString(GitConstants.KEY_SUBMODULE);
    String location = clone.getString(ProtocolConstants.KEY_LOCATION);

    Repository repository = getRepositoryForContentLocation(contentLocation);
    File file = new File(repository.getWorkTree(), DOT_GIT_MODULES);
    assertFalse(file.exists());/*from w  ww  .  j  av  a 2  s .co m*/

    URIish uri = new URIish(gitDir.toURI().toURL());
    WebRequest request = postSubmoduleRequest(submoduleLocation, "test", uri.toString(), location);
    WebResponse response = webConversation.getResponse(request);

    file = new File(repository.getWorkTree(), DOT_GIT_MODULES);
    assertTrue(file.exists());

    assertNotNull(repository);

    StoredConfig repoConfig = repository.getConfig();
    String originalUrl = repoConfig.getString("submodule", "test", "url");
    repoConfig.setString("submodule", "test", "url", "value");
    repoConfig.save();
    assertEquals(repoConfig.getString("submodule", "test", "url"), "value");

    WebRequest reqSync = putSubmoduleRequest(submoduleLocation, "sync");
    WebResponse resSync = webConversation.getResponse(reqSync);

    repoConfig = repository.getConfig();
    assertEquals(repoConfig.getString("submodule", "test", "url"), originalUrl);

}

From source file:org.eclipse.orion.server.tests.servlets.git.GitSubmoduleTest.java

License:Open Source License

@Test
public void testUpdateSubmodule() throws IOException, SAXException, JSONException, CoreException {
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    String workspaceId = getWorkspaceId(workspaceLocation);

    JSONObject project = createProjectOrLink(workspaceLocation, getMethodName().concat("Project1"), null);
    JSONObject clone = clone(workspaceId, project);
    String contentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
    String submoduleLocation = clone.getString(GitConstants.KEY_SUBMODULE);
    String location = clone.getString(ProtocolConstants.KEY_LOCATION);

    Repository repository = getRepositoryForContentLocation(contentLocation);
    File file = new File(repository.getWorkTree(), DOT_GIT_MODULES);
    assertFalse(file.exists());//from   ww  w.  j a  va  2 s  . co m

    URIish uri = new URIish(gitDir.toURI().toURL());
    WebRequest request = postSubmoduleRequest(submoduleLocation, "test", uri.toString(), location);
    WebResponse response = webConversation.getResponse(request);

    file = new File(repository.getWorkTree(), DOT_GIT_MODULES);
    assertTrue(file.exists());

    assertNotNull(repository);

    file = new File(repository.getWorkTree(), "test");
    assertTrue(file.exists() && file.isDirectory());

    FileUtils.delete(file, FileUtils.RECURSIVE);

    WebRequest reqUpdate = putSubmoduleRequest(submoduleLocation, "update");
    WebResponse resUpdate = webConversation.getResponse(reqUpdate);

    file = new File(repository.getWorkTree(), "test");
    assertFalse(file.exists() && file.isDirectory());
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitSubmoduleTest.java

License:Open Source License

@Test
public void testRemoveSubmodule() throws IOException, SAXException, JSONException, CoreException {
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    String workspaceId = getWorkspaceId(workspaceLocation);

    JSONObject project = createProjectOrLink(workspaceLocation, getMethodName().concat("Project1"), null);
    JSONObject clone = clone(workspaceId, project);
    String contentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
    String submoduleLocation = clone.getString(GitConstants.KEY_SUBMODULE);
    String location = clone.getString(ProtocolConstants.KEY_LOCATION);

    Repository repository = getRepositoryForContentLocation(contentLocation);
    File file = new File(repository.getWorkTree(), DOT_GIT_MODULES);
    assertFalse(file.exists());//from  w  ww .ja v a  2s  .  co  m

    URIish uri = new URIish(gitDir.toURI().toURL());
    WebRequest request = postSubmoduleRequest(submoduleLocation, "test", uri.toString(), location);
    WebResponse response = webConversation.getResponse(request);
    request = postSubmoduleRequest(submoduleLocation, "test2", uri.toString(), location);
    response = webConversation.getResponse(request);

    assertNotNull(repository);

    file = new File(repository.getWorkTree(), DOT_GIT_MODULES);
    assertTrue(file.exists());

    file = new File(repository.getWorkTree(), "test");
    assertTrue(file.exists() && file.isDirectory());
    file = new File(repository.getWorkTree(), "test2");
    assertTrue(file.exists() && file.isDirectory());

    WebRequest reqDelete = deleteSubmoduleRequest(submoduleLocation + "test/");
    WebResponse resDelete = webConversation.getResponse(reqDelete);

    file = new File(repository.getWorkTree(), DOT_GIT_MODULES);
    assertTrue(file.exists());
    file = new File(repository.getWorkTree(), "test");
    assertFalse(file.exists() && file.isDirectory());

    reqDelete = deleteSubmoduleRequest(submoduleLocation + "test2/");
    resDelete = webConversation.getResponse(reqDelete);

    file = new File(repository.getWorkTree(), DOT_GIT_MODULES);
    assertFalse(file.exists());
    file = new File(repository.getWorkTree(), "test2");
    assertFalse(file.exists() && file.isDirectory());
}

From source file:org.eclipse.tycho.extras.buildtimestamp.jgit.JGitBuildTimestampProvider.java

License:Open Source License

private String getRelPath(Repository repository, MavenProject project) throws IOException {
    String workTree = repository.getWorkTree().getAbsolutePath();

    String path = project.getBasedir().getAbsolutePath();

    if (!path.startsWith(workTree)) {
        throw new IOException(project + " is not in git repository working tree " + repository.getWorkTree());
    }/*from  w w  w  . ja  v  a2 s  .  c  o  m*/

    path = path.substring(workTree.length());

    if (path.startsWith("/")) {
        path = path.substring(1);
    }

    return path;
}

From source file:org.eclipse.tycho.extras.sourceref.jgit.JGitSourceReferencesProvider.java

License:Open Source License

public String getSourceReferencesHeader(MavenProject project, ScmUrl scmUrl) throws MojoExecutionException {
    File basedir = project.getBasedir().getAbsoluteFile();
    FileRepositoryBuilder builder = new FileRepositoryBuilder().readEnvironment().findGitDir(basedir)
            .setMustExist(true);/* w w  w. j av a2  s . c o  m*/
    Repository repo;
    Git git;
    try {
        repo = builder.build();
        git = Git.wrap(repo);
    } catch (IOException e) {
        throw new MojoExecutionException("IO exception trying to create git repo ", e);
    }
    ObjectId head = resolveHead(repo);

    StringBuilder result = new StringBuilder(scmUrl.getUrl());
    result.append(";path=\"");
    result.append(getRelativePath(basedir, repo.getWorkTree()));
    result.append("\"");

    String tag = findTagForHead(git, head);
    if (tag != null) {
        // may contain e.g. spaces, so we quote it
        result.append(";tag=\"");
        result.append(tag);
        result.append("\"");
    }
    result.append(";commitId=");
    result.append(head.getName());
    return result.toString();
}