Example usage for org.eclipse.jgit.transport RefSpec RefSpec

List of usage examples for org.eclipse.jgit.transport RefSpec RefSpec

Introduction

In this page you can find the example usage for org.eclipse.jgit.transport RefSpec RefSpec.

Prototype

private RefSpec(RefSpec p) 

Source Link

Usage

From source file:at.bitandart.zoubek.mervin.gerrit.GerritReviewRepositoryService.java

License:Open Source License

@SuppressWarnings("restriction")
@Override//  w ww .j av a 2 s  .  co m
public List<IReviewDescriptor> getReviews(URI uri) throws InvalidReviewRepositoryException {

    List<IReviewDescriptor> changeIds = new LinkedList<>();

    try {
        // connect to the local git repository
        Git git = Git.open(new File(uri));

        try {
            // Assume that origin refers to the remote gerrit repository
            // list all remote refs from origin
            Collection<Ref> remoteRefs = git.lsRemote().setTimeout(60).call();

            Pattern changeRefPattern = Pattern.compile(CHANGE_REF_PATTERN);

            // search for change refs
            for (Ref ref : remoteRefs) {

                Matcher matcher = changeRefPattern.matcher(ref.getName());
                if (matcher.matches()) {
                    String changePk = matcher.group(CHANGE_REF_PATTERN_GROUP_CHANGE_PK);
                    String changeId = "<unknown>";
                    GerritReviewDescriptor reviewDescriptor;
                    try {
                        reviewDescriptor = new GerritReviewDescriptor(Integer.parseInt(changePk), changeId);
                    } catch (NumberFormatException nfe) {
                        // FIXME ignore it or throw an exception?
                        break;
                    }

                    if (!changeIds.contains(reviewDescriptor)) {
                        changeIds.add(reviewDescriptor);

                        /*
                         * the change id is present in all commit messages,
                         * so we extract it from the commit message of the
                         * current ref
                         */
                        FetchResult fetchResult = git.fetch().setRefSpecs(new RefSpec(ref.getName())).call();

                        Ref localRef = fetchResult.getAdvertisedRef(ref.getName());
                        RevWalk revWalk = new RevWalk(git.getRepository());
                        RevCommit commit = revWalk.parseCommit(localRef.getObjectId());
                        String[] paragraphs = commit.getFullMessage().split("\n");
                        String lastParagraph = paragraphs[paragraphs.length - 1];
                        Pattern pattern = Pattern.compile(".*Change-Id: (I[^ \n]*).*");
                        Matcher changeIdMatcher = pattern.matcher(lastParagraph);

                        if (changeIdMatcher.matches()) {
                            changeId = changeIdMatcher.group(1);
                            reviewDescriptor.setChangeId(changeId);
                            ;
                        } else {
                            logger.warn(MessageFormat.format(
                                    "Could not find the change id for Gerrit change with primary key {0}",
                                    changePk));
                        }
                        revWalk.close();
                    }
                }
            }

        } catch (GitAPIException e) {
            throw new RepositoryIOException("Error during loading all remote changes", e);
        }

    } catch (IOException e) {
        throw new InvalidReviewRepositoryException("Could not open local git repository", e);
    }
    return changeIds;
}

From source file:at.bitandart.zoubek.mervin.gerrit.GerritReviewRepositoryService.java

License:Open Source License

