org.eclipse.jgit.lib.Repository.resolve()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(11.8k)|赞(0)|评价(0)|浏览(385)

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

Repository.resolve介绍

[英]Parse a git revision string and return an object id. Combinations of these operators are supported:

  • HEAD, MERGE_HEAD, FETCH_HEAD
  • SHA-1: a complete or abbreviated SHA-1
  • refs/...: a complete reference name
  • short-name: a short reference name under refs/heads, refs/tags, or refs/remotes namespace
  • tag-NN-gABBREV: output from describe, parsed by treating ABBREV as an abbreviated SHA-1.
  • id^: first parent of commit id, this is the same as id^1
  • id^0: ensure id is a commit
  • id^n: n-th parent of commit id
  • id~n: n-th historical ancestor of id, by first parent. id~3 is equivalent to id^1^1^1 or id^^^.
  • id:path: Lookup path under tree named by id
  • id^{commit}: ensure id is a commit
  • id^{tree}: ensure id is a tree
  • id^{tag}: ensure id is a tag
  • id^{blob}: ensure id is a blob

The following operators are specified by Git conventions, but are not supported by this method:

  • ref@{n}: n-th version of ref as given by its reflog
  • ref@{time}: value of ref at the designated time
    [中]解析git修订字符串并返回对象id。支持以下运算符的组合:
    *头,合并头,取头
    *SHA-1:完整或缩写的SHA-1
    *参考文献/…:完整的引用名
    *短名称:refs/heads、refs/tags或refs/remotes命名空间下的短引用名称
    *tag NN gABBREV:descripe的输出,通过将ABBREV作为缩写SHA-1进行解析。
    *id^:提交id的第一个父级,与id ^1相同
    *id ^0:确保id为提交
    *id^n:提交id的第n个父级
    *id~n:id的第n个历史祖先,由第一个父代继承。id~3相当于id^1^1^1或id^^。
    *id:path:查找由id命名的树下的路径
    *id^{commit}:确保id是commit
    *id^{tree}:确保id是树
    *id^{tag}:确保id是一个标记
    *id^{blob}:确保id是blob
    以下运算符由Git约定指定,但此方法不支持:
    *ref@{n}:ref的第n个版本,由reflog给出
    *ref@{time}:指定时间的ref值

代码示例

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

@Signature
public Memory resolve(String revstr) throws IOException {
  return GitUtils.valueOf(getWrappedObject().getRepository().resolve(revstr));
}

代码示例来源:origin: apache/usergrid

/**
 * @param gitConfigFolder e.g. /your/project/root/.git
 *
 * @return Returns last commit's UUID, "nocommit" if there are no commits and returns null if an exception occured
 */
public static String getLastCommitUuid( String gitConfigFolder ) throws MojoExecutionException {
  try {
    Repository repo =
        new RepositoryBuilder().setGitDir( new File( gitConfigFolder ) ).readEnvironment().findGitDir()
                    .build();
    RevWalk walk = new RevWalk( repo );
    ObjectId head = repo.resolve( "HEAD" );
    if ( head != null ) {
      RevCommit lastCommit = walk.parseCommit( head );
      return lastCommit.getId().getName();
    }
    else {
      return "nocommit";
    }
  }
  catch ( Exception e ) {
    throw new MojoExecutionException( "Error trying to get the last git commit uuid", e );
  }
}

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

@Signature
public Memory resolveCommit(String revstr) throws IOException, GitAPIException {
  ObjectId objectId = getWrappedObject().getRepository().resolve(revstr);
  if (objectId == null) {
    return Memory.NULL;
  }
  LogCommand command = getWrappedObject()
      .log()
      .add(objectId)
      .setMaxCount(1);
  Iterable<RevCommit> call = command.call();
  for (RevCommit revCommit : call) {
    return GitUtils.valueOf(revCommit);
  }
  return Memory.NULL;
}

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

ObjectId oldHeadTree = git.getRepository().resolve(this.lastProcessedGitHash + "^{tree}");
ObjectId head = this.git.getRepository().resolve("HEAD");
ObjectId headTree = this.git.getRepository().resolve("HEAD^{tree}");

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

public static void main(String[] args) throws IOException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
      // basic syntax is similar to getRef()
      ObjectId id = repository.resolve("HEAD");
      System.out.println("ObjectId of HEAD: " + id);

      // however resolve() supports almost all of the git-syntax, where getRef() only works on names
      id = repository.resolve("HEAD^1");
      System.out.println("ObjectId of HEAD: " + id);

      id = repository.resolve("b419522521af553ae2752fd1b609f2aa11062243");
      System.out.println("ObjectId of specific commit: " + id);

      id = repository.resolve("05d18a76875716fbdbd2c200091b40caa06c713d");
      System.out.println("ObjectId of merged commit: " + id);
    }
  }
}

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

