use of org.eclipse.jgit.lib.Ref in project gerrit by GerritCodeReview.
the class InternalChangeQuery method byCommitsOnBranchNotMergedFromDatabase.
private Iterable<ChangeData> byCommitsOnBranchNotMergedFromDatabase(Repository repo, final ReviewDb db, final Branch.NameKey branch, Collection<String> hashes) throws OrmException, IOException {
Set<Change.Id> changeIds = Sets.newHashSetWithExpectedSize(hashes.size());
String lastPrefix = null;
for (Ref ref : repo.getRefDatabase().getRefs(RefNames.REFS_CHANGES).values()) {
String r = ref.getName();
if ((lastPrefix != null && r.startsWith(lastPrefix)) || !hashes.contains(ref.getObjectId().name())) {
continue;
}
Change.Id id = Change.Id.fromRef(r);
if (id == null) {
continue;
}
if (changeIds.add(id)) {
lastPrefix = r.substring(0, r.lastIndexOf('/'));
}
}
List<ChangeNotes> notes = notesFactory.create(db, branch.getParentKey(), changeIds, cn -> {
Change c = cn.getChange();
return c.getDest().equals(branch) && c.getStatus() != Change.Status.MERGED;
});
return Lists.transform(notes, n -> changeDataFactory.create(db, n));
}
use of org.eclipse.jgit.lib.Ref in project gerrit by GerritCodeReview.
the class SetHead method apply.
@Override
public String apply(final ProjectResource rsrc, Input input) throws AuthException, ResourceNotFoundException, BadRequestException, UnprocessableEntityException, IOException {
if (!rsrc.getControl().isOwner()) {
throw new AuthException("restricted to project owner");
}
if (input == null || Strings.isNullOrEmpty(input.ref)) {
throw new BadRequestException("ref required");
}
String ref = RefNames.fullName(input.ref);
try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
Map<String, Ref> cur = repo.getRefDatabase().exactRef(Constants.HEAD, ref);
if (!cur.containsKey(ref)) {
throw new UnprocessableEntityException(String.format("Ref Not Found: %s", ref));
}
final String oldHead = cur.get(Constants.HEAD).getTarget().getName();
final String newHead = ref;
if (!oldHead.equals(newHead)) {
final RefUpdate u = repo.updateRef(Constants.HEAD, true);
u.setRefLogIdent(identifiedUser.get().newRefLogIdent());
RefUpdate.Result res = u.link(newHead);
switch(res) {
case NO_CHANGE:
case RENAMED:
case FORCED:
case NEW:
break;
case FAST_FORWARD:
case IO_FAILURE:
case LOCK_FAILURE:
case NOT_ATTEMPTED:
case REJECTED:
case REJECTED_CURRENT_BRANCH:
default:
throw new IOException("Setting HEAD failed with " + res);
}
fire(rsrc.getNameKey(), oldHead, newHead);
}
return ref;
} catch (RepositoryNotFoundException e) {
throw new ResourceNotFoundException(rsrc.getName());
}
}
use of org.eclipse.jgit.lib.Ref in project gerrit by GerritCodeReview.
the class ChangeData method draftRefs.
public Map<Account.Id, Ref> draftRefs() throws OrmException {
if (draftsByUser == null) {
if (!lazyLoad) {
return Collections.emptyMap();
}
Change c = change();
if (c == null) {
return Collections.emptyMap();
}
draftsByUser = new HashMap<>();
if (notesMigration.readChanges()) {
for (Ref ref : commentsUtil.getDraftRefs(notes.getChangeId())) {
Account.Id account = Account.Id.fromRefSuffix(ref.getName());
if (account != null && // this point.
!notes().getDraftComments(account, ref).isEmpty()) {
draftsByUser.put(account, ref);
}
}
} else {
for (Comment sc : commentsUtil.draftByChange(db, notes())) {
draftsByUser.put(sc.author.getId(), null);
}
}
}
return draftsByUser;
}
use of org.eclipse.jgit.lib.Ref in project gerrit by GerritCodeReview.
the class ChangeData method isMergeable.
public Boolean isMergeable() throws OrmException {
if (mergeable == null) {
Change c = change();
if (c == null) {
return null;
}
if (c.getStatus() == Change.Status.MERGED) {
mergeable = true;
} else if (c.getStatus() == Change.Status.ABANDONED) {
return null;
} else if (c.isWorkInProgress()) {
return null;
} else {
if (!lazyLoad) {
return null;
}
PatchSet ps = currentPatchSet();
try {
if (ps == null || !changeControl().isPatchVisible(ps, db)) {
return null;
}
} catch (OrmException e) {
if (e.getCause() instanceof NoSuchChangeException) {
return null;
}
throw e;
}
try (Repository repo = repoManager.openRepository(project())) {
Ref ref = repo.getRefDatabase().exactRef(c.getDest().get());
SubmitTypeRecord str = submitTypeRecord();
if (!str.isOk()) {
// No need to log, as SubmitRuleEvaluator already did it for us.
return false;
}
String mergeStrategy = mergeUtilFactory.create(projectCache.get(project())).mergeStrategyName();
mergeable = mergeabilityCache.get(ObjectId.fromString(ps.getRevision().get()), ref, str.type, mergeStrategy, c.getDest(), repo);
} catch (IOException e) {
throw new OrmException(e);
}
}
}
return mergeable;
}
use of org.eclipse.jgit.lib.Ref in project gitiles by GerritCodeReview.
the class RefServlet method getBranchesSoyData.
static List<Map<String, Object>> getBranchesSoyData(HttpServletRequest req, int limit) throws IOException {
RefDatabase refdb = ServletUtils.getRepository(req).getRefDatabase();
Ref head = refdb.exactRef(Constants.HEAD);
Ref headLeaf = head != null && head.isSymbolic() ? head.getLeaf() : null;
return getRefsSoyData(refdb, ViewFilter.getView(req), Constants.R_HEADS, branchComparator(headLeaf), headLeaf, limit);
}
Aggregations