Search in sources :

Example 26 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project archi-modelrepository-plugin by archi-contribs.

the class RestoreCommitAction method run.

@Override
public void run() {
    // Offer to save the model if open and dirty
    IArchimateModel model = getRepository().locateModel();
    if (model != null && IEditorModelManager.INSTANCE.isModelDirty(model)) {
        if (!offerToSaveModel(model)) {
            return;
        }
    }
    boolean response = MessageDialog.openConfirm(fWindow.getShell(), Messages.RestoreCommitAction_0, Messages.RestoreCommitAction_1);
    if (!response) {
        return;
    }
    // Delete the content folders first
    try {
        File modelFolder = new File(getRepository().getLocalRepositoryFolder(), IGraficoConstants.MODEL_FOLDER);
        FileUtils.deleteFolder(modelFolder);
        modelFolder.mkdirs();
        File imagesFolder = new File(getRepository().getLocalRepositoryFolder(), IGraficoConstants.IMAGES_FOLDER);
        FileUtils.deleteFolder(imagesFolder);
        imagesFolder.mkdirs();
    } catch (IOException ex) {
        displayErrorDialog(Messages.RestoreCommitAction_0, ex);
        return;
    }
    // Walk the tree and get the contents of the commit
    try (Repository repository = Git.open(getRepository().getLocalRepositoryFolder()).getRepository()) {
        try (TreeWalk treeWalk = new TreeWalk(repository)) {
            treeWalk.addTree(fCommit.getTree());
            treeWalk.setRecursive(true);
            while (treeWalk.next()) {
                ObjectId objectId = treeWalk.getObjectId(0);
                ObjectLoader loader = repository.open(objectId);
                File file = new File(getRepository().getLocalRepositoryFolder(), treeWalk.getPathString());
                file.getParentFile().mkdirs();
                try (FileOutputStream out = new FileOutputStream(file)) {
                    loader.copyTo(out);
                }
            }
        }
    } catch (IOException ex) {
        displayErrorDialog(Messages.RestoreCommitAction_0, ex);
        return;
    }
    // Reload the model from the Grafico XML files
    try {
        IArchimateModel graficoModel = new GraficoModelLoader(getRepository()).loadModel();
        // If this is null then it failed because of no model in this commit
        if (graficoModel == null) {
            // Reset
            getRepository().resetToRef(IGraficoConstants.HEAD);
            MessageDialog.openError(fWindow.getShell(), Messages.RestoreCommitAction_0, Messages.RestoreCommitAction_2);
            return;
        }
    } catch (Exception ex) {
        displayErrorDialog(Messages.RestoreCommitAction_0, ex);
    }
    // Commit changes
    try {
        // $NON-NLS-1$ //$NON-NLS-2$
        getRepository().commitChanges(Messages.RestoreCommitAction_3 + " '" + fCommit.getShortMessage() + "'", false);
        // Save the checksum
        getRepository().saveChecksum();
    } catch (Exception ex) {
        displayErrorDialog(Messages.RestoreCommitAction_0, ex);
    }
    notifyChangeListeners(IRepositoryListener.HISTORY_CHANGED);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) GraficoModelLoader(org.archicontribs.modelrepository.grafico.GraficoModelLoader) ObjectId(org.eclipse.jgit.lib.ObjectId) FileOutputStream(java.io.FileOutputStream) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) IOException(java.io.IOException) IArchimateModel(com.archimatetool.model.IArchimateModel) File(java.io.File) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) IOException(java.io.IOException)

Example 27 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project jOOQ by jOOQ.

the class GitCommitProvider method read.

