Search in sources :

Example 16 with RefModel

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

the class JGitUtils method getNotesOnCommit.

/**
	 * Returns the list of notes entered about the commit from the refs/notes
	 * namespace. If the repository does not exist or is empty, an empty list is
	 * returned.
	 *
	 * @param repository
	 * @param commit
	 * @return list of notes
	 */
public static List<GitNote> getNotesOnCommit(Repository repository, RevCommit commit) {
    List<GitNote> list = new ArrayList<GitNote>();
    if (!hasCommits(repository)) {
        return list;
    }
    List<RefModel> noteBranches = getNoteBranches(repository, true, -1);
    for (RefModel notesRef : noteBranches) {
        RevTree notesTree = JGitUtils.getCommit(repository, notesRef.getName()).getTree();
        // flat notes list
        String notePath = commit.getName();
        String text = getStringContent(repository, notesTree, notePath);
        if (!StringUtils.isEmpty(text)) {
            List<RevCommit> history = getRevLog(repository, notesRef.getName(), notePath, 0, -1);
            RefModel noteRef = new RefModel(notesRef.displayName, null, history.get(history.size() - 1));
            GitNote gitNote = new GitNote(noteRef, text);
            list.add(gitNote);
            continue;
        }
        // folder structure
        StringBuilder sb = new StringBuilder(commit.getName());
        sb.insert(2, '/');
        notePath = sb.toString();
        text = getStringContent(repository, notesTree, notePath);
        if (!StringUtils.isEmpty(text)) {
            List<RevCommit> history = getRevLog(repository, notesRef.getName(), notePath, 0, -1);
            RefModel noteRef = new RefModel(notesRef.displayName, null, history.get(history.size() - 1));
            GitNote gitNote = new GitNote(noteRef, text);
            list.add(gitNote);
        }
    }
    return list;
}
Also used : RefModel(com.gitblit.models.RefModel) ArrayList(java.util.ArrayList) GitNote(com.gitblit.models.GitNote) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 17 with RefModel

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

the class JGitUtils method getAllRefs.

/**
	 * Returns all refs grouped by their associated object id.
	 *
	 * @param repository
	 * @param includeRemoteRefs
	 * @return all refs grouped by their referenced object id
	 */
public static Map<ObjectId, List<RefModel>> getAllRefs(Repository repository, boolean includeRemoteRefs) {
    List<RefModel> list = getRefs(repository, org.eclipse.jgit.lib.RefDatabase.ALL, true, -1);
    Map<ObjectId, List<RefModel>> refs = new HashMap<ObjectId, List<RefModel>>();
    for (RefModel ref : list) {
        if (!includeRemoteRefs && ref.getName().startsWith(Constants.R_REMOTES)) {
            continue;
        }
        ObjectId objectid = ref.getReferencedObjectId();
        if (!refs.containsKey(objectid)) {
            refs.put(objectid, new ArrayList<RefModel>());
        }
        refs.get(objectid).add(ref);
    }
    return refs;
}
Also used : RefModel(com.gitblit.models.RefModel) HashMap(java.util.HashMap) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) List(java.util.List) ArrayList(java.util.ArrayList)

Example 18 with RefModel

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

the class JGitUtils method getRefs.

/**
	 * Returns a list of references in the repository matching "refs". If the
	 * repository is null or empty, an empty list is returned.
	 *
	 * @param repository
	 * @param refs
	 *            if unspecified, all refs are returned
	 * @param fullName
	 *            if true, /refs/something/yadayadayada is returned. If false,
	 *            yadayadayada is returned.
	 * @param maxCount
	 *            if < 0, all references are returned
	 * @param offset
	 *            if maxCount provided sets the starting point of the records to return
	 * @return list of references
	 */
private static List<RefModel> getRefs(Repository repository, String refs, boolean fullName, int maxCount, int offset) {
    List<RefModel> list = new ArrayList<RefModel>();
    if (maxCount == 0) {
        return list;
    }
    if (!hasCommits(repository)) {
        return list;
    }
    try {
        Map<String, Ref> map = repository.getRefDatabase().getRefs(refs);
        RevWalk rw = new RevWalk(repository);
        for (Entry<String, Ref> entry : map.entrySet()) {
            Ref ref = entry.getValue();
            RevObject object = rw.parseAny(ref.getObjectId());
            String name = entry.getKey();
            if (fullName && !StringUtils.isEmpty(refs)) {
                name = refs + name;
            }
            list.add(new RefModel(name, ref, object));
        }
        rw.dispose();
        Collections.sort(list);
        Collections.reverse(list);
        if (maxCount > 0 && list.size() > maxCount) {
            if (offset < 0) {
                offset = 0;
            }
            int endIndex = offset + maxCount;
            if (endIndex > list.size()) {
                endIndex = list.size();
            }
            list = new ArrayList<RefModel>(list.subList(offset, endIndex));
        }
    } catch (IOException e) {
        error(e, repository, "{0} failed to retrieve {1}", refs);
    }
    return list;
}
Also used : Ref(org.eclipse.jgit.lib.Ref) RefModel(com.gitblit.models.RefModel) RevObject(org.eclipse.jgit.revwalk.RevObject) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 19 with RefModel

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

