private Iterable<RevCommit> getCommitsFromTag(String treeName) { try { List<Ref> call = git.tagList().call(); Iterable<RevCommit> logs = null; for (Ref ref : call) { if (ref.getName().equals(treeName)) { LogCommand log = git.log(); Ref peeledRef = repository.peel(ref); if (peeledRef.getPeeledObjectId() != null) { log.add(peeledRef.getPeeledObjectId()); } else { log.add(ref.getObjectId()); } logs = log.call(); return logs; } } return null; } catch (GitAPIException e) { throw new VisMinerAPIException(e.getMessage(), e); } catch (MissingObjectException e) { throw new VisMinerAPIException(e.getMessage(), e); } catch (IncorrectObjectTypeException e) { throw new VisMinerAPIException(e.getMessage(), e); } }
private boolean isMergedInto(Ref oldHead, AnyObjectId src) throws IOException { RevWalk revWalk = new RevWalk(db); ObjectId oldHeadObjectId = oldHead.getPeeledObjectId(); if (oldHeadObjectId == null) oldHeadObjectId = oldHead.getObjectId(); RevCommit oldHeadCommit = revWalk.lookupCommit(oldHeadObjectId); RevCommit srcCommit = revWalk.lookupCommit(src); return revWalk.isMergedInto(oldHeadCommit, srcCommit); }
/** * Make sure a ref is peeled and has the Storage PACKED. If the given ref has this attributes * simply return it. Otherwise create a new peeled {@link ObjectIdRef} where Storage is set to * PACKED. * * @param f * @return a ref for Storage PACKED having the same name, id, peeledId as f * @throws MissingObjectException * @throws IOException */ private Ref peeledPackedRef(Ref f) throws MissingObjectException, IOException { if (f.getStorage().isPacked() && f.isPeeled()) { return f; } if (!f.isPeeled()) { f = peel(f); } ObjectId peeledObjectId = f.getPeeledObjectId(); if (peeledObjectId != null) { return new ObjectIdRef.PeeledTag(PACKED, f.getName(), f.getObjectId(), peeledObjectId); } else { return new ObjectIdRef.PeeledNonTag(PACKED, f.getName(), f.getObjectId()); } }
public static String getHashOfBound(String url, UpdateBound bound) { if (bound instanceof CommitHashBound) { return bound.getBoundHash(); } String refName = bound.getBound(); try { Collection<Ref> refs = Git.lsRemoteRepository().setRemote(url).setHeads(true).setTags(true).call(); for (Ref ref : refs) { boolean isCorrectRef = ref.getName().contains(refName); if (isCorrectRef) { ObjectId objectId = ref.getPeeledObjectId(); if (objectId == null) { objectId = ref.getObjectId(); } return ObjectId.toString(objectId); } } } catch (GitAPIException e) { return null; } return null; }
private Ref readRef(final TreeMap<String, Ref> avail, final String rn) throws TransportException { final String s; String ref = ROOT_DIR + rn; try { final BufferedReader br = openReader(ref); try { s = br.readLine(); } finally { br.close(); } } catch (FileNotFoundException noRef) { return null; } catch (IOException err) { throw new TransportException(getURI(), "read " + ref, err); } if (s == null) throw new TransportException(getURI(), "Empty ref: " + rn); if (s.startsWith("ref: ")) { final String target = s.substring("ref: ".length()); Ref r = avail.get(target); if (r == null) r = readRef(avail, target); if (r == null) return null; r = new Ref(r.getStorage(), rn, r.getObjectId(), r.getPeeledObjectId(), r.isPeeled()); avail.put(r.getName(), r); return r; } if (ObjectId.isId(s)) { final Ref r = new Ref(loose(avail.get(rn)), rn, ObjectId.fromString(s)); avail.put(r.getName(), r); return r; } throw new TransportException(getURI(), "Bad ref: " + rn + ": " + s); }
public String getProjectVersion(File repoDir) throws IOException, GitAPIException { Git git = Git.open(repoDir); Repository repo = git.getRepository(); // Find base commit between current branch and "master": String branch = repo.getBranch(); RevCommit base = CommitUtils.getBase(repo, "master", branch); CommitCountFilter count = new CommitCountFilter(); CommitFinder finder = new CommitFinder(repo).setFilter(count); finder.findBetween(branch, base); long commitsSinceBase = count.getCount(); // Find tags in "master" before base commit: RevWalk rw = new RevWalk(repo); rw.markStart(base); rw.setRetainBody(false); Ref master = repo.getRef("master"); List<Ref> masterAsList = Arrays.asList(master); List<Ref> tags = git.tagList().call(); Map<RevCommit, Ref> masterTags = new HashMap<RevCommit, Ref>(); for (Ref tag : tags) { tag = repo.peel(tag); ObjectId commitID = tag.getPeeledObjectId(); if (commitID == null) continue; RevCommit commit = rw.parseCommit(commitID); // Only remember tags reachable from "master": if (!RevWalkUtils.findBranchesReachableFrom(commit, rw, masterAsList).isEmpty()) { masterTags.put(commit, tag); } } // Find the shortest distance in commits between base tag in "master": long commitsBetweenBaseAndTag = Long.MAX_VALUE; String tagName = ""; for (RevCommit tagCommit : masterTags.keySet()) { count.reset(); finder.findBetween(base, tagCommit); if (count.getCount() < commitsBetweenBaseAndTag) { commitsBetweenBaseAndTag = count.getCount(); tagName = masterTags.get(tagCommit).getName(); } } if (commitsBetweenBaseAndTag == Long.MAX_VALUE) { // If no tag, get total number of commits: commitsBetweenBaseAndTag = repo.getRefDatabase().getRefs("").size(); } long commitsSinceLastMasterTag = commitsSinceBase + commitsBetweenBaseAndTag; // Construct version string: String version = branch.equals("master") ? "" : (branch + "-"); if (tagName.startsWith("refs/tags/")) { tagName = tagName.substring("refs/tags/".length()); } // v1.1 -> 1.1 if (tagName.matches("v\\d+.*")) { tagName = tagName.substring(1); } if (tagName.isEmpty()) { version = "0"; } version += tagName + ((!tagonly) ? "." + commitsSinceLastMasterTag : ""); return version; }
private void markRefsAdvertised() { for (final Ref r : getRefs()) { markAdvertised(r.getObjectId()); if (r.getPeeledObjectId() != null) markAdvertised(r.getPeeledObjectId()); } }
public static CherryPickResult cherryPickNoMerge(final Git git, Ref src) throws GitAPIException, CantMergeCommitWithZeroParentsException { // Does the same as the original git-cherryPick // except commiting after running merger Repository repo = git.getRepository(); RevCommit newHead = null; List<Ref> cherryPickedRefs = new LinkedList<Ref>(); RevWalk revWalk = new RevWalk(repo); try { // get the head commit Ref headRef = repo.getRef(Constants.HEAD); if (headRef == null) throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported); RevCommit headCommit = revWalk.parseCommit(headRef.getObjectId()); newHead = headCommit; // get the commit to be cherry-picked // handle annotated tags ObjectId srcObjectId = src.getPeeledObjectId(); if (srcObjectId == null) srcObjectId = src.getObjectId(); RevCommit srcCommit = revWalk.parseCommit(srcObjectId); // get the parent of the commit to cherry-pick if (srcCommit.getParentCount() == 0) throw new CantMergeCommitWithZeroParentsException( "Commit with zero parents cannot be merged"); if (srcCommit.getParentCount() > 1) throw new MultipleParentsNotAllowedException( MessageFormat.format( JGitText.get().canOnlyCherryPickCommitsWithOneParent, srcCommit.name(), Integer.valueOf(srcCommit.getParentCount()))); RevCommit srcParent = srcCommit.getParent(0); revWalk.parseHeaders(srcParent); ResolveMerger merger = (ResolveMerger) MergeStrategy.RESOLVE.newMerger(repo); merger.setWorkingTreeIterator(new FileTreeIterator(repo)); merger.setBase(srcParent.getTree()); if (merger.merge(headCommit, srcCommit)) { DirCacheCheckout dco = new DirCacheCheckout( repo, headCommit.getTree(), repo.lockDirCache(), merger.getResultTreeId()); dco.setFailOnConflict(true); dco.checkout(); cherryPickedRefs.add(src); } else { if (merger.failed()) return new CherryPickResult(merger.getFailingPaths()); // there are merge conflicts String message = new MergeMessageFormatter() .formatWithConflicts(srcCommit.getFullMessage(), merger.getUnmergedPaths()); repo.writeCherryPickHead(srcCommit.getId()); repo.writeMergeCommitMsg(message); return CherryPickResult.CONFLICT; } } catch (IOException e) { throw new JGitInternalException( MessageFormat.format(JGitText.get().exceptionCaughtDuringExecutionOfCherryPickCommand, e), e); } finally { revWalk.release(); } return new CherryPickResult(newHead, cherryPickedRefs); }