use of org.eclipse.jgit.errors.RepositoryNotFoundException in project gerrit by GerritCodeReview.
the class AdminSetParent method run.
@Override
protected void run() throws Failure {
if (oldParent == null && children.isEmpty()) {
throw die("child projects have to be specified as " + "arguments or the --children-of option has to be set");
}
if (oldParent == null && !excludedChildren.isEmpty()) {
throw die("--exclude can only be used together with --children-of");
}
final StringBuilder err = new StringBuilder();
final Set<Project.NameKey> grandParents = new HashSet<>();
grandParents.add(allProjectsName);
if (newParent != null) {
newParentKey = newParent.getProject().getNameKey();
// Catalog all grandparents of the "parent", we want to
// catch a cycle in the parent pointers before it occurs.
//
Project.NameKey gp = newParent.getProject().getParent();
while (gp != null && grandParents.add(gp)) {
final ProjectState s = projectCache.get(gp);
if (s != null) {
gp = s.getProject().getParent();
} else {
break;
}
}
}
final List<Project.NameKey> childProjects = new ArrayList<>();
for (final ProjectControl pc : children) {
childProjects.add(pc.getProject().getNameKey());
}
if (oldParent != null) {
try {
childProjects.addAll(getChildrenForReparenting(oldParent));
} catch (PermissionBackendException e) {
throw new Failure(1, "permissions unavailable", e);
}
}
for (final Project.NameKey nameKey : childProjects) {
final String name = nameKey.get();
if (allProjectsName.equals(nameKey)) {
// Don't allow the wild card project to have a parent.
//
err.append("error: Cannot set parent of '").append(name).append("'\n");
continue;
}
if (grandParents.contains(nameKey) || nameKey.equals(newParentKey)) {
// Try to avoid creating a cycle in the parent pointers.
//
err.append("error: Cycle exists between '").append(name).append("' and '").append(newParentKey != null ? newParentKey.get() : allProjectsName.get()).append("'\n");
continue;
}
try (MetaDataUpdate md = metaDataUpdateFactory.create(nameKey)) {
ProjectConfig config = ProjectConfig.read(md);
config.getProject().setParentName(newParentKey);
md.setMessage("Inherit access from " + (newParentKey != null ? newParentKey.get() : allProjectsName.get()) + "\n");
config.commit(md);
} catch (RepositoryNotFoundException notFound) {
err.append("error: Project ").append(name).append(" not found\n");
} catch (IOException | ConfigInvalidException e) {
final String msg = "Cannot update project " + name;
log.error(msg, e);
err.append("error: ").append(msg).append("\n");
}
projectCache.evict(nameKey);
}
if (err.length() > 0) {
while (err.charAt(err.length() - 1) == '\n') {
err.setLength(err.length() - 1);
}
throw die(err.toString());
}
}
use of org.eclipse.jgit.errors.RepositoryNotFoundException in project gerrit by GerritCodeReview.
the class ChangeFinder method find.
/**
* Find changes matching the given identifier.
*
* @param id change identifier, either a numeric ID, a Change-Id, or project~branch~id triplet.
* @param user user to wrap in controls.
* @return possibly-empty list of controls for all matching changes, corresponding to the given
* user; may or may not be visible.
* @throws OrmException if an error occurred querying the database.
*/
public List<ChangeControl> find(String id, CurrentUser user) throws OrmException {
if (id.isEmpty()) {
return Collections.emptyList();
}
// Use the index to search for changes, but don't return any stored fields,
// to force rereading in case the index is stale.
InternalChangeQuery query = queryProvider.get().noFields();
int numTwiddles = 0;
for (char c : id.toCharArray()) {
if (c == '~') {
numTwiddles++;
}
}
if (numTwiddles == 1) {
// Try project~numericChangeId
String project = id.substring(0, id.indexOf('~'));
Integer n = Ints.tryParse(id.substring(project.length() + 1));
if (n != null) {
Change.Id changeId = new Change.Id(n);
try {
return ImmutableList.of(changeControlFactory.controlFor(reviewDb.get(), Project.NameKey.parse(project), changeId, user));
} catch (NoSuchChangeException e) {
return Collections.emptyList();
} catch (IllegalArgumentException e) {
String changeNotFound = String.format("change %s not found in ReviewDb", changeId);
String projectNotFound = String.format("passed project %s when creating ChangeNotes for %s, but actual project is", project, changeId);
if (e.getMessage().equals(changeNotFound) || e.getMessage().startsWith(projectNotFound)) {
return Collections.emptyList();
}
throw e;
} catch (OrmException e) {
// other OrmExceptions (failure in the persistence layer).
if (Throwables.getRootCause(e) instanceof RepositoryNotFoundException) {
return Collections.emptyList();
}
throw e;
}
}
} else if (numTwiddles == 2) {
// Try change triplet
Optional<ChangeTriplet> triplet = ChangeTriplet.parse(id);
if (triplet.isPresent()) {
return asChangeControls(query.byBranchKey(triplet.get().branch(), triplet.get().id()), user);
}
}
// Try numeric changeId
if (id.charAt(0) != '0') {
Integer n = Ints.tryParse(id);
if (n != null) {
return asChangeControls(query.byLegacyChangeId(new Change.Id(n)), user);
}
}
// Try isolated changeId
return asChangeControls(query.byKeyPrefix(id), user);
}
use of org.eclipse.jgit.errors.RepositoryNotFoundException in project gerrit by GerritCodeReview.
the class GetContent method getMessage.
private String getMessage(ChangeNotes notes) throws OrmException, IOException {
Change.Id changeId = notes.getChangeId();
PatchSet ps = psUtil.current(db.get(), notes);
if (ps == null) {
throw new NoSuchChangeException(changeId);
}
try (Repository git = gitManager.openRepository(notes.getProjectName());
RevWalk revWalk = new RevWalk(git)) {
RevCommit commit = revWalk.parseCommit(ObjectId.fromString(ps.getRevision().get()));
return commit.getFullMessage();
} catch (RepositoryNotFoundException e) {
throw new NoSuchChangeException(changeId, e);
}
}
use of org.eclipse.jgit.errors.RepositoryNotFoundException 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.errors.RepositoryNotFoundException in project gitiles by GerritCodeReview.
the class RepositoryFilter method doFilter.
@Override
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
try {
String repo = ViewFilter.trimLeadingSlash(getRegexGroup(req, 1));
try (Repository git = resolver.open(req, repo)) {
req.setAttribute(ATTRIBUTE_REPOSITORY, git);
chain.doFilter(req, res);
} catch (RepositoryNotFoundException e) {
// Drop through the rest of the chain. ViewFilter will pass this
// to HostIndexServlet which will attempt to list repositories
// or send SC_NOT_FOUND there.
chain.doFilter(req, res);
} catch (ServiceMayNotContinueException e) {
sendError(req, res, e.getStatusCode(), e.getMessage());
} finally {
req.removeAttribute(ATTRIBUTE_REPOSITORY);
}
} catch (ServiceNotEnabledException e) {
sendError(req, res, SC_FORBIDDEN);
} catch (ServiceNotAuthorizedException e) {
res.sendError(SC_UNAUTHORIZED);
}
}
Aggregations