the class MetricUtils method getDateMetrics.

/**
	 * Returns the list of metrics for the specified commit reference, branch,
	 * or tag within the repository. If includeTotal is true, the total of all
	 * the metrics will be included as the first element in the returned list.
	 *
	 * If the dateformat is unspecified an attempt is made to determine an
	 * appropriate date format by determining the time difference between the
	 * first commit on the branch and the most recent commit. This assumes that
	 * the commits are linear.
	 *
	 * @param repository
	 * @param objectId
	 *            if null or empty, HEAD is assumed.
	 * @param includeTotal
	 * @param dateFormat
	 * @param timezone
	 * @return list of metrics
	 */
public static List<Metric> getDateMetrics(Repository repository, String objectId, boolean includeTotal, String dateFormat, TimeZone timezone) {
    Metric total = new Metric("TOTAL");
    final Map<String, Metric> metricMap = new HashMap<String, Metric>();
    if (JGitUtils.hasCommits(repository)) {
        final List<RefModel> tags = JGitUtils.getTags(repository, true, -1);
        final Map<ObjectId, RefModel> tagMap = new HashMap<ObjectId, RefModel>();
        for (RefModel tag : tags) {
            tagMap.put(tag.getReferencedObjectId(), tag);
        }
        RevWalk revWalk = null;
        try {
            // resolve branch
            ObjectId branchObject;
            if (StringUtils.isEmpty(objectId)) {
                branchObject = JGitUtils.getDefaultBranch(repository);
            } else {
                branchObject = repository.resolve(objectId);
            }
            revWalk = new RevWalk(repository);
            RevCommit lastCommit = revWalk.parseCommit(branchObject);
            revWalk.markStart(lastCommit);
            DateFormat df;
            if (StringUtils.isEmpty(dateFormat)) {
                // dynamically determine date format
                RevCommit firstCommit = JGitUtils.getFirstCommit(repository, branchObject.getName());
                int diffDays = (lastCommit.getCommitTime() - firstCommit.getCommitTime()) / (60 * 60 * 24);
                total.duration = diffDays;
                if (diffDays <= 365) {
                    // Days
                    df = new SimpleDateFormat("yyyy-MM-dd");
                } else {
                    // Months
                    df = new SimpleDateFormat("yyyy-MM");
                }
            } else {
                // use specified date format
                df = new SimpleDateFormat(dateFormat);
            }
            df.setTimeZone(timezone);
            Iterable<RevCommit> revlog = revWalk;
            for (RevCommit rev : revlog) {
                Date d = JGitUtils.getAuthorDate(rev);
                String p = df.format(d);
                if (!metricMap.containsKey(p)) {
                    metricMap.put(p, new Metric(p));
                }
                Metric m = metricMap.get(p);
                m.count++;
                total.count++;
                if (tagMap.containsKey(rev.getId())) {
                    m.tag++;
                    total.tag++;
                }
            }
        } catch (Throwable t) {
            error(t, repository, "{0} failed to mine log history for date metrics of {1}", objectId);
        } finally {
            if (revWalk != null) {
                revWalk.dispose();
            }
        }
    }
    List<String> keys = new ArrayList<String>(metricMap.keySet());
    Collections.sort(keys);
    List<Metric> metrics = new ArrayList<Metric>();
    for (String key : keys) {
        metrics.add(metricMap.get(key));
    }
    if (includeTotal) {
        metrics.add(0, total);
    }
    return metrics;
}
Also used : RefModel(com.gitblit.models.RefModel) HashMap(java.util.HashMap) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Metric(com.gitblit.models.Metric) SimpleDateFormat(java.text.SimpleDateFormat) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 20 with RefModel

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

the class RefLogUtils method getDailyLog.

/**
     * Returns a commit log grouped by day.
     *
     * @param repositoryName
     * @param repository
     * @param minimumDate
     * @param offset
     * @param maxCount
     * 			if < 0, all entries are returned.
     * @param the timezone to use when aggregating commits by date
     * @return a list of grouped commit log entries
     */
