Search in sources :

Example 1 with RefLogEntry

use of com.gitblit.models.RefLogEntry in project gitblit by gitblit.

the class PushLogTest method testPushLog.

@Test
public void testPushLog() throws IOException {
    String name = "~james/helloworld.git";
    File gitDir = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, name), FS.DETECTED);
    Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
    List<RefLogEntry> pushes = RefLogUtils.getRefLog(name, repository);
    GitBlitSuite.close(repository);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) RefLogEntry(com.gitblit.models.RefLogEntry) Test(org.junit.Test)

Example 2 with RefLogEntry

use of com.gitblit.models.RefLogEntry in project gitblit by gitblit.

the class ReflogPanel method setup.

protected void setup(List<RefLogEntry> changes) {
    ListDataProvider<RefLogEntry> dp = new ListDataProvider<RefLogEntry>(changes);
    DataView<RefLogEntry> changeView = new DataView<RefLogEntry>("change", dp) {

        private static final long serialVersionUID = 1L;

        @Override
        public void populateItem(final Item<RefLogEntry> changeItem) {
            final RefLogEntry change = changeItem.getModelObject();
            String dateFormat = app().settings().getString(Keys.web.datetimestampLongFormat, "EEEE, MMMM d, yyyy HH:mm Z");
            TimeZone timezone = getTimeZone();
            DateFormat df = new SimpleDateFormat(dateFormat);
            df.setTimeZone(timezone);
            Calendar cal = Calendar.getInstance(timezone);
            String fullRefName = change.getChangedRefs().get(0);
            String shortRefName = fullRefName;
            String ticketId = null;
            boolean isTag = false;
            boolean isTicket = false;
            if (shortRefName.startsWith(Constants.R_TICKET)) {
                ticketId = fullRefName.substring(Constants.R_TICKET.length());
                shortRefName = MessageFormat.format(getString("gb.ticketN"), ticketId);
                isTicket = true;
            } else if (shortRefName.startsWith(Constants.R_HEADS)) {
                shortRefName = shortRefName.substring(Constants.R_HEADS.length());
            } else if (shortRefName.startsWith(Constants.R_TAGS)) {
                shortRefName = shortRefName.substring(Constants.R_TAGS.length());
                isTag = true;
            }
            String fuzzydate;
            TimeUtils tu = getTimeUtils();
            Date changeDate = change.date;
            if (TimeUtils.isToday(changeDate, timezone)) {
                fuzzydate = tu.today();
            } else if (TimeUtils.isYesterday(changeDate, timezone)) {
                fuzzydate = tu.yesterday();
            } else {
                // calculate a fuzzy time ago date
                cal.setTime(changeDate);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);
                Date date = cal.getTime();
                fuzzydate = getTimeUtils().timeAgo(date);
            }
            changeItem.add(new Label("whenChanged", fuzzydate + ", " + df.format(changeDate)));
            Label changeIcon = new Label("changeIcon");
            if (Type.DELETE.equals(change.getChangeType(fullRefName))) {
                WicketUtils.setCssClass(changeIcon, "iconic-trash-stroke");
            } else if (isTag) {
                WicketUtils.setCssClass(changeIcon, "iconic-tag");
            } else if (isTicket) {
                WicketUtils.setCssClass(changeIcon, "fa fa-ticket");
            } else {
                WicketUtils.setCssClass(changeIcon, "iconic-upload");
            }
            changeItem.add(changeIcon);
            if (change.user.username.equals(change.user.emailAddress) && change.user.emailAddress.indexOf('@') > -1) {
                // username is an email address - 1.2.1 push log bug
                changeItem.add(new Label("whoChanged", change.user.getDisplayName()));
            } else if (change.user.username.equals(UserModel.ANONYMOUS.username)) {
                // anonymous change
                changeItem.add(new Label("whoChanged", getString("gb.anonymousUser")));
            } else {
                // link to user account page
                changeItem.add(new LinkPanel("whoChanged", null, change.user.getDisplayName(), UserPage.class, WicketUtils.newUsernameParameter(change.user.username)));
            }
            boolean isDelete = false;
            boolean isRewind = false;
            String what;
            String by = null;
            switch(change.getChangeType(fullRefName)) {
                case CREATE:
                    if (isTag) {
                        // new tag
                        what = getString("gb.pushedNewTag");
                    } else {
                        // new branch
                        what = getString("gb.pushedNewBranch");
                    }
                    break;
                case DELETE:
                    isDelete = true;
                    if (isTag) {
                        what = getString("gb.deletedTag");
                    } else {
                        what = getString("gb.deletedBranch");
                    }
                    break;
                case UPDATE_NONFASTFORWARD:
                    isRewind = true;
                default:
                    what = MessageFormat.format(change.getCommitCount() > 1 ? getString("gb.pushedNCommitsTo") : getString("gb.pushedOneCommitTo"), change.getCommitCount());
                    if (change.getAuthorCount() == 1) {
                        by = MessageFormat.format(getString("gb.byOneAuthor"), change.getAuthorIdent().getName());
                    } else {
                        by = MessageFormat.format(getString("gb.byNAuthors"), change.getAuthorCount());
                    }
                    break;
            }
            changeItem.add(new Label("whatChanged", what));
            changeItem.add(new Label("byAuthors", by).setVisible(!StringUtils.isEmpty(by)));
            changeItem.add(new Label("refRewind", getString("gb.rewind")).setVisible(isRewind));
            if (isDelete) {
                // can't link to deleted ref
                changeItem.add(new Label("refChanged", shortRefName));
            } else if (isTag) {
                // link to tag
                changeItem.add(new LinkPanel("refChanged", null, shortRefName, TagPage.class, WicketUtils.newObjectParameter(change.repository, fullRefName)));
            } else if (isTicket) {
                // link to ticket
                changeItem.add(new LinkPanel("refChanged", null, shortRefName, TicketsPage.class, WicketUtils.newObjectParameter(change.repository, ticketId)));
            } else {
                // link to tree
                changeItem.add(new LinkPanel("refChanged", null, shortRefName, TreePage.class, WicketUtils.newObjectParameter(change.repository, fullRefName)));
            }
            int maxCommitCount = 5;
            List<RepositoryCommit> commits = change.getCommits();
            if (commits.size() > maxCommitCount) {
                commits = new ArrayList<RepositoryCommit>(commits.subList(0, maxCommitCount));
            }
            // compare link
            String compareLinkText = null;
            if ((change.getCommitCount() <= maxCommitCount) && (change.getCommitCount() > 1)) {
                compareLinkText = MessageFormat.format(getString("gb.viewComparison"), commits.size());
            } else if (change.getCommitCount() > maxCommitCount) {
                int diff = change.getCommitCount() - maxCommitCount;
                compareLinkText = MessageFormat.format(diff > 1 ? getString("gb.nMoreCommits") : getString("gb.oneMoreCommit"), diff);
            }
            if (StringUtils.isEmpty(compareLinkText)) {
                changeItem.add(new Label("compareLink").setVisible(false));
            } else {
                String endRangeId = change.getNewId(fullRefName);
                String startRangeId = change.getOldId(fullRefName);
                changeItem.add(new LinkPanel("compareLink", null, compareLinkText, ComparePage.class, WicketUtils.newRangeParameter(change.repository, startRangeId, endRangeId)));
            }
            ListDataProvider<RepositoryCommit> cdp = new ListDataProvider<RepositoryCommit>(commits);
            DataView<RepositoryCommit> commitsView = new DataView<RepositoryCommit>("commit", cdp) {

                private static final long serialVersionUID = 1L;

                @Override
                public void populateItem(final Item<RepositoryCommit> commitItem) {
                    final RepositoryCommit commit = commitItem.getModelObject();
                    // author gravatar
                    commitItem.add(new AvatarImage("commitAuthor", commit.getAuthorIdent(), null, 16, false));
                    // merge icon
                    if (commit.getParentCount() > 1) {
                        commitItem.add(WicketUtils.newImage("commitIcon", "commit_merge_16x16.png"));
                    } else {
                        commitItem.add(WicketUtils.newBlankImage("commitIcon"));
                    }
                    // short message
                    String shortMessage = commit.getShortMessage();
                    String trimmedMessage = shortMessage;
                    if (commit.getRefs() != null && commit.getRefs().size() > 0) {
                        trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG_REFS);
                    } else {
                        trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG);
                    }
                    LinkPanel shortlog = new LinkPanel("commitShortMessage", "list", trimmedMessage, CommitPage.class, WicketUtils.newObjectParameter(change.repository, commit.getName()));
                    if (!shortMessage.equals(trimmedMessage)) {
                        WicketUtils.setHtmlTooltip(shortlog, shortMessage);
                    }
                    commitItem.add(shortlog);
                    // commit hash link
                    int hashLen = app().settings().getInteger(Keys.web.shortCommitIdLength, 6);
                    LinkPanel commitHash = new LinkPanel("hashLink", null, commit.getName().substring(0, hashLen), CommitPage.class, WicketUtils.newObjectParameter(change.repository, commit.getName()));
                    WicketUtils.setCssClass(commitHash, "shortsha1");
                    WicketUtils.setHtmlTooltip(commitHash, commit.getName());
                    commitItem.add(commitHash);
                }
            };
            changeItem.add(commitsView);
        }
    };
    add(changeView);
}
Also used : ListDataProvider(org.apache.wicket.markup.repeater.data.ListDataProvider) Label(org.apache.wicket.markup.html.basic.Label) RefLogEntry(com.gitblit.models.RefLogEntry) TimeUtils(com.gitblit.utils.TimeUtils) Item(org.apache.wicket.markup.repeater.Item) TreePage(com.gitblit.wicket.pages.TreePage) Calendar(java.util.Calendar) ComparePage(com.gitblit.wicket.pages.ComparePage) RepositoryCommit(com.gitblit.models.RepositoryCommit) Date(java.util.Date) DataView(org.apache.wicket.markup.repeater.data.DataView) TimeZone(java.util.TimeZone) TicketsPage(com.gitblit.wicket.pages.TicketsPage) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with RefLogEntry

