Example usage for org.eclipse.jgit.revwalk RevWalk close

List of usage examples for org.eclipse.jgit.revwalk RevWalk close

Introduction

In this page you can find the example usage for org.eclipse.jgit.revwalk RevWalk close.

Prototype

@Override
public void close() 

Source Link

Document

Release any resources used by this walker's reader.

Usage

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

License:Open Source License

@SuppressWarnings("restriction")
@Override//from   w ww .  j  av  a2 s  . com
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

/**
 * loads all involved models and diagrams for the given patchSet using the
 * given {@link Git} instance from the given git {@link Ref}.
 * //from  w  w w  .j a  v a  2s. c  o m
 * @param patchSet
 *            the patch set instance to store the involved models into
 * @param ref
 *            the git ref to the commit which contains the patch set.
 * @param git
 *            the git instance to use
 * @return a list containing the resource sets for the old and the new model
 *         resources.
 * @throws IOException
 */
private List<ResourceSet> loadInvolvedModelsAndDiagrams(PatchSet patchSet, Ref ref, Git git)
        throws IOException {

    String commitHash = ref.getObjectId().name();

    RevWalk revWalk = new RevWalk(git.getRepository());
    RevCommit newCommit = revWalk.parseCommit(ref.getObjectId());
    RevCommit oldCommit = newCommit.getParent(0);
    revWalk.parseHeaders(oldCommit);
    revWalk.close();

    String parentCommitHash = oldCommit.getId().name();

    URI repoURI = git.getRepository().getDirectory().toURI();
    String authority = repoURI.getAuthority();
    String path = repoURI.getPath();
    String repoPath = (authority != null ? authority + "/" : "") + (path != null ? path : "");
    if (repoPath.endsWith("/")) {
        repoPath = repoPath.substring(0, repoPath.length() - 1);
    }

    ResourceSet newResourceSet = createGitAwareResourceSet(commitHash, repoPath,
            Collections.<ResourceSet>emptyList());
    ResourceSet oldModelResourceSet = createGitAwareResourceSet(parentCommitHash, repoPath,
            Collections.<ResourceSet>emptyList());

    for (Patch patch : patchSet.getPatches()) {
        if (patch instanceof ModelPatch || patch instanceof DiagramPatch) {
            org.eclipse.emf.common.util.URI newUri = org.eclipse.emf.common.util.URI
                    .createURI(GitURIParser.GIT_COMMIT_SCHEME + "://" + repoPath + "/" + commitHash + "/"
                            + patch.getNewPath());

            org.eclipse.emf.common.util.URI oldUri = org.eclipse.emf.common.util.URI
                    .createURI(GitURIParser.GIT_COMMIT_SCHEME + "://" + repoPath + "/" + parentCommitHash + "/"
                            + patch.getOldPath());

            if (patch.getChangeType() != PatchChangeType.DELETE) {
                // if the patch has been deleted no new resource exists
                Resource newResource = newResourceSet.getResource(newUri, true);
                try {
                    applyResourceContent(newResource, patch, false);
                } catch (IOException e) {
                    throw new IOException(
                            MessageFormat.format("Could not load resource \"{0}\" for patch set {1}",
                                    newUri.toString(), patchSet.getId()),
                            e);
                }
            }

            if (patch.getChangeType() != PatchChangeType.ADD) {
                // if the patch has been added no old resource exists
                Resource oldResource = oldModelResourceSet.getResource(oldUri, true);
                try {
                    applyResourceContent(oldResource, patch, true);
                } catch (IOException e) {
                    throw new IOException(
                            MessageFormat.format("Could not load resource \"{0}\" for patch set {1}",
                                    oldUri.toString(), patchSet.getId()),
                            e);
                }
            }

        }
    }

    List<ResourceSet> resourceSets = new ArrayList<>(2);
    resourceSets.add(oldModelResourceSet);
    resourceSets.add(newResourceSet);
    return resourceSets;
}

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

License:Open Source License

/**
 * loads all patches of from the given list of {@link DiffEntry}s.
 * /*from w  w w  . ja va  2s . c o  m*/
 * @param patchSet
 *            the patchSet to add the patches to.
 * @param ref
 *            the ref to the commit of the patch set.
 * @param repository
 *            the git repository instance
 * @throws RepositoryIOException
 */
