use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.
the class ChangeOperationsImpl method createChange.
private Change.Id createChange(TestChangeCreation changeCreation) throws Exception {
Change.Id changeId = Change.id(seq.nextChangeId());
Project.NameKey project = getTargetProject(changeCreation);
try (Repository repository = repositoryManager.openRepository(project);
ObjectInserter objectInserter = repository.newObjectInserter();
RevWalk revWalk = new RevWalk(objectInserter.newReader())) {
Instant now = TimeUtil.now();
IdentifiedUser changeOwner = getChangeOwner(changeCreation);
PersonIdent authorAndCommitter = changeOwner.newCommitterIdent(now, serverIdent.getTimeZone());
ObjectId commitId = createCommit(repository, revWalk, objectInserter, changeCreation, authorAndCommitter);
String refName = RefNames.fullName(changeCreation.branch());
ChangeInserter inserter = getChangeInserter(changeId, refName, commitId);
try (BatchUpdate batchUpdate = batchUpdateFactory.create(project, changeOwner, now)) {
batchUpdate.setRepository(repository, revWalk, objectInserter);
batchUpdate.insertChange(inserter);
batchUpdate.execute();
}
return changeId;
}
}
use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.
the class VersionedMetaDataOnInit method save.
protected void save(PersonIdent ident, String msg) throws IOException, ConfigInvalidException {
File path = getPath();
if (path == null) {
throw new IOException(project + " does not exist.");
}
try (Repository repo = new FileRepository(path);
ObjectInserter i = repo.newObjectInserter();
ObjectReader r = repo.newObjectReader();
RevWalk rw = new RevWalk(r)) {
inserter = i;
reader = r;
RevTree srcTree = revision != null ? rw.parseTree(revision) : null;
newTree = readTree(srcTree);
CommitBuilder commit = new CommitBuilder();
commit.setAuthor(ident);
commit.setCommitter(ident);
commit.setMessage(msg);
onSave(commit);
ObjectId res = newTree.writeTree(inserter);
if (res.equals(srcTree)) {
return;
}
commit.setTreeId(res);
if (revision != null) {
commit.addParentId(revision);
}
ObjectId newRevision = inserter.insert(commit);
updateRef(repo, ident, newRevision, "commit: " + msg);
revision = rw.parseCommit(newRevision);
} finally {
inserter = null;
reader = null;
}
}
use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.
the class IntBlob method tryStore.
public static RefUpdate tryStore(Repository repo, RevWalk rw, Project.NameKey projectName, String refName, @Nullable ObjectId oldId, int val, GitReferenceUpdated gitRefUpdated) throws IOException {
ObjectId newId;
try (ObjectInserter ins = repo.newObjectInserter()) {
logger.atFine().log("storing value %d on %s in %s (oldId: %s)", val, refName, projectName, oldId == null ? "null" : oldId.name());
newId = ins.insert(OBJ_BLOB, Integer.toString(val).getBytes(UTF_8));
ins.flush();
logger.atFine().log("successfully stored %d on %s as %s in %s", val, refName, newId.name(), projectName);
}
RefUpdate ru = repo.updateRef(refName);
if (oldId != null) {
ru.setExpectedOldObjectId(oldId);
}
ru.disableRefLog();
ru.setNewObjectId(newId);
// Required for non-commitish updates.
ru.setForceUpdate(true);
RefUpdate.Result result = ru.update(rw);
if (refUpdated(result)) {
gitRefUpdated.fire(projectName, ru, null);
}
return ru;
}
use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.
the class PublicKeyStore method rebuildSubkeyMasterKeyMap.
public void rebuildSubkeyMasterKeyMap() throws MissingObjectException, IncorrectObjectTypeException, IOException, PGPException {
if (reader == null) {
load();
}
if (notes != null) {
try (ObjectInserter ins = repo.newObjectInserter()) {
for (Note note : notes) {
for (PGPPublicKeyRing keyRing : new PGPPublicKeyRingCollection(readKeysFromNote(note, null))) {
long masterKeyId = keyRing.getPublicKey().getKeyID();
ObjectId masterKeyObjectId = keyObjectId(masterKeyId);
saveSubkeyMapping(ins, keyRing, masterKeyId, masterKeyObjectId);
}
}
}
}
}
use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.
the class PublicKeyStore method save.
/**
* Save pending keys to the store.
*
* <p>One commit is created and the ref updated. The pending list is cleared if and only if the
* ref update succeeds, which allows for easy retries in case of lock failure.
*
* @param cb commit builder with at least author and identity populated; tree and parent are
* ignored.
* @return result of the ref update.
*/
public RefUpdate.Result save(CommitBuilder cb) throws PGPException, IOException {
if (toAdd.isEmpty() && toRemove.isEmpty()) {
return RefUpdate.Result.NO_CHANGE;
}
if (reader == null) {
load();
}
if (notes == null) {
notes = NoteMap.newEmptyMap();
}
ObjectId newTip;
try (ObjectInserter ins = repo.newObjectInserter()) {
for (PGPPublicKeyRing keyRing : toAdd.values()) {
saveToNotes(ins, keyRing);
}
for (Fingerprint fp : toRemove) {
deleteFromNotes(ins, fp);
}
cb.setTreeId(notes.writeTree(ins));
if (cb.getTreeId().equals(tip != null ? tip.getTree() : EMPTY_TREE_ID)) {
return RefUpdate.Result.NO_CHANGE;
}
if (tip != null) {
cb.setParentId(tip);
}
if (cb.getMessage() == null) {
int n = toAdd.size() + toRemove.size();
cb.setMessage(String.format("Update %d public key%s", n, n != 1 ? "s" : ""));
}
newTip = ins.insert(cb);
ins.flush();
}
RefUpdate ru = repo.updateRef(PublicKeyStore.REFS_GPG_KEYS);
ru.setExpectedOldObjectId(tip);
ru.setNewObjectId(newTip);
ru.setRefLogIdent(cb.getCommitter());
ru.setRefLogMessage("Store public keys", true);
RefUpdate.Result result = ru.update();
reset();
switch(result) {
case FAST_FORWARD:
case NEW:
case NO_CHANGE:
toAdd.clear();
toRemove.clear();
break;
case LOCK_FAILURE:
throw new LockFailureException("Failed to store public keys", ru);
case FORCED:
case IO_FAILURE:
case NOT_ATTEMPTED:
case REJECTED:
case REJECTED_CURRENT_BRANCH:
case RENAMED:
case REJECTED_MISSING_OBJECT:
case REJECTED_OTHER_REASON:
default:
break;
}
return result;
}
Aggregations