Search in sources :

Example 16 with Repository

use of org.eclipse.jgit.lib.Repository in project gitblit by gitblit.

the class BranchTicketService method getIds.

/**
	 * Returns the assigned ticket ids.
	 *
	 * @return the assigned ticket ids
	 */
@Override
public synchronized Set<Long> getIds(RepositoryModel repository) {
    Repository db = repositoryManager.getRepository(repository.name);
    try {
        if (getTicketsBranch(db) == null) {
            return Collections.emptySet();
        }
        Set<Long> ids = new TreeSet<Long>();
        List<PathModel> paths = JGitUtils.getDocuments(db, Arrays.asList("json"), BRANCH);
        for (PathModel path : paths) {
            String name = path.name.substring(path.name.lastIndexOf('/') + 1);
            if (!JOURNAL.equals(name)) {
                continue;
            }
            String tid = path.path.split("/")[2];
            long ticketId = Long.parseLong(tid);
            ids.add(ticketId);
        }
        return ids;
    } finally {
        if (db != null) {
            db.close();
        }
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) PathModel(com.gitblit.models.PathModel) TreeSet(java.util.TreeSet) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 17 with Repository

use of org.eclipse.jgit.lib.Repository 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 18 with Repository

use of org.eclipse.jgit.lib.Repository in project gitblit by gitblit.

the class BranchTicketService method getAttachment.

/**
	 * Retrieves the specified attachment from a ticket.
	 *
	 * @param repository
	 * @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();
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) TicketModel(com.gitblit.models.TicketModel) Attachment(com.gitblit.models.TicketModel.Attachment) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 19 with Repository

use of org.eclipse.jgit.lib.Repository in project gitblit by gitblit.

the class FileTicketService method commitChangeImpl.

/**
	 * Commit a ticket change to the repository.
	 *
	 * @param repository
	 * @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 {
        List<Change> changes = getJournal(db, ticketId);
        changes.add(change);
        String journal = TicketSerializer.serializeJournal(changes).trim();
        String journalPath = toTicketPath(ticketId) + "/" + JOURNAL;
        File file = new File(db.getDirectory(), journalPath);
        file.getParentFile().mkdirs();
        FileUtils.writeContent(file, journal);
        success = true;
    } 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;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Change(com.gitblit.models.TicketModel.Change) File(java.io.File)

Example 20 with Repository

use of org.eclipse.jgit.lib.Repository in project gitblit by gitblit.

the class FileTicketService method getAttachment.

/**
	 * Retrieves the specified attachment from a ticket.
	 *
	 * @param repository
	 * @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);
        File file = new File(db.getDirectory(), attachmentPath);
        if (file.exists()) {
            attachment.content = FileUtils.readContent(file);
            attachment.size = attachment.content.length;
        }
        return attachment;
    } finally {
        db.close();
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) TicketModel(com.gitblit.models.TicketModel) Attachment(com.gitblit.models.TicketModel.Attachment) File(java.io.File)

Aggregations

Repository (org.eclipse.jgit.lib.Repository)326 IOException (java.io.IOException)103 RevWalk (org.eclipse.jgit.revwalk.RevWalk)102 Test (org.junit.Test)81 RevCommit (org.eclipse.jgit.revwalk.RevCommit)76 ObjectId (org.eclipse.jgit.lib.ObjectId)72 File (java.io.File)43 TestRepository (org.eclipse.jgit.junit.TestRepository)40 Change (com.google.gerrit.reviewdb.client.Change)39 OrmException (com.google.gwtorm.server.OrmException)39 Ref (org.eclipse.jgit.lib.Ref)35 Project (com.google.gerrit.reviewdb.client.Project)32 ArrayList (java.util.ArrayList)31 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)27 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)26 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)24 Map (java.util.Map)23 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)23 RepositoryModel (com.gitblit.models.RepositoryModel)20 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)20