Ref getBranch(String branchName) throws GitAPIException { List<Ref> branches = getAllBranches(); for (Ref branch : branches) { if (branch.getName().endsWith(branchName)) { return branch; } } return null; }
private boolean containsBranch(Git git, String label, ListMode listMode) throws GitAPIException { ListBranchCommand command = git.branchList(); if (listMode != null) { command.setListMode(listMode); } List<Ref> branches = command.call(); for (Ref ref : branches) { if (ref.getName().endsWith("/" + label)) { return true; } } return false; }
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); } }
@Test public void shouldBeOnMasterAndTemporaryBranchesDeletedAfterGettingMergeConfig() throws Exception { String original = "first\nsecond\n"; String nextUpdate = "1st\nsecond\n"; String latestUpdate = "first\nsecond\nthird\n"; configRepo.checkin(goConfigRevision(original, "md5-1")); configRepo.checkin(goConfigRevision(nextUpdate, "md5-2")); RevCommit currentRevCommitOnMaster = configRepo.getCurrentRevCommit(); String mergedConfig = configRepo.getConfigMergedWithLatestRevision(goConfigRevision(latestUpdate, "md5-3"), "md5-1"); assertThat(mergedConfig, is("1st\nsecond\nthird\n")); List<Ref> branches = getAllBranches(); assertThat(branches.size(), is(1)); assertThat(branches.get(0).getName().endsWith("master"), is(true)); assertThat(configRepo.getCurrentRevCommit(), is(currentRevCommitOnMaster)); }
@Test public void shouldThrowExceptionWhenThereIsMergeConflict() throws Exception { String original = "first\nsecond\n"; String nextUpdate = "1st\nsecond\n"; String latestUpdate = "2nd\nsecond\n"; configRepo.checkin(goConfigRevision(original, "md5-1")); configRepo.checkin(goConfigRevision(nextUpdate, "md5-2")); RevCommit currentRevCommitOnMaster = configRepo.getCurrentRevCommit(); try { configRepo.getConfigMergedWithLatestRevision(goConfigRevision(latestUpdate, "md5-3"), "md5-1"); fail("should have bombed for merge conflict"); } catch (ConfigMergeException e) { assertThat(e.getMessage(), is(ConfigFileHasChangedException.CONFIG_CHANGED_PLEASE_REFRESH)); } List<Ref> branches = getAllBranches(); assertThat(branches.size(), is(1)); assertThat(branches.get(0).getName().endsWith("master"), is(true)); assertThat(configRepo.getCurrentRevCommit(), is(currentRevCommitOnMaster)); }
/** * Compare a reference to a name. * * @param o1 * the reference instance. * @param o2 * the name to compare to. * @return standard Comparator result of < 0, 0, > 0. */ public static int compareTo(Ref o1, String o2) { return o1.getName().compareTo(o2); }
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; }
/** * Get the name of the ref this update will operate on. * * @return name of underlying ref. */ public String getName() { return getRef().getName(); }
/** * Add a reference to push. * * @param ref * the source reference. The remote name will match. * @return {@code this}. */ public PushCommand add(Ref ref) { refSpecs.add(new RefSpec(ref.getLeaf().getName())); return this; }
private String longDescription(Ref tag, int depth, ObjectId tip) throws IOException { return String.format( "%s-%d-g%s", tag.getName().substring(R_TAGS.length()), //$NON-NLS-1$ Integer.valueOf(depth), w.getObjectReader().abbreviate(tip) .name()); }
/** * Does this specification's destination description match the ref? * * @param r * ref whose name should be tested. * @return true if the names match; false otherwise. */ public boolean matchDestination(Ref r) { return match(r.getName(), getDestination()); }
/** {@inheritDoc} */ @Override public ReflogReader getReflogReader(String refName) throws IOException { Ref ref = findRef(refName); if (ref != null) return new ReflogReaderImpl(this, ref.getName()); return null; }
private String calculateOurName(Ref headRef) { if (ourCommitName != null) return ourCommitName; String targetRefName = headRef.getTarget().getName(); String headName = Repository.shortenRefName(targetRefName); return headName; }
private String calculateOurName(Ref headRef) { if (ourCommitName != null) return ourCommitName; String targetRefName = headRef.getTarget().getName(); String headName = Repository.shortenRefName(targetRefName); return headName; }
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()); } } }
/** {@inheritDoc} */ @Override protected Result doDelete(Result desiredResult) throws IOException { if (getRefDatabase().compareAndRemove(dstRef)) { getRefDatabase().removed(dstRef.getName()); return desiredResult; } return Result.LOCK_FAILURE; }
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; }