Search in sources :

Example 11 with CloneCommand

use of org.eclipse.jgit.api.CloneCommand in project che by eclipse.

the class JGitConnection method clone.

public void clone(CloneParams params) throws GitException, UnauthorizedException {
    String remoteUri = params.getRemoteUrl();
    boolean removeIfFailed = false;
    try {
        if (params.getRemoteName() == null) {
            params.setRemoteName(Constants.DEFAULT_REMOTE_NAME);
        }
        if (params.getWorkingDir() == null) {
            params.setWorkingDir(repository.getWorkTree().getCanonicalPath());
        }
        // If clone fails and the .git folder didn't exist we want to remove it.
        // We have to do this here because the clone command doesn't revert its own changes in case of failure.
        removeIfFailed = !repository.getDirectory().exists();
        CloneCommand cloneCommand = Git.cloneRepository().setDirectory(new File(params.getWorkingDir())).setRemote(params.getRemoteName()).setCloneSubmodules(params.isRecursive()).setURI(remoteUri);
        if (params.getBranchesToFetch().isEmpty()) {
            cloneCommand.setCloneAllBranches(true);
        } else {
            cloneCommand.setBranchesToClone(params.getBranchesToFetch());
        }
        LineConsumer lineConsumer = lineConsumerFactory.newLineConsumer();
        cloneCommand.setProgressMonitor(new BatchingProgressMonitor() {

            @Override
            protected void onUpdate(String taskName, int workCurr) {
                try {
                    lineConsumer.writeLine(taskName + ": " + workCurr + " completed");
                } catch (IOException exception) {
                    LOG.error(exception.getMessage(), exception);
                }
            }

            @Override
            protected void onEndTask(String taskName, int workCurr) {
            }

            @Override
            protected void onUpdate(String taskName, int workCurr, int workTotal, int percentDone) {
                try {
                    lineConsumer.writeLine(taskName + ": " + workCurr + " of " + workTotal + " completed, " + percentDone + "% done");
                } catch (IOException exception) {
                    LOG.error(exception.getMessage(), exception);
                }
            }

            @Override
            protected void onEndTask(String taskName, int workCurr, int workTotal, int percentDone) {
            }
        });
        ((Git) executeRemoteCommand(remoteUri, cloneCommand, params.getUsername(), params.getPassword())).close();
        StoredConfig repositoryConfig = getRepository().getConfig();
        GitUser gitUser = getUser();
        if (gitUser != null) {
            repositoryConfig.setString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_NAME, gitUser.getName());
            repositoryConfig.setString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_EMAIL, gitUser.getEmail());
        }
        repositoryConfig.save();
    } catch (IOException | GitAPIException exception) {
        // Delete .git directory in case it was created
        if (removeIfFailed) {
            deleteRepositoryFolder();
        }
        //try to clone repository by replacing http to https in the url if HTTP 301 redirect happened
        if (exception.getMessage().contains(": 301 Moved Permanently")) {
            remoteUri = "https" + remoteUri.substring(4);
            try {
                clone(params.withRemoteUrl(remoteUri));
            } catch (UnauthorizedException | GitException e) {
                throw new GitException("Failed to clone the repository", e);
            }
            return;
        }
        String message = generateExceptionMessage(exception);
        throw new GitException(message, exception);
    }
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) GitException(org.eclipse.che.api.git.exception.GitException) IOException(java.io.IOException) GitUser(org.eclipse.che.api.git.shared.GitUser) StoredConfig(org.eclipse.jgit.lib.StoredConfig) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) Git(org.eclipse.jgit.api.Git) BatchingProgressMonitor(org.eclipse.jgit.lib.BatchingProgressMonitor) DiffCommitFile(org.eclipse.che.api.git.shared.DiffCommitFile) File(java.io.File)

Example 12 with CloneCommand

use of org.eclipse.jgit.api.CloneCommand in project gitblit by gitblit.

the class GitblitManager method fork.

/*
	 * IGITBLIT
	 */
/**
	 * Creates a personal fork of the specified repository. The clone is view
	 * restricted by default and the owner of the source repository is given
	 * access to the clone.
	 *
	 * @param repository
	 * @param user
	 * @return the repository model of the fork, if successful
	 * @throws GitBlitException
	 */
