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.tests.JGitUtilsTest.java

License:Apache License

@Test
public void testCreateOrphanedBranch() throws Exception {
    Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES, "orphantest");
    assertTrue(JGitUtils.createOrphanBranch(repository,
            "x" + Long.toHexString(System.currentTimeMillis()).toUpperCase(), null));
    FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);
}

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

License:Apache License

@Test
public void testGetFilemode() throws IOException {
    if (JnaUtils.isWindows()) {
        try {// www. j  ava  2 s.  co m
            JnaUtils.getFilemode(GitBlitSuite.REPOSITORIES);
        } catch (UnsupportedOperationException e) {
        }
    } else {
        String repositoryName = "NewJnaTestRepository.git";
        Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES, repositoryName);
        File folder = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, repositoryName), FS.DETECTED);
        assertTrue(folder.exists());

        int mode = JnaUtils.getFilemode(folder);
        assertTrue(mode > 0);
        assertEquals(JnaUtils.S_IFDIR, (mode & JnaUtils.S_IFMT)); // directory
        assertEquals(JnaUtils.S_IRUSR | JnaUtils.S_IWUSR | JnaUtils.S_IXUSR, (mode & JnaUtils.S_IRWXU)); // owner full access

        mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/config");
        assertTrue(mode > 0);
        assertEquals(JnaUtils.S_IFREG, (mode & JnaUtils.S_IFMT)); // directory
        assertEquals(JnaUtils.S_IRUSR | JnaUtils.S_IWUSR, (mode & JnaUtils.S_IRWXU)); // owner full access

        repository.close();
        RepositoryCache.close(repository);
        FileUtils.deleteDirectory(repository.getDirectory());
    }
}

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

License:Apache License

@Test
public void testSetFilemode() throws IOException {
    if (JnaUtils.isWindows()) {
        try {/*from   ww w.  j  a v a2  s. c o  m*/
            JnaUtils.getFilemode(GitBlitSuite.REPOSITORIES);
        } catch (UnsupportedOperationException e) {
        }
    } else {
        String repositoryName = "NewJnaTestRepository.git";
        Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES, repositoryName);
        File folder = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, repositoryName), FS.DETECTED);
        assertTrue(folder.exists());

        File path = new File(folder, "refs");
        int mode = JnaUtils.getFilemode(path);
        assertTrue(mode > 0);
        assertEquals(JnaUtils.S_IFDIR, (mode & JnaUtils.S_IFMT)); // directory
        assertEquals(JnaUtils.S_IRUSR | JnaUtils.S_IWUSR | JnaUtils.S_IXUSR, (mode & JnaUtils.S_IRWXU)); // owner full access

        mode |= JnaUtils.S_ISGID;
        mode |= JnaUtils.S_IRWXG;
        int ret = JnaUtils.setFilemode(path, mode);
        assertEquals(0, ret);
        mode = JnaUtils.getFilemode(path);
        assertTrue(mode > 0);
        assertEquals(JnaUtils.S_ISGID, (mode & JnaUtils.S_ISGID)); // set-gid-bit set
        assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP | JnaUtils.S_IXGRP, (mode & JnaUtils.S_IRWXG)); // group full access

        path = new File(folder, "config");
        mode = JnaUtils.getFilemode(path.getAbsolutePath());
        assertTrue(mode > 0);
        assertEquals(JnaUtils.S_IFREG, (mode & JnaUtils.S_IFMT)); // directory
        assertEquals(JnaUtils.S_IRUSR | JnaUtils.S_IWUSR, (mode & JnaUtils.S_IRWXU)); // owner full access

        mode |= (JnaUtils.S_IRGRP | JnaUtils.S_IWGRP);
        ret = JnaUtils.setFilemode(path.getAbsolutePath(), mode);
        assertEquals(0, ret);
        mode = JnaUtils.getFilemode(path.getAbsolutePath());
        assertTrue(mode > 0);
        assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, (mode & JnaUtils.S_IRWXG)); // group full access

        repository.close();
        RepositoryCache.close(repository);
        FileUtils.deleteDirectory(repository.getDirectory());
    }
}

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

License:Apache License

private RepositoryModel newRepositoryModel(Repository repository) {
    RepositoryModel model = new RepositoryModel();
    model.name = FileUtils.getRelativePath(GitBlitSuite.REPOSITORIES, repository.getDirectory());
    model.hasCommits = JGitUtils.hasCommits(repository);

    // index all local branches
    model.indexedBranches = new ArrayList<String>();
    for (RefModel ref : JGitUtils.getLocalBranches(repository, true, -1)) {
        model.indexedBranches.add(ref.getName());
    }/*from  w  w w  . j a  va  2  s  .  c o m*/
    return model;
}

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

License:Apache License

/**
 * Returns a RefModel for the refs/meta/gitblit/tickets branch in the repository.
 * If the branch can not be found, null is returned.
 *
 * @return a refmodel for the gitblit tickets branch or null
 *//*w  w w  .  j  ava  2 s .c o  m*/
private RefModel getTicketsBranch(Repository db) {
    List<RefModel> refs = JGitUtils.getRefs(db, "refs/");
    Ref oldRef = null;
    for (RefModel ref : refs) {
        if (ref.reference.getName().equals(BRANCH)) {
            return ref;
        } else if (ref.reference.getName().equals("refs/gitblit/tickets")) {
            oldRef = ref.reference;
        }
    }
    if (oldRef != null) {
        // rename old ref to refs/meta/gitblit/tickets
        RefRename cmd;
        try {
            cmd = db.renameRef(oldRef.getName(), BRANCH);
            cmd.setRefLogIdent(new PersonIdent("Gitblit", "gitblit@localhost"));
            cmd.setRefLogMessage("renamed " + oldRef.getName() + " => " + BRANCH);
            Result res = cmd.rename();
            switch (res) {
            case RENAMED:
                log.info(db.getDirectory() + " " + cmd.getRefLogMessage());
                return getTicketsBranch(db);
            default:
                log.error("failed to rename " + oldRef.getName() + " => " + BRANCH + " (" + res.name() + ")");
            }
        } catch (IOException e) {
            log.error("failed to rename tickets branch", e);
        }
    }
    return null;
}

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

License:Apache License

/**
 * Deletes a ticket from the repository.
 *
 * @param ticket/*  ww w .j  a v  a  2  s  .co 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 ww . j a v  a  2  s  .co m
 * @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.FileTicketService.java

License:Apache License

/**
 * Ensures that we have a ticket for this ticket id.
 *
 * @param repository//from www  .  j  ava  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   ww  w  .j  a va 2  s  . c  om*/
        // 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 w  ww  .  jav  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;
}