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

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

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

Repository.exactRef介绍

[英]Get a ref by name.
[中]通过名字获得参考。

代码示例

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

@Signature
public Memory exactRef(String name) throws IOException {
  Ref ref = getWrappedObject().getRepository().exactRef(name);
  return ref == null ? Memory.NULL : GitUtils.valueOf(ref);
}

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

private Ref getRef() throws GitAPIException {
  try {
    return repo.exactRef(R_STASH);
  } catch (IOException e) {
    throw new InvalidRefNameException(MessageFormat.format(
        JGitText.get().cannotRead, R_STASH), e);
  }
}

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

private static String findRef(String ref, Repository repo)
      throws IOException {
    if (!ObjectId.isId(ref)) {
      Ref r = repo.exactRef(R_REMOTES + DEFAULT_REMOTE_NAME + "/" + ref); //$NON-NLS-1$
      if (r != null)
        return r.getName();
    }
    return ref;
  }
}

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

private Ref getHead() throws IOException, RefNotFoundException {
  Ref head = repo.exactRef(Constants.HEAD);
  if (head == null || head.getObjectId() == null)
    throw new RefNotFoundException(MessageFormat.format(
        JGitText.get().refNotResolved, Constants.HEAD));
  return head;
}

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

public static void main(String[] args) throws IOException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
      // the Ref holds an ObjectId for any type of object (tree, commit, blob, tree)
      Ref head = repository.exactRef("refs/heads/master");
      System.out.println("Ref of refs/heads/master: " + head + ": " + head.getName() + " - " + head.getObjectId().getName());
    }
  }
}

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

public static void main(String[] args) throws IOException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
      // the Ref holds an ObjectId for any type of object (tree, commit, blob, tree)
      Ref head = repository.exactRef("refs/heads/master");
      System.out.println("Ref of refs/heads/master: " + head + ": " + head.getName() + " - " + head.getObjectId().getName());
    }
  }
}

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

/**
 * Get ref that HEAD points to in the current submodule's repository
 *
 * @return ref name, null on failures
 * @throws java.io.IOException
 */
public String getHeadRef() throws IOException {
  try (Repository subRepo = getRepository()) {
    if (subRepo == null) {
      return null;
    }
    Ref head = subRepo.exactRef(Constants.HEAD);
    return head != null ? head.getLeaf().getName() : null;
  }
}

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

public static void main(String[] args) throws IOException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
      Ref head = repository.exactRef("refs/heads/master");

      // a RevWalk allows to walk over commits based on some filtering that is defined
      try (RevWalk walk = new RevWalk(repository)) {
        RevCommit commit = walk.parseCommit(head.getObjectId());
        System.out.println("Start-Commit: " + commit);

        System.out.println("Walking all commits starting at HEAD");
        walk.markStart(commit);
        int count = 0;
        for (RevCommit rev : walk) {
          System.out.println("Commit: " + rev);
          count++;
        }
        System.out.println(count);

        walk.dispose();
      }
    }
  }
}

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

private void processOptions() throws InvalidRefNameException,
    RefAlreadyExistsException, IOException {
  if (((!checkoutAllPaths && paths.isEmpty()) || orphan)
      && (name == null || !Repository
          .isValidRefName(Constants.R_HEADS + name)))
    throw new InvalidRefNameException(MessageFormat.format(JGitText
        .get().branchNameInvalid, name == null ? "<null>" : name)); //$NON-NLS-1$
  if (orphan) {
    Ref refToCheck = repo.exactRef(getBranchName());
    if (refToCheck != null)
      throw new RefAlreadyExistsException(MessageFormat.format(
          JGitText.get().refAlreadyExists, name));
  }
}

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

public static void main(String[] args) throws IOException, GitAPIException {
  // first create a test-repository, the return is including the .get directory here!
  File repoDir = createSampleGitRepo();
  // now open the resulting repository with a FileRepositoryBuilder
  FileRepositoryBuilder builder = new FileRepositoryBuilder();
  try (Repository repository = builder.setGitDir(repoDir)
      .readEnvironment() // scan environment GIT_* variables
      .findGitDir() // scan up the file system tree
      .build()) {
    System.out.println("Having repository: " + repository.getDirectory());
    // the Ref holds an ObjectId for any type of object (tree, commit, blob, tree)
    Ref head = repository.exactRef("refs/heads/master");
    System.out.println("Ref of refs/heads/master: " + head);
  }
  // clean up here to not keep using more and more disk-space for these samples
  FileUtils.deleteDirectory(repoDir.getParentFile());
}

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