@Override
public ModelReview loadReview(URI uri, String id, User currentReviewer, IProgressMonitor monitor)
        throws InvalidReviewRepositoryException, InvalidReviewException, RepositoryIOException {
    /*/*from w w w.j  av a2 s  .  com*/
     * Fetch all refs to the patch sets for the particular change and create
     * the model instance from it
     */

    monitor.beginTask("Connecting to repository", IProgressMonitor.UNKNOWN);

    try {
        Git git = Git.open(new File(uri));
        int iId = Integer.parseInt(id);

        // First of all: fetch the patch sets
        // git fetch origin
        // +refs/changes/id%100/<cid>/*:refs/changes/id%100/<cid>/*
        // Refspec of a patchset:
        // +refs/changes/id%100/<cid>/<psId>:refs/changes/id%100/<cid>/<psId>

        monitor.beginTask("Fetching change ref", IProgressMonitor.UNKNOWN);
        git.fetch()
                .setRefSpecs(new RefSpec(MessageFormat.format(
                        "+refs/changes/{0,number,00}/{1}/*:refs/changes/{0,number,00}/{1}/*", iId % 100, iId)))
                .call();

        // create model instance

        ModelReview modelReview = modelReviewFactory.createModelReview();
        modelReview.setId(id);
        modelReview.setRepositoryURI(uri.toString());
        modelReview.setCurrentReviewer(currentReviewer);
        EList<PatchSet> patchSets = modelReview.getPatchSets();

        Repository repository = git.getRepository();
        Map<String, Ref> allRefs = repository.getAllRefs();
        Pattern changeRefPattern = Pattern.compile(CHANGE_REF_PATTERN);

        monitor.beginTask("Loading Patch Sets", allRefs.size());

        List<ResourceSet> resourceSets = new LinkedList<>();

        for (Entry<String, Ref> refEntry : allRefs.entrySet()) {
            Matcher matcher = changeRefPattern.matcher(refEntry.getValue().getName());
            if (matcher.matches() && matcher.group(CHANGE_REF_PATTERN_GROUP_CHANGE_PK).equals(id)) {

                PatchSet patchSet = modelReviewFactory.createPatchSet();
                patchSets.add(patchSet);
                patchSet.setId(matcher.group(CHANGE_REF_PATTERN_GROUP_PATCH_SET_ID));

                monitor.subTask("Loading Patch Set #" + patchSet.getId());

                // load patched files
                loadPatches(patchSet, refEntry.getValue(), git);

                // load involved models
                resourceSets.addAll(loadInvolvedModelsAndDiagrams(patchSet, refEntry.getValue(), git));

                // compare the involved models
                patchSet.setModelComparison(compareModels(patchSet));

                // compare the involved diagrams
                patchSet.setDiagramComparison(compareDiagrams(patchSet));
            }
            monitor.worked(1);
        }

        monitor.beginTask("Sorting Patch Sets", IProgressMonitor.UNKNOWN);

        /*
         * sort by their identifiers, numeric identifiers before string
         * identifiers (gerrit currently has only numeric patch set
         * identifiers, but to be on the save side also consider non-numeric
         * identifiers )
         */
        ECollections.sort(patchSets, new Comparator<PatchSet>() {

            @Override
            public int compare(PatchSet o1, PatchSet o2) {
                String psId1 = o1.getId();
                String psId2 = o2.getId();
                Integer iPsId1 = null;
                Integer iPsId2 = null;
                try {
                    iPsId1 = Integer.valueOf(psId1);
                } catch (NumberFormatException nfe) {
                }
                try {
                    iPsId2 = Integer.valueOf(psId2);
                } catch (NumberFormatException nfe) {
                }

                if (iPsId1 != null && iPsId2 != null) {
                    // both numeric ids
                    return iPsId1.compareTo(iPsId2);
                } else if (iPsId1 != null && iPsId2 == null) {
                    // only one is numeric, the numeric id is always less
                    // than the string id
                    return -1;
                } else if (iPsId1 == null && iPsId2 != null) {
                    // only one is numeric, the numeric id is always less
                    // than the string id
                    return 1;
                }

                // fallback to string sort
                return psId1.compareTo(psId2);
            }
        });

        monitor.beginTask("Loading Comments", IProgressMonitor.UNKNOWN);

        loadComments(repository, modelReview, resourceSets);

        monitor.done();

        return modelReview;

    } catch (IOException e) {
        throw new InvalidReviewRepositoryException("Could not open local git repository", e);
    } catch (NumberFormatException e) {
        throw new InvalidReviewException(MessageFormat.format("Invalid review id: {0}", id));
    } catch (GitAPIException e) {
        throw new RepositoryIOException("Error occured during reading from the git repository", e);
    }
}

From source file:br.com.ingenieux.mojo.beanstalk.bundle.CodeCommitFastDeployMojo.java

License:Apache License

@Override
protected Git getGitRepo() throws Exception {
    if (stagingDirectory.exists() && new File(stagingDirectory, "HEAD").exists()) {
        Git git = Git.open(stagingDirectory);

        git.fetch().setRemote(getRemoteUrl(null, null)).setProgressMonitor(new TextProgressMonitor())
                .setRefSpecs(new RefSpec("refs/heads/master")).call();
    } else {/* w w w . j av a 2  s.c  o  m*/
        Git.cloneRepository().setURI(getRemoteUrl(null, null)).setProgressMonitor(new TextProgressMonitor())
                .setDirectory(stagingDirectory).setNoCheckout(true).setBare(true).call();
    }

    Repository r = null;

    RepositoryBuilder b = new RepositoryBuilder().setGitDir(stagingDirectory).setWorkTree(sourceDirectory);

    r = b.build();

    final Git git = Git.wrap(r);

    return git;
}

From source file:com.barchart.jenkins.cascade.PluginScmGit.java

License:BSD License

/**
 * Source:Target branch reference for fetch with fast-forward.
 * <p>// w  w  w.  j  a  va2 s.com
 * Example: +refs/heads/master:refs/remotes/origin/master
 */
