org.eclipse.jgit.api.Git.diff()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(11.6k)|赞(0)|评价(0)|浏览(634)

本文整理了Java中org.eclipse.jgit.api.Git.diff()方法的一些代码示例,展示了Git.diff()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Git.diff()方法的具体详情如下:
包路径:org.eclipse.jgit.api.Git
类名称:Git
方法名:diff

Git.diff介绍

[英]Return a command object to execute a diff command
[中]返回命令对象以执行diff命令

代码示例

代码示例来源:origin: jphp-group/jphp

@Signature
public Memory diff(ArrayMemory settings) throws GitAPIException {
  DiffCommand command = getWrappedObject().diff();
  if (settings != null) {
    command.setCached(settings.valueOfIndex("cached").toBoolean());
    Memory contextLines = settings.valueOfIndex("contextLines");
    if (contextLines.isNotNull()) {
      command.setContextLines(contextLines.toInteger());
    }
    Memory destPrefix = settings.valueOfIndex("destPrefix");
    if (destPrefix.isNotNull()) {
      command.setDestinationPrefix(destPrefix.toString());
    }
    Memory sourcePrefix = settings.valueOfIndex("sourcePrefix");
    if (sourcePrefix.isNotNull()) {
      command.setSourcePrefix(sourcePrefix.toString());
    }
    command.setShowNameAndStatusOnly(settings.valueOfIndex("showNameAndStatusOnly").toBoolean());
    Memory pathFilter = settings.valueOfIndex("pathFilter");
    if (pathFilter.isNotNull()) {
      command.setPathFilter(PathFilter.create(pathFilter.toString()));
    }
  }
  List<DiffEntry> call = command.call();
  return GitUtils.valueOfDiffEntries(call);
}

代码示例来源:origin: apache/incubator-gobblin

newTreeIter.reset(reader, headTree);
return this.git.diff()
  .setNewTree(newTreeIter)
  .setOldTree(oldTreeIter)

代码示例来源:origin: centic9/jgit-cookbook

private static void listDiff(Repository repository, Git git, String oldCommit, String newCommit) throws GitAPIException, IOException {
  final List<DiffEntry> diffs = git.diff()
      .setOldTree(prepareTreeParser(repository, oldCommit))
      .setNewTree(prepareTreeParser(repository, newCommit))
      .call();
  System.out.println("Found: " + diffs.size() + " differences");
  for (DiffEntry diff : diffs) {
    System.out.println("Diff: " + diff.getChangeType() + ": " +
        (diff.getOldPath().equals(diff.getNewPath()) ? diff.getNewPath() : diff.getOldPath() + " -> " + diff.getNewPath()));
  }
}

代码示例来源:origin: centic9/jgit-cookbook

private static void listDiff(Repository repository, Git git, String oldCommit, String newCommit) throws GitAPIException, IOException {
  final List<DiffEntry> diffs = git.diff()
      .setOldTree(prepareTreeParser(repository, oldCommit))
      .setNewTree(prepareTreeParser(repository, newCommit))
      .call();
  System.out.println("Found: " + diffs.size() + " differences");
  for (DiffEntry diff : diffs) {
    System.out.println("Diff: " + diff.getChangeType() + ": " +
        (diff.getOldPath().equals(diff.getNewPath()) ? diff.getNewPath() : diff.getOldPath() + " -> " + diff.getNewPath()));
  }
}

代码示例来源:origin: centic9/jgit-cookbook

private static @NonNull DiffEntry diffFile(Repository repo, String oldCommit,
            String newCommit, String path) throws IOException, GitAPIException {
    Config config = new Config();
    config.setBoolean("diff", null, "renames", true);
    DiffConfig diffConfig = config.get(DiffConfig.KEY);
    try (Git git = new Git(repo)) {
      List<DiffEntry> diffList = git.diff().
        setOldTree(prepareTreeParser(repo, oldCommit)).
        setNewTree(prepareTreeParser(repo, newCommit)).
        setPathFilter(FollowFilter.create(path, diffConfig)).
        call();
      if (diffList.size() == 0)
        return null;
      if (diffList.size() > 1)
        throw new RuntimeException("invalid diff");
      return diffList.get(0);
    }
  }
}

代码示例来源:origin: centic9/jgit-cookbook

