use of org.eclipse.jgit.lib.ObjectId in project gerrit by GerritCodeReview.
the class TagSet method prepare.
void prepare(TagMatcher m) {
@SuppressWarnings("resource") RevWalk rw = null;
try {
for (Ref currentRef : m.include) {
if (currentRef.isSymbolic()) {
continue;
}
if (currentRef.getObjectId() == null) {
continue;
}
CachedRef savedRef = refs.get(currentRef.getName());
if (savedRef == null) {
// If the reference isn't known to the set, return null
// and force the caller to rebuild the set in a new copy.
m.newRefs.add(currentRef);
continue;
}
// The reference has not been moved. It can be used as-is.
ObjectId savedObjectId = savedRef.get();
if (currentRef.getObjectId().equals(savedObjectId)) {
m.mask.set(savedRef.flag);
continue;
}
// This is very likely for a branch that fast-forwarded.
try {
if (rw == null) {
rw = new RevWalk(m.db);
rw.setRetainBody(false);
}
RevCommit savedCommit = rw.parseCommit(savedObjectId);
RevCommit currentCommit = rw.parseCommit(currentRef.getObjectId());
if (rw.isMergedInto(savedCommit, currentCommit)) {
// Fast-forward. Safely update the reference in-place.
savedRef.compareAndSet(savedObjectId, currentRef.getObjectId());
m.mask.set(savedRef.flag);
continue;
}
// The branch rewound. Walk the list of commits removed from
// the reference. If any matches to a tag, this has to be removed.
boolean err = false;
rw.reset();
rw.markStart(savedCommit);
rw.markUninteresting(currentCommit);
rw.sort(RevSort.TOPO, true);
RevCommit c;
while ((c = rw.next()) != null) {
Tag tag = tags.get(c);
if (tag != null && tag.refFlags.get(savedRef.flag)) {
m.lostRefs.add(new TagMatcher.LostRef(tag, savedRef.flag));
err = true;
}
}
if (!err) {
// All of the tags are still reachable. Update in-place.
savedRef.compareAndSet(savedObjectId, currentRef.getObjectId());
m.mask.set(savedRef.flag);
}
} catch (IOException err) {
// Defer a cache update until later. No conclusion can be made
// based on an exception reading from the repository storage.
log.warn("Error checking tags of " + projectName, err);
}
}
} finally {
if (rw != null) {
rw.close();
}
}
}
use of org.eclipse.jgit.lib.ObjectId in project gerrit by GerritCodeReview.
the class VersionedMetaData method saveFile.
protected void saveFile(String fileName, byte[] raw) throws IOException {
DirCacheEditor editor = newTree.editor();
if (raw != null && 0 < raw.length) {
final ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, raw);
editor.add(new PathEdit(fileName) {
@Override
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.REGULAR_FILE);
ent.setObjectId(blobId);
}
});
} else {
editor.add(new DeletePath(fileName));
}
editor.finish();
}
use of org.eclipse.jgit.lib.ObjectId in project gerrit by GerritCodeReview.
the class MergeUtil method createCherryPickFromCommit.
public CodeReviewCommit createCherryPickFromCommit(ObjectInserter inserter, Config repoConfig, RevCommit mergeTip, RevCommit originalCommit, PersonIdent cherryPickCommitterIdent, String commitMsg, CodeReviewRevWalk rw, int parentIndex, boolean ignoreIdenticalTree) throws MissingObjectException, IncorrectObjectTypeException, IOException, MergeIdenticalTreeException, MergeConflictException {
final ThreeWayMerger m = newThreeWayMerger(inserter, repoConfig);
m.setBase(originalCommit.getParent(parentIndex));
if (m.merge(mergeTip, originalCommit)) {
ObjectId tree = m.getResultTreeId();
if (tree.equals(mergeTip.getTree()) && !ignoreIdenticalTree) {
throw new MergeIdenticalTreeException("identical tree");
}
CommitBuilder mergeCommit = new CommitBuilder();
mergeCommit.setTreeId(tree);
mergeCommit.setParentId(mergeTip);
mergeCommit.setAuthor(originalCommit.getAuthorIdent());
mergeCommit.setCommitter(cherryPickCommitterIdent);
mergeCommit.setMessage(commitMsg);
return rw.parseCommit(inserter.insert(mergeCommit));
}
throw new MergeConflictException("merge conflict");
}
use of org.eclipse.jgit.lib.ObjectId in project gerrit by GerritCodeReview.
the class CreateTag method apply.
@Override
public TagInfo apply(ProjectResource resource, TagInput input) throws RestApiException, IOException, PermissionBackendException {
if (input == null) {
input = new TagInput();
}
if (input.ref != null && !ref.equals(input.ref)) {
throw new BadRequestException("ref must match URL");
}
if (input.revision == null) {
input.revision = Constants.HEAD;
}
ref = RefUtil.normalizeTagRef(ref);
RefControl refControl = resource.getControl().controlForRef(ref);
PermissionBackend.ForRef perm = permissionBackend.user(identifiedUser).project(resource.getNameKey()).ref(ref);
try (Repository repo = repoManager.openRepository(resource.getNameKey())) {
ObjectId revid = RefUtil.parseBaseRevision(repo, resource.getNameKey(), input.revision);
RevWalk rw = RefUtil.verifyConnected(repo, revid);
RevObject object = rw.parseAny(revid);
rw.reset();
boolean isAnnotated = Strings.emptyToNull(input.message) != null;
boolean isSigned = isAnnotated && input.message.contains("-----BEGIN PGP SIGNATURE-----\n");
if (isSigned) {
throw new MethodNotAllowedException("Cannot create signed tag \"" + ref + "\"");
} else if (isAnnotated && !refControl.canPerform(Permission.CREATE_TAG)) {
throw new AuthException("Cannot create annotated tag \"" + ref + "\"");
} else {
perm.check(RefPermission.CREATE);
}
if (repo.getRefDatabase().exactRef(ref) != null) {
throw new ResourceConflictException("tag \"" + ref + "\" already exists");
}
try (Git git = new Git(repo)) {
TagCommand tag = git.tag().setObjectId(object).setName(ref.substring(R_TAGS.length())).setAnnotated(isAnnotated).setSigned(isSigned);
if (isAnnotated) {
tag.setMessage(input.message).setTagger(identifiedUser.get().newCommitterIdent(TimeUtil.nowTs(), TimeZone.getDefault()));
}
Ref result = tag.call();
tagCache.updateFastForward(resource.getNameKey(), ref, ObjectId.zeroId(), result.getObjectId());
referenceUpdated.fire(resource.getNameKey(), ref, ObjectId.zeroId(), result.getObjectId(), identifiedUser.get().getAccount());
try (RevWalk w = new RevWalk(repo)) {
return ListTags.createTagInfo(perm, result, w);
}
}
} catch (InvalidRevisionException e) {
throw new BadRequestException("Invalid base revision");
} catch (GitAPIException e) {
log.error("Cannot create tag \"" + ref + "\"", e);
throw new IOException(e);
}
}
use of org.eclipse.jgit.lib.ObjectId in project gerrit by GerritCodeReview.
the class CreateBranch method apply.
@Override
public BranchInfo apply(ProjectResource rsrc, BranchInput input) throws BadRequestException, AuthException, ResourceConflictException, IOException {
if (input == null) {
input = new BranchInput();
}
if (input.ref != null && !ref.equals(input.ref)) {
throw new BadRequestException("ref must match URL");
}
if (input.revision == null) {
input.revision = Constants.HEAD;
}
while (ref.startsWith("/")) {
ref = ref.substring(1);
}
ref = RefNames.fullName(ref);
if (!Repository.isValidRefName(ref)) {
throw new BadRequestException("invalid branch name \"" + ref + "\"");
}
if (MagicBranch.isMagicBranch(ref)) {
throw new BadRequestException("not allowed to create branches under \"" + MagicBranch.getMagicRefNamePrefix(ref) + "\"");
}
final Branch.NameKey name = new Branch.NameKey(rsrc.getNameKey(), ref);
final RefControl refControl = rsrc.getControl().controlForRef(name);
try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
ObjectId revid = RefUtil.parseBaseRevision(repo, rsrc.getNameKey(), input.revision);
RevWalk rw = RefUtil.verifyConnected(repo, revid);
RevObject object = rw.parseAny(revid);
if (ref.startsWith(Constants.R_HEADS)) {
//
try {
object = rw.parseCommit(object);
} catch (IncorrectObjectTypeException notCommit) {
throw new BadRequestException("\"" + input.revision + "\" not a commit");
}
}
if (!refControl.canCreate(db.get(), repo, object)) {
throw new AuthException("Cannot create \"" + ref + "\"");
}
try {
final RefUpdate u = repo.updateRef(ref);
u.setExpectedOldObjectId(ObjectId.zeroId());
u.setNewObjectId(object.copy());
u.setRefLogIdent(identifiedUser.get().newRefLogIdent());
u.setRefLogMessage("created via REST from " + input.revision, false);
refCreationValidator.validateRefOperation(rsrc.getName(), identifiedUser.get(), u);
final RefUpdate.Result result = u.update(rw);
switch(result) {
case FAST_FORWARD:
case NEW:
case NO_CHANGE:
referenceUpdated.fire(name.getParentKey(), u, ReceiveCommand.Type.CREATE, identifiedUser.get().getAccount());
break;
case LOCK_FAILURE:
if (repo.getRefDatabase().exactRef(ref) != null) {
throw new ResourceConflictException("branch \"" + ref + "\" already exists");
}
String refPrefix = RefUtil.getRefPrefix(ref);
while (!Constants.R_HEADS.equals(refPrefix)) {
if (repo.getRefDatabase().exactRef(refPrefix) != null) {
throw new ResourceConflictException("Cannot create branch \"" + ref + "\" since it conflicts with branch \"" + refPrefix + "\".");
}
refPrefix = RefUtil.getRefPrefix(refPrefix);
}
//$FALL-THROUGH$
case FORCED:
case IO_FAILURE:
case NOT_ATTEMPTED:
case REJECTED:
case REJECTED_CURRENT_BRANCH:
case RENAMED:
default:
{
throw new IOException(result.name());
}
}
BranchInfo info = new BranchInfo();
info.ref = ref;
info.revision = revid.getName();
info.canDelete = permissionBackend.user(identifiedUser).ref(name).testOrFalse(RefPermission.DELETE) ? true : null;
return info;
} catch (IOException err) {
log.error("Cannot create branch \"" + name + "\"", err);
throw err;
}
} catch (RefUtil.InvalidRevisionException e) {
throw new BadRequestException("invalid revision \"" + input.revision + "\"");
}
}
Aggregations