private static String getHeadName(Ref head) { String headName; if (head.isSymbolic()) { headName = head.getTarget().getName(); } else { ObjectId headId = head.getObjectId(); // the callers are checking this already assert headId != null; headName = headId.getName(); } return headName; }
/** * Include a single ref (a name/object pair) in the bundle. * <p> * This is a utility function for: * <code>include(r.getName(), r.getObjectId())</code>. * * @param r * the ref to include. */ public void include(Ref r) { include(r.getName(), r.getObjectId()); if (r.getPeeledObjectId() != null) tagTargets.add(r.getPeeledObjectId()); else if (r.getObjectId() != null && r.getName().startsWith(Constants.R_HEADS)) tagTargets.add(r.getObjectId()); }
public Long commitCountOnMaster() throws GitAPIException, IncorrectObjectTypeException, MissingObjectException { // not inside a doLocked/synchronized block because we don't want to block the server status service. // we do a `git branch` because we switch branches as part of normal git operations, // and we don't care about number of commits on those branches. List<Ref> branches = git.branchList().call(); for (Ref branch : branches) { if (branch.getName().equals("refs/heads/master")) { Iterable<RevCommit> commits = git.log().add(branch.getObjectId()).call(); long count = 0; for (RevCommit commit : commits) { count++; } return count; } } return Long.valueOf(-1); } }
public static ArrayMemory valueOf(Ref ref) { ArrayMemory memory = new ArrayMemory(); memory.refOfIndex("name").assign(ref.getName()); memory.refOfIndex("peeled").assign(ref.isPeeled()); memory.refOfIndex("symbolic").assign(ref.isSymbolic()); memory.refOfIndex("objectId").assign(valueOf(ref.getObjectId())); memory.refOfIndex("storage").assign(valueOf(ref.getStorage())); return memory; }
@Override int valueType() { if (ref.isSymbolic()) { return VALUE_SYMREF; } else if (ref.getStorage() == NEW && ref.getObjectId() == null) { return VALUE_NONE; } else if (ref.getPeeledObjectId() != null) { return VALUE_2ID; } else { return VALUE_1ID; } }
try (Git git = Git.cloneRepository() .setURI(REMOTE_URL) .setDirectory(localPath) .call()) { System.out.println("Having repository: " + git.getRepository().getDirectory()); FetchResult result = git.fetch().setCheckFetchedObjects(true).call(); System.out.println("Messages: " + result.getMessages()); List<Ref> call = git.branchList().call(); for (Ref ref : call) { System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); call = git.branchList().setListMode(ListMode.ALL).call(); for (Ref ref : call) { System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
checkCallable(); try (RevWalk revWalk = new RevWalk(repo)) { Ref headRef = repo.exactRef(Constants.HEAD); if (headRef == null) throw new NoHeadException( JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported); RevCommit headCommit = revWalk.parseCommit(headRef.getObjectId()); ObjectId srcObjectId = src.getPeeledObjectId(); if (srcObjectId == null) srcObjectId = src.getObjectId(); RevCommit srcCommit = revWalk.parseCommit(srcObjectId); String revertName = srcCommit.getId().abbreviate(7).name() + "\""; //$NON-NLS-1$ String newMessage = shortMessage + "\n\n" //$NON-NLS-1$ + "This reverts commit " + srcCommit.getId().getName() //$NON-NLS-1$ + ".\n"; //$NON-NLS-1$ if (merger.merge(headCommit, srcParent)) { .formatWithConflicts(newMessage, merger.getUnmergedPaths()); repo.writeRevertHead(srcCommit.getId());
checkCallable(); try (RevWalk revWalk = new RevWalk(repo)) { Ref headRef = repo.exactRef(Constants.HEAD); if (headRef == null) throw new NoHeadException( JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported); newHead = revWalk.parseCommit(headRef.getObjectId()); ObjectId srcObjectId = src.getPeeledObjectId(); if (srcObjectId == null) srcObjectId = src.getObjectId(); RevCommit srcCommit = revWalk.parseCommit(srcObjectId); String cherryPickName = srcCommit.getId().abbreviate(7).name() + " " + srcCommit.getShortMessage(); //$NON-NLS-1$ merger.setBase(srcParent.getTree()); merger.setCommitNames(new String[] { "BASE", ourName, //$NON-NLS-1$ cherryPickName }); repo.writeCherryPickHead(srcCommit.getId()); repo.writeMergeCommitMsg(message);
public static void main(String[] args) throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { // See e.g. GetRevCommitFromObjectId for how to use a SHA-1 directly Ref head = repository.findRef("HEAD"); System.out.println("Ref of HEAD: " + head + ": " + head.getName() + " - " + head.getObjectId().getName()); // 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("Commit: " + commit); // a commit points to a tree RevTree tree = walk.parseTree(commit.getTree().getId()); System.out.println("Found Tree: " + tree); walk.dispose(); } } } }
public static void main(String[] args) throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { Ref head = repository.findRef("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()); RevTree tree = commit.getTree(); System.out.println("Having tree: " + tree); // now use a TreeWalk to iterate over all files in the Tree // you can set Filters to narrow down the results if needed try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); // not walk the tree recursively so we only get the elements in the top-level directory treeWalk.setRecursive(false); while (treeWalk.next()) { System.out.println("found: " + treeWalk.getPathString()); } } } } } }
try (Git git = new Git(repo)) { CreateBranchCommand command = git.branchCreate(); command.setName(name); if (startCommit != null) Ref headRef = repo.exactRef(Constants.HEAD); if (headRef == null) { r.name())); this.status = CheckoutResult.NOT_TRIED_RESULT; return repo.exactRef(Constants.HEAD); try (RevWalk revWalk = new RevWalk(repo)) { AnyObjectId headId = headRef.getObjectId(); headCommit = headId == null ? null : revWalk.parseCommit(headId); newCommit = revWalk.parseCommit(branch); RevTree headTree = headCommit == null ? null : headCommit.getTree(); if (ref != null && !ref.getName().startsWith(Constants.R_HEADS)) Result updateResult; if (ref != null) updateResult = refUpdate.link(ref.getName()); else if (orphan) { updateResult = refUpdate.link(getBranchName());
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { Ref head = repository.exactRef("refs/heads/master"); System.out.println("Found head: " + head); try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(head.getObjectId()); System.out.println("Found Commit: " + commit); try (Git git = new Git(repository)) { git.notesAdd().setMessage("some note message").setObjectId(commit).call(); System.out.println("Added Note to commit " + commit); List<Note> call = git.notesList().call(); System.out.println("Listing " + call.size() + " notes"); for(Note note : call) { if(!note.getName().equals(head.getObjectId().getName())) { System.out.println("Note " + note + " did not match commit " + head); continue; ObjectLoader loader = repository.open(note.getData()); loader.copyTo(System.out); walk.dispose();
/** * Get latest commit by ref name from local .git */ @Override public GitCommit commit(String refName) throws GitException { try (Git git = gitOpen()) { Repository repo = git.getRepository(); Ref head = repo.findRef(refName); if (head == null) { return null; } try (RevWalk walk = new RevWalk(repo)) { RevCommit commit = walk.parseCommit(head.getObjectId()); walk.dispose(); String id = commit.getId().getName(); String message = commit.getFullMessage(); String author = commit.getAuthorIdent().getName(); return new GitCommit(id, message, author); } } catch (IOException e) { throw new GitException("Fail to get commit message", e); } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { List<Ref> call = git.tagList().call(); for (Ref ref : call) { System.out.println("Tag: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); // fetch all commits for this tag LogCommand log = git.log(); Ref peeledRef = repository.getRefDatabase().peel(ref); if(peeledRef.getPeeledObjectId() != null) { log.add(peeledRef.getPeeledObjectId()); } else { log.add(ref.getObjectId()); } Iterable<RevCommit> logs = log.call(); for (RevCommit rev : logs) { System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */); } } } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Git git = new Git(repository)) { List<Ref> call = git.branchList().call(); for (Ref ref : call) { System.out.println("Branch-Before: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); List<Ref> refs = git.branchList().call(); for(Ref ref : refs) { System.out.println("Had branch: " + ref.getName()); if(ref.getName().equals("refs/heads/testbranch")) { System.out.println("Removing branch before"); git.branchDelete() .call(); call = git.branchList().call(); for (Ref ref : call) { System.out.println("Branch-Created: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); call = git.branchList().call(); for (Ref ref : call) { System.out.println("Branch-After: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
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; } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { System.out.println("Listing local branches:"); try (Git git = new Git(repository)) { List<Ref> call = git.branchList().call(); for (Ref ref : call) { System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); } System.out.println("Now including remote branches:"); call = git.branchList().setListMode(ListMode.ALL).call(); for (Ref ref : call) { System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.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); System.out.println("\nPrint contents of head of master branch, i.e. the latest commit information"); ObjectLoader loader = repository.open(head.getObjectId()); loader.copyTo(System.out); System.out.println("\nPrint contents of tree of head of master branch, i.e. the latest binary tree information"); // a commit points to a tree try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(head.getObjectId()); RevTree tree = walk.parseTree(commit.getTree().getId()); System.out.println("Found Tree: " + tree); loader = repository.open(tree.getId()); loader.copyTo(System.out); walk.dispose(); } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { List<Ref> refs = git.branchList().call(); for (Ref ref : refs) { System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); listReflog(repository, ref); } List<Ref> call = git.tagList().call(); for (Ref ref : call) { System.out.println("Tag: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); listReflog(repository, ref); } } } }
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(); } } } }