use of com.gitblit.models.RefLogEntry in project gitblit by gitblit.

the class DashboardPage method addCharts.

/**
 * Creates the daily activity line chart, the active repositories pie chart,
 * and the active authors pie chart
 *
 * @param recentChanges
 * @param authorExclusions
 * @param daysBack
 */
protected void addCharts(Fragment frag, List<DailyLogEntry> recentChanges, Set<String> authorExclusions, int daysBack) {
    // activity metrics
    Map<String, Metric> repositoryMetrics = new HashMap<String, Metric>();
    Map<String, Metric> authorMetrics = new HashMap<String, Metric>();
    // aggregate repository and author metrics
    int totalCommits = 0;
    for (RefLogEntry change : recentChanges) {
        // aggregate repository metrics
        String repository = StringUtils.stripDotGit(change.repository);
        if (!repositoryMetrics.containsKey(repository)) {
            repositoryMetrics.put(repository, new Metric(repository));
        }
        repositoryMetrics.get(repository).count += 1;
        for (RepositoryCommit commit : change.getCommits()) {
            totalCommits++;
            String author = StringUtils.removeNewlines(commit.getAuthorIdent().getName());
            String authorName = author.toLowerCase();
            String authorEmail = StringUtils.removeNewlines(commit.getAuthorIdent().getEmailAddress()).toLowerCase();
            if (!authorExclusions.contains(authorName) && !authorExclusions.contains(authorEmail)) {
                if (!authorMetrics.containsKey(author)) {
                    authorMetrics.put(author, new Metric(author));
                }
                authorMetrics.get(author).count += 1;
            }
        }
    }
    String headerPattern;
    if (daysBack == 1) {
        // today
        if (totalCommits == 0) {
            headerPattern = getString("gb.todaysActivityNone");
        } else {
            headerPattern = getString("gb.todaysActivityStats");
        }
    } else {
        // multiple days
        if (totalCommits == 0) {
            headerPattern = getString("gb.recentActivityNone");
        } else {
            headerPattern = getString("gb.recentActivityStats");
        }
    }
    frag.add(new Label("feedheader", MessageFormat.format(headerPattern, daysBack, totalCommits, authorMetrics.size())));
    if (app().settings().getBoolean(Keys.web.generateActivityGraph, true)) {
        // build google charts
        Charts charts = new Flotr2Charts();
        // active repositories pie chart
        Chart chart = charts.createPieChart("chartRepositories", getString("gb.activeRepositories"), getString("gb.repository"), getString("gb.commits"));
        for (Metric metric : repositoryMetrics.values()) {
            chart.addValue(metric.name, metric.count);
        }
        chart.setShowLegend(false);
        String url = urlFor(SummaryPage.class, null).toString() + "?r=";
        chart.setClickUrl(url);
        charts.addChart(chart);
        // active authors pie chart
        chart = charts.createPieChart("chartAuthors", getString("gb.activeAuthors"), getString("gb.author"), getString("gb.commits"));
        for (Metric metric : authorMetrics.values()) {
            chart.addValue(metric.name, metric.count);
        }
        chart.setShowLegend(false);
        charts.addChart(chart);
        add(new HeaderContributor(charts));
        frag.add(new Fragment("charts", "chartsFragment", this));
    } else {
        frag.add(new Label("charts").setVisible(false));
    }
}
Also used : Flotr2Charts(com.gitblit.wicket.charting.Flotr2Charts) HashMap(java.util.HashMap) Label(org.apache.wicket.markup.html.basic.Label) Charts(com.gitblit.wicket.charting.Charts) Flotr2Charts(com.gitblit.wicket.charting.Flotr2Charts) RepositoryCommit(com.gitblit.models.RepositoryCommit) Fragment(org.apache.wicket.markup.html.panel.Fragment) RefLogEntry(com.gitblit.models.RefLogEntry) HeaderContributor(org.apache.wicket.behavior.HeaderContributor) Metric(com.gitblit.models.Metric) Chart(com.gitblit.wicket.charting.Chart)