public static void main(String[] args) throws IOException, GitAPIException {
  // first create a test-repository, the return is including the .get directory here!
  File repoDir = createSampleGitRepo();
  // now open the resulting repository with a FileRepositoryBuilder
  FileRepositoryBuilder builder = new FileRepositoryBuilder();
  try (Repository repository = builder.setGitDir(repoDir)
      .readEnvironment() // scan environment GIT_* variables
      .findGitDir() // scan up the file system tree
      .build()) {
    System.out.println("Having repository: " + repository.getDirectory());
    // the Ref holds an ObjectId for any type of object (tree, commit, blob, tree)
    Ref head = repository.exactRef("refs/heads/master");
    System.out.println("Ref of refs/heads/master: " + head);
  }
  // clean up here to not keep using more and more disk-space for these samples
  FileUtils.deleteDirectory(repoDir.getParentFile());
}

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

public static void main(String[] args) throws IOException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
      Ref head = repository.exactRef("refs/heads/master");
      System.out.println("Found head: " + head);

      // a RevWalk allows to walk over commits based on some filtering that is defined
      try (RevWalk walk = new RevWalk(repository)) {
        RevCommit commit = walk.parseCommit(head.getObjectId());
        System.out.println("Found Commit: " + commit);

        // You can also get the commit for an (abbreviated) SHA
        walk.reset();
        ObjectId id = repository.resolve("38d51408bd");
        RevCommit commitAgain = walk.parseCommit(id);
        System.out.println("Found Commit again: " + commitAgain);

        walk.dispose();
      }
    }
  }
}

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

/**
 * Get branch that HEAD currently points to
 *
 * @param subRepo
 *            a {@link org.eclipse.jgit.lib.Repository} object.
 * @return shortened branch name, null on failures
 * @throws java.io.IOException
 */
protected String getHeadBranch(Repository subRepo) throws IOException {
  Ref head = subRepo.exactRef(Constants.HEAD);
  if (head != null && head.isSymbolic())
    return Repository.shortenRefName(head.getLeaf().getName());
  else
    return null;
}

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

private Ref getHead() throws GitAPIException {
  try {
    Ref head = repo.exactRef(Constants.HEAD);
    if (head == null || head.getObjectId() == null)
      throw new NoHeadException(JGitText.get().headRequiredToStash);
    return head;
  } catch (IOException e) {
    throw new JGitInternalException(JGitText.get().stashFailed, e);
  }
}

代码示例来源: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: org.eclipse.jgit/org.eclipse.jgit

private void resetSoftToParent() throws IOException,
    GitAPIException, CheckoutConflictException {
  Ref ref = repo.exactRef(Constants.ORIG_HEAD);
  ObjectId orig_head = ref == null ? null : ref.getObjectId();
  try (Git git = Git.wrap(repo)) {
    // we have already committed the cherry-picked commit.
    // what we need is to have changes introduced by this
    // commit to be on the index
    // resetting is a workaround
    git.reset().setMode(ResetType.SOFT)
        .setRef("HEAD~1").call(); //$NON-NLS-1$
  } finally {
    // set ORIG_HEAD back to where we started because soft
    // reset moved it
    repo.writeOrigHead(orig_head);
  }
}

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

private void updateStashRef(ObjectId commitId, PersonIdent refLogIdent,
    String refLogMessage) throws IOException {
  Ref currentRef = repo.exactRef(Constants.R_STASH);
  RefUpdate refUpdate = repo.updateRef(Constants.R_STASH);
  refUpdate.setNewObjectId(commitId);
  refUpdate.setRefLogIdent(refLogIdent);
  refUpdate.setRefLogMessage(refLogMessage, false);
  refUpdate.setForceRefLog(true);
  if (currentRef != null)
    refUpdate.setExpectedOldObjectId(currentRef.getObjectId());
  else
    refUpdate.setExpectedOldObjectId(ObjectId.zeroId());
  refUpdate.forceUpdate();
}

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

private static AbstractTreeIterator prepareTreeParser(Repository repository, String ref) throws IOException {
    // from the commit we can build the tree which allows us to construct the TreeParser
    Ref head = repository.exactRef(ref);
    try (RevWalk walk = new RevWalk(repository)) {
      RevCommit commit = walk.parseCommit(head.getObjectId());
      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 ref) throws IOException {
    // from the commit we can build the tree which allows us to construct the TreeParser
    Ref head = repository.exactRef(ref);
    try (RevWalk walk = new RevWalk(repository)) {
      RevCommit commit = walk.parseCommit(head.getObjectId());
      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

@Test
  public void testRevWalkDisposeClosesReader() throws IOException {
    try (Repository repo = CookbookHelper.openJGitCookbookRepository()) {
      try (ObjectReader reader = repo.newObjectReader()) {
        try (RevWalk walk = new RevWalk(reader)) {
          walk.dispose();

          Ref head = repo.exactRef("refs/heads/master");
          System.out.println("Found head: " + head);

          ObjectLoader loader = reader.open(head.getObjectId());
          assertNotNull(loader);
        }
      }
    }
  }
}

相关文章

Repository类方法