fId = repository.resolve("HEAD");
} else {
  fId = repository.resolve(first.toString());
  ObjectId sId = repository.resolve(second.toString());

代码示例来源:origin: checkstyle/checkstyle

private static RevCommitsPair resolveRevCommitsPair(Repository repo) {
  RevCommitsPair revCommitIteratorPair;
  try (RevWalk revWalk = new RevWalk(repo); Git git = new Git(repo)) {
    final Iterator<RevCommit> first;
    final Iterator<RevCommit> second;
    final ObjectId headId = repo.resolve(Constants.HEAD);
    final RevCommit headCommit = revWalk.parseCommit(headId);
    if (isMergeCommit(headCommit)) {
      final RevCommit firstParent = headCommit.getParent(0);
      final RevCommit secondParent = headCommit.getParent(1);
      first = git.log().add(firstParent).call().iterator();
      second = git.log().add(secondParent).call().iterator();
    }
    else {
      first = git.log().call().iterator();
      second = Collections.emptyIterator();
    }
    revCommitIteratorPair =
        new RevCommitsPair(new OmitMergeCommitsIterator(first),
            new OmitMergeCommitsIterator(second));
  }
  catch (GitAPIException | IOException ignored) {
    revCommitIteratorPair = new RevCommitsPair();
  }
  return revCommitIteratorPair;
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

private ObjectId getStartPointObjectId() throws AmbiguousObjectException,
    RefNotFoundException, IOException {
  if (startCommit != null)
    return startCommit.getId();
  String startPointOrHead = (startPoint != null) ? startPoint
      : Constants.HEAD;
  ObjectId result = repo.resolve(startPointOrHead);
  if (result == null)
    throw new RefNotFoundException(MessageFormat.format(
        JGitText.get().refNotResolved, startPointOrHead));
  return result;
}

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

public static void main(String[] args) throws IOException {
  try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
    List<String> paths = readElementsAt(repository, "6409ee1597a53c6fbee31edf9cde31dc3afbe20f", "src/main/java/org/dstadler/jgit/porcelain");
    System.out.println("Had paths for commit: " + paths);
    final ObjectId testbranch = repository.resolve("testbranch");
    paths = readElementsAt(repository, testbranch.getName(), "src/main/java/org/dstadler/jgit/porcelain");
    System.out.println("Had paths for tag: " + paths);
  }
}

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

private static RevTree getTree(Repository repository) throws IOException {
  ObjectId lastCommitId = repository.resolve(Constants.HEAD);
  // a RevWalk allows to walk over commits based on some filtering
  try (RevWalk revWalk = new RevWalk(repository)) {
    RevCommit commit = revWalk.parseCommit(lastCommitId);
    System.out.println("Time of commit (seconds since epoch): " + commit.getCommitTime());
    // and using commit's tree find the path
    RevTree tree = commit.getTree();
    System.out.println("Having tree: " + tree);
    return tree;
  }
}

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

public static void main(String[] args) throws IOException {
  try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
    List<String> paths = readElementsAt(repository, "6409ee1597a53c6fbee31edf9cde31dc3afbe20f", "src/main/java/org/dstadler/jgit/porcelain");
    System.out.println("Had paths for commit: " + paths);
    final ObjectId testbranch = repository.resolve("testbranch");
    paths = readElementsAt(repository, testbranch.getName(), "src/main/java/org/dstadler/jgit/porcelain");
    System.out.println("Had paths for tag: " + paths);
  }
}

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

private static RevTree getTree(Repository repository) throws IOException {
  ObjectId lastCommitId = repository.resolve(Constants.HEAD);
  // a RevWalk allows to walk over commits based on some filtering
  try (RevWalk revWalk = new RevWalk(repository)) {
    RevCommit commit = revWalk.parseCommit(lastCommitId);
    System.out.println("Time of commit (seconds since epoch): " + commit.getCommitTime());
    // and using commit's tree find the path
    RevTree tree = commit.getTree();
    System.out.println("Having tree: " + tree);
    return tree;
  }
}

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

