本文整理匯總了Java中org.eclipse.jgit.diff.DiffFormatter.format方法的典型用法代碼示例。如果您正苦於以下問題:Java DiffFormatter.format方法的具體用法?Java DiffFormatter.format怎麽用?Java DiffFormatter.format使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.eclipse.jgit.diff.DiffFormatter
的用法示例。
在下文中一共展示了DiffFormatter.format方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getDiffBetweenCommits
import org.eclipse.jgit.diff.DiffFormatter; //導入方法依賴的package包/類
public String getDiffBetweenCommits(int commitIndex) throws IOException,GitAPIException{
if(commitIndex+1==commitCount)
return "Nothing to Diff. This is first commit";
AbstractTreeIterator current = prepareTreeParser(repository,commitSHA.get(commitIndex));
AbstractTreeIterator parent = prepareTreeParser(repository,commitSHA.get(++commitIndex));
ObjectReader reader = repository.newObjectReader();
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
// finally get the list of changed files
Git git = new Git(repository) ;
List<DiffEntry> diff = git.diff().
setOldTree(parent).
setNewTree(current).
//TODO Set the path filter to filter out the selected file
//setPathFilter(PathFilter.create("README.md")).
call();
for (DiffEntry entry : diff) {
System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId());
DiffFormatter formatter = new DiffFormatter(byteStream) ;
formatter.setRepository(repository);
formatter.format(entry);
}
// System.out.println(byteStream.toString());
String diffContent = byteStream.toString();
return byteStream.toString();
}
示例2: getDiffString
import org.eclipse.jgit.diff.DiffFormatter; //導入方法依賴的package包/類
private String getDiffString() throws GitAPIException, IOException {
ObjectId head = this.repo.resolve("HEAD");
if(head == null) return "";
// The following code is largely written by Tk421 on StackOverflow
// (http://stackoverflow.com/q/23486483)
// Thanks! NOTE: comments are mine.
ByteArrayOutputStream diffOutputStream = new ByteArrayOutputStream();
DiffFormatter formatter = new DiffFormatter(diffOutputStream);
formatter.setRepository(this.repo);
formatter.setPathFilter(PathFilter.create(this.pathFilter.replaceAll("\\\\","/")));
AbstractTreeIterator commitTreeIterator = prepareTreeParser(this.repo, head.getName());
FileTreeIterator workTreeIterator = new FileTreeIterator(this.repo);
// Scan gets difference between the two iterators.
formatter.format(commitTreeIterator, workTreeIterator);
return diffOutputStream.toString();
}
示例3: formatHtmlDiff
import org.eclipse.jgit.diff.DiffFormatter; //導入方法依賴的package包/類
private void formatHtmlDiff(OutputStream out,
Repository repo, RevWalk walk,
AbstractTreeIterator oldTree, AbstractTreeIterator newTree,
String path)
throws IOException {
DiffFormatter diff = new HtmlDiffFormatter(renderer, out);
try {
if (!path.equals("")) {
diff.setPathFilter(PathFilter.create(path));
}
diff.setRepository(repo);
diff.setDetectRenames(true);
diff.format(oldTree, newTree);
} finally {
diff.release();
}
}
示例4: getCommitStatistics
import org.eclipse.jgit.diff.DiffFormatter; //導入方法依賴的package包/類
/** 각 유저가 날짜별로 커밋을 한 정보를 취합함.
* @return
*/
public GitParentStatistics getCommitStatistics(){
GitParentStatistics gitParentStatistics = new GitParentStatistics();
try{
for(RevCommit rc:git.log().all().call()){
String diffs = new String();
ByteArrayOutputStream out = new ByteArrayOutputStream();
if(rc.getParentCount()>0){
DiffFormatter df = new DiffFormatter(out);
df.setRepository(this.localRepo);
df.format(rc.getParent(0), rc);
df.flush();
diffs = out.toString();
} else {
diffs = simpleFileBrowser(rc);
}
int addFile = StringUtils.countOccurrencesOf(diffs, "--- /dev/null");// 추가한 파일 수
int deleteFile = StringUtils.countOccurrencesOf(diffs, "+++ /dev/null");// 삭제한 파일 수
diffs = StringUtils.delete(diffs, "\n--- ");
diffs = StringUtils.delete(diffs, "\n+++ ");
gitParentStatistics.addGitChildStatistics(
new GitChildStatistics(
rc.getAuthorIdent().getEmailAddress(), // 유저 이메일
StringUtils.countOccurrencesOf(diffs, "\n+"), //추가한 라인 수
StringUtils.countOccurrencesOf(diffs, "\n-"), //삭제한 라인 수
addFile,
deleteFile,
rc.getAuthorIdent().getWhen())); // 날짜
}
}catch(Exception e){
System.err.println(e.getMessage());
}
return gitParentStatistics;
}
示例5: testDiffRenameDetectionProblem
import org.eclipse.jgit.diff.DiffFormatter; //導入方法依賴的package包/類
public void testDiffRenameDetectionProblem () throws Exception {
File file = new File(workDir, "file");
File renamed = new File(workDir, "renamed");
File patchFile = new File(workDir.getParentFile(), "diff.patch");
write(file, "hey, i will be renamed\n");
add(file);
commit(file);
file.renameTo(renamed);
write(renamed, "hey, i will be renamed\nand now i am\n");
OutputStream out = new BufferedOutputStream(new FileOutputStream(patchFile));
DiffFormatter formatter = new DiffFormatter(out);
formatter.setRepository(repository);
ObjectReader or = null;
try {
formatter.setDetectRenames(true);
AbstractTreeIterator firstTree = new DirCacheIterator(repository.readDirCache());;
AbstractTreeIterator secondTree = new FileTreeIterator(repository);
formatter.format(firstTree, secondTree);
formatter.flush();
fail("Fixed in JGit, modify and simplify the sources in ExportDiff command");
} catch (IOException ex) {
assertEquals("Missing blob 7b34a309b8dbae2686c9e597efef28a612e48aff", ex.getMessage());
} finally {
if (or != null) {
or.release();
}
formatter.release();
}
}
示例6: formatDiffEntry
import org.eclipse.jgit.diff.DiffFormatter; //導入方法依賴的package包/類
private void formatDiffEntry(DiffFormatter formatter, DiffEntry entry) {
try {
formatter.format(entry);
} catch (IOException e) {
throw new GitRepositoryIOException(repository.getRemote(), e);
}
}
示例7: analyzeDiff
import org.eclipse.jgit.diff.DiffFormatter; //導入方法依賴的package包/類
private void analyzeDiff(Change change, DiffEntry diff) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
DiffFormatter df = new DiffFormatter(output);
df.setRepository(git.getRepository());
df.format(diff);
Scanner scanner = new Scanner(output.toString("UTF-8"));
int added = 0;
int removed = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("+") && !line.startsWith("+++")) {
added++;
} else if (line.startsWith("-") && !line.startsWith("---")) {
removed++;
}
}
output.close();
df.close();
scanner.close();
change.setLinesAdded(added);
change.setLinesRemoved(removed);
}
示例8: parseDiff
import org.eclipse.jgit.diff.DiffFormatter; //導入方法依賴的package包/類
private String parseDiff(Context context, RevWalk walk, RevCommit revCommit) throws Exception {
if (context.isIndexingDiff() && revCommit.getParentCount() > 0) {
RevCommit parentCommit = walk.parseCommit(revCommit.getParent(0).getId());
ByteArrayOutputStream diffOutputStream = new ByteArrayOutputStream();
DiffFormatter diffFormatter = new DiffFormatter(diffOutputStream);
diffFormatter.setRepository(context.getRepository());
diffFormatter.setDiffComparator(RawTextComparator.DEFAULT);
diffFormatter.setDetectRenames(true);
diffFormatter.format(parentCommit.getTree(), revCommit.getTree());
return new String(diffOutputStream.toByteArray());
} else {
return null;
}
}
示例9: convert
import org.eclipse.jgit.diff.DiffFormatter; //導入方法依賴的package包/類
/**
* Construct a generic file object from the attributes of a git DiffEntry.
*
* @param diff DiffEntry data object
* @return CMnFile data object
*/
private CMnFile convert(DiffEntry diff) {
String filename = diff.getNewPath();
CMnFile.Operation op = null;
DiffEntry.ChangeType changeType = diff.getChangeType();
if (changeType == DiffEntry.ChangeType.ADD){
op = CMnFile.Operation.ADD;
} else if (changeType == DiffEntry.ChangeType.DELETE){
op = CMnFile.Operation.DELETE;
} else if (changeType == DiffEntry.ChangeType.RENAME){
op = CMnFile.Operation.RENAME;
} else {
op = CMnFile.Operation.EDIT;
}
CMnFile file = new CMnFile(filename, op);
// Convert the file diff to a string
try {
OutputStream out = new ByteArrayOutputStream();
DiffFormatter df = new DiffFormatter(out);
df.setRepository(repository.getRepository());
df.setDiffComparator(diffComparator);
df.setDetectRenames(true);
df.format(diff);
df.flush();
file.setDiff(out.toString());
} catch (IOException ioex) {
}
return file;
}
示例10: run
import org.eclipse.jgit.diff.DiffFormatter; //導入方法依賴的package包/類
@Override
protected void run() throws GitException {
Repository repository = getRepository();
DiffFormatter formatter = new DiffFormatter(out);
formatter.setRepository(repository);
ObjectReader or = null;
String workTreePath = repository.getWorkTree().getAbsolutePath();
try {
Collection<PathFilter> pathFilters = Utils.getPathFilters(repository.getWorkTree(), roots);
if (!pathFilters.isEmpty()) {
formatter.setPathFilter(PathFilterGroup.create(pathFilters));
}
if (repository.getConfig().get(WorkingTreeOptions.KEY).getAutoCRLF() != CoreConfig.AutoCRLF.FALSE) {
// work-around for autocrlf
formatter.setDiffComparator(new AutoCRLFComparator());
}
or = repository.newObjectReader();
AbstractTreeIterator firstTree = getIterator(firstCommit, or);
AbstractTreeIterator secondTree = getIterator(secondCommit, or);
List<DiffEntry> diffEntries;
if (secondTree instanceof WorkingTreeIterator) {
// remote when fixed in JGit, see ExportDiffTest.testDiffRenameDetectionProblem
formatter.setDetectRenames(false);
diffEntries = formatter.scan(firstTree, secondTree);
formatter.setDetectRenames(true);
RenameDetector detector = formatter.getRenameDetector();
detector.reset();
detector.addAll(diffEntries);
diffEntries = detector.compute(new ContentSource.Pair(ContentSource.create(or), ContentSource.create((WorkingTreeIterator) secondTree)), NullProgressMonitor.INSTANCE);
} else {
formatter.setDetectRenames(true);
diffEntries = formatter.scan(firstTree, secondTree);
}
for (DiffEntry ent : diffEntries) {
if (monitor.isCanceled()) {
break;
}
listener.notifyFile(new File(workTreePath + File.separator + ent.getNewPath()), ent.getNewPath());
formatter.format(ent);
}
formatter.flush();
} catch (IOException ex) {
throw new GitException(ex);
} finally {
if (or != null) {
or.release();
}
formatter.release();
}
}
示例11: doDiff
import org.eclipse.jgit.diff.DiffFormatter; //導入方法依賴的package包/類
protected String doDiff(Git git, String objectId, String baseObjectId, String pathOrBlobPath) throws IOException {
Repository r = git.getRepository();
String blobPath = trimLeadingSlash(pathOrBlobPath);
RevCommit commit;
if (Strings.isNotBlank(objectId)) {
commit = CommitUtils.getCommit(r, objectId);
} else {
commit = CommitUtils.getHead(r);
}
RevCommit baseCommit = null;
if (Strings.isNotBlank(baseObjectId) && !Objects.equals(baseObjectId, objectId)) {
baseCommit = CommitUtils.getCommit(r, baseObjectId);
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DiffFormatter formatter = createDiffFormatter(r, buffer);
RevTree commitTree = commit.getTree();
RevTree baseTree;
if (baseCommit == null) {
if (commit.getParentCount() > 0) {
final RevWalk rw = new RevWalk(r);
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 = formatter.scan(baseTree, commitTree);
if (blobPath != null && blobPath.length() > 0) {
for (DiffEntry diffEntry : diffEntries) {
if (diffEntry.getNewPath().equalsIgnoreCase(blobPath)) {
formatter.format(diffEntry);
break;
}
}
} else {
formatter.format(diffEntries);
}
formatter.flush();
return buffer.toString();
}
示例12: writeRawDiff
import org.eclipse.jgit.diff.DiffFormatter; //導入方法依賴的package包/類
private void writeRawDiff(List<DiffEntry> diff, DiffFormatter formatter) throws IOException {
formatter.format(diff);
formatter.flush();
}
示例13: CommitDTO
import org.eclipse.jgit.diff.DiffFormatter; //導入方法依賴的package包/類
public CommitDTO(Git git, NamespaceName projectNamespaceName, RevCommit commit, String repoUrl, String branch) {
this.namespace = projectNamespaceName.getNamespace();
this.app = projectNamespaceName.getName();
this.repoUrl = repoUrl;
this.branch = branch;
this.sha = commit.getId().getName();
this.author = PersonIdentDTO.newInstance(commit.getAuthorIdent());
this.committer = PersonIdentDTO.newInstance(commit.getCommitterIdent());
this.fullMessage = commit.getFullMessage();
this.name = commit.getName();
this.commitTime = GitHelpers.getCommitDate(commit);
this.shortMessage = commit.getShortMessage();
// TODO how to figure out the number of lines added/removed from DiffEntry + HunkHeader?
// lets try find out the lines added / updated / deleted for this commit
try {
Repository r = git.getRepository();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DiffFormatter formatter = createDiffFormatter(r, buffer);
RevCommit baseCommit = null;
RevTree commitTree = commit.getTree();
RevTree baseTree;
if (baseCommit == null) {
if (commit.getParentCount() > 0) {
final RevWalk rw = new RevWalk(r);
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 = formatter.scan(baseTree, commitTree);
for (DiffEntry diffEntry : diffEntries) {
formatter.format(diffEntry);
/*
FileHeader fileHeader = formatter.toFileHeader(diffEntry);
List<? extends HunkHeader> hunks = fileHeader.getHunks();
for (HunkHeader hunk : hunks) {
// linesAdded += hunk.getOldImage().getLinesAdded();
// linesRemoved += hunk.getOldImage().getLinesDeleted();
}
*/
}
// TODO should we store the diff? thats maybe too big?
formatter.flush();
String diff = buffer.toString();
if (diff != null) {
String[] lines = diff.split("\n");
for (String line : lines) {
if (line.length() == 0 || line.startsWith("diff ") || line.startsWith("index ") || line.startsWith("--- ") || line.startsWith("+++ ")) {
continue;
}
if (line.startsWith("+")) {
linesAdded++;
} else if (line.startsWith("-")) {
linesRemoved++;
}
}
}
} catch (IOException e) {
LOG.warn("Failed to extract lines changed for " + projectNamespaceName + " branch: " + branch + " commit: " + sha + ". " + e, e);
}
}
示例14: subscribe
import org.eclipse.jgit.diff.DiffFormatter; //導入方法依賴的package包/類
@Override
public DdpSubscription subscribe(DdpPublishContext context, String name, JsonArray params) throws IOException {
SimpleDdpSubscription subscription = new SimpleDdpSubscription(context) {
@Override
protected Iterable<Entry<String, Jsonable>> getInitialItems() throws Exception {
Map<String, Jsonable> items = Maps.newHashMap();
String repoName = DdpJson.optionalString(params, 0);
String sha = DdpJson.optionalString(params, 1);
GitUser user = getGitUser(context);
GitRepository gitRepo = store.findRepo(repoName);
if (gitRepo == null) {
throw new IllegalArgumentException();
}
if (!user.canAccess(gitRepo)) {
// log.debug("Attempt to access unauthorized repo {}", repo);
throw new IllegalArgumentException();
}
Repository repository = store.openRepository(user, gitRepo, true);
String repoId = buildIdForRepo(gitRepo);
// The {tree} will return the underlying tree-id instead of the commit-id itself!
// For a description of what the carets do see e.g. http://www.paulboxley.com/blog/2011/06/git-caret-and-tilde
// This means we are selecting the parent of the parent of the parent of the parent of current HEAD and
// take the tree-ish of it
ObjectId head = repository.resolve(sha + "^{tree}");
ObjectId oldHead = repository.resolve(sha + "^^{tree}");
System.out.println("Printing diff between tree: " + oldHead + " and " + head);
// prepare the two iterators to compute the diff between
ObjectReader reader = repository.newObjectReader();
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset(reader, oldHead);
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset(reader, head);
// finally get the list of changed files
List<DiffEntry> diffs = new Git(repository).diff().setNewTree(newTreeIter).setOldTree(oldTreeIter).call();
int i = 0;
for (DiffEntry entry : diffs) {
String id = repoId + "_" + sha + "_" + i++;
JsonObject json = new JsonObject();
json.addProperty("repo", gitRepo.getName());
json.addProperty("sha", sha);
json.addProperty("newPath", entry.getNewPath());
json.addProperty("oldPath", entry.getOldPath());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DiffFormatter formatter = new DiffFormatter(baos);
formatter.setRepository(repository);
formatter.format(entry);
json.addProperty("diff", new String(baos.toByteArray(), Charsets.UTF_8));
items.put(id, Jsonable.fromJson(json));
}
repository.close();
return items.entrySet();
}
};
return subscription;
}