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();
}
}
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();
}
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());
}
}
}
}
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;
}
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;
}
Aggregations