Search in sources :

Example 16 with FetchCommand

use of org.eclipse.jgit.api.FetchCommand in project archi-modelrepository-plugin by archi-contribs.

the class ArchiRepository method fetchFromRemote.

@Override
public FetchResult fetchFromRemote(UsernamePassword npw, ProgressMonitor monitor, boolean isDryrun) throws IOException, GitAPIException {
    try (Git git = Git.open(getLocalRepositoryFolder())) {
        // Check and set tracked master branch
        setTrackedBranch(git.getRepository(), IGraficoConstants.MASTER);
        FetchCommand fetchCommand = git.fetch();
        fetchCommand.setTransportConfigCallback(CredentialsAuthenticator.getTransportConfigCallback(getOnlineRepositoryURL(), npw));
        fetchCommand.setProgressMonitor(monitor);
        fetchCommand.setDryRun(isDryrun);
        return fetchCommand.call();
    }
}
Also used : Git(org.eclipse.jgit.api.Git) FetchCommand(org.eclipse.jgit.api.FetchCommand)

Example 17 with FetchCommand

use of org.eclipse.jgit.api.FetchCommand in project egit by eclipse.

the class FetchOperation method run.

/**
 * @param monitor
 * @throws InvocationTargetException
 */
public void run(IProgressMonitor monitor) throws InvocationTargetException {
    if (operationResult != null)
        throw new IllegalStateException(CoreText.OperationAlreadyExecuted);
    EclipseGitProgressTransformer gitMonitor = new EclipseGitProgressTransformer(monitor);
    try (Git git = new Git(repository)) {
        FetchCommand command;
        if (rc == null)
            command = git.fetch().setRemote(uri.toPrivateString()).setRefSpecs(specs);
        else
            command = git.fetch().setRemote(rc.getName());
        command.setCredentialsProvider(credentialsProvider).setTimeout(timeout).setDryRun(dryRun).setProgressMonitor(gitMonitor);
        if (tagOpt != null)
            command.setTagOpt(tagOpt);
        try {
            operationResult = command.call();
        } catch (JGitInternalException e) {
            throw new InvocationTargetException(e.getCause() != null ? e.getCause() : e);
        } catch (Exception e) {
            throw new InvocationTargetException(e);
        }
    }
}
Also used : Git(org.eclipse.jgit.api.Git) FetchCommand(org.eclipse.jgit.api.FetchCommand) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) EclipseGitProgressTransformer(org.eclipse.egit.core.EclipseGitProgressTransformer) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException)

Example 18 with FetchCommand

use of org.eclipse.jgit.api.FetchCommand 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 19 with FetchCommand

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

the class JGitUtils method fetchRepository.

/**
 * Fetch updates from the remote repository. If refSpecs is unspecifed,
 * remote heads, tags, and notes are retrieved.
 *
 * @param credentialsProvider
 * @param repository
 * @param refSpecs
 * @return FetchResult
 * @throws Exception
 */
public static FetchResult fetchRepository(CredentialsProvider credentialsProvider, Repository repository, RefSpec... refSpecs) throws Exception {
    Git git = new Git(repository);
    FetchCommand fetch = git.fetch();
    List<RefSpec> specs = new ArrayList<RefSpec>();
    if (refSpecs == null || refSpecs.length == 0) {
        specs.add(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
        specs.add(new RefSpec("+refs/tags/*:refs/tags/*"));
        specs.add(new RefSpec("+refs/notes/*:refs/notes/*"));
    } else {
        specs.addAll(Arrays.asList(refSpecs));
    }
    if (credentialsProvider != null) {
        fetch.setCredentialsProvider(credentialsProvider);
    }
    fetch.setRefSpecs(specs);
    FetchResult fetchRes = fetch.call();
    return fetchRes;
}
Also used : Git(org.eclipse.jgit.api.Git) RefSpec(org.eclipse.jgit.transport.RefSpec) FetchResult(org.eclipse.jgit.transport.FetchResult) FetchCommand(org.eclipse.jgit.api.FetchCommand) ArrayList(java.util.ArrayList)

Aggregations

FetchCommand (org.eclipse.jgit.api.FetchCommand)19 Git (org.eclipse.jgit.api.Git)13 FetchResult (org.eclipse.jgit.transport.FetchResult)9 ArrayList (java.util.ArrayList)6 CloneCommand (org.eclipse.jgit.api.CloneCommand)6 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)6 Ref (org.eclipse.jgit.lib.Ref)6 RefSpec (org.eclipse.jgit.transport.RefSpec)6 Test (org.junit.Test)6 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)5 Repository (org.eclipse.jgit.lib.Repository)5 StoredConfig (org.eclipse.jgit.lib.StoredConfig)5 IOException (java.io.IOException)4 ListBranchCommand (org.eclipse.jgit.api.ListBranchCommand)4 MergeCommand (org.eclipse.jgit.api.MergeCommand)4 Status (org.eclipse.jgit.api.Status)4 StatusCommand (org.eclipse.jgit.api.StatusCommand)4 NotMergedException (org.eclipse.jgit.api.errors.NotMergedException)4 ObjectId (org.eclipse.jgit.lib.ObjectId)4 File (java.io.File)3