private static @NonNull DiffEntry diffFile(Repository repo, String oldCommit,
            String newCommit, String path) throws IOException, GitAPIException {
    Config config = new Config();
    config.setBoolean("diff", null, "renames", true);
    DiffConfig diffConfig = config.get(DiffConfig.KEY);
    try (Git git = new Git(repo)) {
      List<DiffEntry> diffList = git.diff().
        setOldTree(prepareTreeParser(repo, oldCommit)).
        setNewTree(prepareTreeParser(repo, newCommit)).
        setPathFilter(FollowFilter.create(path, diffConfig)).
        call();
      if (diffList.size() == 0)
        return null;
      if (diffList.size() > 1)
        throw new RuntimeException("invalid diff");
      return diffList.get(0);
    }
  }
}

代码示例来源:origin: centic9/jgit-cookbook

public static void main(String[] args) throws IOException, GitAPIException {
  try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
    try (Git git = new Git(repository)) {
      if(repository.exactRef("refs/heads/testbranch") == null) {
        // first we need to ensure that the remote branch is visible locally
        Ref ref = git.branchCreate().setName("testbranch").setStartPoint("origin/testbranch").call();
        System.out.println("Created local testbranch with ref: " + ref);
      }
      // the diff works on TreeIterators, we prepare two for the two branches
      AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, "refs/heads/testbranch");
      AbstractTreeIterator newTreeParser = prepareTreeParser(repository, "refs/heads/master");
      // then the procelain diff-command returns a list of diff entries
      List<DiffEntry> diff = git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser).call();
      for (DiffEntry entry : diff) {
        System.out.println("Entry: " + entry);
      }
    }
  }
}

代码示例来源:origin: centic9/jgit-cookbook

List<DiffEntry> diffs= git.diff()
          .setNewTree(newTreeIter)
          .setOldTree(oldTreeIter)

代码示例来源:origin: centic9/jgit-cookbook

public static void main(String[] args) throws IOException, GitAPIException {
  try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
    try (Git git = new Git(repository)) {
      if(repository.exactRef("refs/heads/testbranch") == null) {
        // first we need to ensure that the remote branch is visible locally
        Ref ref = git.branchCreate().setName("testbranch").setStartPoint("origin/testbranch").call();
        System.out.println("Created local testbranch with ref: " + ref);
      }
      // the diff works on TreeIterators, we prepare two for the two branches
      AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, "refs/heads/testbranch");
      AbstractTreeIterator newTreeParser = prepareTreeParser(repository, "refs/heads/master");
      // then the procelain diff-command returns a list of diff entries
      List<DiffEntry> diff = git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser).call();
      for (DiffEntry entry : diff) {
        System.out.println("Entry: " + entry);
      }
    }
  }
}

代码示例来源:origin: centic9/jgit-cookbook

List<DiffEntry> diffs= git.diff()
          .setNewTree(newTreeIter)
          .setOldTree(oldTreeIter)

代码示例来源:origin: centic9/jgit-cookbook

public static void main(String[] args) throws IOException, GitAPIException {
  try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
    // the diff works on TreeIterators, we prepare two for the two branches
    AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, "b97b184b0ce11c0b6a4dcc2b57768ff155cb696b");
    AbstractTreeIterator newTreeParser = prepareTreeParser(repository, "9e0719d7d773b41b49ebf04e6fd7b5c637e96063");
    // then the porcelain diff-command returns a list of diff entries
    try (Git git = new Git(repository)) {
      List<DiffEntry> diff = git.diff().
          setOldTree(oldTreeParser).
          setNewTree(newTreeParser).
          setPathFilter(PathFilter.create("README.md")).
          // to filter on Suffix use the following instead
          //setPathFilter(PathSuffixFilter.create(".java")).
          call();
      for (DiffEntry entry : diff) {
        System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId());
        try (DiffFormatter formatter = new DiffFormatter(System.out)) {
          formatter.setRepository(repository);
          formatter.format(entry);
        }
      }
    }
  }
}

代码示例来源:origin: centic9/jgit-cookbook

public static void main(String[] args) throws IOException, GitAPIException {
  try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
    // the diff works on TreeIterators, we prepare two for the two branches
    AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, "b97b184b0ce11c0b6a4dcc2b57768ff155cb696b");
    AbstractTreeIterator newTreeParser = prepareTreeParser(repository, "9e0719d7d773b41b49ebf04e6fd7b5c637e96063");
    // then the porcelain diff-command returns a list of diff entries
    try (Git git = new Git(repository)) {
      List<DiffEntry> diff = git.diff().
          setOldTree(oldTreeParser).
          setNewTree(newTreeParser).
          setPathFilter(PathFilter.create("README.md")).
          // to filter on Suffix use the following instead
          //setPathFilter(PathSuffixFilter.create(".java")).
          call();
      for (DiffEntry entry : diff) {
        System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId());
        try (DiffFormatter formatter = new DiffFormatter(System.out)) {
          formatter.setRepository(repository);
          formatter.format(entry);
        }
      }
    }
  }
}