private void loadPatches(PatchSet patchSet, Ref ref, Git git) throws RepositoryIOException {

    EList<Patch> patches = patchSet.getPatches();
    Repository repository = git.getRepository();

    try {

        RevWalk revWalk = new RevWalk(repository);
        RevCommit newCommit = revWalk.parseCommit(ref.getObjectId());
        RevCommit oldCommit = newCommit.getParent(0);
        revWalk.parseHeaders(oldCommit);
        ObjectReader objectReader = repository.newObjectReader();
        revWalk.close();

        CanonicalTreeParser newTreeIterator = new CanonicalTreeParser();
        newTreeIterator.reset(objectReader, newCommit.getTree().getId());
        CanonicalTreeParser oldTreeIterator = new CanonicalTreeParser();
        oldTreeIterator.reset(objectReader, oldCommit.getTree().getId());

        List<DiffEntry> diffs = git.diff().setOldTree(oldTreeIterator).setNewTree(newTreeIterator).call();

        for (DiffEntry diff : diffs) {

            String newPath = diff.getNewPath();
            String oldPath = diff.getOldPath();
            Patch patch = null;

            /*
             * only papyrus diagrams are supported for now, so models are in
             * .uml files, diagrams in .notation files
             */

            if (diff.getChangeType() != ChangeType.DELETE) {
                if (newPath.endsWith(".uml")) {
                    patch = modelReviewFactory.createModelPatch();

                } else if (newPath.endsWith(".notation")) {
                    patch = modelReviewFactory.createDiagramPatch();
                } else {
                    patch = modelReviewFactory.createPatch();
                }
            } else {
                if (oldPath.endsWith(".uml")) {
                    patch = modelReviewFactory.createModelPatch();

                } else if (oldPath.endsWith(".notation")) {
                    patch = modelReviewFactory.createDiagramPatch();
                } else {
                    patch = modelReviewFactory.createPatch();
                }
            }

            switch (diff.getChangeType()) {
            case ADD:
                patch.setChangeType(PatchChangeType.ADD);
                break;
            case COPY:
                patch.setChangeType(PatchChangeType.COPY);
                break;
            case DELETE:
                patch.setChangeType(PatchChangeType.DELETE);
                break;
            case MODIFY:
                patch.setChangeType(PatchChangeType.MODIFY);
                break;
            case RENAME:
                patch.setChangeType(PatchChangeType.RENAME);
                break;
            }

            patch.setNewPath(newPath);
            patch.setOldPath(oldPath);

            if (diff.getChangeType() != ChangeType.DELETE) {
                ObjectLoader objectLoader = repository.open(diff.getNewId().toObjectId());
                patch.setNewContent(objectLoader.getBytes());
            }
            if (diff.getChangeType() != ChangeType.ADD) {
                ObjectLoader objectLoader = repository.open(diff.getOldId().toObjectId());
                patch.setOldContent(objectLoader.getBytes());
            }
            patches.add(patch);

        }

    } catch (IOException e) {
        throw new RepositoryIOException(MessageFormat.format(
                "An IO error occured during loading the patches for patch set #{0}", patchSet.getId()), e);
    } catch (GitAPIException e) {
        throw new RepositoryIOException(
                MessageFormat.format("An JGit API error occured during loading the patches for patch set #{0}",
                        patchSet.getId()),
                e);
    }
}

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

License:Open Source License