@Override
public RepositoryModel fork(RepositoryModel repository, UserModel user) throws GitBlitException {
    String cloneName = MessageFormat.format("{0}/{1}.git", user.getPersonalPath(), StringUtils.stripDotGit(StringUtils.getLastPathElement(repository.name)));
    String fromUrl = MessageFormat.format("file://{0}/{1}", repositoryManager.getRepositoriesFolder().getAbsolutePath(), repository.name);
    // clone the repository
    try {
        Repository canonical = getRepository(repository.name);
        File folder = new File(repositoryManager.getRepositoriesFolder(), cloneName);
        CloneCommand clone = new CloneCommand();
        clone.setBare(true);
        // fetch branches with exclusions
        Collection<Ref> branches = canonical.getRefDatabase().getRefs(Constants.R_HEADS).values();
        List<String> branchesToClone = new ArrayList<String>();
        for (Ref branch : branches) {
            String name = branch.getName();
            if (name.startsWith(Constants.R_TICKET)) {
                // exclude ticket branches
                continue;
            }
            branchesToClone.add(name);
        }
        clone.setBranchesToClone(branchesToClone);
        clone.setURI(fromUrl);
        clone.setDirectory(folder);
        Git git = clone.call();
        // fetch tags
        FetchCommand fetch = git.fetch();
        fetch.setRefSpecs(new RefSpec("+refs/tags/*:refs/tags/*"));
        fetch.call();
        git.getRepository().close();
    } catch (Exception e) {
        throw new GitBlitException(e);
    }
    // create a Gitblit repository model for the clone
    RepositoryModel cloneModel = repository.cloneAs(cloneName);
    // owner has REWIND/RW+ permissions
    cloneModel.addOwner(user.username);
    // is not lower than the source repository  (issue-495/ticket-167)
    if (repository.accessRestriction.exceeds(cloneModel.accessRestriction)) {
        cloneModel.accessRestriction = repository.accessRestriction;
    }
    repositoryManager.updateRepositoryModel(cloneName, cloneModel, false);
    // add the owner of the source repository to the clone's access list
    if (!ArrayUtils.isEmpty(repository.owners)) {
        for (String owner : repository.owners) {
            UserModel originOwner = userManager.getUserModel(owner);
            if (originOwner != null && !originOwner.canClone(cloneModel)) {
                // origin owner can't yet clone fork, grant explicit clone access
                originOwner.setRepositoryPermission(cloneName, AccessPermission.CLONE);
                reviseUser(originOwner.username, originOwner);
            }
        }
    }
    // grant origin's user list clone permission to fork
    List<String> users = repositoryManager.getRepositoryUsers(repository);
    List<UserModel> cloneUsers = new ArrayList<UserModel>();
    for (String name : users) {
        if (!name.equalsIgnoreCase(user.username)) {
            UserModel cloneUser = userManager.getUserModel(name);
            if (cloneUser.canClone(repository) && !cloneUser.canClone(cloneModel)) {
                // origin user can't yet clone fork, grant explicit clone access
                cloneUser.setRepositoryPermission(cloneName, AccessPermission.CLONE);
            }
            cloneUsers.add(cloneUser);
        }
    }
    userManager.updateUserModels(cloneUsers);
    // grant origin's team list clone permission to fork
    List<String> teams = repositoryManager.getRepositoryTeams(repository);
    List<TeamModel> cloneTeams = new ArrayList<TeamModel>();
    for (String name : teams) {
        TeamModel cloneTeam = userManager.getTeamModel(name);
        if (cloneTeam.canClone(repository) && !cloneTeam.canClone(cloneModel)) {
            // origin team can't yet clone fork, grant explicit clone access
            cloneTeam.setRepositoryPermission(cloneName, AccessPermission.CLONE);
        }
        cloneTeams.add(cloneTeam);
    }
    userManager.updateTeamModels(cloneTeams);
    // add this clone to the cached model
    repositoryManager.addToCachedRepositoryList(cloneModel);
    if (pluginManager != null) {
        for (RepositoryLifeCycleListener listener : pluginManager.getExtensions(RepositoryLifeCycleListener.class)) {
            try {
                listener.onFork(repository, cloneModel);
            } catch (Throwable t) {
                logger.error(String.format("failed to call plugin onFork %s", repository.name), t);
            }
        }
    }
    return cloneModel;
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) ArrayList(java.util.ArrayList) GitBlitException(com.gitblit.GitBlitException) RepositoryModel(com.gitblit.models.RepositoryModel) JsonIOException(com.google.gson.JsonIOException) GitBlitException(com.gitblit.GitBlitException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) UserModel(com.gitblit.models.UserModel) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) RefSpec(org.eclipse.jgit.transport.RefSpec) TeamModel(com.gitblit.models.TeamModel) FetchCommand(org.eclipse.jgit.api.FetchCommand) RepositoryLifeCycleListener(com.gitblit.extensions.RepositoryLifeCycleListener) File(java.io.File)

Example 13 with CloneCommand

use of org.eclipse.jgit.api.CloneCommand in project gitblit by gitblit.

the class GitDaemonTest method testCloneRestrictedRepo.