private final String read(Repository repository, RevCommit commit, String path) throws IOException {
    try (TreeWalk treeWalk = TreeWalk.forPath(repository, path, commit.getTree())) {
        ObjectId blobId = treeWalk.getObjectId(0);
        try (ObjectReader objectReader = repository.newObjectReader()) {
            ObjectLoader objectLoader = objectReader.open(blobId);
            byte[] bytes = objectLoader.getBytes();
            return new String(bytes, StandardCharsets.UTF_8);
        }
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 28 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project OpenGrok by OpenGrok.

the class GitRepository method getHistoryRev.

/**
 * Try to get file contents for given revision.
 *
 * @param out a required OutputStream
 * @param fullpath full pathname of the file
 * @param rev revision string
 * @return a defined instance with {@code success} == {@code true} if no
 * error occurred and with non-zero {@code iterations} if some data was transferred
 */
private HistoryRevResult getHistoryRev(OutputStream out, String fullpath, String rev) {
    HistoryRevResult result = new HistoryRevResult();
    File directory = new File(getDirectoryName());
    String filename;
    result.success = false;
    try {
        filename = getGitFilePath(Paths.get(getCanonicalDirectoryName()).relativize(Paths.get(fullpath)).toString());
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, String.format("Failed to relativize '%s' in for repository '%s'", fullpath, directory), e);
        return result;
    }
    try (org.eclipse.jgit.lib.Repository repository = getJGitRepository(directory.getAbsolutePath())) {
        ObjectId commitId = repository.resolve(rev);
        // A RevWalk allows walking over commits based on some filtering that is defined.
        try (RevWalk revWalk = new RevWalk(repository)) {
            RevCommit commit = revWalk.parseCommit(commitId);
            // and using commit's tree find the path
            RevTree tree = commit.getTree();
            // Now try to find a specific file.
            try (TreeWalk treeWalk = new TreeWalk(repository)) {
                treeWalk.addTree(tree);
                treeWalk.setRecursive(true);
                treeWalk.setFilter(PathFilter.create(filename));
                if (!treeWalk.next()) {
                    LOGGER.log(Level.FINEST, "Did not find expected file ''{0}'' in revision {1} " + "for repository ''{2}''", new Object[] { filename, rev, directory });
                    return result;
                }
                ObjectId objectId = treeWalk.getObjectId(0);
                ObjectLoader loader = repository.open(objectId);
                CountingOutputStream countingOutputStream = new CountingOutputStream(out);
                loader.copyTo(countingOutputStream);
                result.iterations = countingOutputStream.getCount();
                result.success = true;
            }
            revWalk.dispose();
        }
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, String.format("Failed to get file '%s' in revision %s for repository '%s'", filename, rev, directory), e);
    }
    return result;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) CountingOutputStream(org.eclipse.jgit.util.io.CountingOutputStream) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) File(java.io.File) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) Repository(org.eclipse.jgit.lib.Repository) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 29 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project fuse-karaf by jboss-fuse.

the class GitPatchRepositoryImpl method getFileContent.

@Override
public String getFileContent(Git fork, String sha1, String fileName) throws IOException {
    ObjectReader objectReader = fork.getRepository().newObjectReader();
    RevCommit commit = new RevWalk(fork.getRepository()).parseCommit(fork.getRepository().resolve(sha1));
    TreeWalk tw = new TreeWalk(fork.getRepository());
    tw.addTree(commit.getTree());
    tw.setRecursive(false);
    tw.setFilter(PathFilter.create(fileName));
    if (tw.next()) {
        ObjectId objectId = tw.getObjectId(0);
        ObjectLoader loader = fork.getRepository().open(objectId);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        loader.copyTo(out);
        return new String(out.toByteArray(), "UTF-8");
    }
    return null;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 30 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project gerrit by GerritCodeReview.

the class FileContentUtil method getContent.

public BinaryResult getContent(Repository repo, ProjectState project, ObjectId revstr, String path) throws IOException, ResourceNotFoundException, BadRequestException {
    try (RevWalk rw = new RevWalk(repo)) {
        RevCommit commit = rw.parseCommit(revstr);
        try (TreeWalk tw = TreeWalk.forPath(rw.getObjectReader(), path, commit.getTree())) {
            if (tw == null) {
                throw new ResourceNotFoundException();
            }
            org.eclipse.jgit.lib.FileMode mode = tw.getFileMode(0);
            ObjectId id = tw.getObjectId(0);
            if (mode == org.eclipse.jgit.lib.FileMode.GITLINK) {
                return BinaryResult.create(id.name()).setContentType(X_GIT_GITLINK).base64();
            }
            if (mode == org.eclipse.jgit.lib.FileMode.TREE) {
                throw new BadRequestException("cannot retrieve content of directories");
            }
            ObjectLoader obj = repo.open(id, OBJ_BLOB);
            byte[] raw;
            try {
                raw = obj.getCachedBytes(MAX_SIZE);
            } catch (LargeObjectException e) {
                raw = null;
            }
            String type;
            if (mode == org.eclipse.jgit.lib.FileMode.SYMLINK) {
                type = X_GIT_SYMLINK;
            } else {
                type = registry.getMimeType(path, raw).toString();
                type = resolveContentType(project, path, FileMode.FILE, type);
            }
            return asBinaryResult(raw, obj).setContentType(type).base64();
        }
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)37 RevWalk (org.eclipse.jgit.revwalk.RevWalk)24 ObjectId (org.eclipse.jgit.lib.ObjectId)21 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)20 RevCommit (org.eclipse.jgit.revwalk.RevCommit)19 IOException (java.io.IOException)14 Repository (org.eclipse.jgit.lib.Repository)14 ObjectReader (org.eclipse.jgit.lib.ObjectReader)12 File (java.io.File)9 FileOutputStream (java.io.FileOutputStream)6 Ref (org.eclipse.jgit.lib.Ref)6 RevTree (org.eclipse.jgit.revwalk.RevTree)6 LargeObjectException (org.eclipse.jgit.errors.LargeObjectException)5 FileMode (org.eclipse.jgit.lib.FileMode)5 IArchimateModel (com.archimatetool.model.IArchimateModel)4 HashMap (java.util.HashMap)3 FilestoreModel (com.gitblit.models.FilestoreModel)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 CompileException (com.googlecode.prolog_cafe.exceptions.CompileException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2