代码示例来源:origin: org.openmrs.maven.plugins/openmrs-sdk-maven-plugin

/**
 * @inheritDoc
 */
@Override
public boolean checkIfUncommitedChanges(Git git) {
  try {
    List<DiffEntry> diff = git.diff().call();
    return !diff.isEmpty();
  } catch (GitAPIException e) {
    throw new RuntimeException("could not check if there are any uncommited changes", e);
  }
}

代码示例来源:origin: Verigreen/verigreen

@Override
public boolean isThereAnyDifs() {
  
  boolean ans = false;
  try {
    List<DiffEntry> list = _git.diff().call();
    if (list.size() > 0) {
      ans = true;
    }
  } catch (GitAPIException e) {
    throw new RuntimeException("Failed to get diffs", e);
  }
  
  return ans;
}

代码示例来源:origin: com.centurylink.mdw/mdw-common

/**
 * Check path vs repository
 */
public boolean isDiff(String path) throws Exception {
  return !git.diff().setPathFilter(PathFilter.create(path)).call().isEmpty();
}

代码示例来源:origin: google/git-appraise-eclipse

/**
 * Gets the diff between heads on two branches.
 * See
 * https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ShowBranchDiff.java.
 */
private List<DiffEntry> calculateBranchDiffs(Git git, String targetRef, String reviewRef)
  throws IOException, GitAPIException {
 AbstractTreeIterator oldTreeParser = prepareTreeParser(targetRef);
 AbstractTreeIterator newTreeParser = prepareTreeParser(reviewRef);
 return git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser).call();
}

代码示例来源:origin: omegat-org/omegat

private boolean indexIsEmpty(DirCache dc) throws Exception {
  DirCacheIterator dci = new DirCacheIterator(dc);
  AbstractTreeIterator old = prepareTreeParser(repository, repository.resolve(Constants.HEAD));
  try (Git git = new Git(repository)) {
    List<DiffEntry> diffs = git.diff().setOldTree(old).setNewTree(dci).call();
    return diffs.isEmpty();
  }
}

代码示例来源:origin: google/git-appraise-eclipse

/**
 * Gets the diff between two commits.
 */
private List<DiffEntry> calculateCommitDiffs(Git git, RevCommit first, RevCommit last)
  throws IOException, GitAPIException {
 AbstractTreeIterator oldTreeParser = prepareTreeParser(first);
 AbstractTreeIterator newTreeParser = prepareTreeParser(last);
 return git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser).call();
}

代码示例来源:origin: stackoverflow.com

Git git = new Git(gitRepository);

ObjectId newTreeId = ObjectId.fromString(commitSha + "^{tree}");
ObjectId oldTreeId = gitRepository.resolve(commitSha + "^^{tree}");

CanonicalTreeParser newTree = new CanonicalTreeParser();
newTree.reset(reader, newTreeId);

CanonicalTreeParser oldTree = new CanonicalTreeParser();
oldTree.reset(reader, oldTreeId);

for (DiffEntry de : git.diff().setNewTree(newTree).setOldTree(oldTree).call())
{
  /* Print the file diff */
  DiffFormatter formatter = new DiffFormatter(System.out);
  formatter.setRepository(gitRepository);
  formatter.format(de);
}

代码示例来源:origin: org.apereo.cas/cas-mgmt-support-version-control

/**
 * Returns a list fo differences between the last two commits in a branch.
 *
 * @param branch - The branch to check for differences against.
 * @return - List of DiffEntry.
 * @throws GitAPIException - failed.
 * @throws IOException - failed
 */
public List<DiffEntry> getDiffsToRevert(final String branch) throws GitAPIException, IOException {
  val oldTreeIter = new CanonicalTreeParser();
  try (val reader = git.getRepository().newObjectReader()) {
    oldTreeIter.reset(reader, git.getRepository().resolve(branch + TOP));
    val newTreeIter = new CanonicalTreeParser();
    newTreeIter.reset(reader, git.getRepository().resolve(branch + TOP_MINUS_1));
    return git.diff().setOldTree(oldTreeIter).setNewTree(newTreeIter).call();
  }
}

相关文章