public static RefSpec refFetch(final String remoteBranchName, final String remoteName,
        final String remoteTrackingBranchName) {
    return new RefSpec(ref(true, remoteBranchName, remoteName, remoteTrackingBranchName));
}

From source file:com.barchart.jenkins.cascade.PluginScmGit.java

License:BSD License

/**
 * Source:Target branch reference for push w/o fast-forward.
 * <p>// w w  w.j  a v a2 s.c om
 * Example: refs/heads/master:refs/heads/master
 */
public static RefSpec refPush(final String localBranchName, final String remoteBranchName) {
    return new RefSpec(ref(false, localBranchName, remoteBranchName));
}

From source file:com.cloudbees.jenkins.plugins.gogs.GogsSCMSource.java

License:Open Source License

@Override
protected List<RefSpec> getRefSpecs() {
    return new ArrayList<>(Arrays.asList(new RefSpec("+refs/heads/*:refs/remotes/origin/*"),
            // For PRs we check out the head, then perhaps merge with the base branch.
            new RefSpec("+refs/pull/*/head:refs/remotes/origin/pr/*")));
}

From source file:com.cloudcontrolled.cctrl.maven.plugin.push.CloudcontrolledPush.java

License:Apache License

private String push(String remoteLocation) throws MojoExecutionException {
    Repository repository = null;//from  www  .ja v  a  2  s .  co m
    Git git;
    try {
        repository = getRepository();
        git = Git.wrap(repository);

        PushCommand pushCommand = git.push();
        pushCommand.setRemote(remoteLocation);
        pushCommand.setRefSpecs(new RefSpec(repository.getFullBranch()));

        Iterable<PushResult> pushResult = pushCommand.call();
        Iterator<PushResult> result = pushResult.iterator();

        StringBuffer buffer = new StringBuffer();
        if (result.hasNext()) {
            while (result.hasNext()) {
                String line = result.next().getMessages();
                if (!line.isEmpty()) {
                    buffer.append(line);
                    if (result.hasNext()) {
                        buffer.append(System.getProperty("line.separator"));
                    }
                }
            }
        }

        return buffer.toString();
    } catch (Exception e) {
        throw new MojoExecutionException(e.getClass().getSimpleName(), e);
    } finally {
        if (repository != null) {
            repository.close();
        }
    }
}

From source file:com.denimgroup.threadfix.service.repository.GitServiceImpl.java

License:Mozilla Public License

@Override
public boolean testConfiguration(Application application, String repo, String branch) throws GitAPIException {
    InitCommand initCommand = new InitCommand();
    File applicationDirectory = DiskUtils.getScratchFile(baseDirectory + application.getId() + "-test");
    initCommand.setDirectory(applicationDirectory);

    Git otherGit = initCommand.call();/*  www  . j  ava 2  s .c o m*/

    otherGit.getRepository().getConfig().setString("remote", "origin", "url", repo);

    String targetRefSpec = branch == null || branch.isEmpty() ? Constants.R_HEADS + "*:refs/remotes/origin/*"
            : Constants.R_HEADS + branch;

    FetchCommand fetchCommand = otherGit.fetch()
            .setCredentialsProvider(getUnencryptedApplicationCredentials(application)).setDryRun(true)
            .setRefSpecs(new RefSpec(targetRefSpec)).setRemote("origin");

    fetchCommand.call();

    return true;
}

From source file:com.genuitec.eclipse.gerrit.tools.internal.fbranches.BranchingUtils.java

License:Open Source License

public static PushOperationSpecification setupPush(Repository repository, String refSpec)
        throws IOException, URISyntaxException {
    PushOperationSpecification spec = new PushOperationSpecification();
    Collection<RemoteRefUpdate> updates = Transport.findRemoteRefUpdatesFor(repository,
            Collections.singletonList(new RefSpec(refSpec)), null);

    RemoteConfig config = new RemoteConfig(repository.getConfig(), "origin");

    for (URIish uri : config.getPushURIs()) {
        spec.addURIRefUpdates(uri, updates);
        break;//from   w ww.  j  av a 2  s  . c  o m
    }
    if (spec.getURIsNumber() == 0) {
        for (URIish uri : config.getURIs()) {
            spec.addURIRefUpdates(uri, updates);
            break;
        }
    }
    if (spec.getURIsNumber() == 0) {
        throw new RuntimeException("Cannot find URI for push");
    }
    return spec;
}

From source file:com.gitblit.manager.GitblitManager.java

License:Apache License

/**
 * 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.//from  w  ww.  j av a 2 s .  c  o m
 *
 * @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);

    // ensure initial access restriction of the fork
    // 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;
}