Search in sources :

Example 1 with PathChangeModel

use of com.gitblit.models.PathModel.PathChangeModel in project gitblit by gitblit.

the class JGitUtilsTest method testFilesInCommit.

@Test
public void testFilesInCommit() throws Exception {
    Repository repository = GitBlitSuite.getHelloworldRepository();
    RevCommit commit = JGitUtils.getCommit(repository, GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifteen));
    List<PathChangeModel> paths = JGitUtils.getFilesInCommit(repository, commit);
    commit = JGitUtils.getCommit(repository, GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted));
    List<PathChangeModel> deletions = JGitUtils.getFilesInCommit(repository, commit);
    commit = JGitUtils.getFirstCommit(repository, null);
    List<PathChangeModel> additions = JGitUtils.getFilesInCommit(repository, commit);
    List<PathChangeModel> latestChanges = JGitUtils.getFilesInCommit(repository, null);
    repository.close();
    assertTrue("No changed paths found!", paths.size() == 1);
    for (PathChangeModel path : paths) {
        assertTrue("PathChangeModel hashcode incorrect!", path.hashCode() == (path.commitId.hashCode() + path.path.hashCode()));
        assertTrue("PathChangeModel equals itself failed!", path.equals(path));
        assertFalse("PathChangeModel equals string failed!", path.equals(""));
    }
    assertEquals(ChangeType.DELETE, deletions.get(0).changeType);
    assertEquals(ChangeType.ADD, additions.get(0).changeType);
    assertTrue(latestChanges.size() > 0);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) PathChangeModel(com.gitblit.models.PathModel.PathChangeModel) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 2 with PathChangeModel

use of com.gitblit.models.PathModel.PathChangeModel in project gitblit by gitblit.

the class LuceneService method index.

/**
 * Incrementally update the index with the specified commit for the
 * repository.
 *
 * @param repositoryName
 * @param repository
 * @param branch
 *            the fully qualified branch name (e.g. refs/heads/master)
 * @param commit
 * @return true, if successful
 */
private IndexResult index(String repositoryName, Repository repository, String branch, RevCommit commit) {
    IndexResult result = new IndexResult();
    try {
        String[] encodings = storedSettings.getStrings(Keys.web.blobEncodings).toArray(new String[0]);
        List<PathChangeModel> changedPaths = JGitUtils.getFilesInCommit(repository, commit);
        String revDate = DateTools.timeToString(commit.getCommitTime() * 1000L, Resolution.MINUTE);
        IndexWriter writer = getIndexWriter(repositoryName);
        for (PathChangeModel path : changedPaths) {
            if (path.isSubmodule()) {
                continue;
            }
            // delete the indexed blob
            deleteBlob(repositoryName, branch, path.name);
            // re-index the blob
            if (!ChangeType.DELETE.equals(path.changeType)) {
                result.blobCount++;
                Document doc = new Document();
                doc.add(new Field(FIELD_OBJECT_TYPE, SearchObjectType.blob.name(), StringField.TYPE_STORED));
                doc.add(new Field(FIELD_BRANCH, branch, TextField.TYPE_STORED));
                doc.add(new Field(FIELD_COMMIT, commit.getName(), TextField.TYPE_STORED));
                doc.add(new Field(FIELD_PATH, path.path, TextField.TYPE_STORED));
                doc.add(new Field(FIELD_DATE, revDate, StringField.TYPE_STORED));
                doc.add(new Field(FIELD_AUTHOR, getAuthor(commit), TextField.TYPE_STORED));
                doc.add(new Field(FIELD_COMMITTER, getCommitter(commit), TextField.TYPE_STORED));
                // determine extension to compare to the extension
                // blacklist
                String ext = null;
                String name = path.name.toLowerCase();
                if (name.indexOf('.') > -1) {
                    ext = name.substring(name.lastIndexOf('.') + 1);
                }
                if (StringUtils.isEmpty(ext) || !excludedExtensions.contains(ext)) {
                    // read the blob content
                    String str = JGitUtils.getStringContent(repository, commit.getTree(), path.path, encodings);
                    if (str != null) {
                        doc.add(new Field(FIELD_CONTENT, str, TextField.TYPE_STORED));
                        writer.addDocument(doc);
                    }
                }
            }
        }
        writer.commit();
        // get any annotated commit tags
        List<String> commitTags = new ArrayList<String>();
        for (RefModel ref : JGitUtils.getTags(repository, false, -1)) {
            if (ref.isAnnotatedTag() && ref.getReferencedObjectId().equals(commit.getId())) {
                commitTags.add(ref.displayName);
            }
        }
        // create and write the Lucene document
        Document doc = createDocument(commit, commitTags);
        doc.add(new Field(FIELD_BRANCH, branch, TextField.TYPE_STORED));
        result.commitCount++;
        result.success = index(repositoryName, doc);
    } catch (Exception e) {
        logger.error(MessageFormat.format("Exception while indexing commit {0} in {1}", commit.getId().getName(), repositoryName), e);
    }
    return result;
}
Also used : StringField(org.apache.lucene.document.StringField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) RefModel(com.gitblit.models.RefModel) PathChangeModel(com.gitblit.models.PathModel.PathChangeModel) IndexWriter(org.apache.lucene.index.IndexWriter) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) ParseException(java.text.ParseException) InvalidTokenOffsetsException(org.apache.lucene.search.highlight.InvalidTokenOffsetsException) IOException(java.io.IOException)

