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);
}
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);
}
}
}
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;
}
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;
}
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();
}
}
}
Aggregations