List of usage examples for org.eclipse.jgit.lib Repository close
@Override public void close()
Decrement the use count, and maybe close resources.
From source file:com.gitblit.tickets.BranchTicketService.java
License:Apache License
/** * 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/*from w ww .j a va2 s . c o m*/ * @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(); } }
From source file:com.gitblit.tickets.BranchTicketService.java
License:Apache License
/** * Retrieves the ticket from the repository by first looking-up the changeId * associated with the ticketId.//from ww w .j a v a2s . c o m * * @param repository * @param ticketId * @return a ticket, if it exists, otherwise null */ @Override protected TicketModel getTicketImpl(RepositoryModel repository, long ticketId) { Repository db = repositoryManager.getRepository(repository.name); try { List<Change> changes = getJournal(db, ticketId); if (ArrayUtils.isEmpty(changes)) { log.warn("Empty journal for {}:{}", repository, ticketId); return null; } TicketModel ticket = TicketModel.buildTicket(changes); if (ticket != null) { ticket.project = repository.projectPath; ticket.repository = repository.name; ticket.number = ticketId; } return ticket; } finally { db.close(); } }
From source file:com.gitblit.tickets.BranchTicketService.java
License:Apache License
/** * Retrieves the journal for the ticket. * * @param repository/*from w w w. j a v a 2s .c om*/ * @param ticketId * @return a journal, if it exists, otherwise null */ @Override protected List<Change> getJournalImpl(RepositoryModel repository, long ticketId) { Repository db = repositoryManager.getRepository(repository.name); try { List<Change> changes = getJournal(db, ticketId); if (ArrayUtils.isEmpty(changes)) { log.warn("Empty journal for {}:{}", repository, ticketId); return null; } return changes; } finally { db.close(); } }
From source file:com.gitblit.tickets.BranchTicketService.java
License:Apache License
/** * Retrieves the specified attachment from a ticket. * * @param repository/*from w ww .ja va 2 s . co m*/ * @param ticketId * @param filename * @return an attachment, if found, null otherwise */ @Override public Attachment getAttachment(RepositoryModel repository, long ticketId, String filename) { if (ticketId <= 0L) { return null; } // deserialize the ticket model so that we have the attachment metadata TicketModel ticket = getTicket(repository, ticketId); Attachment attachment = ticket.getAttachment(filename); // attachment not found if (attachment == null) { return null; } // retrieve the attachment content Repository db = repositoryManager.getRepository(repository.name); try { String attachmentPath = toAttachmentPath(ticketId, attachment.name); RevTree tree = JGitUtils.getCommit(db, BRANCH).getTree(); byte[] content = JGitUtils.getByteContent(db, tree, attachmentPath, false); attachment.content = content; attachment.size = content.length; return attachment; } finally { db.close(); } }
From source file:com.gitblit.tickets.BranchTicketService.java
License:Apache License
/** * Deletes a ticket from the repository. * * @param ticket/*from w w w . ja va 2 s .c o m*/ * @return true if successful */ @Override protected synchronized boolean deleteTicketImpl(RepositoryModel repository, TicketModel ticket, String deletedBy) { if (ticket == null) { throw new RuntimeException("must specify a ticket!"); } boolean success = false; Repository db = repositoryManager.getRepository(ticket.repository); try { RefModel ticketsBranch = getTicketsBranch(db); if (ticketsBranch == null) { throw new RuntimeException(BRANCH + " does not exist!"); } String ticketPath = toTicketPath(ticket.number); TreeWalk treeWalk = null; try { ObjectId treeId = db.resolve(BRANCH + "^{tree}"); // Create the in-memory index of the new/updated ticket DirCache index = DirCache.newInCore(); DirCacheBuilder builder = index.builder(); // Traverse HEAD to add all other paths treeWalk = new TreeWalk(db); int hIdx = -1; if (treeId != null) { hIdx = treeWalk.addTree(treeId); } treeWalk.setRecursive(true); while (treeWalk.next()) { String path = treeWalk.getPathString(); CanonicalTreeParser hTree = null; if (hIdx != -1) { hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class); } if (!path.startsWith(ticketPath)) { // add entries from HEAD for all other paths if (hTree != null) { final DirCacheEntry entry = new DirCacheEntry(path); entry.setObjectId(hTree.getEntryObjectId()); entry.setFileMode(hTree.getEntryFileMode()); // add to temporary in-core index builder.add(entry); } } } // finish temporary in-core index used for this commit builder.finish(); success = commitIndex(db, index, deletedBy, "- " + ticket.number); } catch (Throwable t) { log.error(MessageFormat.format("Failed to delete ticket {0,number,0} from {1}", ticket.number, db.getDirectory()), t); } finally { // release the treewalk if (treeWalk != null) { treeWalk.close(); } } } finally { db.close(); } return success; }
From source file:com.gitblit.tickets.BranchTicketService.java
License:Apache License
/** * Commit a ticket change to the repository. * * @param repository/*from w w w .jav a 2 s .com*/ * @param ticketId * @param change * @return true, if the change was committed */ @Override protected synchronized boolean commitChangeImpl(RepositoryModel repository, long ticketId, Change change) { boolean success = false; Repository db = repositoryManager.getRepository(repository.name); try { DirCache index = createIndex(db, ticketId, change); success = commitIndex(db, index, change.author, "#" + ticketId); } catch (Throwable t) { log.error(MessageFormat.format("Failed to commit ticket {0,number,0} to {1}", ticketId, db.getDirectory()), t); } finally { db.close(); } return success; }
From source file:com.gitblit.tickets.BranchTicketService.java
License:Apache License
@Override protected boolean deleteAllImpl(RepositoryModel repository) { Repository db = repositoryManager.getRepository(repository.name); try {/*from ww w .j a va2 s .c om*/ RefModel branch = getTicketsBranch(db); if (branch != null) { return JGitUtils.deleteBranchRef(db, BRANCH); } return true; } catch (Exception e) { log.error(null, e); } finally { if (db != null) { db.close(); } } return false; }
From source file:com.gitblit.tickets.FileTicketService.java
License:Apache License
/** * Ensures that we have a ticket for this ticket id. * * @param repository/*from w w w . j a va 2 s .c om*/ * @param ticketId * @return true if the ticket exists */ @Override public boolean hasTicket(RepositoryModel repository, long ticketId) { boolean hasTicket = false; Repository db = repositoryManager.getRepository(repository.name); try { String journalPath = toTicketPath(ticketId) + "/" + JOURNAL; hasTicket = new File(db.getDirectory(), journalPath).exists(); } finally { db.close(); } return hasTicket; }
From source file:com.gitblit.tickets.FileTicketService.java
License:Apache License
@Override public synchronized Set<Long> getIds(RepositoryModel repository) { Set<Long> ids = new TreeSet<Long>(); Repository db = repositoryManager.getRepository(repository.name); try {//from w ww. j a v a 2 s.c o m // identify current highest ticket id by scanning the paths in the tip tree File dir = new File(db.getDirectory(), TICKETS_PATH); dir.mkdirs(); List<File> journals = findAll(dir, JOURNAL); for (File journal : journals) { // Reconstruct ticketId from the path // id/26/326/journal.json String path = FileUtils.getRelativePath(dir, journal); String tid = path.split("/")[1]; long ticketId = Long.parseLong(tid); ids.add(ticketId); } } finally { if (db != null) { db.close(); } } return ids; }
From source file:com.gitblit.tickets.FileTicketService.java
License:Apache License
/** * Assigns a new ticket id./*from ww w. j a v a 2 s .c om*/ * * @param repository * @return a new long id */ @Override public synchronized long assignNewId(RepositoryModel repository) { long newId = 0L; Repository db = repositoryManager.getRepository(repository.name); try { if (!lastAssignedId.containsKey(repository.name)) { lastAssignedId.put(repository.name, new AtomicLong(0)); } AtomicLong lastId = lastAssignedId.get(repository.name); if (lastId.get() <= 0) { Set<Long> ids = getIds(repository); for (long id : ids) { if (id > lastId.get()) { lastId.set(id); } } } // assign the id and touch an empty journal to hold it's place newId = lastId.incrementAndGet(); String journalPath = toTicketPath(newId) + "/" + JOURNAL; File journal = new File(db.getDirectory(), journalPath); journal.getParentFile().mkdirs(); journal.createNewFile(); } catch (IOException e) { log.error("failed to assign ticket id", e); return 0L; } finally { db.close(); } return newId; }