Search in sources :

Example 11 with RefModel

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

the class BranchTicketService method getTickets.

/**
	 * Returns all the tickets in the repository. Querying tickets from the
	 * repository requires deserializing all tickets. This is an  expensive
	 * process and not recommended. Tickets are indexed by Lucene and queries
	 * should be executed against that index.
	 *
	 * @param repository
	 * @param filter
	 *            optional filter to only return matching results
	 * @return a list of tickets
	 */
@Override
public List<TicketModel> getTickets(RepositoryModel repository, TicketFilter filter) {
    List<TicketModel> list = new ArrayList<TicketModel>();
    Repository db = repositoryManager.getRepository(repository.name);
    try {
        RefModel ticketsBranch = getTicketsBranch(db);
        if (ticketsBranch == null) {
            return list;
        }
        // Collect the set of all json files
        List<PathModel> paths = JGitUtils.getDocuments(db, Arrays.asList("json"), BRANCH);
        // Deserialize each ticket and optionally filter out unwanted tickets
        for (PathModel path : paths) {
            String name = path.name.substring(path.name.lastIndexOf('/') + 1);
            if (!JOURNAL.equals(name)) {
                continue;
            }
            String json = readTicketsFile(db, path.path);
            if (StringUtils.isEmpty(json)) {
                // journal was touched but no changes were written
                continue;
            }
            try {
                // Reconstruct ticketId from the path
                // id/26/326/journal.json
                String tid = path.path.split("/")[2];
                long ticketId = Long.parseLong(tid);
                List<Change> changes = TicketSerializer.deserializeJournal(json);
                if (ArrayUtils.isEmpty(changes)) {
                    log.warn("Empty journal for {}:{}", repository, path.path);
                    continue;
                }
                TicketModel ticket = TicketModel.buildTicket(changes);
                ticket.project = repository.projectPath;
                ticket.repository = repository.name;
                ticket.number = ticketId;
                // add the ticket, conditionally, to the list
                if (filter == null) {
                    list.add(ticket);
                } else {
                    if (filter.accept(ticket)) {
                        list.add(ticket);
                    }
                }
            } catch (Exception e) {
                log.error("failed to deserialize {}/{}\n{}", new Object[] { repository, path.path, e.getMessage() });
                log.error(null, e);
            }
        }
        // sort the tickets by creation
        Collections.sort(list);
        return list;
    } finally {
        db.close();
    }
}
Also used : RefModel(com.gitblit.models.RefModel) ArrayList(java.util.ArrayList) TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException) IOException(java.io.IOException) Repository(org.eclipse.jgit.lib.Repository) PathModel(com.gitblit.models.PathModel)

Example 12 with RefModel

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

the class JGitUtilsTest method testBranches.

@Test
public void testBranches() throws Exception {
    Repository repository = GitBlitSuite.getJGitRepository();
    assertTrue(JGitUtils.getLocalBranches(repository, true, 0).size() == 0);
    for (RefModel model : JGitUtils.getLocalBranches(repository, true, -1)) {
        assertTrue(model.getName().startsWith(Constants.R_HEADS));
        assertTrue(model.equals(model));
        assertFalse(model.equals(""));
        assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode() + model.getName().hashCode());
        assertTrue(model.getShortMessage().equals(model.getShortMessage()));
    }
    for (RefModel model : JGitUtils.getRemoteBranches(repository, true, -1)) {
        assertTrue(model.getName().startsWith(Constants.R_REMOTES));
        assertTrue(model.equals(model));
        assertFalse(model.equals(""));
        assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode() + model.getName().hashCode());
        assertTrue(model.getShortMessage().equals(model.getShortMessage()));
    }
    assertTrue(JGitUtils.getRemoteBranches(repository, true, 8).size() == 8);
    repository.close();
}
Also used : Repository(org.eclipse.jgit.lib.Repository) RefModel(com.gitblit.models.RefModel) Test(org.junit.Test)

Example 13 with RefModel

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

the class JGitUtilsTest method testRefs.

@Test
public void testRefs() throws Exception {
    Repository repository = GitBlitSuite.getJGitRepository();
    Map<ObjectId, List<RefModel>> map = JGitUtils.getAllRefs(repository);
    repository.close();
    assertTrue(map.size() > 0);
    for (Map.Entry<ObjectId, List<RefModel>> entry : map.entrySet()) {
        List<RefModel> list = entry.getValue();
        for (RefModel ref : list) {
            if (ref.displayName.equals("refs/tags/spearce-gpg-pub")) {
                assertEquals("refs/tags/spearce-gpg-pub", ref.toString());
                assertEquals("8bbde7aacf771a9afb6992434f1ae413e010c6d8", ref.getObjectId().getName());
                assertEquals("spearce@spearce.org", ref.getAuthorIdent().getEmailAddress());
                assertTrue(ref.getShortMessage().startsWith("GPG key"));
                assertTrue(ref.getFullMessage().startsWith("GPG key"));
                assertEquals(Constants.OBJ_BLOB, ref.getReferencedObjectType());
            } else if (ref.displayName.equals("refs/tags/v0.12.1")) {
                assertTrue(ref.isAnnotatedTag());
            }
        }
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) RefModel(com.gitblit.models.RefModel) ObjectId(org.eclipse.jgit.lib.ObjectId) PlotCommitList(org.eclipse.jgit.revplot.PlotCommitList) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Example 14 with RefModel

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

the class RepositoryPage method getBestCommitId.

protected String getBestCommitId(RevCommit commit) {
    String head = null;
    try {
        head = r.resolve(getRepositoryModel().HEAD).getName();
    } catch (Exception e) {
    }
    String id = commit.getName();
    if (!StringUtils.isEmpty(head) && head.equals(id)) {
        // match default branch
        return Repository.shortenRefName(getRepositoryModel().HEAD);
    }
    // find first branch match
    for (RefModel ref : JGitUtils.getLocalBranches(r, false, -1)) {
        if (ref.getObjectId().getName().equals(id)) {
            return Repository.shortenRefName(ref.getName());
        }
    }
    // return sha
    return id;
}
Also used : RefModel(com.gitblit.models.RefModel) RestartResponseException(org.apache.wicket.RestartResponseException) GitBlitException(com.gitblit.GitBlitException)

Example 15 with RefModel

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

the class JGitUtils method getDefaultBranch.

/**
	 * Returns the default branch to use for a repository. Normally returns
	 * whatever branch HEAD points to, but if HEAD points to nothing it returns
	 * the most recently updated branch.
	 *
	 * @param repository
	 * @return the objectid of a branch
	 * @throws Exception
	 */
public static ObjectId getDefaultBranch(Repository repository) throws Exception {
    ObjectId object = repository.resolve(Constants.HEAD);
    if (object == null) {
        // no HEAD
        // perhaps non-standard repository, try local branches
        List<RefModel> branchModels = getLocalBranches(repository, true, -1);
        if (branchModels.size() > 0) {
            // use most recently updated branch
            RefModel branch = null;
            Date lastDate = new Date(0);
            for (RefModel branchModel : branchModels) {
                if (branchModel.getDate().after(lastDate)) {
                    branch = branchModel;
                    lastDate = branch.getDate();
                }
            }
            object = branch.getReferencedObjectId();
        }
    }
    return object;
}
Also used : RefModel(com.gitblit.models.RefModel) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) Date(java.util.Date)

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