use of org.eclipse.jgit.revwalk.RevTree in project che by eclipse.
the class JGitConnection method showFileContent.
@Override
public ShowFileContentResponse showFileContent(String file, String version) throws GitException {
String content;
ObjectId revision;
try {
revision = getRepository().resolve(version);
try (RevWalk revWalk = new RevWalk(getRepository())) {
RevCommit revCommit = revWalk.parseCommit(revision);
RevTree tree = revCommit.getTree();
try (TreeWalk treeWalk = new TreeWalk(getRepository())) {
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
treeWalk.setFilter(PathFilter.create(file));
if (!treeWalk.next()) {
throw new GitException("fatal: Path '" + file + "' does not exist in '" + version + "'" + lineSeparator());
}
ObjectId objectId = treeWalk.getObjectId(0);
ObjectLoader loader = repository.open(objectId);
content = new String(loader.getBytes());
}
}
} catch (IOException exception) {
throw new GitException(exception.getMessage());
}
return newDto(ShowFileContentResponse.class).withContent(content);
}
use of org.eclipse.jgit.revwalk.RevTree in project che by eclipse.
the class JGitDiffPage method commitToIndex.
/**
* Show changes between specified revision and index. If
* <code>commitId == null</code> then view changes between HEAD and index.
*
* @param commitId
* id of commit, pass <code>null</code> is the same as pass HEAD
* @param formatter
* diff formatter
* @return list of diff entries
* @throws IOException
* if any i/o errors occurs
*/
private List<DiffEntry> commitToIndex(String commitId, DiffFormatter formatter) throws IOException {
if (commitId == null) {
commitId = Constants.HEAD;
}
ObjectId commitA = repository.resolve(commitId);
if (commitA == null) {
throw new IllegalArgumentException("Invalid commit id " + commitId);
}
RevTree treeA;
try (RevWalk revWalkA = new RevWalk(repository)) {
treeA = revWalkA.parseTree(commitA);
}
DirCache dirCache = null;
List<DiffEntry> diff;
try (ObjectReader reader = repository.newObjectReader()) {
dirCache = repository.lockDirCache();
CanonicalTreeParser iterA = new CanonicalTreeParser();
iterA.reset(reader, treeA);
DirCacheIterator iterB = new DirCacheIterator(dirCache);
if (!params.isNoRenames()) {
// Use embedded RenameDetector it works well with index and
// revision history.
formatter.setDetectRenames(true);
int renameLimit = params.getRenameLimit();
if (renameLimit > 0) {
formatter.getRenameDetector().setRenameLimit(renameLimit);
}
}
diff = formatter.scan(iterA, iterB);
} finally {
if (dirCache != null) {
dirCache.unlock();
}
}
return diff;
}
use of org.eclipse.jgit.revwalk.RevTree in project gitblit by gitblit.
the class JGitUtils method getNotesOnCommit.
/**
* Returns the list of notes entered about the commit from the refs/notes
* namespace. If the repository does not exist or is empty, an empty list is
* returned.
*
* @param repository
* @param commit
* @return list of notes
*/
public static List<GitNote> getNotesOnCommit(Repository repository, RevCommit commit) {
List<GitNote> list = new ArrayList<GitNote>();
if (!hasCommits(repository)) {
return list;
}
List<RefModel> noteBranches = getNoteBranches(repository, true, -1);
for (RefModel notesRef : noteBranches) {
RevTree notesTree = JGitUtils.getCommit(repository, notesRef.getName()).getTree();
// flat notes list
String notePath = commit.getName();
String text = getStringContent(repository, notesTree, notePath);
if (!StringUtils.isEmpty(text)) {
List<RevCommit> history = getRevLog(repository, notesRef.getName(), notePath, 0, -1);
RefModel noteRef = new RefModel(notesRef.displayName, null, history.get(history.size() - 1));
GitNote gitNote = new GitNote(noteRef, text);
list.add(gitNote);
continue;
}
// folder structure
StringBuilder sb = new StringBuilder(commit.getName());
sb.insert(2, '/');
notePath = sb.toString();
text = getStringContent(repository, notesTree, notePath);
if (!StringUtils.isEmpty(text)) {
List<RevCommit> history = getRevLog(repository, notesRef.getName(), notePath, 0, -1);
RefModel noteRef = new RefModel(notesRef.displayName, null, history.get(history.size() - 1));
GitNote gitNote = new GitNote(noteRef, text);
list.add(gitNote);
}
}
return list;
}
use of org.eclipse.jgit.revwalk.RevTree in project gitblit by gitblit.
the class DiffUtils method getDiffStat.
/**
* Returns the diffstat between the two commits for the specified file or folder.
*
* @param repository
* @param baseCommit
* if base commit is unspecified, the diffstat is generated against
* the primary parent of the specified commit.
* @param commit
* @param path
* if path is specified, the diffstat is generated only for the
* specified file or folder. if unspecified, the diffstat is
* generated for the entire diff between the two commits.
* @return patch as a string
*/
public static DiffStat getDiffStat(Repository repository, RevCommit baseCommit, RevCommit commit, String path) {
DiffStat stat = null;
try {
RawTextComparator cmp = RawTextComparator.DEFAULT;
DiffStatFormatter df = new DiffStatFormatter(commit.getName(), repository);
df.setRepository(repository);
df.setDiffComparator(cmp);
df.setDetectRenames(true);
RevTree commitTree = commit.getTree();
RevTree baseTree;
if (baseCommit == null) {
if (commit.getParentCount() > 0) {
final RevWalk rw = new RevWalk(repository);
RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
baseTree = parent.getTree();
} else {
// FIXME initial commit. no parent?!
baseTree = commitTree;
}
} else {
baseTree = baseCommit.getTree();
}
List<DiffEntry> diffEntries = df.scan(baseTree, commitTree);
if (path != null && path.length() > 0) {
for (DiffEntry diffEntry : diffEntries) {
if (diffEntry.getNewPath().equalsIgnoreCase(path)) {
df.format(diffEntry);
break;
}
}
} else {
df.format(diffEntries);
}
stat = df.getDiffStat();
df.flush();
} catch (Throwable t) {
LOGGER.error("failed to generate commit diff!", t);
}
return stat;
}
use of org.eclipse.jgit.revwalk.RevTree in project gitblit by gitblit.
the class DiffUtils method getDiff.
/**
* Returns the diff between two commits for the specified file.
*
* @param repository
* @param baseCommit
* if base commit is null the diff is to the primary parent of
* the commit.
* @param commit
* @param path
* if the path is specified, the diff is restricted to that file
* or folder. if unspecified, the diff is for the entire commit.
* @param comparator
* @param outputType
* @param handler
* to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.
* May be {@code null}, resulting in the default behavior.
* @param tabLength
* @return the diff
*/
public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit, String path, DiffComparator comparator, DiffOutputType outputType, final BinaryDiffHandler handler, int tabLength) {
DiffStat stat = null;
String diff = null;
try {
ByteArrayOutputStream os = null;
DiffFormatter df;
switch(outputType) {
case HTML:
df = new GitBlitDiffFormatter(commit.getName(), repository, path, handler, tabLength);
break;
case PLAIN:
default:
os = new ByteArrayOutputStream();
df = new DiffFormatter(os);
break;
}
df.setRepository(repository);
df.setDiffComparator((comparator == null ? DiffComparator.SHOW_WHITESPACE : comparator).textComparator);
df.setDetectRenames(true);
RevTree commitTree = commit.getTree();
RevTree baseTree;
if (baseCommit == null) {
if (commit.getParentCount() > 0) {
final RevWalk rw = new RevWalk(repository);
RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
rw.dispose();
baseTree = parent.getTree();
} else {
// FIXME initial commit. no parent?!
baseTree = commitTree;
}
} else {
baseTree = baseCommit.getTree();
}
List<DiffEntry> diffEntries = df.scan(baseTree, commitTree);
if (path != null && path.length() > 0) {
for (DiffEntry diffEntry : diffEntries) {
if (diffEntry.getNewPath().equalsIgnoreCase(path)) {
df.format(diffEntry);
break;
}
}
} else {
df.format(diffEntries);
}
df.flush();
if (df instanceof GitBlitDiffFormatter) {
// workaround for complex private methods in DiffFormatter
diff = ((GitBlitDiffFormatter) df).getHtml();
stat = ((GitBlitDiffFormatter) df).getDiffStat();
} else {
diff = os.toString();
}
} catch (Throwable t) {
LOGGER.error("failed to generate commit diff!", t);
}
return new DiffOutput(outputType, diff, stat);
}
Aggregations