Example usage for org.eclipse.jgit.lib Repository close

List of usage examples for org.eclipse.jgit.lib Repository close

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository close.

Prototype

@Override
public void close() 

Source Link

Document

Decrement the use count, and maybe close resources.

Usage

From source file:com.gitblit.tests.LuceneExecutorTest.java

License:Apache License

@Test
public void testMultiSearch() throws Exception {
    List<String> list = new ArrayList<String>();
    Repository repository = GitBlitSuite.getHelloworldRepository();
    list.add(newRepositoryModel(repository).name);
    repository.close();

    repository = GitBlitSuite.getJGitRepository();
    list.add(newRepositoryModel(repository).name);
    repository.close();//from w  w  w  . ja va  2  s . co m

    List<SearchResult> results = lucene.search("test", 1, 10, list);
    assertEquals(10, results.size());
}

From source file:com.gitblit.tests.MetricUtilsTest.java

License:Apache License

private void testMetrics(Repository repository) throws Exception {
    List<Metric> metrics = MetricUtils.getDateMetrics(repository, null, true, null, TimeZone.getDefault());
    repository.close();
    assertTrue("No date metrics found!", metrics.size() > 0);
}

From source file:com.gitblit.tests.MetricUtilsTest.java

License:Apache License

@Test
public void testAuthorMetrics() throws Exception {
    Repository repository = GitBlitSuite.getHelloworldRepository();
    List<Metric> byEmail = MetricUtils.getAuthorMetrics(repository, null, true);
    List<Metric> byName = MetricUtils.getAuthorMetrics(repository, null, false);
    repository.close();
    assertEquals("No author metrics found!", 9, byEmail.size());
    assertEquals("No author metrics found!", 8, byName.size());
}

From source file:com.gitblit.tests.TicgitUtilsTest.java

License:Apache License

@Test
public void testTicgitBranch() throws Exception {
    Repository repository = GitBlitSuite.getTicgitRepository();
    RefModel branch = TicgitUtils.getTicketsBranch(repository);
    repository.close();
    assertNotNull("Ticgit branch does not exist!", branch);

    repository = GitBlitSuite.getHelloworldRepository();
    branch = TicgitUtils.getTicketsBranch(repository);
    repository.close();//from   w w w  .j av a  2s.  c om
    assertNull("Ticgit branch exists!", branch);
}

From source file:com.gitblit.tests.TicgitUtilsTest.java

License:Apache License

@Test
public void testRetrieveTickets() throws Exception {
    Repository repository = GitBlitSuite.getTicgitRepository();
    List<TicketModel> ticketsA = TicgitUtils.getTickets(repository);
    List<TicketModel> ticketsB = TicgitUtils.getTickets(repository);
    repository.close();
    assertTrue("No tickets found!", ticketsA.size() > 0);
    for (int i = 0; i < ticketsA.size(); i++) {
        TicketModel ticketA = ticketsA.get(i);
        TicketModel ticketB = ticketsB.get(i);
        assertTrue("Tickets are not equal!", ticketA.equals(ticketB));
        assertFalse(ticketA.equals(""));
        assertTrue(ticketA.hashCode() == ticketA.id.hashCode());
        for (int j = 0; j < ticketA.comments.size(); j++) {
            Comment commentA = ticketA.comments.get(j);
            Comment commentB = ticketB.comments.get(j);
            assertTrue("Comments are not equal!", commentA.equals(commentB));
            assertFalse(commentA.equals(""));
            assertEquals(commentA.hashCode(), commentA.text.hashCode());
        }//  w  w w. j a va2 s  . c  o m
    }

    repository = GitBlitSuite.getHelloworldRepository();
    List<TicketModel> ticketsC = TicgitUtils.getTickets(repository);
    repository.close();
    assertNull(ticketsC);
}

From source file:com.gitblit.tests.TicgitUtilsTest.java

License:Apache License