Example 3 with PathChangeModel

use of com.gitblit.models.PathModel.PathChangeModel in project gitblit by gitblit.

the class JGitUtils method getFilesInRange.

/**
 * 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
 * @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;
}
Also used : PathChangeModel(com.gitblit.models.PathModel.PathChangeModel) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 4 with PathChangeModel

use of com.gitblit.models.PathModel.PathChangeModel in project gitblit by gitblit.

the class TicketNotifier method formatLastChange.

protected String formatLastChange(TicketModel ticket) {
    Change lastChange = ticket.changes.get(ticket.changes.size() - 1);
    UserModel user = getUserModel(lastChange.author);
    // define the fields we do NOT want to see in an email notification
    Set<TicketModel.Field> fieldExclusions = new HashSet<TicketModel.Field>();
    fieldExclusions.addAll(Arrays.asList(Field.watchers, Field.voters));
    StringBuilder sb = new StringBuilder();
    boolean newTicket = lastChange.isStatusChange() && Status.New == lastChange.getStatus();
    boolean isFastForward = true;
    List<RevCommit> commits = null;
    DiffStat diffstat = null;
    String pattern;
    if (lastChange.hasPatchset()) {
        // patchset uploaded
        Patchset patchset = lastChange.patchset;
        String base = "";
        // determine the changed paths
        Repository repo = null;
        try {
            repo = repositoryManager.getRepository(ticket.repository);
            if (patchset.isFF() && (patchset.rev > 1)) {
                // fast-forward update, just show the new data
                isFastForward = true;
                Patchset prev = ticket.getPatchset(patchset.number, patchset.rev - 1);
                base = prev.tip;
            } else {
                // proposal OR non-fast-forward update
                isFastForward = false;
                base = patchset.base;
            }
            diffstat = DiffUtils.getDiffStat(repo, base, patchset.tip);
            commits = JGitUtils.getRevLog(repo, base, patchset.tip);
        } catch (Exception e) {
            Logger.getLogger(getClass()).error("failed to get changed paths", e);
        } finally {
            if (repo != null) {
                repo.close();
            }
        }
        String compareUrl = ticketService.getCompareUrl(ticket, base, patchset.tip);
        if (newTicket) {
            // new proposal
            pattern = "**{0}** is proposing a change.";
            sb.append(MessageFormat.format(pattern, user.getDisplayName()));
            fieldExclusions.add(Field.status);
            fieldExclusions.add(Field.title);
            fieldExclusions.add(Field.body);
        } else {
            // describe the patchset
            if (patchset.isFF()) {
                pattern = "**{0}** added {1} {2} to patchset {3}.";
                sb.append(MessageFormat.format(pattern, user.getDisplayName(), patchset.added, patchset.added == 1 ? "commit" : "commits", patchset.number));
            } else {
                pattern = "**{0}** uploaded patchset {1}. *({2})*";
                sb.append(MessageFormat.format(pattern, user.getDisplayName(), patchset.number, patchset.type.toString().toUpperCase()));
            }
        }
        sb.append(HARD_BRK);
        sb.append(MessageFormat.format("{0} {1}, {2} {3}, <span style=\"color:darkgreen;\">+{4} insertions</span>, <span style=\"color:darkred;\">-{5} deletions</span> from {6}. [compare]({7})", commits.size(), commits.size() == 1 ? "commit" : "commits", diffstat.paths.size(), diffstat.paths.size() == 1 ? "file" : "files", diffstat.getInsertions(), diffstat.getDeletions(), isFastForward ? "previous revision" : "merge base", compareUrl));
        // note commit additions on a rebase,if any
        switch(lastChange.patchset.type) {
            case Rebase:
                if (lastChange.patchset.added > 0) {
                    sb.append(SOFT_BRK);
                    sb.append(MessageFormat.format("{0} {1} added.", lastChange.patchset.added, lastChange.patchset.added == 1 ? "commit" : "commits"));
                }
                break;
            default:
                break;
        }
        sb.append(HARD_BRK);
    } else if (lastChange.isStatusChange()) {
        if (newTicket) {
            fieldExclusions.add(Field.status);
            fieldExclusions.add(Field.title);
            fieldExclusions.add(Field.body);
            pattern = "**{0}** created this ticket.";
            sb.append(MessageFormat.format(pattern, user.getDisplayName()));
        } else if (lastChange.hasField(Field.mergeSha)) {
            // closed by merged
            pattern = "**{0}** closed this ticket by merging {1} to {2}.";
            // identify patchset that closed the ticket
            String merged = ticket.mergeSha;
            for (Patchset patchset : ticket.getPatchsets()) {
                if (patchset.tip.equals(ticket.mergeSha)) {
                    merged = patchset.toString();
                    break;
                }
            }
            sb.append(MessageFormat.format(pattern, user.getDisplayName(), merged, ticket.mergeTo));
        } else {
            // workflow status change by user
            pattern = "**{0}** changed the status of this ticket to **{1}**.";
            sb.append(MessageFormat.format(pattern, user.getDisplayName(), lastChange.getStatus().toString().toUpperCase()));
        }
        sb.append(HARD_BRK);
    } else if (lastChange.hasReview()) {
        // review
        Review review = lastChange.review;
        pattern = "**{0}** has reviewed patchset {1,number,0} revision {2,number,0}.";
        sb.append(MessageFormat.format(pattern, user.getDisplayName(), review.patchset, review.rev));
        sb.append(HARD_BRK);
        String d = settings.getString(Keys.web.datestampShortFormat, "yyyy-MM-dd");
        String t = settings.getString(Keys.web.timeFormat, "HH:mm");
        DateFormat df = new SimpleDateFormat(d + " " + t);
        List<Change> reviews = ticket.getReviews(ticket.getPatchset(review.patchset, review.rev));
        sb.append("| Date | Reviewer      | Score | Description  |\n");
        sb.append("| :--- | :------------ | :---: | :----------- |\n");
        for (Change change : reviews) {
            String name = change.author;
            UserModel u = userManager.getUserModel(change.author);
            if (u != null) {
                name = u.getDisplayName();
            }
            String score;
            switch(change.review.score) {
                case approved:
                    score = MessageFormat.format(addPattern, change.review.score.getValue());
                    break;
                case vetoed:
                    score = MessageFormat.format(delPattern, Math.abs(change.review.score.getValue()));
                    break;
                default:
                    score = "" + change.review.score.getValue();
            }
            String date = df.format(change.date);
            sb.append(String.format("| %1$s | %2$s | %3$s | %4$s |\n", date, name, score, change.review.score.toString()));
        }
        sb.append(HARD_BRK);
    } else if (lastChange.hasComment()) {
        // comment update
        sb.append(MessageFormat.format("**{0}** commented on this ticket.", user.getDisplayName()));
        sb.append(HARD_BRK);
    } else if (lastChange.hasReference()) {
        // reference update
        String type = "?";
        switch(lastChange.reference.getSourceType()) {
            case Commit:
                {
                    type = "commit";
                }
                break;
            case Ticket:
                {
                    type = "ticket";
                }
                break;
            default:
                {
                }
                break;
        }
        sb.append(MessageFormat.format("**{0}** referenced this ticket in {1} {2}", type, lastChange.toString()));
        sb.append(HARD_BRK);
    } else {
        // general update
        pattern = "**{0}** has updated this ticket.";
        sb.append(MessageFormat.format(pattern, user.getDisplayName()));
        sb.append(HARD_BRK);
    }
    // ticket link
    sb.append(MessageFormat.format("[view ticket {0,number,0}]({1})", ticket.number, ticketService.getTicketUrl(ticket)));
    sb.append(HARD_BRK);
    if (newTicket) {
        // ticket title
        sb.append(MessageFormat.format("### {0}", ticket.title));
        sb.append(HARD_BRK);
        // ticket description, on state change
        if (StringUtils.isEmpty(ticket.body)) {
            sb.append("<span style=\"color: #888;\">no description entered</span>");
        } else {
            sb.append(ticket.body);
        }
        sb.append(HARD_BRK);
        sb.append(HR);
    }
    // field changes
    if (lastChange.hasFieldChanges()) {
        Map<Field, String> filtered = new HashMap<Field, String>();
        for (Map.Entry<Field, String> fc : lastChange.fields.entrySet()) {
            if (!fieldExclusions.contains(fc.getKey())) {
                // field is included
                filtered.put(fc.getKey(), fc.getValue());
            }
        }
        // sort by field ordinal
        List<Field> fields = new ArrayList<Field>(filtered.keySet());
        Collections.sort(fields);
        if (filtered.size() > 0) {
            sb.append(HARD_BRK);
            sb.append("| Field Changes               ||\n");
            sb.append("| ------------: | :----------- |\n");
            for (Field field : fields) {
                String value;
                if (filtered.get(field) == null) {
                    value = "";
                } else {
                    value = filtered.get(field).replace("\r\n", "<br/>").replace("\n", "<br/>").replace("|", "&#124;");
                }
                sb.append(String.format("| **%1$s:** | %2$s |\n", field.name(), value));
            }
            sb.append(HARD_BRK);
        }
    }
    // new comment
    if (lastChange.hasComment()) {
        sb.append(HR);
        sb.append(lastChange.comment.text);
        sb.append(HARD_BRK);
    }
    // insert the patchset details and review instructions
    if (lastChange.hasPatchset() && ticket.isOpen()) {
        if (commits != null && commits.size() > 0) {
            // append the commit list
            String title = isFastForward ? "Commits added to previous patchset revision" : "All commits in patchset";
            sb.append(MessageFormat.format("| {0} |||\n", title));
            sb.append("| SHA | Author | Title |\n");
            sb.append("| :-- | :----- | :---- |\n");
            for (RevCommit commit : commits) {
                sb.append(MessageFormat.format("| {0} | {1} | {2} |\n", commit.getName(), commit.getAuthorIdent().getName(), StringUtils.trimString(commit.getShortMessage(), Constants.LEN_SHORTLOG).replace("|", "&#124;")));
            }
            sb.append(HARD_BRK);
        }
        if (diffstat != null) {
            // append the changed path list
            String title = isFastForward ? "Files changed since previous patchset revision" : "All files changed in patchset";
            sb.append(MessageFormat.format("| {0} |||\n", title));
            sb.append("| :-- | :----------- | :-: |\n");
            for (PathChangeModel path : diffstat.paths) {
                String add = MessageFormat.format(addPattern, path.insertions);
                String del = MessageFormat.format(delPattern, path.deletions);
                String diff = null;
                switch(path.changeType) {
                    case ADD:
                        diff = add;
                        break;
                    case DELETE:
                        diff = del;
                        break;
                    case MODIFY:
                        if (path.insertions > 0 && path.deletions > 0) {
                            // insertions & deletions
                            diff = add + "/" + del;
                        } else if (path.insertions > 0) {
                            // just insertions
                            diff = add;
                        } else {
                            // just deletions
                            diff = del;
                        }
                        break;
                    default:
                        diff = path.changeType.name();
                        break;
                }
                sb.append(MessageFormat.format("| {0} | {1} | {2} |\n", getChangeType(path.changeType), path.name, diff));
            }
            sb.append(HARD_BRK);
        }
        sb.append(formatPatchsetInstructions(ticket, lastChange.patchset));
    }
    return sb.toString();
}
Also used : HashMap(java.util.HashMap) DiffStat(com.gitblit.utils.DiffUtils.DiffStat) ArrayList(java.util.ArrayList) Patchset(com.gitblit.models.TicketModel.Patchset) Review(com.gitblit.models.TicketModel.Review) UserModel(com.gitblit.models.UserModel) Field(com.gitblit.models.TicketModel.Field) HashSet(java.util.HashSet) RevCommit(org.eclipse.jgit.revwalk.RevCommit) PathChangeModel(com.gitblit.models.PathModel.PathChangeModel) TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change) IOException(java.io.IOException) Repository(org.eclipse.jgit.lib.Repository) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 5 with PathChangeModel

use of com.gitblit.models.PathModel.PathChangeModel in project gitblit by gitblit.

the class JGitUtils method getFilesInCommit.

/**
 * 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
 * @param commit
 *            if null, HEAD is assumed.
 * @param calculateDiffStat
 *            if true, each PathChangeModel will have insertions/deletions
 * @return list of files changed in a commit
 */