@Override
public void saveReview(URI uri, ModelReview modelReview, User currentReviewer, IProgressMonitor monitor)
        throws InvalidReviewRepositoryException, InvalidReviewException, RepositoryIOException {

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

    String repoFileURI = COMMENTS_FILE_URI;

    try {//from w  w  w . ja v a  2 s  . co m
        Git git = Git.open(new File(uri));
        Repository repository = git.getRepository();
        ObjectInserter objectInserter = repository.newObjectInserter();

        String commentRefName = getCommentRefName(modelReview);
        Ref commentRef = repository.exactRef(commentRefName);

        DirCache index = DirCache.newInCore();
        DirCacheBuilder dirCacheBuilder = index.builder();

        monitor.beginTask("Preparing commit...", IProgressMonitor.UNKNOWN);

        if (commentRef != null) {

            /*
             * The ref already exists so we have to copy the previous
             * RevTree to keep all already attached files
             */

            RevWalk revWalk = new RevWalk(repository);
            RevCommit prevCommit = revWalk.parseCommit(commentRef.getObjectId());
            RevTree tree = prevCommit.getTree();

            List<String> ignoredFiles = new ArrayList<>();
            /*
             * add file path of the new file to the ignored file paths, as
             * we don't want any already existing old file in our new tree
             */
            ignoredFiles.add(repoFileURI);
            buildDirCacheFromTree(tree, repository, dirCacheBuilder, ignoredFiles);

            revWalk.close();
        }

        monitor.beginTask("Writing comments file...", IProgressMonitor.UNKNOWN);

        ResourceSet resourceSet = new ResourceSetImpl();
        Resource resource = resourceSet.createResource(org.eclipse.emf.common.util.URI.createURI(repoFileURI));

        addCommentsToResource(modelReview, resource);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        resource.save(outputStream, null);

        // insert file as object
        byte[] content = outputStream.toByteArray();
        long length = content.length;
        InputStream inputStream = new ByteArrayInputStream(content);
        ObjectId objectId = objectInserter.insert(Constants.OBJ_BLOB, length, inputStream);
        inputStream.close();

        // create tree entry
        DirCacheEntry entry = new DirCacheEntry(repoFileURI);
        entry.setFileMode(FileMode.REGULAR_FILE);
        entry.setLastModified(System.currentTimeMillis());
        entry.setLength(length);
        entry.setObjectId(objectId);
        dirCacheBuilder.add(entry);

        dirCacheBuilder.finish();

        // write new tree in database
        ObjectId indexTreeId = index.writeTree(objectInserter);

        monitor.beginTask("Commiting comments...", IProgressMonitor.UNKNOWN);

        // create commit
        CommitBuilder commitBuilder = new CommitBuilder();
        PersonIdent personIdent = new PersonIdent("Mervin", "mervin@mervin.modelreview");
        commitBuilder.setCommitter(personIdent);
        commitBuilder.setAuthor(personIdent);
        commitBuilder.setMessage(
                MessageFormat.format("Updated comments by user \"{0}\"", currentReviewer.getName()));

        if (commentRef != null) {
            commitBuilder.setParentId(commentRef.getObjectId());
        }
        commitBuilder.setTreeId(indexTreeId);

        // commit
        ObjectId commitId = objectInserter.insert(commitBuilder);
        objectInserter.flush();

        RefUpdate refUpdate = repository.updateRef(commentRefName);
        refUpdate.setNewObjectId(commitId);
        if (commentRef != null)
            refUpdate.setExpectedOldObjectId(commentRef.getObjectId());
        else
            refUpdate.setExpectedOldObjectId(ObjectId.zeroId());

        /*
         * TODO the result handling below is copied from the CommitCommand
         * class, I don't know if this is really necessary in our case
         */
        Result result = refUpdate.forceUpdate();
        switch (result) {
        case NEW:
        case FORCED:
        case FAST_FORWARD: {
            if (repository.getRepositoryState() == RepositoryState.MERGING_RESOLVED) {
                /*
                 * Commit was successful. Now delete the files used for
                 * merge commits
                 */
                repository.writeMergeCommitMsg(null);
                repository.writeMergeHeads(null);
            } else if (repository.getRepositoryState() == RepositoryState.CHERRY_PICKING_RESOLVED) {
                repository.writeMergeCommitMsg(null);
                repository.writeCherryPickHead(null);
            } else if (repository.getRepositoryState() == RepositoryState.REVERTING_RESOLVED) {
                repository.writeMergeCommitMsg(null);
                repository.writeRevertHead(null);
            }
            break;
        }
        case REJECTED:
        case LOCK_FAILURE:
            throw new RepositoryIOException("Error occured during writing to the git repository",
                    new ConcurrentRefUpdateException("Could not lock ref " + refUpdate.getRef().getName(),
                            refUpdate.getRef(), result));
        default:
            throw new RepositoryIOException("Error occured during writing to the git repository",
                    new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed,
                            refUpdate.getRef().getName(), commitId.toString(), result)));
        }

    } catch (IOException e) {
        throw new InvalidReviewRepositoryException("Could not open local git repository", e);
    } finally {
        monitor.done();
    }

}

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

