ListBranches extends Handler<ListBranchesResult> { @Override public ListBranchesResult call() throws NoSuchProjectException { final ProjectControl pctl = projectControlFactory.validateFor( projectName, ProjectControl.OWNER | ProjectControl.VISIBLE); final List<Branch> branches = new ArrayList<Branch>(); Branch headBranch = null; Branch configBranch = null; final Set<String> targets = new HashSet<String>(); final Repository db; try { db = repoManager.openRepository(projectName); } catch (RepositoryNotFoundException noGitRepository) { return new ListBranchesResult(branches, false, true); } try { final Map<String, Ref> all = db.getAllRefs(); if (!all.containsKey(Constants.HEAD)) { try { Ref head = db.getRef(Constants.HEAD); if (head != null) { all.put(Constants.HEAD, head); } } catch (IOException e) { } } for (final Ref ref : all.values()) { if (ref.isSymbolic()) { targets.add(ref.getTarget().getName()); } } for (final Ref ref : all.values()) { if (ref.isSymbolic()) { String target = ref.getTarget().getName(); RefControl targetRefControl = pctl.controlForRef(target); if (!targetRefControl.isVisible()) { continue; } if (target.startsWith(Constants.R_HEADS)) { target = target.substring(Constants.R_HEADS.length()); } Branch b = createBranch(ref.getName()); b.setRevision(new RevId(target)); if (Constants.HEAD.equals(ref.getName())) { b.setCanDelete(false); headBranch = b; } else { b.setCanDelete(targetRefControl.canDelete()); branches.add(b); } continue; } final RefControl refControl = pctl.controlForRef(ref.getName()); if (refControl.isVisible()) { if (ref.getName().startsWith(Constants.R_HEADS)) { branches.add(createBranch(ref, refControl, targets)); } else if (GitRepositoryManager.REF_CONFIG.equals(ref.getName())) { configBranch = createBranch(ref, refControl, targets); } } } } finally { db.close(); } Collections.sort(branches, new Comparator<Branch>() { @Override public int compare(final Branch a, final Branch b) { return a.getName().compareTo(b.getName()); } }); if (configBranch != null) { branches.add(0, configBranch); } if (headBranch != null) { branches.add(0, headBranch); } return new ListBranchesResult(branches, pctl.canAddRefs(), false); } @Inject  ListBranches(final ProjectControl.Factory projectControlFactory,
      final GitRepositoryManager repoManager,

      @Assisted final Project.NameKey name); @Override ListBranchesResult call();  }
@Test public void testProjectNotVisible() throws Exception { final NoSuchProjectException err = new NoSuchProjectException(name); validate().andThrow(err); doReplay(); try { new ListBranches(pcf, grm, name).call(); fail("did not throw when expected not authorized"); } catch (NoSuchProjectException e2) { assertSame(err, e2); } doVerify(); }
@Test public void testSortByName() throws Exception { Map<String, Ref> u = new LinkedHashMap<String, Ref>(); u.put("foo", new ObjectIdRef.Unpeeled(LOOSE, R_HEADS + "foo", idA)); u.put("bar", new ObjectIdRef.Unpeeled(LOOSE, R_HEADS + "bar", idA)); u.put(HEAD, new SymbolicRef(HEAD, new ObjectIdRef.Unpeeled(LOOSE, R_HEADS + "master", null))); validate().andReturn(pc); expect(grm.openRepository(eq(name))).andReturn(mockDb); expect(mockDb.getAllRefs()).andReturn(u); for (Ref ref : u.values()) { assumeVisible(ref, true, targets(u)); } expect(pc.canAddRefs()).andReturn(true); mockDb.close(); expectLastCall(); doReplay(); final ListBranchesResult r = new ListBranches(pcf, grm, name).call(); doVerify(); assertNotNull(r); assertEquals(3, r.getBranches().size()); assertEquals(HEAD, r.getBranches().get(0).getShortName()); assertEquals("bar", r.getBranches().get(1).getShortName()); assertEquals("foo", r.getBranches().get(2).getShortName()); }
@Test public void testHeadNotVisible() throws Exception { ObjectIdRef.Unpeeled bar = new ObjectIdRef.Unpeeled(LOOSE, R_HEADS + "bar", idA); Map<String, Ref> u = new LinkedHashMap<String, Ref>(); u.put(bar.getName(), bar); u.put(HEAD, new SymbolicRef(HEAD, bar)); validate().andReturn(pc); expect(grm.openRepository(eq(name))).andReturn(mockDb); expect(mockDb.getAllRefs()).andReturn(u); assumeVisible(bar, false, targets(u)); assumeVisible(bar, false, targets(u)); expect(pc.canAddRefs()).andReturn(true); mockDb.close(); expectLastCall(); doReplay(); final ListBranchesResult r = new ListBranches(pcf, grm, name).call(); doVerify(); assertNotNull(r); assertTrue(r.getBranches().isEmpty()); }
@Test public void testHeadVisibleButBranchHidden() throws Exception { ObjectIdRef.Unpeeled bar = new ObjectIdRef.Unpeeled(LOOSE, R_HEADS + "bar", idA); ObjectIdRef.Unpeeled foo = new ObjectIdRef.Unpeeled(LOOSE, R_HEADS + "foo", idA); Map<String, Ref> u = new LinkedHashMap<String, Ref>(); u.put(bar.getName(), bar); u.put(HEAD, new SymbolicRef(HEAD, bar)); u.put(foo.getName(), foo); validate().andReturn(pc); expect(grm.openRepository(eq(name))).andReturn(mockDb); expect(mockDb.getAllRefs()).andReturn(u); assumeVisible(bar, true, targets(u)); assumeVisible(bar, true, targets(u)); assumeVisible(foo, false, targets(u)); expect(pc.canAddRefs()).andReturn(true); mockDb.close(); expectLastCall(); doReplay(); final ListBranchesResult r = new ListBranches(pcf, grm, name).call(); doVerify(); assertNotNull(r); assertEquals(2, r.getBranches().size()); assertEquals(HEAD, r.getBranches().get(0).getShortName()); assertFalse(r.getBranches().get(0).getCanDelete()); assertEquals("bar", r.getBranches().get(1).getShortName()); assertFalse(r.getBranches().get(1).getCanDelete()); }