@Test
public void testCloneRestrictedRepo() throws Exception {
    GitBlitSuite.close(ticgit2Folder);
    if (ticgit2Folder.exists()) {
        FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);
    }
    // restrict repository access
    RepositoryModel model = repositories().getRepositoryModel("ticgit.git");
    model.accessRestriction = AccessRestrictionType.CLONE;
    model.authorizationControl = AuthorizationControl.NAMED;
    repositories().updateRepositoryModel(model.name, model, false);
    // delete any existing working folder
    boolean cloned = false;
    try {
        CloneCommand clone = Git.cloneRepository();
        clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
        clone.setDirectory(ticgit2Folder);
        clone.setBare(false);
        clone.setCloneAllBranches(true);
        GitBlitSuite.close(clone.call());
        cloned = true;
    } catch (Exception e) {
    // swallow the exception which we expect
    }
    assertFalse("Anonymous was able to clone the repository?!", cloned);
    FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);
    // restore anonymous repository access
    model.accessRestriction = AccessRestrictionType.NONE;
    model.authorizationControl = AuthorizationControl.NAMED;
    repositories().updateRepositoryModel(model.name, model, false);
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) RepositoryModel(com.gitblit.models.RepositoryModel) Test(org.junit.Test)

Example 14 with CloneCommand

use of org.eclipse.jgit.api.CloneCommand in project gitblit by gitblit.

the class GitDaemonTest method testPushRestrictedRepo.

@Test
public void testPushRestrictedRepo() throws Exception {
    GitBlitSuite.close(ticgitFolder);
    if (ticgitFolder.exists()) {
        FileUtils.delete(ticgitFolder, FileUtils.RECURSIVE | FileUtils.RETRY);
    }
    // restore anonymous repository access
    RepositoryModel model = repositories().getRepositoryModel("ticgit.git");
    model.accessRestriction = AccessRestrictionType.PUSH;
    model.authorizationControl = AuthorizationControl.NAMED;
    repositories().updateRepositoryModel(model.name, model, false);
    CloneCommand clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
    clone.setDirectory(ticgitFolder);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    GitBlitSuite.close(clone.call());
    assertTrue(true);
    Git git = Git.open(ticgitFolder);
    File file = new File(ticgitFolder, "TODO");
    OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
    BufferedWriter w = new BufferedWriter(os);
    w.write("// hellol中文 " + new Date().toString() + "\n");
    w.close();
    git.add().addFilepattern(file.getName()).call();
    git.commit().setMessage("test commit").call();
    Iterable<PushResult> results = git.push().setPushAll().call();
    GitBlitSuite.close(git);
    for (PushResult result : results) {
        for (RemoteRefUpdate update : result.getRemoteUpdates()) {
            assertEquals(Status.REJECTED_OTHER_REASON, update.getStatus());
        }
    }
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) Git(org.eclipse.jgit.api.Git) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) RepositoryModel(com.gitblit.models.RepositoryModel) PushResult(org.eclipse.jgit.transport.PushResult) File(java.io.File) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 15 with CloneCommand

use of org.eclipse.jgit.api.CloneCommand in project gitblit by gitblit.

the class GitDaemonTest method testAnonymousClone.

@Test
public void testAnonymousClone() throws Exception {
    GitBlitSuite.close(ticgitFolder);
    if (ticgitFolder.exists()) {
        FileUtils.delete(ticgitFolder, FileUtils.RECURSIVE | FileUtils.RETRY);
    }
    // set push restriction
    RepositoryModel model = repositories().getRepositoryModel("ticgit.git");
    model.accessRestriction = AccessRestrictionType.PUSH;
    model.authorizationControl = AuthorizationControl.NAMED;
    repositories().updateRepositoryModel(model.name, model, false);
    CloneCommand clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
    clone.setDirectory(ticgitFolder);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    GitBlitSuite.close(clone.call());
    assertTrue(true);
    // restore anonymous repository access
    model.accessRestriction = AccessRestrictionType.NONE;
    model.authorizationControl = AuthorizationControl.NAMED;
    repositories().updateRepositoryModel(model.name, model, false);
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) RepositoryModel(com.gitblit.models.RepositoryModel) Test(org.junit.Test)

Aggregations

CloneCommand (org.eclipse.jgit.api.CloneCommand)25 File (java.io.File)18 Git (org.eclipse.jgit.api.Git)18 RepositoryModel (com.gitblit.models.RepositoryModel)16 Test (org.junit.Test)15 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)14 PushResult (org.eclipse.jgit.transport.PushResult)13 RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)13 BufferedWriter (java.io.BufferedWriter)11 FileOutputStream (java.io.FileOutputStream)11 OutputStreamWriter (java.io.OutputStreamWriter)11 Date (java.util.Date)11 UserModel (com.gitblit.models.UserModel)6 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)6 IOException (java.io.IOException)5 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)4 Status (org.eclipse.jgit.transport.RemoteRefUpdate.Status)4 Ref (org.eclipse.jgit.lib.Ref)2 Repository (org.eclipse.jgit.lib.Repository)2