License:Open Source License

/**
 * loads the referenced commit//from w  w  w .ja v  a  2 s . co m
 * 
 * @throws IOException
 *             if an error occurs during parsing the URI
 */
private void loadCommit() throws IOException {
    if (repository == null)
        loadRepository();
    RevWalk revWalk = new RevWalk(repository);
    commit = revWalk.parseCommit(ObjectId.fromString(commitHash));
    revWalk.close();
}

From source file:br.com.riselabs.cotonet.builder.NetworkBuilder.java

License:Open Source License

/**
 * Returns the conflicting merge scenarios
 * /*from   w  w w .j  a  v  a  2s  .  c om*/
 * @return - a list of merge scenarios. it may be empty in case of no
 *         conflict.
 * @throws IOException
 */
private List<MergeScenario> getMergeScenarios() throws IOException {
    List<MergeScenario> result = new ArrayList<MergeScenario>();
    List<RevCommit> mergeCommits = new ArrayList<RevCommit>();
    Iterable<RevCommit> gitlog;
    try {
        Git git = Git.wrap(getProject().getRepository());
        gitlog = git.log().call();
        for (RevCommit commit : gitlog) {
            if (commit.getParentCount() == 2) {
                mergeCommits.add(commit);
                // collecting merge commits
                // we know there is only to parents
                RevCommit leftParent = commit.getParent(0);
                RevCommit rightParent = commit.getParent(1);
                ThreeWayMerger merger = MergeStrategy.RECURSIVE.newMerger(getProject().getRepository(), true);
                // selecting the conflicting ones
                boolean noConflicts = false;
                try {
                    noConflicts = merger.merge(leftParent, rightParent);
                } catch (NoMergeBaseException e) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("[" + project.getName() + ":" + project.getUrl() + "] "
                            + "Skipping merge scenario due to '" + e.getMessage() + "'\n");
                    sb.append("---> Skipped scenario:\n");
                    sb.append("::Base (<several>): \n");
                    sb.append("::Left (" + leftParent.getAuthorIdent().getWhen().toString() + "):"
                            + leftParent.getName() + "\n");
                    sb.append("::Right (" + rightParent.getAuthorIdent().getWhen().toString() + "):"
                            + rightParent.getName() + "\n");
                    Logger.log(log, sb.toString());
                    Logger.logStackTrace(log, e);
                    continue;
                }
                if (noConflicts) {
                    continue;
                }
                RevWalk walk = new RevWalk(getProject().getRepository());
                // for merges without a base commit
                if (merger.getBaseCommitId() == null)
                    continue;
                RevCommit baseCommit = walk.lookupCommit(merger.getBaseCommitId());
                walk.close();

                Timestamp mergeDate = new Timestamp(commit.getAuthorIdent().getWhen().getTime());
                result.add(new MergeScenario(baseCommit, leftParent, rightParent, commit, mergeDate));
            }
        }
    } catch (GitAPIException e) {
        Logger.logStackTrace(log, e);
    }
    return result;
}

From source file:com.centurylink.mdw.dataaccess.file.VersionControlGit.java

License:Apache License

public boolean isTracked(String path) throws IOException {
    ObjectId objectId = localRepo.resolve(Constants.HEAD);
    RevTree tree;/*from  www  .  j ava2  s  . com*/
    RevWalk walk = null;
    if (objectId != null) {
        walk = new RevWalk(localRepo);
        tree = walk.parseTree(objectId);
    } else {
        tree = null;
    }

    try (TreeWalk treeWalk = new TreeWalk(localRepo)) {
        treeWalk.setRecursive(true);
        if (tree != null)
            treeWalk.addTree(tree);
        else
            treeWalk.addTree(new EmptyTreeIterator());

        treeWalk.addTree(new DirCacheIterator(localRepo.readDirCache()));
        treeWalk.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(path)));
        return treeWalk.next();
    } finally {
        if (walk != null)
            walk.close();
    }
}

From source file:com.gitblit.git.PatchsetReceivePack.java

