use of org.eclipse.jgit.lib.ObjectReader in project fabric8 by jboss-fuse.
the class GitPatchRepositoryImpl method diff.
@Override
public List<DiffEntry> diff(Git git, RevCommit commit1, RevCommit commit2, boolean showNameAndStatusOnly) throws GitAPIException, IOException {
ObjectReader reader = git.getRepository().newObjectReader();
CanonicalTreeParser ctp1 = new CanonicalTreeParser();
CanonicalTreeParser ctp2 = new CanonicalTreeParser();
if (commit1.getTree() == null) {
commit1 = new RevWalk(git.getRepository()).parseCommit(commit1);
}
if (commit2.getTree() == null) {
commit2 = new RevWalk(git.getRepository()).parseCommit(commit2);
}
ctp1.reset(reader, commit1.getTree());
ctp2.reset(reader, commit2.getTree());
return git.diff().setShowNameAndStatusOnly(showNameAndStatusOnly).setOldTree(ctp1).setNewTree(ctp2).call();
}
use of org.eclipse.jgit.lib.ObjectReader in project fabric8 by jboss-fuse.
the class GitConflictResolutionIT method reportingDiffs.
@Test
public void reportingDiffs() throws Exception {
prepareChanges();
ObjectReader reader = git.getRepository().newObjectReader();
RevWalk rw = new RevWalk(git.getRepository());
CanonicalTreeParser ctp1 = new CanonicalTreeParser();
CanonicalTreeParser ctp2 = new CanonicalTreeParser();
CanonicalTreeParser ctp3 = new CanonicalTreeParser();
ctp1.reset(reader, rw.parseCommit(git.getRepository().resolve("master")).getTree());
ctp2.reset(reader, rw.parseCommit(git.getRepository().resolve("custom")).getTree());
ctp3.reset(reader, rw.parseCommit(git.getRepository().resolve("patched")).getTree());
TreeWalk walk = new TreeWalk(reader);
walk.addTree(ctp1);
walk.addTree(ctp3);
walk.setRecursive(true);
List<DiffEntry> diffs = DiffEntry.scan(walk);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DiffFormatter df = new DiffFormatter(baos);
df.setDiffAlgorithm(DiffAlgorithm.getAlgorithm(DiffAlgorithm.SupportedAlgorithm.HISTOGRAM));
df.setDiffComparator(RawTextComparator.DEFAULT);
df.setRepository(git.getRepository());
df.format(diffs.get(0));
// System.out.println(new String(baos.toByteArray()));
AbbreviatedObjectId id1 = diffs.get(0).getOldId();
AbbreviatedObjectId id2 = diffs.get(0).getNewId();
byte[] bytes1 = reader.open(id1.toObjectId()).getBytes();
byte[] bytes2 = reader.open(id2.toObjectId()).getBytes();
RawText rt1 = new RawText(bytes1);
RawText rt2 = new RawText(bytes2);
EditList edits = DiffAlgorithm.getAlgorithm(DiffAlgorithm.SupportedAlgorithm.HISTOGRAM).diff(RawTextComparator.DEFAULT, rt1, rt2);
int aCur = 0;
for (Edit curEdit : edits) {
boolean prolog = aCur < curEdit.getBeginA();
if (prolog) {
System.out.print("<div class=\"edit unchanged\">");
}
while (aCur < curEdit.getBeginA()) {
System.out.println(rt1.getString(aCur++));
}
if (prolog) {
System.out.print("</div>");
}
if (curEdit.getType() == Edit.Type.INSERT) {
System.out.print("<div class=\"edit added\">");
for (int i = curEdit.getBeginB(); i < curEdit.getEndB(); i++) {
System.out.println(rt2.getString(i));
}
System.out.print("</div>");
}
if (curEdit.getType() == Edit.Type.REPLACE) {
System.out.print("<div class=\"edit changed\"><div class=\"edit removed\">");
for (int i = curEdit.getBeginA(); i < curEdit.getEndA(); i++) {
System.out.println(rt1.getString(i));
}
System.out.print("</div><div class=\"edit added\">");
for (int i = curEdit.getBeginB(); i < curEdit.getEndB(); i++) {
System.out.println(rt2.getString(i));
}
aCur = curEdit.getEndA();
System.out.print("</div></div>");
}
}
boolean prolog = aCur < rt1.size();
if (prolog) {
System.out.print("<div class=\"edit unchanged\">");
}
while (aCur < rt1.size()) {
System.out.println(rt1.getString(aCur++));
}
if (prolog) {
System.out.print("</div>");
}
}
use of org.eclipse.jgit.lib.ObjectReader in project searchcode-server by boyter.
the class GitService method fetchFileRevision.
/**
* Given a repository location, revision and file path will retrieve that files contents. N.B. it returns the whole
* file so you MAY end up running into serious memory issues, and should be aware of this
*/
public String fetchFileRevision(String repoLocation, String revision, String filePath) throws MissingObjectException, IncorrectObjectTypeException, IOException {
Repository localRepository = new FileRepository(new File(repoLocation));
ObjectId id = localRepository.resolve(revision);
ObjectReader reader = localRepository.newObjectReader();
try {
RevWalk walk = new RevWalk(reader);
RevCommit commit = walk.parseCommit(id);
RevTree tree = commit.getTree();
TreeWalk treewalk = TreeWalk.forPath(reader, filePath, tree);
if (treewalk != null) {
byte[] data = reader.open(treewalk.getObjectId(0)).getBytes();
return new String(data, "utf-8");
} else {
return "";
}
} finally {
reader.close();
}
}
use of org.eclipse.jgit.lib.ObjectReader 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.ObjectReader in project OpenGrok by OpenGrok.
the class GitRepository method determineCurrentVersion.
@Override
public String determineCurrentVersion(CommandTimeoutType cmdType) throws IOException {
try (org.eclipse.jgit.lib.Repository repository = getJGitRepository(getDirectoryName())) {
Ref head = repository.exactRef(Constants.HEAD);
if (head != null && head.getObjectId() != null) {
try (RevWalk walk = new RevWalk(repository);
ObjectReader reader = repository.newObjectReader()) {
RevCommit commit = walk.parseCommit(head.getObjectId());
int commitTime = commit.getCommitTime();
Date date = new Date((long) (commitTime) * 1000);
return String.format("%s %s %s %s", format(date), reader.abbreviate(head.getObjectId()).name(), commit.getAuthorIdent().getName(), commit.getShortMessage());
}
}
}
return null;
}
Aggregations