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

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

Introduction

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

Prototype


public File getDirectory() 

Source Link

Document

Get local metadata directory

Usage

From source file:com.gitblit.tickets.FileTicketService.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// w  ww  .  j a v  a  2  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 {
        // Collect the set of all json files
        File dir = new File(db.getDirectory(), TICKETS_PATH);
        List<File> journals = findAll(dir, JOURNAL);

        // Deserialize each ticket and optionally filter out unwanted tickets
        for (File journal : journals) {
            String json = null;
            try {
                json = new String(FileUtils.readContent(journal), Constants.ENCODING);
            } catch (Exception e) {
                log.error(null, e);
            }
            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 path = FileUtils.getRelativePath(dir, journal);
                String tid = path.split("/")[1];
                long ticketId = Long.parseLong(tid);
                List<Change> changes = TicketSerializer.deserializeJournal(json);
                if (ArrayUtils.isEmpty(changes)) {
                    log.warn("Empty journal for {}:{}", repository, journal);
                    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, journal, 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.FileTicketService.java

License:Apache License

/**
 * Returns the journal for the specified ticket.
 *
 * @param db/*w ww.j a v  a 2 s  .  c om*/
 * @param ticketId
 * @return a list of changes
 */
private List<Change> getJournal(Repository db, long ticketId) {
    if (ticketId <= 0L) {
        return new ArrayList<Change>();
    }

    String journalPath = toTicketPath(ticketId) + "/" + JOURNAL;
    File journal = new File(db.getDirectory(), journalPath);
    if (!journal.exists()) {
        return new ArrayList<Change>();
    }

    String json = null;
    try {
        json = new String(FileUtils.readContent(journal), Constants.ENCODING);
    } catch (Exception e) {
        log.error(null, e);
    }
    if (StringUtils.isEmpty(json)) {
        return new ArrayList<Change>();
    }
    List<Change> list = TicketSerializer.deserializeJournal(json);
    return list;
}

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

License:Apache License

/**
 * Retrieves the specified attachment from a ticket.
 *
 * @param repository//from ww w .  j a  va  2  s. c  o  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);
        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();
    }
}

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

License:Apache License

/**
 * Deletes a ticket from the repository.
 *
 * @param ticket//from w w w.j  av  a2s  .c  om
 * @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 {
        String ticketPath = toTicketPath(ticket.number);
        File dir = new File(db.getDirectory(), ticketPath);
        if (dir.exists()) {
            success = FileUtils.delete(dir);
        }
        success = true;
    } finally {
        db.close();
    }
    return success;
}

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

License:Apache License

/**
 * Commit a ticket change to the repository.
 *
 * @param repository//from w w w  .j  a v a2  s.c om
 * @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;
}

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

License:Apache License

@Override
protected boolean deleteAllImpl(RepositoryModel repository) {
    Repository db = repositoryManager.getRepository(repository.name);
    if (db == null) {
        // the tickets no longer exist because the db no longer exists
        return true;
    }//from   w w w.j a v  a 2  s. c o  m
    try {
        File dir = new File(db.getDirectory(), TICKETS_PATH);
        return FileUtils.delete(dir);
    } catch (Exception e) {
        log.error(null, e);
    } finally {
        db.close();
    }
    return false;
}

From source file:com.gitblit.utils.CompressionUtils.java

License:Apache License

/**
 * Log an error message and exception.//  w w w .  j av  a2 s .co  m
 *
 * @param t
 * @param repository
 *            if repository is not null it MUST be the {0} parameter in the
 *            pattern.
 * @param pattern
 * @param objects
 */
private static void error(Throwable t, Repository repository, String pattern, Object... objects) {
    List<Object> parameters = new ArrayList<Object>();
    if (objects != null && objects.length > 0) {
        for (Object o : objects) {
            parameters.add(o);
        }
    }
    if (repository != null) {
        parameters.add(0, repository.getDirectory().getAbsolutePath());
    }
    LOGGER.error(MessageFormat.format(pattern, parameters.toArray()), t);
}

From source file:com.gitblit.utils.JGitUtils.java

License:Apache License

/**
 * Creates a bare, shared repository.//from w w w  . j  a v a2s. c o m
 *
 * @param repositoriesFolder
 * @param name
 * @param shared
 *          the setting for the --shared option of "git init".
 * @return Repository
 */
public static Repository createRepository(File repositoriesFolder, String name, String shared) {
    try {
        Repository repo = null;
        try {
            Git git = Git.init().setDirectory(new File(repositoriesFolder, name)).setBare(true).call();
            repo = git.getRepository();
        } catch (GitAPIException e) {
            throw new RuntimeException(e);
        }

        GitConfigSharedRepository sharedRepository = new GitConfigSharedRepository(shared);
        if (sharedRepository.isShared()) {
            StoredConfig config = repo.getConfig();
            config.setString("core", null, "sharedRepository", sharedRepository.getValue());
            config.setBoolean("receive", null, "denyNonFastforwards", true);
            config.save();

            if (!JnaUtils.isWindows()) {
                Iterator<File> iter = org.apache.commons.io.FileUtils.iterateFilesAndDirs(repo.getDirectory(),
                        TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
                // Adjust permissions on file/directory
                while (iter.hasNext()) {
                    adjustSharedPerm(iter.next(), sharedRepository);
                }
            }
        }

        return repo;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.gitblit.utils.JGitUtils.java

License:Apache License

/**
 * Returns the date of the first commit on a branch. If the repository does
 * not exist, Date(0) is returned. If the repository does exist bit is
 * empty, the last modified date of the repository folder is returned.
 *
 * @param repository//from  w  ww.j  a v  a2  s. co m
 * @param branch
 *            if unspecified, HEAD is assumed.
 * @return Date of the first commit on a branch
 */
public static Date getFirstChange(Repository repository, String branch) {
    RevCommit commit = getFirstCommit(repository, branch);
    if (commit == null) {
        if (repository == null || !repository.getDirectory().exists()) {
            return new Date(0);
        }
        // fresh repository
        return new Date(repository.getDirectory().lastModified());
    }
    return getCommitDate(commit);
}

From source file:com.gitblit.utils.JGitUtils.java

License:Apache License

/**
 * Determine if a repository has any commits. This is determined by checking
 * the for loose and packed objects.//from w ww. j av  a2  s  . c  o m
 *
 * @param repository
 * @return true if the repository has commits
 */
public static boolean hasCommits(Repository repository) {
    if (repository != null && repository.getDirectory().exists()) {
        return (new File(repository.getDirectory(), "objects").list().length > 2)
                || (new File(repository.getDirectory(), "objects/pack").list().length > 0);
    }
    return false;
}