@Signature public Memory exactRef(String name) throws IOException { Ref ref = getWrappedObject().getRepository().exactRef(name); return ref == null ? Memory.NULL : GitUtils.valueOf(ref); }
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); } }
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; } }
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; }
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()); } } }
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()); } } }
/** * 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; } }
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(); } } } }
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)); } }
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()); }
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()); }
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(); } } } }
/** * 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; }
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); } }
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); } } } }
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); } }
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(); }
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; } } }
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; } } }
@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); } } } } }