License:Apache License

/**
 * Prepares a patchset command.//from  w  ww .  j  a  v  a  2 s.  c  o  m
 *
 * @param cmd
 * @return the patchset command
 */
private PatchsetCommand preparePatchset(ReceiveCommand cmd) {
    String branch = getIntegrationBranch(cmd.getRefName());
    long number = getTicketId(cmd.getRefName());

    TicketModel ticket = null;
    if (number > 0 && ticketService.hasTicket(repository, number)) {
        ticket = ticketService.getTicket(repository, number);
    }

    if (ticket == null) {
        if (number > 0) {
            // requested ticket does not exist
            sendError("Sorry, {0} does not have ticket {1,number,0}!", repository.name, number);
            sendRejection(cmd, "Invalid ticket number");
            return null;
        }
    } else {
        if (ticket.isMerged()) {
            // ticket already merged & resolved
            Change mergeChange = null;
            for (Change change : ticket.changes) {
                if (change.isMerge()) {
                    mergeChange = change;
                    break;
                }
            }
            if (mergeChange != null) {
                sendError("Sorry, {0} already merged {1} from ticket {2,number,0} to {3}!", mergeChange.author,
                        mergeChange.patchset, number, ticket.mergeTo);
            }
            sendRejection(cmd, "Ticket {0,number,0} already resolved", number);
            return null;
        } else if (!StringUtils.isEmpty(ticket.mergeTo)) {
            // ticket specifies integration branch
            branch = ticket.mergeTo;
        }
    }

    final int shortCommitIdLen = settings.getInteger(Keys.web.shortCommitIdLength, 6);
    final String shortTipId = cmd.getNewId().getName().substring(0, shortCommitIdLen);
    final RevCommit tipCommit = JGitUtils.getCommit(getRepository(), cmd.getNewId().getName());
    final String forBranch = branch;
    RevCommit mergeBase = null;
    Ref forBranchRef = getAdvertisedRefs().get(Constants.R_HEADS + forBranch);
    if (forBranchRef == null || forBranchRef.getObjectId() == null) {
        // unknown integration branch
        sendError("Sorry, there is no integration branch named ''{0}''.", forBranch);
        sendRejection(cmd, "Invalid integration branch specified");
        return null;
    } else {
        // determine the merge base for the patchset on the integration branch
        String base = JGitUtils.getMergeBase(getRepository(), forBranchRef.getObjectId(), tipCommit.getId());
        if (StringUtils.isEmpty(base)) {
            sendError("");
            sendError("There is no common ancestry between {0} and {1}.", forBranch, shortTipId);
            sendError("Please reconsider your proposed integration branch, {0}.", forBranch);
            sendError("");
            sendRejection(cmd, "no merge base for patchset and {0}", forBranch);
            return null;
        }
        mergeBase = JGitUtils.getCommit(getRepository(), base);
    }

    // ensure that the patchset can be cleanly merged right now
    MergeStatus status = JGitUtils.canMerge(getRepository(), tipCommit.getName(), forBranch,
            repository.mergeType);
    switch (status) {
    case ALREADY_MERGED:
        sendError("");
        sendError("You have already merged this patchset.", forBranch);
        sendError("");
        sendRejection(cmd, "everything up-to-date");
        return null;
    case MERGEABLE:
        break;
    default:
        if (ticket == null || requireMergeablePatchset) {
            sendError("");
            sendError("Your patchset can not be cleanly merged into {0}.", forBranch);
            sendError("Please rebase your patchset and push again.");
            sendError("NOTE:", number);
            sendError("You should push your rebase to refs/for/{0,number,0}", number);
            sendError("");
            sendError("  git push origin HEAD:refs/for/{0,number,0}", number);
            sendError("");
            sendRejection(cmd, "patchset not mergeable");
            return null;
        }
    }

    // check to see if this commit is already linked to a ticket
    if (ticket != null
            && JGitUtils.getTicketNumberFromCommitBranch(getRepository(), tipCommit) == ticket.number) {
        sendError("{0} has already been pushed to ticket {1,number,0}.", shortTipId, ticket.number);
        sendRejection(cmd, "everything up-to-date");
        return null;
    }

    List<TicketLink> ticketLinks = JGitUtils.identifyTicketsFromCommitMessage(getRepository(), settings,
            tipCommit);

    PatchsetCommand psCmd;
    if (ticket == null) {
        /*
         *  NEW TICKET
         */
        Patchset patchset = newPatchset(null, mergeBase.getName(), tipCommit.getName());

        int minLength = 10;
        int maxLength = 100;
        String minTitle = MessageFormat.format("  minimum length of a title is {0} characters.", minLength);
        String maxTitle = MessageFormat.format("  maximum length of a title is {0} characters.", maxLength);

        if (patchset.commits > 1) {
            sendError("");
            sendError("You may not create a ''{0}'' branch proposal ticket from {1} commits!", forBranch,
                    patchset.commits);
            sendError("");
            // display an ellipsized log of the commits being pushed
            RevWalk walk = getRevWalk();
            walk.reset();
            walk.sort(RevSort.TOPO);
            int boundary = 3;
            int count = 0;
            try {
                walk.markStart(tipCommit);
                walk.markUninteresting(mergeBase);

                for (;;) {

                    RevCommit c = walk.next();
                    if (c == null) {
                        break;
                    }

                    if (count < boundary || count >= (patchset.commits - boundary)) {

                        walk.parseBody(c);
                        sendError("   {0}  {1}", c.getName().substring(0, shortCommitIdLen),
                                StringUtils.trimString(c.getShortMessage(), 60));

                    } else if (count == boundary) {

                        sendError("   ... more commits ...");

                    }

                    count++;
                }

            } catch (IOException e) {
                // Should never happen, the core receive process would have
                // identified the missing object earlier before we got control.
                LOGGER.error("failed to get commit count", e);
            } finally {
                walk.close();
            }

            sendError("");
            sendError("Possible Solutions:");
            sendError("");
            int solution = 1;
            String forSpec = cmd.getRefName().substring(Constants.R_FOR.length());
            if (forSpec.equals("default") || forSpec.equals("new")) {
                try {
                    // determine other possible integration targets
                    List<String> bases = Lists.newArrayList();
                    for (Ref ref : getRepository().getRefDatabase().getRefs(Constants.R_HEADS).values()) {
                        if (!ref.getName().startsWith(Constants.R_TICKET)
                                && !ref.getName().equals(forBranchRef.getName())) {
                            if (JGitUtils.isMergedInto(getRepository(), ref.getObjectId(), tipCommit)) {
                                bases.add(Repository.shortenRefName(ref.getName()));
                            }
                        }
                    }

                    if (!bases.isEmpty()) {

                        if (bases.size() == 1) {
                            // suggest possible integration targets
                            String base = bases.get(0);
                            sendError("{0}. Propose this change for the ''{1}'' branch.", solution++, base);
                            sendError("");
                            sendError("   git push origin HEAD:refs/for/{0}", base);
                            sendError("   pt propose {0}", base);
                            sendError("");
                        } else {
                            // suggest possible integration targets
                            sendError("{0}. Propose this change for a different branch.", solution++);
                            sendError("");
                            for (String base : bases) {
                                sendError("   git push origin HEAD:refs/for/{0}", base);
                                sendError("   pt propose {0}", base);
                                sendError("");
                            }
                        }

                    }
                } catch (IOException e) {
                    LOGGER.error(null, e);
                }
            }
            sendError("{0}. Squash your changes into a single commit with a meaningful message.", solution++);
            sendError("");
            sendError("{0}. Open a ticket for your changes and then push your {1} commits to the ticket.",
                    solution++, patchset.commits);
            sendError("");
            sendError("   git push origin HEAD:refs/for/{id}");
            sendError("   pt propose {id}");
            sendError("");
            sendRejection(cmd, "too many commits");
            return null;
        }

        // require a reasonable title/subject
        String title = tipCommit.getFullMessage().trim().split("\n")[0];
        if (title.length() < minLength) {
            // reject, title too short
            sendError("");
            sendError("Please supply a longer title in your commit message!");
            sendError("");
            sendError(minTitle);
            sendError(maxTitle);
            sendError("");
            sendRejection(cmd, "ticket title is too short [{0}/{1}]", title.length(), maxLength);
            return null;
        }
        if (title.length() > maxLength) {
            // reject, title too long
            sendError("");
            sendError("Please supply a more concise title in your commit message!");
            sendError("");
            sendError(minTitle);
            sendError(maxTitle);
            sendError("");
            sendRejection(cmd, "ticket title is too long [{0}/{1}]", title.length(), maxLength);
            return null;
        }

        // assign new id
        long ticketId = ticketService.assignNewId(repository);

        // create the patchset command
        psCmd = new PatchsetCommand(user.username, patchset);
        psCmd.newTicket(tipCommit, forBranch, ticketId, cmd.getRefName());
    } else {
        /*
         *  EXISTING TICKET
         */
        Patchset patchset = newPatchset(ticket, mergeBase.getName(), tipCommit.getName());
        psCmd = new PatchsetCommand(user.username, patchset);
        psCmd.updateTicket(tipCommit, forBranch, ticket, cmd.getRefName());
    }

    // confirm user can push the patchset
    boolean pushPermitted = ticket == null || !ticket.hasPatchsets() || ticket.isAuthor(user.username)
            || ticket.isPatchsetAuthor(user.username) || ticket.isResponsible(user.username)
            || user.canPush(repository);

    switch (psCmd.getPatchsetType()) {
    case Proposal:
        // proposals (first patchset) are always acceptable
        break;
    case FastForward:
        // patchset updates must be permitted
        if (!pushPermitted) {
            // reject
            sendError("");
            sendError("To push a patchset to this ticket one of the following must be true:");
            sendError("  1. you created the ticket");
            sendError("  2. you created the first patchset");
            sendError("  3. you are specified as responsible for the ticket");
            sendError("  4. you have push (RW) permissions to {0}", repository.name);
            sendError("");
            sendRejection(cmd, "not permitted to push to ticket {0,number,0}", ticket.number);
            return null;
        }
        break;
    default:
        // non-fast-forward push
        if (!pushPermitted) {
            // reject
            sendRejection(cmd, "non-fast-forward ({0})", psCmd.getPatchsetType());
            return null;
        }
        break;
    }

    Change change = psCmd.getChange();
    change.pendingLinks = ticketLinks;

    return psCmd;
}

