use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.
the class GetBlame method apply.
@Override
public Response<List<BlameInfo>> apply(FileResource resource) throws RestApiException, OrmException, IOException, InvalidChangeOperationException {
if (!allowBlame) {
throw new BadRequestException("blame is disabled");
}
Project.NameKey project = resource.getRevision().getChange().getProject();
try (Repository repository = repoManager.openRepository(project);
ObjectInserter ins = repository.newObjectInserter();
ObjectReader reader = ins.newReader();
RevWalk revWalk = new RevWalk(reader)) {
String refName = resource.getRevision().getEdit().isPresent() ? resource.getRevision().getEdit().get().getRefName() : resource.getRevision().getPatchSet().getRefName();
Ref ref = repository.findRef(refName);
if (ref == null) {
throw new ResourceNotFoundException("unknown ref " + refName);
}
ObjectId objectId = ref.getObjectId();
RevCommit revCommit = revWalk.parseCommit(objectId);
RevCommit[] parents = revCommit.getParents();
String path = resource.getPatchKey().getFileName();
List<BlameInfo> result;
if (!base) {
result = blame(revCommit, path, repository, revWalk);
} else if (parents.length == 0) {
throw new ResourceNotFoundException("Initial commit doesn't have base");
} else if (parents.length == 1) {
result = blame(parents[0], path, repository, revWalk);
} else if (parents.length == 2) {
ObjectId automerge = autoMerger.merge(repository, revWalk, ins, revCommit, mergeStrategy);
result = blame(automerge, path, repository, revWalk);
} else {
throw new ResourceNotFoundException("Cannot generate blame for merge commit with more than 2 parents");
}
Response<List<BlameInfo>> r = Response.ok(result);
if (resource.isCacheable()) {
r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
}
return r;
}
}
use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.
the class CommitMsgHookTest method file.
private DirCacheEntry file(final String name) throws IOException {
try (ObjectInserter oi = repository.newObjectInserter()) {
final DirCacheEntry e = new DirCacheEntry(name);
e.setFileMode(FileMode.REGULAR_FILE);
e.setObjectId(oi.insert(Constants.OBJ_BLOB, Constants.encode(name)));
oi.flush();
return e;
}
}
use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.
the class AccountsUpdate method createUserBranch.
private void createUserBranch(Account account) throws IOException {
try (Repository repo = repoManager.openRepository(allUsersName);
ObjectInserter oi = repo.newObjectInserter()) {
String refName = RefNames.refsUsers(account.getId());
if (repo.exactRef(refName) != null) {
throw new IOException(String.format("User branch %s for newly created account %s already exists.", refName, account.getId().get()));
}
createUserBranch(repo, oi, committerIdent, authorIdent, account);
}
}
use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.
the class ExternalIdsOnInit method insert.
public synchronized void insert(String commitMessage, Collection<ExternalId> extIds) throws OrmException, IOException, ConfigInvalidException {
File path = getPath();
if (path != null) {
try (Repository repo = new FileRepository(path);
RevWalk rw = new RevWalk(repo);
ObjectInserter ins = repo.newObjectInserter()) {
ObjectId rev = ExternalIdReader.readRevision(repo);
NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);
for (ExternalId extId : extIds) {
ExternalIdsUpdate.insert(rw, ins, noteMap, extId);
}
PersonIdent serverIdent = new GerritPersonIdentProvider(flags.cfg).get();
ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, commitMessage, serverIdent, serverIdent);
}
}
}
use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.
the class ReceiveCommits method insertChangesAndPatchSets.
private void insertChangesAndPatchSets() {
ReceiveCommand magicBranchCmd = magicBranch != null ? magicBranch.cmd : null;
if (magicBranchCmd != null && magicBranchCmd.getResult() != NOT_ATTEMPTED) {
logWarn(String.format("Skipping change updates on %s because ref update failed: %s %s", project.getName(), magicBranchCmd.getResult(), Strings.nullToEmpty(magicBranchCmd.getMessage())));
return;
}
try (BatchUpdate bu = batchUpdateFactory.create(db, project.getNameKey(), user.materializedCopy(), TimeUtil.nowTs());
ObjectInserter ins = repo.newObjectInserter();
ObjectReader reader = ins.newReader();
RevWalk rw = new RevWalk(reader)) {
bu.setRepository(repo, rw, ins).updateChangesInParallel();
bu.setRequestId(receiveId);
bu.setRefLogMessage("push");
logDebug("Adding {} replace requests", newChanges.size());
for (ReplaceRequest replace : replaceByChange.values()) {
replace.addOps(bu, replaceProgress);
}
logDebug("Adding {} create requests", newChanges.size());
for (CreateRequest create : newChanges) {
create.addOps(bu);
}
logDebug("Adding {} group update requests", newChanges.size());
updateGroups.forEach(r -> r.addOps(bu));
logDebug("Adding {} additional ref updates", actualCommands.size());
actualCommands.forEach(c -> bu.addRepoOnlyOp(new UpdateOneRefOp(c)));
logDebug("Executing batch");
try {
bu.execute();
} catch (UpdateException e) {
throw INSERT_EXCEPTION.apply(e);
}
if (magicBranchCmd != null) {
magicBranchCmd.setResult(OK);
}
for (ReplaceRequest replace : replaceByChange.values()) {
String rejectMessage = replace.getRejectMessage();
if (rejectMessage == null) {
if (replace.inputCommand.getResult() == NOT_ATTEMPTED) {
// Not necessarily the magic branch, so need to set OK on the original value.
replace.inputCommand.setResult(OK);
}
} else {
logDebug("Rejecting due to message from ReplaceOp");
reject(replace.inputCommand, rejectMessage);
}
}
} catch (ResourceConflictException e) {
addMessage(e.getMessage());
reject(magicBranchCmd, "conflict");
} catch (RestApiException | IOException err) {
logError("Can't insert change/patch set for " + project.getName(), err);
reject(magicBranchCmd, "internal server error: " + err.getMessage());
}
if (magicBranch != null && magicBranch.submit) {
try {
submit(newChanges, replaceByChange.values());
} catch (ResourceConflictException e) {
addMessage(e.getMessage());
reject(magicBranchCmd, "conflict");
} catch (RestApiException | OrmException e) {
logError("Error submitting changes to " + project.getName(), e);
reject(magicBranchCmd, "error during submit");
}
}
}
Aggregations