@Test
public void testReadTicket() throws Exception {
    Repository repository = GitBlitSuite.getTicgitRepository();
    List<TicketModel> tickets = TicgitUtils.getTickets(repository);
    TicketModel ticket = TicgitUtils.getTicket(repository, tickets.get(tickets.size() - 1).name);
    repository.close();
    assertNotNull(ticket);/*from   w  w w . j  a  v  a2s  .  c  o m*/
    assertEquals("1206206148_add-attachment-to-ticket_138", ticket.name);
}

From source file:com.gitblit.tickets.BranchTicketService.java

License:Apache License

/**
 * Listen for tickets branch changes and (re)index tickets, as appropriate
 *//* ww w.j  a  v  a  2 s.c  o  m*/
@Override
public synchronized void onRefsChanged(RefsChangedEvent event) {
    if (!(event instanceof ReceiveCommandEvent)) {
        return;
    }

    ReceiveCommandEvent branchUpdate = (ReceiveCommandEvent) event;
    RepositoryModel repository = branchUpdate.model;
    ReceiveCommand cmd = branchUpdate.cmd;
    try {
        switch (cmd.getType()) {
        case CREATE:
        case UPDATE_NONFASTFORWARD:
            // reindex everything
            reindex(repository);
            break;
        case UPDATE:
            // incrementally index ticket updates
            resetCaches(repository);
            long start = System.nanoTime();
            log.info("incrementally indexing {} ticket branch due to received ref update", repository.name);
            Repository db = repositoryManager.getRepository(repository.name);
            try {
                Set<Long> ids = new HashSet<Long>();
                List<PathChangeModel> paths = JGitUtils.getFilesInRange(db, cmd.getOldId().getName(),
                        cmd.getNewId().getName());
                for (PathChangeModel 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);
                    if (!ids.contains(ticketId)) {
                        ids.add(ticketId);
                        TicketModel ticket = getTicket(repository, ticketId);
                        log.info(MessageFormat.format("indexing ticket #{0,number,0}: {1}", ticketId,
                                ticket.title));
                        indexer.index(ticket);
                    }
                }
                long end = System.nanoTime();
                log.info("incremental indexing of {0} ticket(s) completed in {1} msecs", ids.size(),
                        TimeUnit.NANOSECONDS.toMillis(end - start));
            } finally {
                db.close();
            }
            break;
        default:
            log.warn("Unexpected receive type {} in BranchTicketService.onRefsChanged" + cmd.getType());
            break;
        }
    } catch (Exception e) {
        log.error("failed to reindex " + repository.name, e);
    }
}

From source file:com.gitblit.tickets.BranchTicketService.java

License:Apache License

/**
 * Ensures that we have a ticket for this ticket id.
 *
 * @param repository/*from   w  w w.j  av a  2s .com*/
 * @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 {
        RefModel ticketsBranch = getTicketsBranch(db);
        if (ticketsBranch == null) {
            return false;
        }
        String ticketPath = toTicketPath(ticketId);
        RevCommit tip = JGitUtils.getCommit(db, BRANCH);
        hasTicket = !JGitUtils.getFilesInPath(db, ticketPath, tip).isEmpty();
    } finally {
        db.close();
    }
    return hasTicket;
}

From source file:com.gitblit.tickets.BranchTicketService.java

License:Apache License

/**
 * Returns the assigned ticket ids.//  w w w .  ja  v  a2 s .  c  o m
 *
 * @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();
        }
    }
}

From source file:com.gitblit.tickets.BranchTicketService.java

License:Apache License

/**
 * Assigns a new ticket id./*  w w  w.j  av  a2 s . c o  m*/
 *
 * @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 (getTicketsBranch(db) == null) {
            createTicketsBranch(db);
        }

        // identify current highest ticket id by scanning the paths in the tip tree
        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;
        writeTicketsFile(db, journalPath, "", "gitblit", "assigned id #" + newId);
    } finally {
        db.close();
    }
    return newId;
}