本文整理匯總了Java中org.eclipse.jgit.treewalk.TreeWalk.reset方法的典型用法代碼示例。如果您正苦於以下問題:Java TreeWalk.reset方法的具體用法?Java TreeWalk.reset怎麽用?Java TreeWalk.reset使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.eclipse.jgit.treewalk.TreeWalk
的用法示例。
在下文中一共展示了TreeWalk.reset方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getSubmoduleCommitId
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
public static String getSubmoduleCommitId(Repository repository, String path, RevCommit commit) {
String commitId = null;
RevWalk rw = new RevWalk(repository);
TreeWalk tw = new TreeWalk(repository);
tw.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(path)));
try {
tw.reset(commit.getTree());
while (tw.next()) {
if (tw.isSubtree() && !path.equals(tw.getPathString())) {
tw.enterSubtree();
continue;
}
if (FileMode.GITLINK == tw.getFileMode(0)) {
commitId = tw.getObjectId(0).getName();
break;
}
}
} catch (Throwable t) {
error(t, repository, "{0} can't find {1} in commit {2}", path, commit.name());
} finally {
rw.dispose();
tw.release();
}
return commitId;
}
示例2: isDirectory
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
/** 파일주소와 커밋아이디를 바탕으로 디렉토리인지 검사함.
* @param commitID
* @param filePath
* @return
*/
public boolean isDirectory(String commitID, String filePath){
if(filePath.length() == 0)
return true;
try{
ObjectId revId = this.localRepo.resolve(commitID);
TreeWalk treeWalk = new TreeWalk(this.localRepo);
treeWalk.addTree(new RevWalk(this.localRepo).parseTree(revId));
treeWalk.setRecursive(true);
while (treeWalk.next()) {
if(treeWalk.getPathString().equals(filePath)){
return false;
}
}
treeWalk.reset(new RevWalk(this.localRepo).parseTree(revId));
while (treeWalk.next()) {
if(treeWalk.getPathString().startsWith(filePath)){
return true;
}
}
}catch(Exception e){
return false;
}
return false;
}
示例3: listFiles
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
private void listFiles() throws GitException {
RevWalk revWalk = new RevWalk(repository);
TreeWalk walk = new TreeWalk(repository);
try {
List<GitFileInfo> result;
walk.reset();
walk.setRecursive(true);
RevCommit parentCommit = null;
if (revCommit.getParentCount() > 0) {
for (RevCommit commit : revCommit.getParents()) {
revWalk.markStart(revWalk.lookupCommit(commit));
}
revWalk.setRevFilter(RevFilter.MERGE_BASE);
Iterator<RevCommit> it = revWalk.iterator();
if (it.hasNext()) {
parentCommit = it.next();
}
if (parentCommit != null) {
walk.addTree(parentCommit.getTree().getId());
}
}
walk.addTree(revCommit.getTree().getId());
walk.setFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, PathFilter.ANY_DIFF));
if (parentCommit != null) {
result = Utils.getDiffEntries(repository, walk, GitClassFactoryImpl.getInstance());
} else {
result = new ArrayList<GitFileInfo>();
while (walk.next()) {
result.add(new GitFileInfo(new File(repository.getWorkTree(), walk.getPathString()), walk.getPathString(), GitFileInfo.Status.ADDED, null, null));
}
}
this.modifiedFiles = result.toArray(new GitFileInfo[result.size()]);
} catch (IOException ex) {
throw new GitException(ex);
} finally {
revWalk.release();
walk.release();
}
}
示例4: checkout
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
public void checkout () throws IOException, GitException {
TreeWalk treeWalk = new TreeWalk(repository);
Collection<String> relativePaths = Utils.getRelativePaths(repository.getWorkTree(), roots);
if (!relativePaths.isEmpty()) {
treeWalk.setFilter(PathFilterGroup.createFromStrings(relativePaths));
}
treeWalk.setRecursive(true);
treeWalk.reset();
treeWalk.addTree(new DirCacheIterator(cache));
treeWalk.addTree(new FileTreeIterator(repository));
String lastAddedPath = null;
ObjectReader od = repository.newObjectReader();
try {
while (treeWalk.next() && !monitor.isCanceled()) {
File path = new File(repository.getWorkTree(), treeWalk.getPathString());
if (treeWalk.getPathString().equals(lastAddedPath)) {
// skip conflicts
continue;
} else {
lastAddedPath = treeWalk.getPathString();
}
DirCacheIterator dit = treeWalk.getTree(0, DirCacheIterator.class);
FileTreeIterator fit = treeWalk.getTree(1, FileTreeIterator.class);
if (dit != null && (recursively || directChild(roots, repository.getWorkTree(), path)) && (fit == null || fit.isModified(dit.getDirCacheEntry(), checkContent, od))) {
// update entry
listener.notifyFile(path, treeWalk.getPathString());
checkoutEntry(repository, path, dit.getDirCacheEntry(), od);
}
}
} finally {
od.release();
treeWalk.release();
}
}
示例5: updateFollowFilter
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
private void updateFollowFilter(ObjectId[] trees, DiffConfig cfg)
throws MissingObjectException, IncorrectObjectTypeException,
CorruptObjectException, IOException {
TreeWalk tw = pathFilter;
FollowFilter oldFilter = (FollowFilter) tw.getFilter();
tw.setFilter(TreeFilter.ANY_DIFF);
tw.reset(trees);
List<DiffEntry> files = DiffEntry.scan(tw);
RenameDetector rd = new RenameDetector(tw.getObjectReader(), cfg);
rd.addAll(files);
files = rd.compute();
TreeFilter newFilter = oldFilter;
for (DiffEntry ent : files) {
if (isRename(ent) && ent.getNewPath().equals(oldFilter.getPath())) {
newFilter = FollowFilter.create(ent.getOldPath(), cfg);
RenameCallback callback = oldFilter.getRenameCallback();
if (callback != null) {
callback.renamed(ent);
// forward the callback to the new follow filter
((FollowFilter) newFilter).setRenameCallback(callback);
}
break;
}
}
tw.setFilter(newFilter);
}
示例6: streamFromRepo
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
protected boolean streamFromRepo(HttpServletRequest request, HttpServletResponse response, Repository repository, RevCommit commit,
String requestedPath) throws IOException {
boolean served = false;
RevWalk rw = new RevWalk(repository);
TreeWalk tw = new TreeWalk(repository);
try {
tw.reset();
tw.addTree(commit.getTree());
PathFilter f = PathFilter.create(requestedPath);
tw.setFilter(f);
tw.setRecursive(true);
MutableObjectId id = new MutableObjectId();
ObjectReader reader = tw.getObjectReader();
while (tw.next()) {
FileMode mode = tw.getFileMode(0);
if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
continue;
}
tw.getObjectId(id, 0);
String filename = StringUtils.getLastPathElement(requestedPath);
try {
String userAgent = request.getHeader("User-Agent");
if (userAgent != null && userAgent.indexOf("MSIE 5.5") > -1) {
response.setHeader("Content-Disposition", "filename=\"" + URLEncoder.encode(filename, Constants.ENCODING) + "\"");
} else if (userAgent != null && userAgent.indexOf("MSIE") > -1) {
response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(filename, Constants.ENCODING) + "\"");
} else {
response.setHeader("Content-Disposition", "attachment; filename=\""
+ new String(filename.getBytes(Constants.ENCODING), "latin1") + "\"");
}
} catch (UnsupportedEncodingException e) {
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
}
long len = reader.getObjectSize(id, org.eclipse.jgit.lib.Constants.OBJ_BLOB);
setContentType(response, "application/octet-stream");
response.setIntHeader("Content-Length", (int) len);
ObjectLoader ldr = repository.open(id);
ldr.copyTo(response.getOutputStream());
served = true;
}
} finally {
tw.close();
rw.dispose();
}
response.flushBuffer();
return served;
}
示例7: zip
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
/**
* Zips the contents of the tree at the (optionally) specified revision and the (optionally) specified basepath to the supplied outputstream.
*
* @param repository
* @param basePath
* if unspecified, entire repository is assumed.
* @param objectId
* if unspecified, HEAD is assumed.
* @param os
* @return true if repository was successfully zipped to supplied output stream
*/
public static boolean zip(Repository repository, String basePath, String objectId, OutputStream os) {
RevCommit commit = JGitUtils.getCommit(repository, objectId);
if (commit == null) {
return false;
}
boolean success = false;
RevWalk rw = new RevWalk(repository);
TreeWalk tw = new TreeWalk(repository);
try {
tw.reset();
tw.addTree(commit.getTree());
ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os);
zos.setComment("Generated by Gitblit");
if (!StringUtils.isEmpty(basePath)) {
PathFilter f = PathFilter.create(basePath);
tw.setFilter(f);
}
tw.setRecursive(true);
MutableObjectId id = new MutableObjectId();
ObjectReader reader = tw.getObjectReader();
long modified = commit.getAuthorIdent().getWhen().getTime();
while (tw.next()) {
FileMode mode = tw.getFileMode(0);
if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
continue;
}
tw.getObjectId(id, 0);
ZipArchiveEntry entry = new ZipArchiveEntry(tw.getPathString());
entry.setSize(reader.getObjectSize(id, Constants.OBJ_BLOB));
entry.setComment(commit.getName());
entry.setUnixMode(mode.getBits());
entry.setTime(modified);
zos.putArchiveEntry(entry);
ObjectLoader ldr = repository.open(id);
ldr.copyTo(zos);
zos.closeArchiveEntry();
}
zos.finish();
success = true;
} catch (IOException e) {
error(e, repository, "{0} failed to zip files from commit {1}", commit.getName());
} finally {
tw.close();
rw.close();
rw.dispose();
}
return success;
}
示例8: getByteContent
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
/**
* Retrieves the raw byte content of a file in the specified tree.
*
* @param repository
* @param tree
* if null, the RevTree from HEAD is assumed.
* @param path
* @return content as a byte []
*/
public static byte[] getByteContent(Repository repository, RevTree tree, final String path, boolean throwError) {
RevWalk rw = new RevWalk(repository);
TreeWalk tw = new TreeWalk(repository);
tw.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(path)));
byte[] content = null;
try {
if (tree == null) {
ObjectId object = getDefaultBranch(repository);
RevCommit commit = rw.parseCommit(object);
tree = commit.getTree();
}
tw.reset(tree);
while (tw.next()) {
if (tw.isSubtree() && !path.equals(tw.getPathString())) {
tw.enterSubtree();
continue;
}
ObjectId entid = tw.getObjectId(0);
FileMode entmode = tw.getFileMode(0);
if (entmode != FileMode.GITLINK) {
RevObject ro = rw.lookupAny(entid, entmode.getObjectType());
rw.parseBody(ro);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectLoader ldr = repository.open(ro.getId(), Constants.OBJ_BLOB);
byte[] tmp = new byte[4096];
InputStream in = ldr.openStream();
int n;
while ((n = in.read(tmp)) > 0) {
os.write(tmp, 0, n);
}
in.close();
content = os.toByteArray();
}
}
} catch (Throwable t) {
if (throwError) {
error(t, repository, "{0} can't find {1} in tree {2}", path, tree.name());
}
} finally {
rw.dispose();
tw.release();
}
return content;
}
示例9: zip
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
/**
* Zips the contents of the tree at the (optionally) specified revision and
* the (optionally) specified basepath to the supplied outputstream.
*
* @param repository
* @param basePath
* if unspecified, entire repository is assumed.
* @param objectId
* if unspecified, HEAD is assumed.
* @param os
* @return true if repository was successfully zipped to supplied output
* stream
*/
public static boolean zip(Repository repository, String basePath, String objectId,
OutputStream os) {
RevCommit commit = JGitUtils.getCommit(repository, objectId);
if (commit == null) {
return false;
}
boolean success = false;
RevWalk rw = new RevWalk(repository);
TreeWalk tw = new TreeWalk(repository);
try {
tw.reset();
tw.addTree(commit.getTree());
ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os);
zos.setComment("Generated by Gitblit");
if (!StringUtils.isEmpty(basePath)) {
PathFilter f = PathFilter.create(basePath);
tw.setFilter(f);
}
tw.setRecursive(true);
MutableObjectId id = new MutableObjectId();
ObjectReader reader = tw.getObjectReader();
long modified = commit.getAuthorIdent().getWhen().getTime();
while (tw.next()) {
FileMode mode = tw.getFileMode(0);
if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
continue;
}
tw.getObjectId(id, 0);
ZipArchiveEntry entry = new ZipArchiveEntry(tw.getPathString());
entry.setSize(reader.getObjectSize(id, Constants.OBJ_BLOB));
entry.setComment(commit.getName());
entry.setUnixMode(mode.getBits());
entry.setTime(modified);
zos.putArchiveEntry(entry);
ObjectLoader ldr = repository.open(id);
ldr.copyTo(zos);
zos.closeArchiveEntry();
}
zos.finish();
success = true;
} catch (IOException e) {
error(e, repository, "{0} failed to zip files from commit {1}", commit.getName());
} finally {
tw.release();
rw.dispose();
}
return success;
}
示例10: testAddMixedLineEndings
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
public void testAddMixedLineEndings () throws Exception {
File f = new File(workDir, "f");
String content = "";
for (int i = 0; i < 10000; ++i) {
content += i + "\r\n";
}
write(f, content);
File[] files = new File[] { f };
GitClient client = getClient(workDir);
client.add(files, NULL_PROGRESS_MONITOR);
client.commit(files, "commit", null, null, NULL_PROGRESS_MONITOR);
Map<File, GitStatus> statuses = client.getStatus(files, NULL_PROGRESS_MONITOR);
assertEquals(1, statuses.size());
assertStatus(statuses, workDir, f, true, Status.STATUS_NORMAL, Status.STATUS_NORMAL, Status.STATUS_NORMAL, false);
// lets turn autocrlf on
StoredConfig cfg = repository.getConfig();
cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOCRLF, "true");
cfg.save();
// when this starts failing, remove the work around
ObjectInserter inserter = repository.newObjectInserter();
TreeWalk treeWalk = new TreeWalk(repository);
treeWalk.setFilter(PathFilterGroup.createFromStrings("f"));
treeWalk.setRecursive(true);
treeWalk.reset();
treeWalk.addTree(new FileTreeIterator(repository));
while (treeWalk.next()) {
String path = treeWalk.getPathString();
assertEquals("f", path);
WorkingTreeIterator fit = treeWalk.getTree(0, WorkingTreeIterator.class);
InputStream in = fit.openEntryStream();
try {
inserter.insert(Constants.OBJ_BLOB, fit.getEntryLength(), in);
fail("this should fail, remove the work around");
} catch (EOFException ex) {
assertEquals("Input did not match supplied length. 10000 bytes are missing.", ex.getMessage());
} finally {
in.close();
inserter.release();
}
break;
}
// no err should occur
write(f, content + "hello");
statuses = client.getStatus(files, NULL_PROGRESS_MONITOR);
assertEquals(1, statuses.size());
assertStatus(statuses, workDir, f, true, Status.STATUS_NORMAL, Status.STATUS_MODIFIED, Status.STATUS_MODIFIED, false);
client.add(files, NULL_PROGRESS_MONITOR);
statuses = client.getStatus(files, NULL_PROGRESS_MONITOR);
assertEquals(1, statuses.size());
assertStatus(statuses, workDir, f, true, Status.STATUS_MODIFIED, Status.STATUS_NORMAL, Status.STATUS_MODIFIED, false);
client.commit(files, "message", null, null, NULL_PROGRESS_MONITOR);
statuses = client.getStatus(files, NULL_PROGRESS_MONITOR);
assertEquals(1, statuses.size());
assertStatus(statuses, workDir, f, true, Status.STATUS_NORMAL, Status.STATUS_NORMAL, Status.STATUS_NORMAL, false);
}
示例11: testLineEndingsWindows
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
public void testLineEndingsWindows () throws Exception {
if (!isWindows()) {
return;
}
// lets turn autocrlf on
StoredConfig cfg = repository.getConfig();
cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOCRLF, "true");
cfg.save();
File f = new File(workDir, "f");
write(f, "a\r\nb\r\n");
File[] roots = new File[] { f };
GitClient client = getClient(workDir);
runExternally(workDir, Arrays.asList("git.cmd", "add", "f"));
DirCacheEntry e1 = repository.readDirCache().getEntry("f");
client.add(roots, NULL_PROGRESS_MONITOR);
DirCacheEntry e2 = repository.readDirCache().getEntry("f");
assertStatus(client.getStatus(roots, NULL_PROGRESS_MONITOR),
workDir, f, true, Status.STATUS_ADDED, Status.STATUS_NORMAL, Status.STATUS_ADDED, false);
List<String> res = runExternally(workDir, Arrays.asList("git.cmd", "status", "-s"));
assertEquals(Arrays.asList("A f"), res);
assertEquals(e1.getFileMode(), e2.getFileMode());
assertEquals(e1.getPathString(), e2.getPathString());
assertEquals(e1.getRawMode(), e2.getRawMode());
assertEquals(e1.getStage(), e2.getStage());
assertEquals(e1.getLength(), e2.getLength());
assertEquals(e1.getObjectId(), e2.getObjectId());
write(f, "a\nb\n");
res = runExternally(workDir, Arrays.asList("git.cmd", "status", "-s"));
assertEquals(Arrays.asList("AM f"), res);
assertStatus(client.getStatus(roots, NULL_PROGRESS_MONITOR),
workDir, f, true, Status.STATUS_ADDED, Status.STATUS_MODIFIED, Status.STATUS_ADDED, false);
res = runExternally(workDir, Arrays.asList("git.cmd", "commit", "-m", "gugu"));
res = runExternally(workDir, Arrays.asList("git.cmd", "checkout", "--", "f"));
RevCommit commit = Utils.findCommit(repository, "HEAD");
TreeWalk walk = new TreeWalk(repository);
walk.reset();
walk.addTree(commit.getTree());
walk.setFilter(PathFilter.create("f"));
walk.setRecursive(true);
walk.next();
assertEquals("f", walk.getPathString());
ObjectLoader loader = repository.getObjectDatabase().open(walk.getObjectId(0));
assertEquals(4, loader.getSize());
assertEquals("a\nb\n", new String(loader.getBytes()));
assertEquals(e1.getObjectId(), walk.getObjectId(0));
res = runExternally(workDir, Arrays.asList("git.cmd", "status", "-s"));
assertEquals(0, res.size());
assertStatus(client.getStatus(roots, NULL_PROGRESS_MONITOR),
workDir, f, true, Status.STATUS_NORMAL, Status.STATUS_NORMAL, Status.STATUS_NORMAL, false);
}
示例12: newTreeWalk
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
@Nonnull
public static TreeWalk newTreeWalk(AnyObjectId tree, ObjectReader reader) throws IOException {
TreeWalk tw = new TreeWalk(reader);
tw.reset(tree);
return tw;
}
示例13: ExtractCommitInfo
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
private static CommitNode ExtractCommitInfo(RevCommit _revCommit, Repository _repo, Date later) {
CommitNode c = new CommitNode(null);
RevWalk rw = new RevWalk(_repo);
try {
/*if (commit == null) {
ObjectId object = getDefaultBranch(repository);
commit = rw.parseCommit(object);
}*/
if (_revCommit.getParentCount() == 0) {
TreeWalk tw = new TreeWalk(_repo);
tw.reset();
tw.setRecursive(true);
tw.addTree(_revCommit.getTree());
c.date = _revCommit.getAuthorIdent().getWhen();
if ( (later != null) && c.date.before(later)){
return null;
}
while (tw.next()) {
if (isJavaFile(tw.getPathString())){
c.files.add(new FileNode(tw.getPathString(), tw.getPathString(),
tw.getObjectId(0), null, ChangeType.ADD));
}
}
tw.release();
} else {
RevCommit parent = rw.parseCommit(_revCommit.getParent(0).getId());
DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);
df.setRepository(_repo);
df.setDiffComparator(RawTextComparator.DEFAULT);
df.setDetectRenames(true);
List<DiffEntry> diffs = df.scan(parent.getTree(), _revCommit.getTree());
c.date = _revCommit.getAuthorIdent().getWhen();
if ((later != null) && c.date.before(later)){
return null;
}
for (DiffEntry diff : diffs) {
if (diff.getChangeType().equals(ChangeType.ADD)) {
if (isJavaFile(diff.getNewPath())){
c.files.add(new FileNode(diff.getNewPath(), null,
diff.getNewId().toObjectId(), null, diff.getChangeType()));
}
} else if (diff.getChangeType().equals(ChangeType.DELETE)) {
if (isJavaFile(diff.getOldPath())){
c.files.add(new FileNode(null, diff.getOldPath(),
null, diff.getOldId().toObjectId(), diff.getChangeType()));
}
} else if (diff.getChangeType().equals(ChangeType.RENAME)) {
if (isJavaFile(diff.getNewPath())){
c.files.add(new FileNode(diff.getNewPath(), diff.getOldPath(),
diff.getNewId().toObjectId(), diff.getOldId().toObjectId(), diff.getChangeType()));
}
} else if (diff.getChangeType().equals(ChangeType.MODIFY)){
if (isJavaFile(diff.getNewPath())){
c.files.add(new FileNode(diff.getNewPath(), null,
diff.getNewId().toObjectId(), diff.getOldId().toObjectId(), diff.getChangeType()));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rw.dispose();
}
return c;
}
示例14: parse
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
public void parse(RepositoryNode _repo) {
RevWalk rw = new RevWalk(_repo.getRepository());
try {
/*if (commit == null) {
ObjectId object = getDefaultBranch(repository);
commit = rw.parseCommit(object);
}*/
if (revCommit.getParentCount() == 0) {
TreeWalk tw = new TreeWalk(_repo.getRepository());
tw.reset();
tw.setRecursive(true);
tw.addTree(revCommit.getTree());
while (tw.next()) {
if (isJavaFile(tw.getPathString())){
files.add(new FileNode(tw.getPathString(), tw.getPathString(),
tw.getObjectId(0), null, ChangeType.ADD));
}
}
tw.release();
} else {
RevCommit parent = rw.parseCommit(revCommit.getParent(0).getId());
DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);
df.setRepository(_repo.getRepository());
df.setDiffComparator(RawTextComparator.DEFAULT);
df.setDetectRenames(true);
List<DiffEntry> diffs = df.scan(parent.getTree(), revCommit.getTree());
for (DiffEntry diff : diffs) {
if (diff.getChangeType().equals(ChangeType.ADD)) {
if (isJavaFile(diff.getNewPath())){
files.add(new FileNode(diff.getNewPath(), null,
diff.getNewId().toObjectId(), null, diff.getChangeType()));
}
} else if (diff.getChangeType().equals(ChangeType.DELETE)) {
if (isJavaFile(diff.getOldPath())){
files.add(new FileNode(null, diff.getOldPath(),
null, diff.getOldId().toObjectId(), diff.getChangeType()));
}
} else if (diff.getChangeType().equals(ChangeType.RENAME)) {
if (isJavaFile(diff.getNewPath())){
files.add(new FileNode(diff.getNewPath(), diff.getOldPath(),
diff.getNewId().toObjectId(), diff.getOldId().toObjectId(), diff.getChangeType()));
}
} else if (diff.getChangeType().equals(ChangeType.MODIFY)){
if (isJavaFile(diff.getNewPath())){
files.add(new FileNode(diff.getNewPath(), null,
diff.getNewId().toObjectId(), diff.getOldId().toObjectId(), diff.getChangeType()));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rw.dispose();
}
for (FileNode f : files){
f.Parse(_repo.getRepository());
}
}
示例15: getFilesInCommit
import org.eclipse.jgit.treewalk.TreeWalk; //導入方法依賴的package包/類
/**
* Returns the list of files changed in a specified commit. If the
* repository does not exist or is empty, an empty list is returned.
*
* @param repository
* @param commit
* if null, HEAD is assumed.
* @return list of files changed in a commit
*/
public List<PathChangeModel> getFilesInCommit(RevCommit commit) {
List<PathChangeModel> list = new ArrayList<PathChangeModel>();
if (!hasCommits(repository)) {
return list;
}
RevWalk rw = new RevWalk(repository);
try {
if (commit == null) {
ObjectId object = getDefaultBranch(repository);
commit = rw.parseCommit(object);
}
if (commit.getParentCount() == 0) {
TreeWalk tw = new TreeWalk(repository);
tw.reset();
tw.setRecursive(true);
tw.addTree(commit.getTree());
while (tw.next()) {
list.add(new PathChangeModel(tw.getPathString(), tw.getPathString(), 0, tw
.getRawMode(0), commit.getId().getName(), ChangeType.ADD));
}
tw.release();
} else {
RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);
df.setRepository(repository);
df.setDiffComparator(RawTextComparator.DEFAULT);
df.setDetectRenames(true);
List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree());
for (DiffEntry diff : diffs) {
if (diff.getChangeType().equals(ChangeType.DELETE)) {
list.add(new PathChangeModel(diff.getOldPath(), diff.getOldPath(), 0, diff
.getNewMode().getBits(), commit.getId().getName(), diff
.getChangeType()));
} else if (diff.getChangeType().equals(ChangeType.RENAME)) {
list.add(new PathChangeModel(diff.getOldPath(), diff.getNewPath(), 0, diff
.getNewMode().getBits(), commit.getId().getName(), diff
.getChangeType()));
} else {
list.add(new PathChangeModel(diff.getNewPath(), diff.getNewPath(), 0, diff
.getNewMode().getBits(), commit.getId().getName(), diff
.getChangeType()));
}
}
}
} catch (Throwable t) {
System.err.println("Repository failed to determine files in commit!");
} finally {
rw.dispose();
}
return list;
}