From source file:com.gitblit.tickets.BranchTicketService.java

License:Apache License

/**
 * Reads a file from the tickets branch.
 *
 * @param db//from  www  .  j av  a  2s.c  o  m
 * @param file
 * @return the file content or null
 */
private String readTicketsFile(Repository db, String file) {
    RevWalk rw = null;
    try {
        ObjectId treeId = db.resolve(BRANCH + "^{tree}");
        if (treeId == null) {
            return null;
        }
        rw = new RevWalk(db);
        RevTree tree = rw.lookupTree(treeId);
        if (tree != null) {
            return JGitUtils.getStringContent(db, tree, file, Constants.ENCODING);
        }
    } catch (IOException e) {
        log.error("failed to read " + file, e);
    } finally {
        if (rw != null) {
            rw.close();
        }
    }
    return null;
}

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

License:Apache License

/**
 * Returns the list of files changed in a specified commit. If the
 * repository does not exist or is empty, an empty list is returned.
 *
 * @param repository// w  w w.  ja  v  a 2  s .  c o m
 * @param startCommit
 *            earliest commit
 * @param endCommit
 *            most recent commit. if null, HEAD is assumed.
 * @return list of files changed in a commit range
 */
public static List<PathChangeModel> getFilesInRange(Repository repository, String startCommit,
        String endCommit) {
    List<PathChangeModel> list = new ArrayList<PathChangeModel>();
    if (!hasCommits(repository)) {
        return list;
    }
    try {
        ObjectId startRange = repository.resolve(startCommit);
        ObjectId endRange = repository.resolve(endCommit);
        RevWalk rw = new RevWalk(repository);
        RevCommit start = rw.parseCommit(startRange);
        RevCommit end = rw.parseCommit(endRange);
        list.addAll(getFilesInRange(repository, start, end));
        rw.close();
    } catch (Throwable t) {
        error(t, repository, "{0} failed to determine files in range {1}..{2}!", startCommit, endCommit);
    }
    return list;
}