Example 4 with RefLogEntry

use of com.gitblit.models.RefLogEntry in project gitblit by gitblit.

the class RefLogUtils method getRefLog.

/**
 * Returns the list of reflog entries as they were recorded by Gitblit.
 * Each RefLogEntry may represent multiple ref updates.
 *
 * @param repositoryName
 * @param repository
 * @param minimumDate
 * @param offset
 * @param maxCount
 * 			if < 0, all entries are returned.
 * @return a list of reflog entries
 */
public static List<RefLogEntry> getRefLog(String repositoryName, Repository repository, Date minimumDate, int offset, int maxCount) {
    List<RefLogEntry> list = new ArrayList<RefLogEntry>();
    RefModel ref = getRefLogBranch(repository);
    if (ref == null) {
        return list;
    }
    if (maxCount == 0) {
        return list;
    }
    Map<ObjectId, List<RefModel>> allRefs = JGitUtils.getAllRefs(repository);
    List<RevCommit> pushes;
    if (minimumDate == null) {
        pushes = JGitUtils.getRevLog(repository, GB_REFLOG, offset, maxCount);
    } else {
        pushes = JGitUtils.getRevLog(repository, GB_REFLOG, minimumDate);
    }
    for (RevCommit push : pushes) {
        if (push.getAuthorIdent().getName().equalsIgnoreCase("gitblit")) {
            // skip gitblit/internal commits
            continue;
        }
        UserModel user = newUserModelFrom(push.getAuthorIdent());
        Date date = push.getAuthorIdent().getWhen();
        RefLogEntry log = new RefLogEntry(repositoryName, date, user);
        // only report HEADS and TAGS for now
        List<PathChangeModel> changedRefs = new ArrayList<PathChangeModel>();
        for (PathChangeModel refChange : JGitUtils.getFilesInCommit(repository, push)) {
            if (refChange.path.startsWith(Constants.R_HEADS) || refChange.path.startsWith(Constants.R_TAGS)) {
                changedRefs.add(refChange);
            }
        }
        if (changedRefs.isEmpty()) {
            // skip empty commits
            continue;
        }
        list.add(log);
        for (PathChangeModel change : changedRefs) {
            switch(change.changeType) {
                case DELETE:
                    log.updateRef(change.path, ReceiveCommand.Type.DELETE);
                    break;
                default:
                    String content = JGitUtils.getStringContent(repository, push.getTree(), change.path);
                    String[] fields = content.split(" ");
                    String oldId = fields[1];
                    String newId = fields[2];
                    log.updateRef(change.path, ReceiveCommand.Type.valueOf(fields[0]), oldId, newId);
                    if (ObjectId.zeroId().getName().equals(newId)) {
                        // ref deletion
                        continue;
                    }
                    try {
                        List<RevCommit> pushedCommits = JGitUtils.getRevLog(repository, oldId, newId);
                        for (RevCommit pushedCommit : pushedCommits) {
                            RepositoryCommit repoCommit = log.addCommit(change.path, pushedCommit);
                            if (repoCommit != null) {
                                repoCommit.setRefs(allRefs.get(pushedCommit.getId()));
                            }
                        }
                    } catch (Exception e) {
                    }
            }
        }
    }
    Collections.sort(list);
    return list;
}
Also used : RefModel(com.gitblit.models.RefModel) ObjectId(org.eclipse.jgit.lib.ObjectId) PathChangeModel(com.gitblit.models.PathModel.PathChangeModel) ArrayList(java.util.ArrayList) RepositoryCommit(com.gitblit.models.RepositoryCommit) RefLogEntry(com.gitblit.models.RefLogEntry) Date(java.util.Date) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException) IOException(java.io.IOException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) UserModel(com.gitblit.models.UserModel) ArrayList(java.util.ArrayList) List(java.util.List) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 5 with RefLogEntry