@Signature
public Memory merge(String[] refs, ArrayMemory settings) throws IOException, GitAPIException {
  MergeCommand command = getWrappedObject().merge();
  for (String ref : refs) {
    Repository repository = getWrappedObject().getRepository();
    ObjectId objectId = repository.resolve(ref);
    command.include(objectId);
  }
  if (settings != null) {
    command.setCommit(settings.valueOfIndex("commit").toBoolean());
    command.setMessage(settings.valueOfIndex("message").toString());
    command.setSquash(settings.valueOfIndex("squash").toBoolean());
    Memory fastForward = settings.valueOfIndex("fastForward");
    
    if (fastForward.isNumber()) {
      command.setFastForward(MergeCommand.FastForwardMode.valueOf(fastForward.toString()));
    }
    Memory strategy = settings.valueOfIndex("strategy");
    if (strategy.isNotNull()) {
      command.setStrategy(MergeStrategy.get(strategy.toString()));
    }
  }
  MergeResult call = command.call();
  return GitUtils.valueOf(call);
}

代码示例来源:origin: git-commit-id/maven-git-commit-id-plugin

protected RevCommit findEvalCommitObjectId(@Nonnull String evaluateOnCommit, @Nonnull Repository repo) throws RuntimeException {
 try {
  ObjectId evalCommitId = repo.resolve(evaluateOnCommit);
  try (RevWalk walk = new RevWalk(repo)) {
   RevCommit evalCommit = walk.parseCommit(evalCommitId);
   walk.dispose();
   log.info("evalCommit is [{}]", evalCommit.getName());
   return evalCommit;
  }
 } catch (IOException ex) {
  throw new RuntimeException("Unable to obtain " + evaluateOnCommit + " commit!", ex);
 }
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

private ObjectId getStashId() throws GitAPIException {
  final String revision = stashRef != null ? stashRef : DEFAULT_REF;
  final ObjectId stashId;
  try {
    stashId = repo.resolve(revision);
  } catch (IOException e) {
    throw new InvalidRefNameException(MessageFormat.format(
        JGitText.get().stashResolveFailed, revision), e);
  }
  if (stashId == null)
    throw new InvalidRefNameException(MessageFormat.format(
        JGitText.get().stashResolveFailed, revision));
  return stashId;
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

private ObjectId getStartPointObjectId() throws AmbiguousObjectException,
    RefNotFoundException, IOException {
  if (startCommit != null)
    return startCommit.getId();
  String startPointOrHead = getStartPointOrHead();
  ObjectId result = repo.resolve(startPointOrHead);
  if (result == null)
    throw new RefNotFoundException(MessageFormat.format(
        JGitText.get().refNotResolved, startPointOrHead));
  return result;
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

private ObjectId resolveRefToCommitId() {
  try {
    return repo.resolve(getRefOrHEAD() + "^{commit}"); //$NON-NLS-1$
  } catch (IOException e) {
    throw new JGitInternalException(
        MessageFormat.format(JGitText.get().cannotRead, getRefOrHEAD()),
        e);
  }
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

private Collection<Ref> filterRefs(Collection<Ref> refs)
    throws RefNotFoundException, IOException {
  if (containsCommitish == null)
    return refs;
  try (RevWalk walk = new RevWalk(repo)) {
    ObjectId resolved = repo.resolve(containsCommitish);
    if (resolved == null)
      throw new RefNotFoundException(MessageFormat.format(
          JGitText.get().refNotResolved, containsCommitish));
    RevCommit containsCommit = walk.parseCommit(resolved);
    return RevWalkUtils.findBranchesReachableFrom(containsCommit, walk,
        refs);
  }
}

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

private static AbstractTreeIterator prepareTreeParser(Repository repository, String objectId) throws IOException {
  // from the commit we can build the tree which allows us to construct the TreeParser
  //noinspection Duplicates
  try (RevWalk walk = new RevWalk(repository)) {
    RevCommit commit = walk.parseCommit(repository.resolve(objectId));
    RevTree tree = walk.parseTree(commit.getTree().getId());
    CanonicalTreeParser treeParser = new CanonicalTreeParser();
    try (ObjectReader reader = repository.newObjectReader()) {
      treeParser.reset(reader, tree.getId());
    }
    walk.dispose();
    return treeParser;
  }
}

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

private static AbstractTreeIterator prepareTreeParser(Repository repository, String objectId) throws IOException {
  // from the commit we can build the tree which allows us to construct the TreeParser
  //noinspection Duplicates
  try (RevWalk walk = new RevWalk(repository)) {
    RevCommit commit = walk.parseCommit(repository.resolve(objectId));
    RevTree tree = walk.parseTree(commit.getTree().getId());
    CanonicalTreeParser treeParser = new CanonicalTreeParser();
    try (ObjectReader reader = repository.newObjectReader()) {
      treeParser.reset(reader, tree.getId());
    }
    walk.dispose();
    return treeParser;
  }
}

相关文章

Repository类方法