public static List<PathChangeModel> getFilesInCommit(Repository repository, RevCommit commit, boolean calculateDiffStat) {
    List<PathChangeModel> list = new ArrayList<PathChangeModel>();
    if (!hasCommits(repository)) {
        return list;
    }
    RevWalk rw = new RevWalk(repository);
    try {
        if (commit == null) {
            ObjectId object = getDefaultBranch(repository);
            commit = rw.parseCommit(object);
        }
        if (commit.getParentCount() == 0) {
            TreeWalk tw = new TreeWalk(repository);
            tw.reset();
            tw.setRecursive(true);
            tw.addTree(commit.getTree());
            while (tw.next()) {
                long size = 0;
                FilestoreModel filestoreItem = null;
                ObjectId objectId = tw.getObjectId(0);
                try {
                    if (!tw.isSubtree() && (tw.getFileMode(0) != FileMode.GITLINK)) {
                        size = tw.getObjectReader().getObjectSize(objectId, Constants.OBJ_BLOB);
                        if (isPossibleFilestoreItem(size)) {
                            filestoreItem = getFilestoreItem(tw.getObjectReader().open(objectId));
                        }
                    }
                } catch (Throwable t) {
                    error(t, null, "failed to retrieve blob size for " + tw.getPathString());
                }
                list.add(new PathChangeModel(tw.getPathString(), tw.getPathString(), filestoreItem, size, tw.getRawMode(0), objectId.getName(), commit.getId().getName(), ChangeType.ADD));
            }
            tw.close();
        } else {
            RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
            DiffStatFormatter df = new DiffStatFormatter(commit.getName(), repository);
            df.setRepository(repository);
            df.setDiffComparator(RawTextComparator.DEFAULT);
            df.setDetectRenames(true);
            List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree());
            for (DiffEntry diff : diffs) {
                // create the path change model
                PathChangeModel pcm = PathChangeModel.from(diff, commit.getName(), repository);
                if (calculateDiffStat) {
                    // update file diffstats
                    df.format(diff);
                    PathChangeModel pathStat = df.getDiffStat().getPath(pcm.path);
                    if (pathStat != null) {
                        pcm.insertions = pathStat.insertions;
                        pcm.deletions = pathStat.deletions;
                    }
                }
                list.add(pcm);
            }
        }
    } catch (Throwable t) {
        error(t, repository, "{0} failed to determine files in commit!");
    } finally {
        rw.dispose();
    }
    return list;
}
Also used : PathChangeModel(com.gitblit.models.PathModel.PathChangeModel) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) FilestoreModel(com.gitblit.models.FilestoreModel) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Aggregations

PathChangeModel (com.gitblit.models.PathModel.PathChangeModel)9 ArrayList (java.util.ArrayList)7 RevCommit (org.eclipse.jgit.revwalk.RevCommit)6 IOException (java.io.IOException)4 UserModel (com.gitblit.models.UserModel)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 Repository (org.eclipse.jgit.lib.Repository)3 RefModel (com.gitblit.models.RefModel)2 TicketModel (com.gitblit.models.TicketModel)2 Change (com.gitblit.models.TicketModel.Change)2 Patchset (com.gitblit.models.TicketModel.Patchset)2 Review (com.gitblit.models.TicketModel.Review)2 HashSet (java.util.HashSet)2 ConcurrentRefUpdateException (org.eclipse.jgit.api.errors.ConcurrentRefUpdateException)2 DiffEntry (org.eclipse.jgit.diff.DiffEntry)2 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)2 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 ReceiveCommandEvent (com.gitblit.git.ReceiveCommandEvent)1 FilestoreModel (com.gitblit.models.FilestoreModel)1 RefLogEntry (com.gitblit.models.RefLogEntry)1