public static List<DailyLogEntry> getDailyLog(String repositoryName, Repository repository, Date minimumDate, int offset, int maxCount, TimeZone timezone) {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    df.setTimeZone(timezone);
    Map<ObjectId, List<RefModel>> allRefs = JGitUtils.getAllRefs(repository);
    Map<String, DailyLogEntry> tags = new HashMap<String, DailyLogEntry>();
    Map<String, DailyLogEntry> pulls = new HashMap<String, DailyLogEntry>();
    Map<String, DailyLogEntry> dailydigests = new HashMap<String, DailyLogEntry>();
    String linearParent = null;
    for (RefModel local : JGitUtils.getLocalBranches(repository, true, -1)) {
        if (!local.getDate().after(minimumDate)) {
            // branch not recently updated
            continue;
        }
        String branch = local.getName();
        List<RepositoryCommit> commits = CommitCache.instance().getCommits(repositoryName, repository, branch, minimumDate);
        linearParent = null;
        for (RepositoryCommit commit : commits) {
            if (linearParent != null) {
                if (!commit.getName().equals(linearParent)) {
                    // only follow linear branch commits
                    continue;
                }
            }
            Date date = commit.getCommitDate();
            String dateStr = df.format(date);
            if (!dailydigests.containsKey(dateStr)) {
                dailydigests.put(dateStr, new DailyLogEntry(repositoryName, date));
            }
            DailyLogEntry digest = dailydigests.get(dateStr);
            if (commit.getParentCount() == 0) {
                linearParent = null;
                digest.updateRef(branch, ReceiveCommand.Type.CREATE);
            } else {
                linearParent = commit.getParents()[0].getId().getName();
                digest.updateRef(branch, ReceiveCommand.Type.UPDATE, linearParent, commit.getName());
            }
            RepositoryCommit repoCommit = digest.addCommit(commit);
            if (repoCommit != null) {
                List<RefModel> matchedRefs = allRefs.get(commit.getId());
                repoCommit.setRefs(matchedRefs);
                if (!ArrayUtils.isEmpty(matchedRefs)) {
                    for (RefModel ref : matchedRefs) {
                        if (ref.getName().startsWith(Constants.R_TAGS)) {
                            // treat tags as special events in the log
                            if (!tags.containsKey(dateStr)) {
                                UserModel tagUser = newUserModelFrom(ref.getAuthorIdent());
                                Date tagDate = commit.getAuthorIdent().getWhen();
                                tags.put(dateStr, new DailyLogEntry(repositoryName, tagDate, tagUser));
                            }
                            RefLogEntry tagEntry = tags.get(dateStr);
                            tagEntry.updateRef(ref.getName(), ReceiveCommand.Type.CREATE);
                            RepositoryCommit rc = repoCommit.clone(ref.getName());
                            tagEntry.addCommit(rc);
                        } else if (ref.getName().startsWith(Constants.R_PULL)) {
                            // treat pull requests as special events in the log
                            if (!pulls.containsKey(dateStr)) {
                                UserModel commitUser = newUserModelFrom(ref.getAuthorIdent());
                                Date commitDate = commit.getAuthorIdent().getWhen();
                                pulls.put(dateStr, new DailyLogEntry(repositoryName, commitDate, commitUser));
                            }
                            RefLogEntry pullEntry = pulls.get(dateStr);
                            pullEntry.updateRef(ref.getName(), ReceiveCommand.Type.CREATE);
                            RepositoryCommit rc = repoCommit.clone(ref.getName());
                            pullEntry.addCommit(rc);
                        }
                    }
                }
            }
        }
    }
    List<DailyLogEntry> list = new ArrayList<DailyLogEntry>(dailydigests.values());
    list.addAll(tags.values());
    //list.addAll(pulls.values());
    Collections.sort(list);
    return list;
}
Also used : RefModel(com.gitblit.models.RefModel) ObjectId(org.eclipse.jgit.lib.ObjectId) HashMap(java.util.HashMap) DailyLogEntry(com.gitblit.models.DailyLogEntry) ArrayList(java.util.ArrayList) RepositoryCommit(com.gitblit.models.RepositoryCommit) Date(java.util.Date) RefLogEntry(com.gitblit.models.RefLogEntry) UserModel(com.gitblit.models.UserModel) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ArrayList(java.util.ArrayList) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

RefModel (com.gitblit.models.RefModel)32 ArrayList (java.util.ArrayList)18 ObjectId (org.eclipse.jgit.lib.ObjectId)13 Repository (org.eclipse.jgit.lib.Repository)12 RevCommit (org.eclipse.jgit.revwalk.RevCommit)12 List (java.util.List)10 IOException (java.io.IOException)8 HashMap (java.util.HashMap)8 RepositoryModel (com.gitblit.models.RepositoryModel)6 RepositoryCommit (com.gitblit.models.RepositoryCommit)5 UserModel (com.gitblit.models.UserModel)5 Date (java.util.Date)5 TreeSet (java.util.TreeSet)4 RevWalk (org.eclipse.jgit.revwalk.RevWalk)4 DateFormat (java.text.DateFormat)3 SimpleDateFormat (java.text.SimpleDateFormat)3 IndexWriter (org.apache.lucene.index.IndexWriter)3 Ref (org.eclipse.jgit.lib.Ref)3 GitBlitException (com.gitblit.GitBlitException)2 PathChangeModel (com.gitblit.models.PathModel.PathChangeModel)2