use of com.gitblit.models.RefLogEntry in project gitblit by gitblit.

the class RefLogUtils method getLogByRef.

/**
 * Returns the list of ref changes separated by ref (e.g. each ref has it's own
 * RefLogEntry object).
 *
 * @param repositoryName
 * @param repository
 * @param minimumDate
 * @return a list of ref log entries separated by ref
 */
public static List<RefLogEntry> getLogByRef(String repositoryName, Repository repository, Date minimumDate) {
    // break the reflog into refs and then merge them back into a list
    Map<String, List<RefLogEntry>> refMap = new HashMap<String, List<RefLogEntry>>();
    List<RefLogEntry> entries = getRefLog(repositoryName, repository, minimumDate);
    for (RefLogEntry entry : entries) {
        for (String ref : entry.getChangedRefs()) {
            if (!refMap.containsKey(ref)) {
                refMap.put(ref, new ArrayList<RefLogEntry>());
            }
            // construct new ref-specific log entry
            RefLogEntry refPush = new RefLogEntry(entry.repository, entry.date, entry.user);
            refPush.updateRef(ref, entry.getChangeType(ref), entry.getOldId(ref), entry.getNewId(ref));
            refPush.addCommits(entry.getCommits(ref));
            refMap.get(ref).add(refPush);
        }
    }
    // merge individual ref entries into master list
    List<RefLogEntry> refLog = new ArrayList<RefLogEntry>();
    for (List<RefLogEntry> entry : refMap.values()) {
        refLog.addAll(entry);
    }
    Collections.sort(refLog);
    return refLog;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) RefLogEntry(com.gitblit.models.RefLogEntry)

Aggregations

RefLogEntry (com.gitblit.models.RefLogEntry)8 RepositoryCommit (com.gitblit.models.RepositoryCommit)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Date (java.util.Date)3 DailyLogEntry (com.gitblit.models.DailyLogEntry)2 RefModel (com.gitblit.models.RefModel)2 UserModel (com.gitblit.models.UserModel)2 File (java.io.File)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Label (org.apache.wicket.markup.html.basic.Label)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 Repository (org.eclipse.jgit.lib.Repository)2 FileRepositoryBuilder (org.eclipse.jgit.storage.file.FileRepositoryBuilder)2 Test (org.junit.Test)2 Metric (com.gitblit.models.Metric)1 PathChangeModel (com.gitblit.models.PathModel.PathChangeModel)1 TimeUtils (com.gitblit.utils.TimeUtils)1